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.adapter;
15 import java.awt.Shape;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Rectangle2D;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
23 import java.util.regex.Pattern;
24 import java.util.regex.PatternSyntaxException;
26 import org.simantics.databoard.Bindings;
27 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
28 import org.simantics.databoard.util.Bean;
29 import org.simantics.datatypes.literal.RGB;
30 import org.simantics.db.AsyncReadGraph;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.Session;
34 import org.simantics.db.WriteGraph;
35 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
36 import org.simantics.db.common.request.ResourceRead;
37 import org.simantics.db.common.request.TernaryRead;
38 import org.simantics.db.common.request.WriteRequest;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.layer0.variable.Variable;
41 import org.simantics.db.layer0.variable.Variables;
42 import org.simantics.db.procedure.AsyncProcedure;
43 import org.simantics.diagram.content.ResourceTerminal;
44 import org.simantics.diagram.flag.AbstractFlagType;
45 import org.simantics.diagram.flag.BasicFlagType;
46 import org.simantics.diagram.flag.FlagSceneGraph;
47 import org.simantics.diagram.flag.FlagUtil;
48 import org.simantics.diagram.flag.IFlagType;
49 import org.simantics.diagram.flag.IFlagType.FlagInfo;
50 import org.simantics.diagram.flag.IFlagTypeReader;
51 import org.simantics.diagram.function.All;
52 import org.simantics.diagram.function.PredefinedVariables;
53 import org.simantics.diagram.query.DiagramRequests;
54 import org.simantics.diagram.query.FlagTables;
55 import org.simantics.diagram.query.FlagTypeFilter;
56 import org.simantics.diagram.query.FlagTypeFilters;
57 import org.simantics.diagram.query.FlagTypeVisual;
58 import org.simantics.diagram.query.FlagTypeVisuals;
59 import org.simantics.diagram.stubs.DiagramResource;
60 import org.simantics.diagram.synchronization.CompositeHintSynchronizer;
61 import org.simantics.diagram.synchronization.IHintSynchronizer;
62 import org.simantics.diagram.synchronization.SynchronizationHints;
63 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
64 import org.simantics.diagram.synchronization.graph.FlagSynchronizer;
65 import org.simantics.diagram.synchronization.graph.TransformSynchronizer;
66 import org.simantics.diagram.ui.DiagramModelHints;
67 import org.simantics.g2d.canvas.ICanvasContext;
68 import org.simantics.g2d.diagram.IDiagram;
69 import org.simantics.g2d.diagram.handler.Topology.Terminal;
70 import org.simantics.g2d.element.ElementClass;
71 import org.simantics.g2d.element.ElementUtils;
72 import org.simantics.g2d.element.IElement;
73 import org.simantics.g2d.element.handler.TextEditor;
74 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
75 import org.simantics.g2d.elementclass.FlagClass;
76 import org.simantics.g2d.elementclass.FlagClass.Mode;
77 import org.simantics.g2d.elementclass.FlagHandler;
78 import org.simantics.g2d.utils.Alignment;
79 import org.simantics.g2d.utils.geom.DirectionSet;
80 import org.simantics.layer0.Layer0;
81 import org.simantics.modeling.ModelingResources;
82 import org.simantics.modeling.template2d.ontology.Template2dResource;
83 import org.simantics.structural2.modelingRules.IModelingRules;
84 import org.simantics.utils.ui.ErrorLogger;
87 * @author Tuukka Lehtonen
89 public class FlagClassFactory extends SyncElementFactory {
91 private static final Bean[] NO_BEANS = {};
93 private static final IHintSynchronizer HINT_SYNCHRONIZER = new CompositeHintSynchronizer(FlagSynchronizer.INSTANCE, TransformSynchronizer.INSTANCE);
95 public static ElementClass createFlagClass(Resource elementClass, Resource terminalResource) {
96 Terminal terminal = new ResourceTerminal(terminalResource, new AffineTransform(), DirectionSet.ANY);
97 return FlagClass.create(terminal, FlagSceneGraph.INSTANCE).newClassWith(new StaticObjectAdapter(elementClass));
101 public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {
102 DiagramResource DIA = graph.getService(DiagramResource.class);
103 procedure.execute(graph, createFlagClass(DIA.Flag, DIA.Flag_Terminal));
107 public void load(ReadGraph g, ICanvasContext canvas, IDiagram diagram, Resource flag, IElement e) throws DatabaseException {
108 Layer0 l0 = g.getService(Layer0.class);
109 DiagramResource dr = g.getService(DiagramResource.class);
111 ElementClass ec = e.getElementClass();
112 final FlagHandler fh = ec.getSingleItem(FlagHandler.class);
114 fh.connectData(e, flag, FlagUtil.getPossibleCounterpart(g, flag));
115 fh.setExternal(e, FlagUtil.isExternal(g, flag));
117 TextEditor ed = ec.getAtMostOneItemOfClass(TextEditor.class);
119 final Session session = g.getSession();
120 ed.setModifier(e, new TextEditor.Modifier() {
122 public String getValue(IElement element) {
123 String s = ElementUtils.getText(element);
124 return s != null ? s : "";
128 public String isValid(IElement element, String text) {
133 public void modify(final IElement element, final String text) {
134 final Resource flag = (Resource) ElementUtils.getObject(element);
136 session.syncRequest(new WriteRequest() {
138 public void perform(WriteGraph graph) throws DatabaseException {
139 Layer0 l0 = Layer0.getInstance(graph);
140 DiagramGraphUtil.setRelatedValue(graph, flag, l0.HasLabel, l0.String, text, Bindings.STRING);
141 Resource otherFlag = FlagUtil.getPossibleCounterpart(graph, flag);
142 if (otherFlag != null)
143 DiagramGraphUtil.setRelatedValue(graph, otherFlag, l0.HasLabel, l0.String, text, Bindings.STRING);
146 } catch (DatabaseException e) {
147 ErrorLogger.defaultLogError("Flag label editing failed, see exception for details.", e);
153 Resource diagramRuntime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
154 AffineTransform at = DiagramGraphUtil.getDynamicAffineTransform(g, diagramRuntime, flag);
155 ElementUtils.setTransform(e, at);
157 String label = g.getPossibleRelatedValue(flag, l0.HasLabel);
160 ElementUtils.setText(e, label);
162 boolean shapeIsSet = false;
163 boolean flagTextIsSet = false;
164 boolean flagTypeIsSet = false;
165 boolean textAreaIsSet = false;
168 e.setHint(FlagClass.KEY_TEXT_HORIZONTAL_ALIGN, Alignment.LEADING);
169 e.setHint(FlagClass.KEY_TEXT_VERTICAL_ALIGN, Alignment.CENTER);
171 IModelingRules modelingRules = diagram.getHint(DiagramModelHints.KEY_MODELING_RULES);
172 if (modelingRules != null) {
173 Resource connectionType = DiagramGraphUtil.getConnectionTypeForFlag(g, flag);
174 IFlagTypeReader ftr = null;
175 if (connectionType != null) {
176 //System.out.println("FLAG " + NameUtils.getSafeName(g, flag) + ", CONNECTION TYPE " + NameUtils.getSafeName(g, connectionType));
177 ftr = g.getPossibleAdapter(connectionType, IFlagTypeReader.class);
180 //System.out.println("FLAG " + NameUtils.getSafeName(g, flag) + ", NO CONNECTION TYPE");
181 ftr = g.getPossibleAdapter(flag, IFlagTypeReader.class);
185 IFlagType ft = ftr.read(g, flag, modelingRules);
187 FlagInfo info = ft.getInfo(g, canvas);
189 Shape shape = info.getShape();
191 e.setHint(FlagClass.KEY_SHAPE, shape);
195 FlagClass.Type type = info.getType();
197 e.setHint(FlagClass.KEY_FLAG_TYPE, type);
198 flagTypeIsSet = true;
201 String[] flagText = info.getText();
202 if (flagText != null) {
203 e.setHint(FlagClass.KEY_FLAG_TEXT, flagText);
204 flagTextIsSet = true;
207 Font flagFont = info.getFont();
208 if(flagFont != null) {
209 e.setHint(FlagClass.KEY_FLAG_FONT, flagFont);
212 if (info.getTextArea() != null) {
213 e.setHint(FlagClass.KEY_FLAG_TEXT_AREA, info.getTextArea());
214 textAreaIsSet = true;
217 if (info.getHorizontalAlignment() != null)
218 e.setHint(FlagClass.KEY_TEXT_HORIZONTAL_ALIGN, info.getHorizontalAlignment());
219 if (info.getVerticalAlignment() != null)
220 e.setHint(FlagClass.KEY_TEXT_VERTICAL_ALIGN, info.getVerticalAlignment());
225 // Fall back to reading flag type from a property.
226 e.setHint(FlagClass.KEY_FLAG_TYPE, DiagramGraphUtil.toFlagType(dr, g.getPossibleObject(flag, dr.HasFlagType), FlagClass.Type.In));
228 // e.setHint(FlagClass.KEY_FLAG_TEXT, new String[] { label });
229 e.setHint(FlagClass.KEY_FLAG_TEXT, g.syncRequest(DiagramRequests.getFlagText(flag)));
230 if (!textAreaIsSet) {
231 FlagClass.Type type = e.getHint(FlagClass.KEY_FLAG_TYPE);
232 Mode mode = AbstractFlagType.getMode(g, flag);
233 e.setHint(FlagClass.KEY_FLAG_TEXT_AREA, BasicFlagType.getArea(type, mode));
236 FlagClass.Type type = e.getHint(FlagClass.KEY_FLAG_TYPE);
237 Mode mode = AbstractFlagType.getMode(g, flag);
238 e.setHint(FlagClass.KEY_SHAPE, BasicFlagType.getShape(type, mode));
241 e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, HINT_SYNCHRONIZER);
242 e.setHint(FlagSceneGraph.KEY_FLAG_VISUALS, NO_BEANS);
244 DiagramResource DIA = DiagramResource.getInstance(g);
245 Layer0 L0 = Layer0.getInstance(g);
247 Resource template = diagramRuntime != null ? All.getTemplate(g, diagramRuntime) : null;
248 Resource flagTable = getFlagTable(g, template, flag);
250 if (template != null && flagTable != null) {
252 Resource flagTypeVisual = getVisualOfFlag(g, template, flagTable, flag);
253 if (flagTypeVisual != null) {
254 Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);
255 float tableWidth = g.getRelatedValue(flagTable, TEMPLATE2D.FlagTable_HasWidth, Bindings.FLOAT);
256 Resource align = g.getPossibleObject(flagTable, TEMPLATE2D.FlagTable_HasAlignment);
257 float height = g.getRelatedValue(flagTable, TEMPLATE2D.FlagTable_HasRowHeigth, Bindings.FLOAT);
258 float halfHeight = height / 2;
259 float horizontalOffset = tableWidth;
262 if (align.equals(TEMPLATE2D.FlagTable_Alignment_Left)) {
263 horizontalOffset = 0.0f;
264 } else if (align.equals(TEMPLATE2D.FlagTable_Alignment_Right)) {
265 tableWidth = -tableWidth;
266 horizontalOffset = -horizontalOffset;
270 Collection<Resource> monitorsAndTexts = g.getObjects(flagTypeVisual, L0.ConsistsOf);
271 List<Bean> flagVisuals = Collections.emptyList();
272 if (!monitorsAndTexts.isEmpty()) {
273 flagVisuals = new ArrayList<Bean>(monitorsAndTexts.size());
275 ModelingResources MOD = ModelingResources.getInstance(g);
276 Resource diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
277 Resource compositeResource = g.getSingleObject(diagramResource, MOD.DiagramToComposite);
278 Variable compositeVariable = Variables.getVariable(g, compositeResource);
280 for (Resource visual : monitorsAndTexts) {
281 if (!acceptVisual(g, flag, visual))
284 if (g.isInstanceOf(visual, DIA.Scenegraph_Monitor)) {
285 FlagTextInfo i = g.syncRequest(new ReadFlagTextInfo(visual), TransientCacheListener.<FlagTextInfo>instance());
286 FlagTextInfo monitorInfo = (FlagTextInfo) i.clone();
287 if (monitorInfo.transform != null)
288 monitorInfo.transform[4] += horizontalOffset;
290 String path = g.getRelatedValue(visual, DIA.Scenegraph_Monitor_reference, Bindings.STRING);
292 if (path != null && path.length() > 0) {
293 value = evaluatePath(g,flag,path);
295 monitorInfo.text = value;
296 monitorInfo.id = Long.toString(visual.getResourceId());
298 flagVisuals.add(monitorInfo);
299 } else if (g.isInstanceOf(visual, DIA.Scenegraph_Text)) {
300 FlagTextInfo i = g.syncRequest(new ReadFlagTextInfo(visual), TransientCacheListener.<FlagTextInfo>instance());
301 FlagTextInfo info = (FlagTextInfo) i.clone();
302 if (info.transform != null)
303 info.transform[4] += horizontalOffset;
305 String path = g.getRelatedValue(visual, DIA.Scenegraph_Text_text, Bindings.STRING);
306 if (path != null && path.length() > 0) {
309 info.id = Long.toString(visual.getResourceId());
311 flagVisuals.add(info);
312 } else if (g.isInstanceOf(visual, DIA.Scenegraph_SVGImage)) {
313 SVGImageInfo info = g.syncRequest(new ReadSVGImageInfo(visual, compositeResource, compositeVariable), TransientCacheListener.<SVGImageInfo>instance());
314 flagVisuals.add(info);
319 if (flagVisuals.size() > 0) {
320 // Make sure that the flag shape is set to something that
321 // should contain the flag visual to make selection borders
323 Rectangle2D newShape = new Rectangle2D.Double();
324 newShape.setFrameFromDiagonal(0, -halfHeight, tableWidth, halfHeight);
325 e.setHint(FlagClass.KEY_SHAPE, newShape);
326 e.setHint(FlagSceneGraph.KEY_FLAG_VISUALS, flagVisuals.toArray(NO_BEANS));
332 private static String evaluatePath(ReadGraph graph, Resource resource, String path) throws DatabaseException{
333 Variable resourceVariable = Variables.getPossibleVariable(graph, resource);
334 if (resourceVariable == null)
336 return evaluatePath(graph, resource, resourceVariable, path);
339 private static String evaluatePath(ReadGraph graph, Resource resource, Variable resourceVariable, String path) throws DatabaseException{
340 PredefinedVariables vars = PredefinedVariables.getInstance();
341 Variable property = vars.getVariable(graph, path, resource, resourceVariable);
342 if (property == null)
344 Object value = property.getPossibleValue(graph);
345 if (value == null || !(value instanceof String))
347 return value.toString();
350 static class ReadSVGImageInfo extends TernaryRead<Resource, Resource, Variable, SVGImageInfo> {
352 public ReadSVGImageInfo(Resource svgImageNode, Resource composite, Variable compositeVariable) {
353 super(svgImageNode, composite, compositeVariable);
357 public SVGImageInfo perform(ReadGraph graph) throws DatabaseException {
358 SVGImageInfo info = new SVGImageInfo();
359 DiagramResource DIA = DiagramResource.getInstance(graph);
360 info.id = Long.toString(parameter.getResourceId());
361 Resource document = graph.getPossibleObject(parameter, DIA.Scenegraph_SVGImage_document);
362 if (document != null) {
363 Template2dResource TMPL = Template2dResource.getInstance(graph);
364 String path = graph.getPossibleRelatedValue(document, TMPL.Profiles_VariableReference_path, Bindings.STRING);
366 String svg = evaluatePath(graph, parameter2, parameter3, path);
367 if (svg != null && !svg.isEmpty())
368 info.svgDocument = svg;
369 //System.out.println("svgDocument: " + info.svgDocument);
372 info.transform = graph.getPossibleRelatedValue(parameter, DIA.Scenegraph_SVGImage_transform, Bindings.DOUBLE_ARRAY);
378 static class ReadFlagTextInfo extends ResourceRead<FlagTextInfo> {
380 public ReadFlagTextInfo(Resource textNode) {
385 public FlagTextInfo perform(ReadGraph graph) throws DatabaseException {
386 return createTextInfo(graph, resource);
395 * @throws DatabaseException
396 * @throws RuntimeBindingConstructionException
398 private static FlagTextInfo createTextInfo(ReadGraph g, Resource visual) throws DatabaseException {
399 DiagramResource DIA = DiagramResource.getInstance(g);
400 //Template2d TEMPLATE2D = Template2d.getInstance(g);
401 //Layer0 L0 = Layer0.getInstance(g);
403 FlagTextInfo info = new FlagTextInfo();
404 info.id = Long.toString(visual.getResourceId());
406 info.font = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_font, Bindings.getBindingUnchecked(Font.class));
407 info.color = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_color, RGB.Integer.BINDING/*Bindings.getBindingUnchecked(RGB.Integer.class)*/);
408 info.borderColor = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_borderColor, RGB.Integer.BINDING/*Bindings.getBindingUnchecked(RGB.Integer.class)*/);
409 info.backgroundColor = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_backgroundColor, RGB.Integer.BINDING/*Bindings.getBindingUnchecked(RGB.Integer.class)*/);
410 Float width = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_width, Bindings.getBindingUnchecked(Float.class));
414 Float borderWidth = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_borderWidth, Bindings.getBindingUnchecked(Float.class));
415 if (borderWidth != null)
416 info.borderWidth = borderWidth;
418 Boolean wrapText = g.getRelatedValue2(visual, DIA.Scenegraph_AbstractText_wrapText, Bindings.BOOLEAN);
419 if (wrapText != null)
420 info.wrapText = wrapText;
422 info.hAlignment = Alignment.LEADING;
423 Byte hAlignment = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_horizontalAlignment, Bindings.BYTE);
424 if (hAlignment != null) {
426 info.hAlignment = Alignment.TRAILING;
427 else if (hAlignment == 2)
428 info.hAlignment = Alignment.CENTER;
431 info.vAlignment = Alignment.LEADING;
432 Byte vAlignment = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_verticalAlignment, Bindings.BYTE);
433 if (vAlignment != null) {
435 info.vAlignment = Alignment.TRAILING;
436 else if (vAlignment == 2)
437 info.vAlignment = Alignment.CENTER;
438 else if (vAlignment == 3)
439 info.vAlignment = Alignment.BASELINE;
442 info.transform = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_transform, Bindings.getBindingUnchecked(double[].class));
446 private static Resource getFlagTable(ReadGraph g, Resource template, Resource flag) throws DatabaseException {
447 if (template == null || flag == null)
450 DiagramResource DIA = DiagramResource.getInstance(g);
451 String tableName = g.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableBinding, Bindings.STRING);
452 if (tableName == null)
455 Map<String, Resource> flagTables = g.syncRequest(new FlagTables(template), TransientCacheListener.<Map<String, Resource>>instance());
456 return flagTables.get(tableName);
459 private Resource getVisualOfFlag(ReadGraph g, Resource template, Resource flagTable, Resource flag) throws DatabaseException {
460 if (template == null || flagTable == null || flag == null)
463 // First make sure that there are possible visuals.
464 // There's no point in proceeding if no visuals exist.
465 List<FlagTypeVisual> flagTypeVisuals = g.syncRequest( new FlagTypeVisuals(flagTable),
466 TransientCacheListener.<List<FlagTypeVisual>>instance());
467 if (flagTypeVisuals == null || flagTypeVisuals.isEmpty())
470 Variable flagVariable = Variables.getPossibleVariable(g, flag);
471 if (flagVariable == null)
474 for (FlagTypeVisual visual : flagTypeVisuals) {
475 Resource visualComposite = visual.getVisualComposite();
477 String filterReference = visual.getFilteredPropertyReference();
478 if (filterReference == null || filterReference.isEmpty())
479 return visualComposite;
481 String filterPattern = visual.getFilterPattern();
482 if (filterPattern == null || filterPattern.isEmpty())
483 return visualComposite;
485 String value = evaluatePath(g, flag, flagVariable, filterReference);
487 return visualComposite;
490 if (!Pattern.matches(filterPattern, value)) {
491 // filter is defined but property don't match
494 } catch (PatternSyntaxException ex) {
495 ErrorLogger.defaultLogError(ex);
499 return visualComposite;
507 * @param flag the flag to which to apply the filter reference paths
508 * @param visual the visual to look for filters from
509 * @return <code>true</code> if filter passes, <code>false</code> if not
510 * @throws DatabaseException
512 private boolean acceptVisual(ReadGraph graph, Resource flag, Resource visual) throws DatabaseException {
513 List<FlagTypeFilter> filters = graph.syncRequest(new FlagTypeFilters(visual),
514 TransientCacheListener.<List<FlagTypeFilter>>instance());
515 if (filters.isEmpty())
518 Variable flagVariable = Variables.getPossibleVariable(graph, flag);
519 if (flagVariable == null)
522 for (FlagTypeFilter filter : filters) {
523 String reference = filter.getReference();
524 if (reference == null || reference.isEmpty())
527 String pattern = filter.getPattern();
528 if (pattern == null || pattern.isEmpty())
531 String value = evaluatePath(graph, flag, flagVariable, reference);
536 if (Pattern.matches(pattern, value) == filter.isMatchRequired()) {
539 } catch (PatternSyntaxException ex) {
540 ErrorLogger.defaultLogError(ex);