1 18 19 package org.objectweb.jac.ide.diagrams; 20 21 import CH.ifa.draw.framework.DrawingEditor; 22 import CH.ifa.draw.framework.ConnectionFigure; 23 import CH.ifa.draw.framework.Figure; 24 import CH.ifa.draw.framework.FigureEnumeration; 25 import CH.ifa.draw.framework.Connector; 26 import CH.ifa.draw.framework.Drawing; 27 28 import CH.ifa.draw.util.Geom; 29 30 import org.objectweb.jac.ide.Class; 31 import org.objectweb.jac.util.Log; 32 33 import java.awt.Point ; 34 import java.awt.event.MouseEvent ; 35 import java.util.Enumeration ; 36 37 public class RelationLinkCreationTool extends AbstractTool { 38 39 40 Connector myStartConnector; 41 Connector myEndConnector; 42 Connector myTargetConnector; 43 44 Figure myTarget; 45 46 47 LinkFigure myConnection; 48 49 public RelationLinkCreationTool(DrawingEditor newDrawingEditor) { 50 super(newDrawingEditor); 51 } 52 53 56 public void mouseMove(MouseEvent e, int x, int y) { 57 trackConnectors(e, x, y); 58 } 59 60 65 public void mouseDown(MouseEvent e, int x, int y) 66 { 67 Log.trace("diagram","mouseDown"); 68 int ex = e.getX(); 69 int ey = e.getY(); 70 myTarget = findConnectionStart(ex, ey, drawing()); 71 Log.trace("diagram","target figure = "+myTarget); 72 if (myTarget != null 73 && (myTarget.getClass()==ClassFigure.class || 74 myTarget.getClass()==AspectFigure.class)) { 75 myStartConnector = findConnector(ex, ey, myTarget); 76 if (myStartConnector != null) { 77 Point p = new Point (ex, ey); 78 myConnection = createLinkFigure(); 79 myConnection.startPoint(p.x, p.y); 80 myConnection.endPoint(p.x, p.y); 81 view().add(myConnection); 82 } 83 } 84 } 85 86 protected LinkFigure createLinkFigure() { 87 return new RelationLinkFigure(); 88 } 89 90 93 public void mouseDrag(MouseEvent e, int x, int y) { 94 Log.trace("diagram",2,"mouseDrag "+x+","+y); 95 Point p = new Point (e.getX(), e.getY()); 96 if (myConnection != null) { 97 trackConnectors(e, x, y); 98 if (myTargetConnector != null) { 99 p = Geom.center(myTargetConnector.displayBox()); 100 } 101 myConnection.endPoint(p.x, p.y); 102 } 103 } 104 105 109 public void mouseUp(MouseEvent e, int x, int y) { 110 Log.trace("diagram","mouseUp "+x+","+y); 111 Log.trace("diagram"," myStartConnector="+myStartConnector); 112 ModelElementFigure dest = null; 113 if (myStartConnector != null) { 114 dest = findTarget(e.getX(), e.getY(), drawing()); 115 Log.trace("diagram"," dest="+dest); 116 } 117 118 if (dest instanceof ClassFigure) { 119 myEndConnector = findConnector(e.getX(), e.getY(), dest); 120 Log.trace("diagram"," myEndConnector="+myEndConnector); 121 if (myEndConnector != null) { 122 myConnection.connectStart(myStartConnector); 123 myConnection.connectEnd(myEndConnector); 124 myConnection.updateConnection(); 125 ClassFigure source = (ClassFigure)myStartConnector.owner(); 126 createRelation(source.getClassElement(), 127 ((ClassFigure)dest).getClassElement()); 128 } 129 } 130 else if (myConnection != null) { 131 ((DiagramView)editor()).showStatus( 132 "Invalid or empty ending element for relation."); 133 view().remove(myConnection); 134 } 135 136 myConnection = null; 137 myStartConnector = null; 138 myEndConnector = null; 139 editor().toolDone(); 140 } 141 142 147 protected void createRelation(Class source, Class target) { 148 if (source != null && target != null) { 149 diagramView().createRelation(source,target,(RelationLinkFigure)myConnection,false); 150 view().clearSelection(); 151 view().addToSelection(myConnection); 152 } 153 } 154 155 public void deactivate() { 156 super.deactivate(); 157 if (myTarget != null) { 158 myTarget.connectorVisibility(false); 159 } 160 } 161 162 163 166 protected Figure findSource(int x, int y, Drawing drawing) { 167 return findConnectableFigure(x, y, drawing); 168 } 169 170 173 protected ModelElementFigure findTarget(int x, int y, Drawing drawing) { 174 ModelElementFigure target = findConnectableFigure(x, y, drawing); 175 Figure start = myStartConnector.owner(); 176 177 if (target != null 178 && myConnection != null 179 && target.canConnect() 180 && myConnection.canConnect(start, target)) { 181 return target; 182 } 183 return null; 184 } 185 186 189 protected ConnectionFigure findConnection(int x, int y, Drawing drawing) { 190 Enumeration k = drawing.figuresReverse(); 191 while (k.hasMoreElements()) { 192 Figure figure = (Figure) k.nextElement(); 193 figure = figure.findFigureInside(x, y); 194 if (figure != null && (figure instanceof ConnectionFigure)) { 195 return (ConnectionFigure)figure; 196 } 197 } 198 return null; 199 } 200 201 protected void trackConnectors(MouseEvent e, int x, int y) { 202 Figure c = null; 203 204 if (myStartConnector == null) { 205 c = findSource(x, y, drawing()); 206 } 207 else { 208 c = findTarget(x, y, drawing()); 209 } 210 211 if (c != myTarget) { 213 if (myTarget != null) { 214 myTarget.connectorVisibility(false); 215 } 216 myTarget = c; 217 if (myTarget != null) { 218 myTarget.connectorVisibility(true); 219 } 220 } 221 222 Connector cc = null; 223 if (c != null) { 224 cc = findConnector(e.getX(), e.getY(), c); 225 } 226 if (cc != myTargetConnector) { 227 myTargetConnector = cc; 228 } 229 230 view().checkDamage(); 231 } 232 233 private Connector findConnector(int x, int y, Figure f) { 234 return f.connectorAt(x, y); 235 } 236 237 240 protected Figure findConnectionStart(int x, int y, Drawing drawing) { 241 Figure target = findConnectableFigure(x, y, drawing); 242 if ((target != null) && target.canConnect()) { 243 return target; 244 } 245 return null; 246 } 247 248 private ModelElementFigure findConnectableFigure( 249 int x, int y, Drawing drawing) 250 { 251 FigureEnumeration k = drawing.figuresReverse(); 252 while (k.hasMoreElements()) { 253 Figure figure = k.nextFigure(); 254 if (!figure.includes(myConnection) && figure.canConnect() 255 && figure.containsPoint(x, y)) { 256 return (ModelElementFigure)figure; 257 } 258 } 259 return null; 260 } 261 262 } 263 | Popular Tags |