-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.diagram.synchronization.graph;\r
-\r
-import java.awt.geom.AffineTransform;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.CommonDBUtils;\r
-import org.simantics.db.common.utils.OrderedSetUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.util.Layer0Utils;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.diagram.synchronization.CopyAdvisor;\r
-import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;\r
-import org.simantics.diagram.synchronization.ModificationAdapter;\r
-import org.simantics.diagram.synchronization.SynchronizationHints;\r
-import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;\r
-import org.simantics.diagram.ui.DiagramModelHints;\r
-import org.simantics.g2d.diagram.DiagramHints;\r
-import org.simantics.g2d.diagram.DiagramMutator;\r
-import org.simantics.g2d.diagram.IDiagram;\r
-import org.simantics.g2d.element.ElementHints;\r
-import org.simantics.g2d.element.ElementUtils;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.layer0.Layer0;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class AddElement extends ModificationAdapter {\r
-\r
- //private static final boolean DEBUG_ELEMENT_COPY = false;\r
-\r
- IModifiableSynchronizationContext context;\r
- IDiagram diagram;\r
- Resource diagramResource;\r
- IElement element;\r
- Resource copyOf;\r
-\r
- public AddElement(IModifiableSynchronizationContext context, IDiagram d, IElement element) {\r
- super(ADD_NODE_PRIORITY);\r
-\r
- assert context != null;\r
- assert d != null;\r
- assert element != null;\r
-\r
- this.context = context;\r
- this.diagram = d;\r
- this.diagramResource = d.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
- assert this.diagramResource != null;\r
- this.element = element;\r
- this.copyOf = element.getHint(ElementHints.KEY_COPY_OF_OBJECT);\r
- }\r
-\r
- @Override\r
- public void perform(WriteGraph g) throws Exception {\r
- BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);\r
- DiagramMutator mutator = diagram.getHint(DiagramHints.KEY_MUTATOR);\r
- \r
- CommonDBUtils.selectClusterSet(g, diagramResource);\r
-\r
- // 1. Resolve the element class to instantiate the new element from.\r
- Resource elementClass = ElementUtils.checkedAdapt(element.getElementClass(), Resource.class);\r
-\r
- // 2. Resolve custom element writer\r
- ElementWriter writer = element.removeHint(DiagramModelHints.KEY_ELEMENT_WRITER);\r
- if (writer == null)\r
- writer = g.adapt(elementClass, ElementWriter.class);\r
-\r
- Resource resource = null;\r
-\r
- // 3. Try to copy the element from an existing element if requested.\r
- if (copyOf instanceof Resource) {\r
- CopyAdvisor ca = diagram.getHint(SynchronizationHints.COPY_ADVISOR);\r
- if (ca != null) {\r
- Resource sourceDiagram = g.getPossibleObject(copyOf, Layer0.getInstance(g).PartOf);\r
- resource = CopyAdvisorUtil.copy(context, g, ca, copyOf, sourceDiagram, diagramResource);\r
- }\r
- }\r
-\r
- if (resource == null) {\r
- // Copying either was not issued or couldn't be performed.\r
- // Create target resource for the new element.\r
- resource = g.newResource();\r
- g.claim(resource, br.L0.InstanceOf, null, elementClass);\r
- }\r
-\r
- // Add comment to change set.\r
-// CommentMetadata cm = g.getMetadata(CommentMetadata.class);\r
-// g.addMetadata(cm.add("Added element " + resource));\r
-\r
- // 4. add the new element to the diagram composite\r
- OrderedSetUtils.add(g, diagramResource, resource);\r
-\r
- // 4.1. Give running name to element and increment the counter attached to the diagram.\r
- String name = claimFreshElementName(g, diagramResource, resource);\r
- \r
- // Add comment to change set.\r
- Layer0Utils.addCommentMetadata(g, "Added element " + name + " " + resource + " to composite " + diagramResource);\r
- \r
- // 4.2. Make the diagram consist of the new element\r
- g.claim(diagramResource, br.L0.ConsistsOf, resource);\r
-\r
- // 5. Synchronize the transformation of the element\r
- AffineTransform at = element.getHint(ElementHints.KEY_TRANSFORM);\r
- if (at != null)\r
- DiagramGraphUtil.setTransform(g, resource, at);\r
- \r
- // 6. Perform custom per element type synchronization to graph\r
- writer.addToGraph(g, element, resource);\r
-\r
- // 7. Register the newly created element resource with mutator in case\r
- // any other modification needs to reference it later on in the mutator\r
- // commit process.\r
- element.setHint(ElementHints.KEY_OBJECT, resource);\r
- mutator.register(element, resource);\r
-\r
- // 7. Put the element on all the currently active layers if possible.\r
- GraphLayerManager glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);\r
- if (glm != null) {\r
- glm.removeFromAllLayers(g, resource);\r
- glm.putElementOnVisibleLayers(diagram, g, resource);\r
- }\r
- }\r
-\r
-// String findFreshName(ReadGraph graph, Resource container) throws DatabaseException {\r
-// Set<String> children = graph.syncRequest(new UnescapedChildMapOfResource(container)).keySet();\r
-// String id;\r
-// for (int i = 0; children.contains(id = String.valueOf(i)); ++i);\r
-// return id;\r
-// }\r
-\r
- /**\r
- * Names the specified element on the specified diagram based on a numeric\r
- * ascending counter attached to the diagram. The purpose of the counter is\r
- * to optimize name generation compared to always getting the names of each\r
- * diagram element to make sure nothing collides. This method is much faster\r
- * but obviously there is room for error.\r
- * \r
- * @param graph\r
- * @param diagram\r
- * @param element\r
- * @return\r
- * @throws DatabaseException\r
- */\r
- public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- DiagramResource DIA = DiagramResource.getInstance(graph);\r
- // Give running name to element and increment the counter attached to the diagram.\r
- Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);\r
- if (l == null)\r
- l = Long.valueOf(0L);\r
- String name = l.toString();\r
- graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);\r
- graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);\r
- return name;\r
- }\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.diagram.synchronization.graph;
+
+import java.awt.geom.AffineTransform;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.utils.CommonDBUtils;
+import org.simantics.db.common.utils.OrderedSetUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.synchronization.CopyAdvisor;
+import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
+import org.simantics.diagram.synchronization.ModificationAdapter;
+import org.simantics.diagram.synchronization.SynchronizationHints;
+import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
+import org.simantics.diagram.ui.DiagramModelHints;
+import org.simantics.g2d.diagram.DiagramHints;
+import org.simantics.g2d.diagram.DiagramMutator;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.layer0.Layer0;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class AddElement extends ModificationAdapter {
+
+ //private static final boolean DEBUG_ELEMENT_COPY = false;
+
+ IModifiableSynchronizationContext context;
+ IDiagram diagram;
+ Resource diagramResource;
+ IElement element;
+ Resource copyOf;
+
+ public AddElement(IModifiableSynchronizationContext context, IDiagram d, IElement element) {
+ super(ADD_NODE_PRIORITY);
+
+ assert context != null;
+ assert d != null;
+ assert element != null;
+
+ this.context = context;
+ this.diagram = d;
+ this.diagramResource = d.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
+ assert this.diagramResource != null;
+ this.element = element;
+ this.copyOf = element.getHint(ElementHints.KEY_COPY_OF_OBJECT);
+ }
+
+ @Override
+ public void perform(WriteGraph g) throws Exception {
+ BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
+ DiagramMutator mutator = diagram.getHint(DiagramHints.KEY_MUTATOR);
+
+ CommonDBUtils.selectClusterSet(g, diagramResource);
+
+ // 1. Resolve the element class to instantiate the new element from.
+ Resource elementClass = ElementUtils.checkedAdapt(element.getElementClass(), Resource.class);
+
+ // 2. Resolve custom element writer
+ ElementWriter writer = element.removeHint(DiagramModelHints.KEY_ELEMENT_WRITER);
+ if (writer == null)
+ writer = g.adapt(elementClass, ElementWriter.class);
+
+ Resource resource = null;
+
+ // 3. Try to copy the element from an existing element if requested.
+ if (copyOf instanceof Resource) {
+ CopyAdvisor ca = diagram.getHint(SynchronizationHints.COPY_ADVISOR);
+ if (ca != null) {
+ Resource sourceDiagram = g.getPossibleObject(copyOf, Layer0.getInstance(g).PartOf);
+ resource = CopyAdvisorUtil.copy(context, g, ca, copyOf, sourceDiagram, diagramResource);
+ }
+ }
+
+ if (resource == null) {
+ // Copying either was not issued or couldn't be performed.
+ // Create target resource for the new element.
+ resource = g.newResource();
+ g.claim(resource, br.L0.InstanceOf, null, elementClass);
+ }
+
+ // Add comment to change set.
+// CommentMetadata cm = g.getMetadata(CommentMetadata.class);
+// g.addMetadata(cm.add("Added element " + resource));
+
+ // 4. add the new element to the diagram composite
+ OrderedSetUtils.add(g, diagramResource, resource);
+
+ // 4.1. Give running name to element and increment the counter attached to the diagram.
+ String name = claimFreshElementName(g, diagramResource, resource);
+
+ // Add comment to change set.
+ Layer0Utils.addCommentMetadata(g, "Added element " + name + " " + resource + " to composite " + diagramResource);
+
+ // 4.2. Make the diagram consist of the new element
+ g.claim(diagramResource, br.L0.ConsistsOf, resource);
+
+ // 5. Synchronize the transformation of the element
+ AffineTransform at = element.getHint(ElementHints.KEY_TRANSFORM);
+ if (at != null)
+ DiagramGraphUtil.setTransform(g, resource, at);
+
+ // 6. Perform custom per element type synchronization to graph
+ writer.addToGraph(g, element, resource);
+
+ // 7. Register the newly created element resource with mutator in case
+ // any other modification needs to reference it later on in the mutator
+ // commit process.
+ element.setHint(ElementHints.KEY_OBJECT, resource);
+ mutator.register(element, resource);
+
+ // 7. Put the element on all the currently active layers if possible.
+ GraphLayerManager glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
+ if (glm != null) {
+ glm.removeFromAllLayers(g, resource);
+ glm.putElementOnVisibleLayers(diagram, g, resource);
+ }
+ }
+
+// String findFreshName(ReadGraph graph, Resource container) throws DatabaseException {
+// Set<String> children = graph.syncRequest(new UnescapedChildMapOfResource(container)).keySet();
+// String id;
+// for (int i = 0; children.contains(id = String.valueOf(i)); ++i);
+// return id;
+// }
+
+ /**
+ * Names the specified element on the specified diagram based on a numeric
+ * ascending counter attached to the diagram. The purpose of the counter is
+ * to optimize name generation compared to always getting the names of each
+ * diagram element to make sure nothing collides. This method is much faster
+ * but obviously there is room for error.
+ *
+ * @param graph
+ * @param diagram
+ * @param element
+ * @return
+ * @throws DatabaseException
+ */
+ public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ // Give running name to element and increment the counter attached to the diagram.
+ Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG);
+ if (l == null)
+ l = Long.valueOf(0L);
+ String name = l.toString();
+ graph.claimLiteral(element, L0.HasName, name, Bindings.STRING);
+ graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG);
+ return name;
+ }
+
}
\ No newline at end of file