1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.participant;
14 import java.awt.Color;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Rectangle2D;
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;
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
56 * @author Tuukka Lehtonen
58 public class DiagramModelActivityTracker extends AbstractCanvasParticipant {
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");
64 private static final Color DEFAULT_ACTIVE_BACKGROUND = Color.WHITE;
65 private static final Color DEFAULT_INACTIVE_BACKGROUND = new Color(242, 242, 242);
68 IsInActiveModelListener listener;
71 public DiagramModelActivityTracker(Resource input) {
73 throw new NullPointerException("null input");
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)
81 if (newValue != null) {
82 Rectangle2D bounds = (Rectangle2D) newValue;
83 AffineTransform at = new AffineTransform();
85 // at.translate(bounds.getWidth() / 2, bounds.getHeight() / 2);
87 // at.rotate(-Math.PI/6);
88 // at.translate(bounds.getWidth() - 150, bounds.getHeight() - 30);
90 bannerNode.setTransform(at);
93 bannerNode.setColor(null);
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)
102 if (newValue == null) {
103 bannerNode.setText("");
105 bannerNode.setText(newValue);
106 //bannerNode.setColor(new Color(192, 32, 32, 128));
107 //bannerNode.setColor(new Color(192, 32, 32, 32));
112 public void addedToContext(ICanvasContext ctx) {
113 super.addedToContext(ctx);
115 listener = new IsInActiveModelListener(ctx);
116 Simantics.getSession().async(new IsActiveDiagram(input), listener);
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);
125 public void removedFromContext(ICanvasContext ctx) {
126 if (listener != null) {
130 super.removedFromContext(ctx);
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);
151 public void cleanupSG() {
152 if (bannerNode != null) {
158 static class IsActiveDiagram extends UnaryRead<Resource, String> {
159 public IsActiveDiagram(Resource parameter) {
163 public String perform(ReadGraph graph) throws DatabaseException {
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";
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";
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);
181 return "This diagram defines a published (read-only) user component";
184 if(graph.isInstanceOf(indexRoot, L0.SharedOntology)) {
188 boolean activeModel = graph.syncRequest( new IsActive(indexRoot) );
190 return "The model of this diagram is not active";
192 DiagramResource DIA = DiagramResource.getInstance(graph);
193 Instances query = graph.adapt(DIA.DiagramActivityCondition, Instances.class);
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;
205 static class IsInActiveModelListener implements Listener<String> {
206 ICanvasContext context;
207 public IsInActiveModelListener(ICanvasContext context) {
208 this.context = context;
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);
216 context.getThreadAccess().asyncExec(new Runnable() {
219 ICanvasContext ctx = context;
222 if (ctx.isDisposed())
224 IHintContext hints = ctx.getDefaultHintContext();
226 hints.setHint(Hints.KEY_BACKGROUND_COLOR, color);
227 hints.setHint(KEY_IN_ACTIVE_MODEL, result);
231 public void dispose() {
235 public boolean isDisposed() {
236 ICanvasContext context = this.context;
237 return context == null || context.isDisposed();
240 public void exception(Throwable t) {
241 ErrorLogger.defaultLogError(t);