|
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 |
|
| 28 |
public DiagramGeometryWrapper(IFile file, SapphireDiagramEditorPart diagramPart) |
| 29 |
throws ResourceStoreException |
| 30 |
{ |
| 31 |
this.file = file; |
| 32 |
this.diagramPart = diagramPart; |
| 33 |
this.nodeGeometries = new HashMap<DiagramNodePart, Bounds>(); |
| 34 |
this.connectionBendpoints = new HashMap<DiagramConnectionPart, List<Point>>(); |
| 35 |
read(); |
| 36 |
} |
| 37 |
|
| 38 |
public void addNode(DiagramNodePart nodePart, int x, int y, int w, int h) |
| 39 |
{ |
| 40 |
Bounds bounds = new Bounds(x, y, w, h); |
| 41 |
this.nodeGeometries.put(nodePart, bounds); |
| 42 |
} |
| 43 |
|
| 44 |
public void removeNode(DiagramNodePart nodePart) |
| 45 |
{ |
| 46 |
this.nodeGeometries.remove(nodePart); |
| 47 |
} |
| 48 |
|
| 49 |
public void updateNode(DiagramNodePart nodePart, int x, int y) |
| 50 |
{ |
| 51 |
Bounds bounds = this.nodeGeometries.get(nodePart); |
| 52 |
if (bounds != null) |
| 53 |
{ |
| 54 |
bounds.x = x; |
| 55 |
bounds.y = y; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public void updateNode(DiagramNodePart nodePart, int x, int y, int w, int h) |
| 60 |
{ |
| 61 |
Bounds bounds = this.nodeGeometries.get(nodePart); |
| 62 |
if (bounds != null) |
| 63 |
{ |
| 64 |
bounds.x = x; |
| 65 |
bounds.y = y; |
| 66 |
bounds.height = h; |
| 67 |
bounds.width = w; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public Bounds getNode(DiagramNodePart nodePart) |
| 72 |
{ |
| 73 |
return this.nodeGeometries.get(nodePart); |
| 74 |
} |
| 75 |
|
| 76 |
public void addConnectionBendpoint(DiagramConnectionPart connPart, int index, int x, int y) |
| 77 |
{ |
| 78 |
List<Point> bendpoints = this.connectionBendpoints.get(connPart); |
| 79 |
if (bendpoints == null) |
| 80 |
{ |
| 81 |
bendpoints = new ArrayList<Point>(); |
| 82 |
this.connectionBendpoints.put(connPart, bendpoints); |
| 83 |
} |
| 84 |
Point newPt = new Point(x, y); |
| 85 |
bendpoints.add(index, newPt); |
| 86 |
} |
| 87 |
|
| 88 |
public void removeConnectionBendpoint(DiagramConnectionPart connPart, int index) |
| 89 |
{ |
| 90 |
List<Point> bendpoints = this.connectionBendpoints.get(connPart); |
| 91 |
if (bendpoints != null) |
| 92 |
{ |
| 93 |
bendpoints.remove(index); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public boolean setConnectionBendpoint(DiagramConnectionPart connPart, int index, int x, int y) |
| 98 |
{ |
| 99 |
List<Point> bendpoints = this.connectionBendpoints.get(connPart); |
| 100 |
if (bendpoints != null && index < bendpoints.size()) |
| 101 |
{ |
| 102 |
bendpoints.set(index, new Point(x, y)); |
| 103 |
return true; |
| 104 |
} |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
public List<Point> getConnectionBendpoints(DiagramConnectionPart connPart) |
| 110 |
{ |
| 111 |
return this.connectionBendpoints.get(connPart); |
| 112 |
} |
| 113 |
|
| 114 |
public void removeConnectionBendpoints(DiagramConnectionPart connPart) |
| 115 |
{ |
| 116 |
this.connectionBendpoints.remove(connPart); |
| 117 |
} |
| 118 |
|
| 119 |
public void removeAllConnectionBendpoints() |
| 120 |
{ |
| 121 |
this.connectionBendpoints.clear(); |
| 122 |
} |
| 123 |
|
| 124 |
public void read() throws ResourceStoreException |
| 125 |
{ |
| 126 |
final XmlResourceStore resourceStore = new XmlResourceStore( new WorkspaceFileResourceStore(this.file )); |
| 127 |
this.geometryModel = IDiagramGeometry.TYPE.instantiate(new RootXmlResource( resourceStore )); |
| 128 |
|
| 129 |
ModelElementList<IDiagramNodeGeometry> nodes = this.geometryModel.getDiagramNodeGeometries(); |
| 130 |
for (IDiagramNodeGeometry node : nodes) |
| 131 |
{ |
| 132 |
String id = node.getNodeId().getContent(); |
| 133 |
DiagramNodePart nodePart = getNodeElement(id); |
| 134 |
if (nodePart != null) |
| 135 |
{ |
| 136 |
int x = node.getX().getContent() != null ? node.getX().getContent() : -1; |
| 137 |
int y = node.getY().getContent() != null ? node.getY().getContent() : -1; |
| 138 |
int width = node.getWidth().getContent() != null ? node.getWidth().getContent() : -1; |
| 139 |
int height = node.getHeight().getContent() != null ? node.getHeight().getContent() : -1; |
| 140 |
this.addNode(nodePart, x, y, width, height); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
ModelElementList<IDiagramConnectionGeometry> connList = this.geometryModel.getDiagramConnectionGeometries(); |
| 145 |
for (IDiagramConnectionGeometry connBend : connList) |
| 146 |
{ |
| 147 |
String connId = connBend.getConnectionId().getContent(); |
| 148 |
DiagramConnectionPart connPart = getConnectionElement(connId); |
| 149 |
if (connPart != null) |
| 150 |
{ |
| 151 |
ModelElementList<IBendPoint> bps = connBend.getConnectionBendpoints(); |
| 152 |
int index = 0; |
| 153 |
for (IBendPoint pt : bps) |
| 154 |
{ |
| 155 |
this.addConnectionBendpoint(connPart, index++, pt.getX().getContent(), pt.getY().getContent()); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public void write() throws ResourceStoreException |
| 162 |
{ |
| 163 |
this.geometryModel.getDiagramNodeGeometries().clear(); |
| 164 |
Iterator<DiagramNodePart> it = this.nodeGeometries.keySet().iterator(); |
| 165 |
while (it.hasNext()) |
| 166 |
{ |
| 167 |
DiagramNodePart nodePart = it.next(); |
| 168 |
Bounds bounds = this.nodeGeometries.get(nodePart); |
| 169 |
String id = nodePart.getInstanceId(); |
| 170 |
|
| 171 |
if (bounds != null && id != null) |
| 172 |
{ |
| 173 |
IDiagramNodeGeometry diagramNode = this.geometryModel.getDiagramNodeGeometries().addNewElement(); |
| 174 |
diagramNode.setNodeId(id); |
| 175 |
diagramNode.setX(bounds.x); |
| 176 |
diagramNode.setY(bounds.y); |
| 177 |
diagramNode.setWidth(bounds.width); |
| 178 |
diagramNode.setHeight(bounds.height); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
this.geometryModel.getDiagramConnectionGeometries().clear(); |
| 183 |
Iterator<DiagramConnectionPart> connIt = this.connectionBendpoints.keySet().iterator(); |
| 184 |
while (connIt.hasNext()) |
| 185 |
{ |
| 186 |
DiagramConnectionPart connPart = connIt.next(); |
| 187 |
List<Point> bps = this.connectionBendpoints.get(connPart); |
| 188 |
String id = connPart.getInstanceId(); |
| 189 |
|
| 190 |
if (bps != null && id != null) |
| 191 |
{ |
| 192 |
IDiagramConnectionGeometry conn = this.geometryModel.getDiagramConnectionGeometries().addNewElement(); |
| 193 |
conn.setConnectionId(id); |
| 194 |
for (Point pt : bps) |
| 195 |
{ |
| 196 |
IBendPoint pt2 = conn.getConnectionBendpoints().addNewElement(); |
| 197 |
pt2.setX(pt.x); |
| 198 |
pt2.setY(pt.y); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
this.geometryModel.resource().save(); |
| 204 |
} |
| 205 |
|
| 206 |
private DiagramNodePart getNodeElement(String nodeId) |
| 207 |
{ |
| 208 |
for (DiagramNodeTemplate nodeTemplate : this.diagramPart.getNodeTemplates()) |
| 209 |
{ |
| 210 |
for (DiagramNodePart nodePart : nodeTemplate.getDiagramNodes()) |
| 211 |
{ |
| 212 |
String nodeId2 = nodePart.getInstanceId(); |
| 213 |
if (nodeId != null && nodeId2 != null && nodeId.equals(nodeId2)) |
| 214 |
{ |
| 215 |
return nodePart; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
return null; |
| 220 |
} |
| 221 |
|
| 222 |
private DiagramConnectionPart getConnectionElement(String connId) |
| 223 |
{ |
| 224 |
for (DiagramConnectionTemplate connTemplate : this.diagramPart.getConnectionTemplates()) |
| 225 |
{ |
| 226 |
for (DiagramConnectionPart connPart : connTemplate.getDiagramConnections()) |
| 227 |
{ |
| 228 |
String connId2 = connPart.getInstanceId(); |
| 229 |
if (connId != null && connId2 != null && connId.equals(connId2)) |
| 230 |
{ |
| 231 |
return connPart; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
return null; |
| 236 |
} |
| 237 |
|
| 238 |
// ------------------------------------------------------------------- |
| 239 |
// Inner classes |
| 240 |
// ------------------------------------------------------------------- |
| 241 |
|
| 242 |
public static class Point |
| 243 |
{ |
| 244 |
public int x; |
| 245 |
public int y; |
| 246 |
|
| 247 |
public Point(int x, int y) |
| 248 |
{ |
| 249 |
this.x = x; |
| 250 |
this.y = y; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
public static final class Bounds extends Point |
| 255 |
{ |
| 256 |
public int width; |
| 257 |
public int height; |
| 258 |
|
| 259 |
public Bounds(int x, int y, int width, int height) |
| 260 |
{ |
| 261 |
super(x, y); |
| 262 |
this.width = width; |
| 263 |
this.height = height; |
| 264 |
} |
| 265 |
} |
| 266 |
} |