]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/participant/DiagramModelActivityTracker.java
Paster.getGraph() method for overriding classes.
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / participant / DiagramModelActivityTracker.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.participant;
13
14 import java.awt.Color;
15 import java.awt.Font;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Rectangle2D;
18
19 import org.simantics.Simantics;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.PossibleIndexRoot;
23 import org.simantics.db.common.request.UnaryRead;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.adapter.Instances;
26 import org.simantics.db.layer0.request.IsActive;
27 import org.simantics.db.layer0.util.Layer0Utils;
28 import org.simantics.db.procedure.Listener;
29 import org.simantics.diagram.elements.TextNode;
30 import org.simantics.diagram.stubs.DiagramResource;
31 import org.simantics.g2d.canvas.Hints;
32 import org.simantics.g2d.canvas.ICanvasContext;
33 import org.simantics.g2d.canvas.SGDesignation;
34 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
35 import org.simantics.g2d.canvas.impl.HintReflection.HintListener;
36 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;
37 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
38 import org.simantics.g2d.utils.Alignment;
39 import org.simantics.layer0.Layer0;
40 import org.simantics.modeling.ModelingResources;
41 import org.simantics.scenegraph.g2d.G2DParentNode;
42 import org.simantics.structural.stubs.StructuralResource2;
43 import org.simantics.utils.datastructures.hints.IHintContext;
44 import org.simantics.utils.datastructures.hints.IHintContext.Key;
45 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
46 import org.simantics.utils.datastructures.hints.IHintObservable;
47 import org.simantics.utils.ui.ErrorLogger;
48
49 /**
50  * DiagramModelActivityTracker tracks whether or not the specified input
51  * resource is part of the diagram model or not and modifies the canvas
52  * background color accordingly. If the diagram is not in an active model, an
53  * <b>INACTIVE MODEL</b> banner is also shown in the background using
54  * {@link TextNode}.
55  * 
56  * @author Tuukka Lehtonen
57  */
58 public class DiagramModelActivityTracker extends AbstractCanvasParticipant {
59
60     public static final Key    KEY_IN_ACTIVE_MODEL         = new KeyOf(String.class, "IN_ACTIVE_MODEL");
61     public static final Key    KEY_ACTIVE_BACKGROUND       = new KeyOf(Color.class, "ACTIVE_BACKGROUND");
62     public static final Key    KEY_INACTIVE_BACKGROUND     = new KeyOf(Color.class, "INACTIVE_BACKGROUND");
63
64     private static final Color DEFAULT_ACTIVE_BACKGROUND   = Color.WHITE;
65     private static final Color DEFAULT_INACTIVE_BACKGROUND = new Color(242, 242, 242);
66
67     Resource                   input;
68     IsInActiveModelListener    listener;
69     TextNode                   bannerNode;
70
71     public DiagramModelActivityTracker(Resource input) {
72         if (input == null)
73             throw new NullPointerException("null input");
74         this.input = input;
75     }
76
77     @HintListener(Class=Hints.class, Field="KEY_CONTROL_BOUNDS")
78     public void controlBoundsChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
79         if (bannerNode == null)
80             return;
81         if (newValue != null) {
82             Rectangle2D bounds = (Rectangle2D) newValue;
83             AffineTransform at = new AffineTransform();
84             at.translate(5,5);
85 //            at.translate(bounds.getWidth() / 2, bounds.getHeight() / 2);
86 //            at.scale(2, 2);
87 //            at.rotate(-Math.PI/6);
88 //            at.translate(bounds.getWidth() - 150, bounds.getHeight() - 30);
89 //            at.scale(1, 1);
90             bannerNode.setTransform(at);
91         } else {
92             // Disable rendering
93             bannerNode.setColor(null);
94         }
95     }
96
97     @HintListener(Class=DiagramModelActivityTracker.class, Field="KEY_IN_ACTIVE_MODEL")
98     public void containingModelActivityChanged(IHintObservable sender, Key key, Object oldValue, Object _newValue) {
99         String newValue = (String)_newValue;
100         if (bannerNode == null)
101             return;
102         if (newValue == null) {
103                 bannerNode.setText("");
104         } else {
105                 bannerNode.setText(newValue);
106             //bannerNode.setColor(new Color(192, 32, 32, 128));
107             //bannerNode.setColor(new Color(192, 32, 32, 32));
108         }
109     }
110
111     @Override
112     public void addedToContext(ICanvasContext ctx) {
113         super.addedToContext(ctx);
114
115         listener = new IsInActiveModelListener(ctx);
116         Simantics.getSession().async(new IsActiveDiagram(input), listener);
117
118         if (!ctx.getHintStack().containsHint(KEY_ACTIVE_BACKGROUND))
119             ctx.getDefaultHintContext().setHint(KEY_ACTIVE_BACKGROUND, DEFAULT_ACTIVE_BACKGROUND);
120         if (!ctx.getHintStack().containsHint(KEY_INACTIVE_BACKGROUND))
121             ctx.getDefaultHintContext().setHint(KEY_INACTIVE_BACKGROUND, DEFAULT_INACTIVE_BACKGROUND);
122     }
123
124     @Override
125     public void removedFromContext(ICanvasContext ctx) {
126         if (listener != null) {
127             listener.dispose();
128             listener = null;
129         }
130         super.removedFromContext(ctx);
131     }
132
133     @SGInit(designation = SGDesignation.CONTROL)
134     public void initSG(G2DParentNode parent) {
135         bannerNode = parent.addNode("inactivity-banner", TextNode.class);
136         bannerNode.setZIndex(Integer.MIN_VALUE / 4);
137         //bannerNode.setZIndex(Integer.MAX_VALUE / 4);
138         bannerNode.setHorizontalAlignment((byte) Alignment.LEADING.ordinal());
139         bannerNode.setVerticalAlignment((byte) Alignment.LEADING.ordinal());
140         //bannerNode.setText("Model is not active.");
141         bannerNode.setText("");
142         bannerNode.setFont(Font.decode("Arial 16"));
143         //bannerNode.setBackgroundColor(new Color(192, 192, 192, 128));
144         bannerNode.setPadding(10, 10);
145         bannerNode.setBorderColor(Color.GRAY);
146         bannerNode.setBorderWidth(0);
147         bannerNode.setEditable(false);
148     }
149
150     @SGCleanup
151     public void cleanupSG() {
152         if (bannerNode != null) {
153             bannerNode.remove();
154             bannerNode = null;
155         }
156     }
157
158     static class IsActiveDiagram extends UnaryRead<Resource, String> {
159         public IsActiveDiagram(Resource parameter) {
160             super(parameter);
161         }
162         @Override
163         public String perform(ReadGraph graph) throws DatabaseException {
164                 
165             Resource indexRoot = graph.syncRequest( new PossibleIndexRoot(parameter) );
166             if (indexRoot == null) {
167                 return "This diagram is not part of any model or shared library";
168             }
169                 Layer0 L0 = Layer0.getInstance(graph);
170                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
171                 ModelingResources MOD = ModelingResources.getInstance(graph);
172                 Resource composite = graph.getPossibleObject(parameter, MOD.DiagramToComposite);
173                 if(composite == null)
174                 return "This diagram does not have a corresponding configuration";
175                 
176                 Resource componentType = graph.getPossibleObject(composite, STR.Defines);
177                 if(componentType != null) {
178                         // This is a component type
179                         boolean published = Layer0Utils.isPublished(graph, componentType);
180                         if(published)
181                     return "This diagram defines a published (read-only) user component";
182                 }
183                 
184                 if(graph.isInstanceOf(indexRoot, L0.SharedOntology)) {
185                         return "";
186                 }
187                 
188                 boolean activeModel = graph.syncRequest( new IsActive(indexRoot) );
189                 if(!activeModel)
190                 return "The model of this diagram is not active";
191                 
192                 DiagramResource DIA = DiagramResource.getInstance(graph);
193                 Instances query = graph.adapt(DIA.DiagramActivityCondition, Instances.class);
194
195                 for(Resource condition : query.find(graph, indexRoot)) {
196                 String value = Simantics.tryInvokeSCL(graph, condition, DIA.DiagramActivityCondition_test, parameter);
197                 if(value != null && !value.isEmpty()) return value;
198                 }
199                 
200             return "";
201             
202         }
203     }
204
205     static class IsInActiveModelListener implements Listener<String> {
206         ICanvasContext context;
207         public IsInActiveModelListener(ICanvasContext context) {
208             this.context = context;
209         }
210         @Override
211         public void execute(final String result) {
212             final Color color = (result.isEmpty())
213                     ? context.getHintStack().<Color>getHint(KEY_ACTIVE_BACKGROUND)
214                     : context.getHintStack().<Color>getHint(KEY_INACTIVE_BACKGROUND);
215
216             context.getThreadAccess().asyncExec(new Runnable() {
217                 @Override
218                 public void run() {
219                     ICanvasContext ctx = context;
220                     if (ctx == null)
221                         return;
222                     if (ctx.isDisposed())
223                         return;
224                     IHintContext hints = ctx.getDefaultHintContext();
225                     if (color != null)
226                         hints.setHint(Hints.KEY_BACKGROUND_COLOR, color);
227                     hints.setHint(KEY_IN_ACTIVE_MODEL, result);
228                 }
229             });
230         }
231         public void dispose() {
232             context = null;
233         }
234         @Override
235         public boolean isDisposed() {
236             ICanvasContext context = this.context;
237             return context == null || context.isDisposed();
238         }
239         @Override
240         public void exception(Throwable t) {
241             ErrorLogger.defaultLogError(t);
242         }
243     }
244
245 }