1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.adapter;
\r
14 import java.awt.AlphaComposite;
\r
15 import java.awt.Color;
\r
16 import java.util.HashMap;
\r
17 import java.util.Map;
\r
19 import org.simantics.databoard.Bindings;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.RequestProcessor;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.diagram.profile.ProfileKeys;
\r
25 import org.simantics.diagram.stubs.DiagramResource;
\r
26 import org.simantics.g2d.canvas.ICanvasContext;
\r
27 import org.simantics.layer0.Layer0;
\r
28 import org.simantics.scenegraph.INode;
\r
29 import org.simantics.scenegraph.profile.DataNodeMap;
\r
30 import org.simantics.scenegraph.profile.EvaluationContext;
\r
31 import org.simantics.scenegraph.profile.Group;
\r
32 import org.simantics.scenegraph.profile.Style;
\r
33 import org.simantics.scenegraph.profile.common.ObserverGroupListener;
\r
34 import org.simantics.scenegraph.profile.common.ProfileVariables;
\r
36 public class ConstantStyle implements Style {
\r
39 final Map<String, Object> values = new HashMap<String, Object>();
\r
40 ObserverGroupListener listener = null;
\r
42 public ConstantStyle(ReadGraph graph, Resource style) throws DatabaseException {
\r
43 Layer0 L0 = Layer0.getInstance(graph);
\r
44 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
45 this.name = graph.getPossibleRelatedValue(style, L0.HasName, Bindings.STRING);
\r
46 String[] values = graph.getPossibleRelatedValue(style, DIA.HasValues, Bindings.getBindingUnchecked(String[].class));
\r
47 for(int i=0;i<values.length;i+=2) {
\r
48 String key = values[i];
\r
49 Object value = haxx(key, values[i+1]);
\r
50 this.values.put(key, value);
\r
54 private Object haxx(String key, Object value) {
\r
55 if("alpha".equals(key)) {
\r
56 return AlphaComposite.getInstance(AlphaComposite. SRC_OVER, Float.parseFloat((String)value));
\r
57 } else if("color".equals(key)) {
\r
58 return new Color(Integer.parseInt(value.toString(), 16));
\r
65 public void deactivate(Resource diagram, Resource entry, Group group,
\r
66 EvaluationContext observer) {
\r
67 if (listener != null && !listener.isDisposed()) {
\r
75 public void activate(RequestProcessor backend, Resource diagram, Resource entry, Group group, final EvaluationContext observer) {
\r
77 if (listener != null && !listener.isDisposed())
\r
80 listener = new ObserverGroupListener(this, group, observer);
\r
82 group.trackItems(backend, diagram, listener);
\r
87 public void apply(Resource entry, Group group, final EvaluationContext evaluationContext) {
\r
89 // throw new UnsupportedOperationException();
\r
91 // IDiagram diagram = evaluationContext.getConstant(ProfileKeys.DIAGRAM);
\r
92 ICanvasContext context = evaluationContext.getConstant(ProfileKeys.CANVAS);
\r
94 assert context.getThreadAccess().currentThreadAccess();
\r
96 if(listener == null) {
\r
97 System.out.println("ConstantStyle " + this + " was not active in apply!");
\r
101 final DataNodeMap map = evaluationContext.getConstant(ProfileKeys.NODE_MAP);
\r
103 // DataElementMap map = diagram.getDiagramClass().getSingleItem(DataElementMap.class);
\r
105 for(Map.Entry<String, Object> value : values.entrySet()) {
\r
107 for(Object item : listener.getItems()) {
\r
109 final INode node = map.getNode(item);
\r
111 // final IElement element = map.getElement(diagram, item);
\r
113 //observer.exception(new NullPointerException("ConstantStyle.apply: no element for " + item));
\r
114 evaluationContext.update();
\r
118 ProfileVariables.claimNodeProperty(node, value.getKey(), value.getValue(), evaluationContext);
\r
120 //// System.out.println("apply[" + element + "]: " + value.getKey() + " => " + value.getValue());
\r
121 // Variables.set(node, value.getKey(), value.getValue(), evaluationContext);
\r
123 // PropertySetter setter = element.getElementClass().getAtMostOneItemOfClass(PropertySetter.class);
\r
124 // if(setter != null)
\r
125 // setter.syncPropertiesToNode(element);
\r
133 public String toString() {
\r