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.Connector; 24 import CH.ifa.draw.framework.Drawing; 25 import CH.ifa.draw.framework.Figure; 26 import CH.ifa.draw.framework.FigureEnumeration; 27 import CH.ifa.draw.standard.AbstractTool; 28 import CH.ifa.draw.util.Geom; 29 import org.objectweb.jac.core.Wrapping; 30 import org.objectweb.jac.core.Wrappee; 31 import org.objectweb.jac.ide.ModelElement; 32 import org.objectweb.jac.ide.Class; 33 import org.objectweb.jac.ide.PointcutLink; 34 import org.objectweb.jac.ide.Diagram; 35 import org.objectweb.jac.util.Log; 36 import java.awt.Point ; 37 import java.awt.event.MouseEvent ; 38 import java.util.Enumeration ; 39 40 public class PointcutLinkCreationTool extends AbstractTool { 41 42 45 private Connector myStartConnector; 46 private Connector myEndConnector; 47 private Connector myTargetConnector; 48 49 private Figure myTarget; 50 51 54 private ConnectionFigure myConnection; 55 56 60 private Figure myAddedFigure; 61 62 66 private ConnectionFigure fPrototype; 67 68 69 public PointcutLinkCreationTool(DrawingEditor newDrawingEditor, ConnectionFigure newPrototype) { 70 super(newDrawingEditor); 71 fPrototype = newPrototype; 72 } 73 74 77 public void mouseMove(MouseEvent e, int x, int y) { 78 trackConnectors(e, x, y); 79 } 80 81 86 public void mouseDown(MouseEvent e, int x, int y) 87 { 88 int ex = e.getX(); 89 int ey = e.getY(); 90 setTargetFigure(findConnectionStart(ex, ey, drawing())); 91 if (getTargetFigure() != null 92 && getTargetFigure().getClass()==AspectFigure.class) { 93 setStartConnector(findConnector(ex, ey, getTargetFigure())); 94 if (getStartConnector() != null) { 95 Point p = new Point (ex, ey); 96 setConnection(createConnection()); 97 getConnection().startPoint(p.x, p.y); 98 getConnection().endPoint(p.x, p.y); 99 setAddedFigure(view().add(getConnection())); 100 } 101 } 102 } 103 104 107 public void mouseDrag(MouseEvent e, int x, int y) { 108 Point p = new Point (e.getX(), e.getY()); 109 if (getConnection() != null) { 110 trackConnectors(e, x, y); 111 if (getTargetConnector() != null) { 112 p = Geom.center(getTargetConnector().displayBox()); 113 } 114 getConnection().endPoint(p.x, p.y); 115 } 116 } 117 118 122 public void mouseUp(MouseEvent e, int x, int y) { 123 Figure c = null; 124 if (getStartConnector() != null) { 125 c = findTarget(e.getX(), e.getY(), drawing()); 126 } 127 128 if (c != null && (c.getClass()==ClassFigure.class || 129 c.getClass()==InstanceFigure.class)) { 130 setEndConnector(findConnector(e.getX(), e.getY(), c)); 131 if (getEndConnector() != null) { 132 getConnection().connectStart(getStartConnector()); 133 getConnection().connectEnd(getEndConnector()); 134 getConnection().updateConnection(); 135 136 141 AspectFigure source = (AspectFigure)getStartConnector().owner(); 142 ModelElementFigure target = (ModelElementFigure)c; 143 Log.trace("figures","creating a new poincut link between "+ 144 source.getSubstance()+" and "+target.getSubstance()); 145 146 createRelation(source.getClassElement(),target.getSubstance()); 147 148 } 149 } 150 else if (getConnection() != null) { 151 ((DiagramView)editor()).showStatus("Invalid or empty ending element for relation."); 152 view().remove(getConnection()); 153 } 154 155 setConnection(null); 156 setStartConnector(null); 157 setEndConnector(null); 158 setAddedFigure(null); 159 editor().toolDone(); 160 } 161 162 163 protected void createRelation(org.objectweb.jac.ide.Class source, ModelElement target) { 164 165 if(source != null && target != null ) { 166 PointcutLink rel = new PointcutLink(source,(Class )target); 167 LinkFigure linkFigure = (LinkFigure)getConnection(); 169 org.objectweb.jac.ide.LinkFigure linkFig = 170 new org.objectweb.jac.ide.LinkFigure(rel); 171 linkFigure.setLinkFigure(linkFig); 172 173 view().add(linkFigure.createName("newPointcut")); 174 view().add(linkFigure.createEndRole("ALL:ALL")); 175 view().add(linkFigure.createStartRole("?")); 176 ((org.objectweb.jac.ide.Aspect)source).addPointcutLink(rel); 179 180 Diagram diagram = (Diagram)((DiagramView)editor()).getSubstance(); 181 182 diagram.addFigure(linkFig); 183 184 Wrapping.invokeRoleMethod((Wrappee)rel,"registerObject", 185 new Object []{getConnection(),null}); 186 } 187 } 188 189 public void deactivate() { 190 super.deactivate(); 191 if (getTargetFigure() != null) { 192 getTargetFigure().connectorVisibility(false); 193 } 194 } 195 196 200 protected ConnectionFigure createConnection() { 201 return (ConnectionFigure)fPrototype.clone(); 202 } 203 204 207 protected Figure findSource(int x, int y, Drawing drawing) { 208 return findConnectableFigure(x, y, drawing); 209 } 210 211 214 protected Figure findTarget(int x, int y, Drawing drawing) { 215 Figure target = findConnectableFigure(x, y, drawing); 216 Figure start = getStartConnector().owner(); 217 218 if (target != null 219 && getConnection() != null 220 && target.canConnect() 221 && !target.includes(start) 222 && getConnection().canConnect(start, target)) { 223 return target; 224 } 225 return null; 226 } 227 228 231 protected ConnectionFigure findConnection(int x, int y, Drawing drawing) { 232 Enumeration k = drawing.figuresReverse(); 233 while (k.hasMoreElements()) { 234 Figure figure = (Figure) k.nextElement(); 235 figure = figure.findFigureInside(x, y); 236 if (figure != null && (figure instanceof ConnectionFigure)) { 237 return (ConnectionFigure)figure; 238 } 239 } 240 return null; 241 } 242 243 private void setConnection(ConnectionFigure newConnection) { 244 myConnection = newConnection; 245 } 246 247 250 protected ConnectionFigure getConnection() { 251 return myConnection; 252 } 253 254 protected void trackConnectors(MouseEvent e, int x, int y) { 255 Figure c = null; 256 257 if (getStartConnector() == null) { 258 c = findSource(x, y, drawing()); 259 } 260 else { 261 c = findTarget(x, y, drawing()); 262 } 263 264 if (c != getTargetFigure()) { 266 if (getTargetFigure() != null) { 267 getTargetFigure().connectorVisibility(false); 268 } 269 setTargetFigure(c); 270 if (getTargetFigure() != null) { 271 getTargetFigure().connectorVisibility(true); 272 } 273 } 274 275 Connector cc = null; 276 if (c != null) { 277 cc = findConnector(e.getX(), e.getY(), c); 278 } 279 if (cc != getTargetConnector()) { 280 setTargetConnector(cc); 281 } 282 283 view().checkDamage(); 284 } 285 286 private Connector findConnector(int x, int y, Figure f) { 287 return f.connectorAt(x, y); 288 } 289 290 293 protected Figure findConnectionStart(int x, int y, Drawing drawing) { 294 Figure target = findConnectableFigure(x, y, drawing); 295 if ((target != null) && target.canConnect()) { 296 return target; 297 } 298 return null; 299 } 300 301 private Figure findConnectableFigure(int x, int y, Drawing drawing) { 302 FigureEnumeration k = drawing.figuresReverse(); 303 while (k.hasMoreElements()) { 304 Figure figure = k.nextFigure(); 305 if (!figure.includes(getConnection()) && figure.canConnect() 306 && figure.containsPoint(x, y)) { 307 return figure; 308 } 309 } 310 return null; 311 } 312 313 private void setStartConnector(Connector newStartConnector) { 314 myStartConnector = newStartConnector; 315 } 316 317 protected Connector getStartConnector() { 318 return myStartConnector; 319 } 320 321 private void setEndConnector(Connector newEndConnector) { 322 myEndConnector = newEndConnector; 323 } 324 325 protected Connector getEndConnector() { 326 return myEndConnector; 327 } 328 329 private void setTargetConnector(Connector newTargetConnector) { 330 myTargetConnector = newTargetConnector; 331 } 332 333 protected Connector getTargetConnector() { 334 return myTargetConnector; 335 } 336 337 private void setTargetFigure(Figure newTarget) { 338 myTarget = newTarget; 339 } 340 341 protected Figure getTargetFigure() { 342 return myTarget; 343 } 344 345 349 protected Figure getAddedFigure() { 350 return myAddedFigure; 351 } 352 353 private void setAddedFigure(Figure newAddedFigure) { 354 myAddedFigure = newAddedFigure; 355 } 356 357 } 358 | Popular Tags |