]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/participant/TranslateMode2.java
Some enhancements to GraphLayer-related utilities for Diagram layers
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / participant / TranslateMode2.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.geom.AffineTransform;
15 import java.awt.geom.Point2D;
16 import java.awt.geom.Rectangle2D;
17 import java.util.ArrayList;
18 import java.util.Collection;
19
20 import org.simantics.Simantics;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.CommentMetadata;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.diagram.content.ConnectionUtil;
27 import org.simantics.diagram.elements.ElementTransforms;
28 import org.simantics.diagram.elements.ElementTransforms.TransformedObject;
29 import org.simantics.diagram.flag.IOTableUtil;
30 import org.simantics.diagram.flag.IOTablesInfo;
31 import org.simantics.diagram.stubs.DiagramResource;
32 import org.simantics.diagram.ui.DiagramModelHints;
33 import org.simantics.g2d.canvas.Hints;
34 import org.simantics.g2d.diagram.participant.pointertool.TranslateMode;
35 import org.simantics.g2d.element.ElementHints;
36 import org.simantics.g2d.element.ElementUtils;
37 import org.simantics.g2d.element.IElement;
38 import org.simantics.g2d.element.handler.Move;
39 import org.simantics.scenegraph.g2d.IG2DNode;
40 import org.simantics.scenegraph.g2d.nodes.Decoration;
41 import org.simantics.scenegraph.utils.NodeUtil;
42 import org.simantics.utils.ui.ErrorLogger;
43
44 /**
45  * This participant handles the diagram in translate elements mode with support
46  * for route graph connections.
47  * 
48  * @author Tuukka Lehtonen
49  */
50 public class TranslateMode2 extends TranslateMode {
51
52     public TranslateMode2(Point2D startingPoint, Point2D currentPoint, int mouseId, Collection<IElement> elements) {
53         super(startingPoint, currentPoint, mouseId, elements);
54     }
55
56     @Override
57     protected boolean commit() {
58         for (IElement el : elementsToReallyTranslate) {
59             Move move = el.getElementClass().getAtMostOneItemOfClass(Move.class);
60             if (move != null) {
61                 Point2D oldPos = move.getPosition(el);
62                 move.moveTo(el, oldPos.getX() + dx, oldPos.getY() + dy);
63             }
64         }
65  
66         try {
67             Simantics.getSession().syncRequest(new WriteRequest() {
68                 @Override
69                 public void perform(WriteGraph graph) throws DatabaseException {
70                     DiagramResource DIA = DiagramResource.getInstance(graph);
71
72                     Collection<TransformedObject> transformed = new ArrayList<TransformedObject>();
73                     for (IElement e : elementsToReallyTranslate) {
74                         Object obj = ElementUtils.getObject(e);
75                         if (obj instanceof Resource) {
76                             Resource r = (Resource) obj;
77                             if (graph.isInstanceOf(r, DIA.RouteGraphConnection))
78                                 continue;
79                             if (graph.isInstanceOf(r, DIA.Flag)) {
80                                 if(handleFlag(graph, DIA, e, r))
81                                     continue;
82                             }
83                             AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform());
84                             transformed.add( new TransformedObject((Resource) obj, at) );
85                         }
86                     }
87
88                     if (!transformed.isEmpty()) {
89                         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
90                         graph.addMetadata(cm.add("Translated " + transformed.size() + " "
91                                 + (transformed.size() == 1 ? "element" : "elements")
92                                 + " by (" + dx + "," + dy + ") mm."));
93                         graph.markUndoPoint();
94                     }
95
96                     // Normal transforms
97                     ElementTransforms.setTransformRequest(transformed).perform(graph);
98
99                     // Custom handling for moving interior route nodes
100                     ConnectionUtil cu = new ConnectionUtil(graph);
101                     for (IElement c : translatedConnections) {
102                         Object obj = (Object) ElementUtils.getObject(c);
103                         if (obj instanceof Resource) {
104                             Resource connection = (Resource) obj;
105                             if (graph.isInstanceOf(connection, DIA.RouteGraphConnection)) {
106                                 cu.translateRouteNodes(connection, dx, dy);
107                             }
108                         }
109                     }
110                 }
111             });
112         } catch (DatabaseException err) {
113             ErrorLogger.defaultLogError(err);
114         }
115
116         for (IElement dirty : elementsToDirty)
117             dirty.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
118
119         setDirty();
120         remove();
121         return false;
122     }
123     
124     IOTablesInfo ioTablesInfo = null;
125     
126     private boolean handleFlag(WriteGraph graph, DiagramResource DIA,
127             IElement flagElement, Resource flagResource) throws DatabaseException {
128         if(ioTablesInfo == null)
129             ioTablesInfo = IOTableUtil.getIOTablesInfo(graph, 
130                     (Resource)diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE));
131         
132         // Use the center of the flag for determining the table binding
133         Object sgNode = flagElement.getHint(ElementHints.KEY_SG_NODE);
134         if(!(sgNode instanceof IG2DNode))
135             return false;
136         Rectangle2D flagBounds = NodeUtil.getLocalBounds((IG2DNode)sgNode, Decoration.class);
137         Point2D translateVector = getTranslateVector();
138         double flagX = flagBounds.getCenterX()+translateVector.getX();
139         double flagY = flagBounds.getCenterY()+translateVector.getY();
140         return ioTablesInfo.updateBinding(graph, DIA, flagResource, flagX, flagY);
141     }
142
143 }