|
Added
Link Here
|
| 1 |
package org.eclipse.sapphire.ui.diagram.editor; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.HashMap; |
| 5 |
import java.util.Iterator; |
| 6 |
import java.util.List; |
| 7 |
|
| 8 |
import org.eclipse.core.resources.IFile; |
| 9 |
import org.eclipse.sapphire.modeling.ModelElementList; |
| 10 |
import org.eclipse.sapphire.modeling.ResourceStoreException; |
| 11 |
import org.eclipse.sapphire.modeling.WorkspaceFileResourceStore; |
| 12 |
import org.eclipse.sapphire.modeling.xml.RootXmlResource; |
| 13 |
import org.eclipse.sapphire.modeling.xml.XmlResourceStore; |
| 14 |
import org.eclipse.sapphire.ui.diagram.geometry.IBendPoint; |
| 15 |
import org.eclipse.sapphire.ui.diagram.geometry.IDiagramConnectionGeometry; |
| 16 |
import org.eclipse.sapphire.ui.diagram.geometry.IDiagramGeometry; |
| 17 |
import org.eclipse.sapphire.ui.diagram.geometry.IDiagramNodeGeometry; |
| 18 |
|
| 19 |
public class DiagramGeometryWrapper |
| 20 |
{ |
| 21 |
private IFile file; |
| 22 |
private IDiagramGeometry geometryModel; |
| 23 |
private SapphireDiagramEditorPart diagramPart; |
| 24 |
|
| 25 |
private HashMap<DiagramNodePart, Bounds> nodeGeometries; |
| 26 |
private HashMap<DiagramConnectionPart, List<Point>> connectionBendpoints; |
| 27 |
private HashMap<DiagramNodePart, HashMap<DiagramConnectionPart, List<Point>>> embeddedConnectionBendpoints; |
| 28 |
|
| 29 |
public DiagramGeometryWrapper(IFile file, SapphireDiagramEditorPart diagramPart) |
| 30 |
throws ResourceStoreException |
| 31 |
{ |
| 32 |
this.file = file; |
| 33 |
this.diagramPart = diagramPart; |
| 34 |
this.nodeGeometries = new HashMap<DiagramNodePart, Bounds>(); |
| 35 |
this.connectionBendpoints = new HashMap<DiagramConnectionPart, List<Point>>(); |
| 36 |
this.embeddedConnectionBendpoints = |
| 37 |
new HashMap<DiagramNodePart, HashMap<DiagramConnectionPart, List<Point>>>(); |
| 38 |
read(); |
| 39 |
} |
| 40 |
|
| 41 |
public void addNode(DiagramNodePart nodePart, int x, int y, int w, int h) |
| 42 |
{ |
| 43 |
Bounds bounds = new Bounds(x, y, w, h); |
| 44 |
this.nodeGeometries.put(nodePart, bounds); |
| 45 |
} |
| 46 |
|
| 47 |
public void removeNode(DiagramNodePart nodePart) |
| 48 |
{ |
| 49 |
this.nodeGeometries.remove(nodePart); |
| 50 |
} |
| 51 |
|
| 52 |
public void updateNode(DiagramNodePart nodePart, int x, int y) |
| 53 |
{ |
| 54 |
Bounds bounds = this.nodeGeometries.get(nodePart); |
| 55 |
if (bounds != null) |
| 56 |
{ |
| 57 |
bounds.x = x; |
| 58 |
bounds.y = y; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public void updateNode(DiagramNodePart nodePart, int x, int y, int w, int h) |
| 63 |
{ |
| 64 |
Bounds bounds = this.nodeGeometries.get(nodePart); |
| 65 |
if (bounds != null) |
| 66 |
{ |
| 67 |
bounds.x = x; |
| 68 |
bounds.y = y; |
| 69 |
bounds.height = h; |
| 70 |
bounds.width = w; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public Bounds getNode(DiagramNodePart nodePart) |
| 75 |
{ |
| 76 |
return this.nodeGeometries.get(nodePart); |
| 77 |
} |
| 78 |
|
| 79 |
public void addConnectionBendpoint(DiagramConnectionPart connPart, int index, int x, int y) |
| 80 |
{ |
| 81 |
HashMap<DiagramConnectionPart, List<Point>> connBendpointsMap = |
| 82 |
getConnectionBenpointsMap(connPart, true); |
| 83 |
List<Point> bendpoints = connBendpointsMap.get(connPart); |
| 84 |
if (bendpoints == null) |
| 85 |
{ |
| 86 |
bendpoints = new ArrayList<Point>(); |
| 87 |
connBendpointsMap.put(connPart, bendpoints); |
| 88 |
} |
| 89 |
Point newPt = new Point(x, y); |
| 90 |
bendpoints.add(index, newPt); |
| 91 |
} |
| 92 |
|
| 93 |
public void removeConnectionBendpoint(DiagramConnectionPart connPart, int index) |
| 94 |
{ |
| 95 |
HashMap<DiagramConnectionPart, List<Point>> connBendpointsMap = |
| 96 |
getConnectionBenpointsMap(connPart, false); |
| 97 |
if (connBendpointsMap == null) |
| 98 |
{ |
| 99 |
throw new RuntimeException("Could not locate connection benpoints map for the embedded connection"); |
| 100 |
} |
| 101 |
List<Point> bendpoints = connBendpointsMap.get(connPart); |
| 102 |
if (bendpoints != null) |
| 103 |
{ |
| 104 |
bendpoints.remove(index); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public boolean updateConnectionBendpoint(DiagramConnectionPart connPart, int index, int x, int y) |
| 109 |
{ |
| 110 |
HashMap<DiagramConnectionPart, List<Point>> connBendpointsMap = |
| 111 |
getConnectionBenpointsMap(connPart, false); |
| 112 |
if (connBendpointsMap == null) |
| 113 |
{ |
| 114 |
throw new RuntimeException("Could not locate connection benpoints map for the embedded connection"); |
| 115 |
} |
| 116 |
List<Point> bendpoints = connBendpointsMap.get(connPart); |
| 117 |
if (bendpoints != null && index < bendpoints.size()) |
| 118 |
{ |
| 119 |
bendpoints.set(index, new Point(x, y)); |
| 120 |
return true; |
| 121 |
} |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
public List<Point> getConnectionBendpoints(DiagramConnectionPart connPart) |
| 127 |
{ |
| 128 |
HashMap<DiagramConnectionPart, List<Point>> connBendpointsMap = |
| 129 |
getConnectionBenpointsMap(connPart, false); |
| 130 |
if (connBendpointsMap != null) |
| 131 |
{ |
| 132 |
return connBendpointsMap.get(connPart); |
| 133 |
} |
| 134 |
return null; |
| 135 |
} |
| 136 |
|
| 137 |
public void read() throws ResourceStoreException |
| 138 |
{ |
| 139 |
final XmlResourceStore resourceStore = new XmlResourceStore( new WorkspaceFileResourceStore(this.file )); |
| 140 |
this.geometryModel = IDiagramGeometry.TYPE.instantiate(new RootXmlResource( resourceStore )); |
| 141 |
|
| 142 |
ModelElementList<IDiagramNodeGeometry> nodes = this.geometryModel.getDiagramNodeGeometries(); |
| 143 |
for (IDiagramNodeGeometry node : nodes) |
| 144 |
{ |
| 145 |
String id = node.getNodeId().getContent(); |
| 146 |
DiagramNodePart nodePart = getNodeElement(id); |
| 147 |
if (nodePart != null) |
| 148 |
{ |
| 149 |
int x = node.getX().getContent() != null ? node.getX().getContent() : -1; |
| 150 |
int y = node.getY().getContent() != null ? node.getY().getContent() : -1; |
| 151 |
int width = node.getWidth().getContent() != null ? node.getWidth().getContent() : -1; |
| 152 |
int height = node.getHeight().getContent() != null ? node.getHeight().getContent() : -1; |
| 153 |
this.addNode(nodePart, x, y, width, height); |
| 154 |
|
| 155 |
ModelElementList<IDiagramConnectionGeometry> connList = node.getEmbeddedConnectionGeometries(); |
| 156 |
for (IDiagramConnectionGeometry connBend : connList) |
| 157 |
{ |
| 158 |
String connId = connBend.getConnectionId().getContent(); |
| 159 |
DiagramConnectionPart connPart = getConnectionElement(nodePart, connId); |
| 160 |
if (connPart != null) |
| 161 |
{ |
| 162 |
ModelElementList<IBendPoint> bps = connBend.getConnectionBendpoints(); |
| 163 |
int index = 0; |
| 164 |
for (IBendPoint pt : bps) |
| 165 |
{ |
| 166 |
this.addConnectionBendpoint(connPart, index++, pt.getX().getContent(), pt.getY().getContent()); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
ModelElementList<IDiagramConnectionGeometry> connList = this.geometryModel.getDiagramConnectionGeometries(); |
| 175 |
for (IDiagramConnectionGeometry connBend : connList) |
| 176 |
{ |
| 177 |
String connId = connBend.getConnectionId().getContent(); |
| 178 |
DiagramConnectionPart connPart = getConnectionElement(connId); |
| 179 |
if (connPart != null) |
| 180 |
{ |
| 181 |
ModelElementList<IBendPoint> bps = connBend.getConnectionBendpoints(); |
| 182 |
int index = 0; |
| 183 |
for (IBendPoint pt : bps) |
| 184 |
{ |
| 185 |
this.addConnectionBendpoint(connPart, index++, pt.getX().getContent(), pt.getY().getContent()); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
public void write() throws ResourceStoreException |
| 192 |
{ |
| 193 |
this.geometryModel.getDiagramNodeGeometries().clear(); |
| 194 |
Iterator<DiagramNodePart> it = this.nodeGeometries.keySet().iterator(); |
| 195 |
while (it.hasNext()) |
| 196 |
{ |
| 197 |
DiagramNodePart nodePart = it.next(); |
| 198 |
Bounds bounds = this.nodeGeometries.get(nodePart); |
| 199 |
String id = nodePart.getInstanceId(); |
| 200 |
|
| 201 |
if (bounds != null && id != null) |
| 202 |
{ |
| 203 |
IDiagramNodeGeometry diagramNode = this.geometryModel.getDiagramNodeGeometries().addNewElement(); |
| 204 |
diagramNode.setNodeId(id); |
| 205 |
diagramNode.setX(bounds.x); |
| 206 |
diagramNode.setY(bounds.y); |
| 207 |
diagramNode.setWidth(bounds.width); |
| 208 |
diagramNode.setHeight(bounds.height); |
| 209 |
|
| 210 |
// save the embedded connection bendpoints |
| 211 |
HashMap<DiagramConnectionPart, List<Point>> embeddedConnBendpointsMap = |
| 212 |
this.embeddedConnectionBendpoints.get(nodePart); |
| 213 |
if (embeddedConnBendpointsMap != null) |
| 214 |
{ |
| 215 |
diagramNode.getEmbeddedConnectionGeometries().clear(); |
| 216 |
addConnectionBenpointsToModel(embeddedConnBendpointsMap, |
| 217 |
diagramNode.getEmbeddedConnectionGeometries()); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
this.geometryModel.getDiagramConnectionGeometries().clear(); |
| 223 |
addConnectionBenpointsToModel(this.connectionBendpoints, |
| 224 |
this.geometryModel.getDiagramConnectionGeometries()); |
| 225 |
this.geometryModel.resource().save(); |
| 226 |
} |
| 227 |
|
| 228 |
private DiagramNodePart getNodeElement(String nodeId) |
| 229 |
{ |
| 230 |
for (DiagramNodeTemplate nodeTemplate : this.diagramPart.getNodeTemplates()) |
| 231 |
{ |
| 232 |
for (DiagramNodePart nodePart : nodeTemplate.getDiagramNodes()) |
| 233 |
{ |
| 234 |
String nodeId2 = nodePart.getInstanceId(); |
| 235 |
if (nodeId != null && nodeId2 != null && nodeId.equals(nodeId2)) |
| 236 |
{ |
| 237 |
return nodePart; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
return null; |
| 242 |
} |
| 243 |
|
| 244 |
private DiagramConnectionPart getConnectionElement(String connId) |
| 245 |
{ |
| 246 |
for (DiagramConnectionTemplate connTemplate : this.diagramPart.getConnectionTemplates()) |
| 247 |
{ |
| 248 |
for (DiagramConnectionPart connPart : connTemplate.getDiagramConnections()) |
| 249 |
{ |
| 250 |
String connId2 = connPart.getInstanceId(); |
| 251 |
if (connId != null && connId2 != null && connId.equals(connId2)) |
| 252 |
{ |
| 253 |
return connPart; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
return null; |
| 258 |
} |
| 259 |
|
| 260 |
private DiagramConnectionPart getConnectionElement(DiagramNodePart nodePart, String connId) |
| 261 |
{ |
| 262 |
DiagramNodeTemplate nodeTemplate = nodePart.getDiagramNodeTemplate(); |
| 263 |
DiagramEmbeddedConnectionTemplate connTemplate = |
| 264 |
nodeTemplate.getEmbeddedConnectionTemplate(); |
| 265 |
if (connTemplate != null) |
| 266 |
{ |
| 267 |
List<DiagramConnectionPart> connParts = connTemplate.getDiagramConnections(); |
| 268 |
for (DiagramConnectionPart connPart : connParts) |
| 269 |
{ |
| 270 |
String connId2 = connPart.getInstanceId(); |
| 271 |
if (connId != null && connId2 != null && connId.equals(connId2)) |
| 272 |
{ |
| 273 |
return connPart; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
return null; |
| 278 |
} |
| 279 |
|
| 280 |
private HashMap<DiagramConnectionPart, List<Point>> getConnectionBenpointsMap(DiagramConnectionPart connPart, boolean create) |
| 281 |
{ |
| 282 |
HashMap<DiagramConnectionPart, List<Point>> connBendpointsMap = null; |
| 283 |
if (connPart instanceof DiagramEmbeddedConnectionPart) |
| 284 |
{ |
| 285 |
DiagramNodePart srcNodePart = ((DiagramEmbeddedConnectionPart)connPart).getSourceNodePart(); |
| 286 |
if (srcNodePart == null) |
| 287 |
{ |
| 288 |
throw new RuntimeException("Could not locate the source node for the embedded connection"); |
| 289 |
} |
| 290 |
connBendpointsMap = this.embeddedConnectionBendpoints.get(srcNodePart); |
| 291 |
if (connBendpointsMap == null) |
| 292 |
{ |
| 293 |
if (create) |
| 294 |
{ |
| 295 |
connBendpointsMap = new HashMap<DiagramConnectionPart, List<Point>>(); |
| 296 |
this.embeddedConnectionBendpoints.put(srcNodePart, connBendpointsMap); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
else |
| 301 |
{ |
| 302 |
connBendpointsMap = this.connectionBendpoints; |
| 303 |
} |
| 304 |
return connBendpointsMap; |
| 305 |
} |
| 306 |
|
| 307 |
private void addConnectionBenpointsToModel(HashMap<DiagramConnectionPart, List<Point>> connBendpointsMap, |
| 308 |
ModelElementList<IDiagramConnectionGeometry> connGeometries) |
| 309 |
{ |
| 310 |
Iterator<DiagramConnectionPart> connIt = connBendpointsMap.keySet().iterator(); |
| 311 |
while (connIt.hasNext()) |
| 312 |
{ |
| 313 |
DiagramConnectionPart connPart = connIt.next(); |
| 314 |
List<Point> bps = connBendpointsMap.get(connPart); |
| 315 |
String id = connPart.getInstanceId(); |
| 316 |
|
| 317 |
if (bps != null && id != null) |
| 318 |
{ |
| 319 |
IDiagramConnectionGeometry conn = connGeometries.addNewElement(); |
| 320 |
conn.setConnectionId(id); |
| 321 |
for (Point pt : bps) |
| 322 |
{ |
| 323 |
IBendPoint pt2 = conn.getConnectionBendpoints().addNewElement(); |
| 324 |
pt2.setX(pt.x); |
| 325 |
pt2.setY(pt.y); |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
// ------------------------------------------------------------------- |
| 333 |
// Inner classes |
| 334 |
// ------------------------------------------------------------------- |
| 335 |
|
| 336 |
public static class Point |
| 337 |
{ |
| 338 |
public int x; |
| 339 |
public int y; |
| 340 |
|
| 341 |
public Point(int x, int y) |
| 342 |
{ |
| 343 |
this.x = x; |
| 344 |
this.y = y; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
public static final class Bounds extends Point |
| 349 |
{ |
| 350 |
public int width; |
| 351 |
public int height; |
| 352 |
|
| 353 |
public Bounds(int x, int y, int width, int height) |
| 354 |
{ |
| 355 |
super(x, y); |
| 356 |
this.width = width; |
| 357 |
this.height = height; |
| 358 |
} |
| 359 |
} |
| 360 |
} |