| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SelectionRerouteEdge |
|
| 4.5;4.5 |
| 1 | /* $Id: SelectionRerouteEdge.java 17865 2010-01-12 20:45:26Z linus $ | |
| 2 | ***************************************************************************** | |
| 3 | * Copyright (c) 2009 Contributors - see below | |
| 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 | * bobtarling | |
| 11 | ***************************************************************************** | |
| 12 | * | |
| 13 | * Some portions of this file was previously release using the BSD License: | |
| 14 | */ | |
| 15 | ||
| 16 | // Copyright (c) 1996-2007 The Regents of the University of California. All | |
| 17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
| 18 | // software and its documentation without fee, and without a written | |
| 19 | // agreement is hereby granted, provided that the above copyright notice | |
| 20 | // and this paragraph appear in all copies. This software program and | |
| 21 | // documentation are copyrighted by The Regents of the University of | |
| 22 | // California. The software program and documentation are supplied "AS | |
| 23 | // IS", without any accompanying services from The Regents. The Regents | |
| 24 | // does not warrant that the operation of the program will be | |
| 25 | // uninterrupted or error-free. The end-user understands that the program | |
| 26 | // was developed for research purposes and is advised not to rely | |
| 27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
| 28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
| 29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
| 30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
| 31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
| 32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
| 33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
| 35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
| 36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
| 37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
| 38 | ||
| 39 | package org.argouml.uml.diagram.ui; | |
| 40 | ||
| 41 | import org.argouml.uml.diagram.UMLMutableGraphSupport; | |
| 42 | ||
| 43 | import java.awt.Rectangle; | |
| 44 | ||
| 45 | import java.awt.event.MouseEvent; | |
| 46 | ||
| 47 | import java.util.Enumeration; | |
| 48 | ||
| 49 | import org.tigris.gef.base.Globals; | |
| 50 | import org.tigris.gef.base.Editor; | |
| 51 | import org.tigris.gef.base.Layer; | |
| 52 | import org.tigris.gef.base.LayerManager; | |
| 53 | import org.tigris.gef.base.ModeManager; | |
| 54 | import org.tigris.gef.base.ModeCreatePolyEdge; | |
| 55 | import org.tigris.gef.base.FigModifyingMode; | |
| 56 | ||
| 57 | import org.tigris.gef.presentation.Fig; | |
| 58 | import org.tigris.gef.presentation.FigEdge; | |
| 59 | ||
| 60 | /** | |
| 61 | * A general class for rerouting edges, achieved by delegating | |
| 62 | * the re-routing logic to the graphmodels; extends | |
| 63 | * functionality in SelectionEdgeClarifiers. | |
| 64 | * | |
| 65 | * <p>If a graphmodel does not override canChangeConnectedNode() | |
| 66 | * then rerouting is not possible and ArgoUML should behave as if | |
| 67 | * rerouting had never been implemented. | |
| 68 | * | |
| 69 | * @author alexb | |
| 70 | * @since 0.13.2 | |
| 71 | */ | |
| 72 | public class SelectionRerouteEdge extends SelectionEdgeClarifiers { | |
| 73 | ||
| 74 | /** | |
| 75 | * Used to determine if the association is now to self, | |
| 76 | * in which case The association needs automatic layout. | |
| 77 | */ | |
| 78 | private FigNodeModelElement sourceFig; | |
| 79 | ||
| 80 | /** | |
| 81 | * Used to determine if the association is now to self, | |
| 82 | * in which case The association needs automatic layout. | |
| 83 | */ | |
| 84 | private FigNodeModelElement destFig; | |
| 85 | ||
| 86 | /** | |
| 87 | * The re-routing capability it armed if the mouse was previously | |
| 88 | * dragged. | |
| 89 | * <p>prevents just selecting the message then clicking somewhere | |
| 90 | * else on the diagram, | |
| 91 | */ | |
| 92 | private boolean armed; | |
| 93 | ||
| 94 | /** | |
| 95 | * The index of the point on the line of the message. | |
| 96 | * <p>0 = sender end | |
| 97 | * <p>1..* = receiver end | |
| 98 | */ | |
| 99 | private int pointIndex; | |
| 100 | ||
| 101 | /** | |
| 102 | * Creates a new instance of SelectionRerouteEdge | |
| 103 | * | |
| 104 | * @param feme the given Fig | |
| 105 | */ | |
| 106 | public SelectionRerouteEdge(FigEdgeModelElement feme) { | |
| 107 | // TODO: There is a cyclic dependency between SelectionRerouteEdge | |
| 108 | // and FigEdgeModelElement | |
| 109 | 0 | super(feme); |
| 110 | ||
| 111 | // set it to an invalid number by default | |
| 112 | // to make sure it is set correctly. | |
| 113 | 0 | pointIndex = -1; |
| 114 | 0 | } |
| 115 | ||
| 116 | /** | |
| 117 | * Set up for re-routing. | |
| 118 | * | |
| 119 | * {@inheritDoc} | |
| 120 | */ | |
| 121 | public void mousePressed(MouseEvent me) { | |
| 122 | ||
| 123 | // calculate the source and dest figs for to self assoc | |
| 124 | 0 | sourceFig = |
| 125 | (FigNodeModelElement) ((FigEdge) getContent()).getSourceFigNode(); | |
| 126 | 0 | destFig = |
| 127 | (FigNodeModelElement) ((FigEdge) getContent()).getDestFigNode(); | |
| 128 | ||
| 129 | 0 | final Rectangle mousePosition = |
| 130 | new Rectangle(me.getX() - 5, me.getY() - 5, 10, 10); | |
| 131 | //reset the pointIndex | |
| 132 | 0 | pointIndex = -1; |
| 133 | 0 | final int npoints = getContent().getNumPoints(); |
| 134 | 0 | final int[] xs = getContent().getXs(); |
| 135 | 0 | final int[] ys = getContent().getYs(); |
| 136 | 0 | if (mousePosition.contains(xs[0], ys[0])) { |
| 137 | 0 | pointIndex = 0; |
| 138 | 0 | } else if (mousePosition.contains( |
| 139 | xs[npoints - 1], ys[npoints - 1])) { | |
| 140 | 0 | pointIndex = npoints - 1; |
| 141 | } | |
| 142 | ||
| 143 | 0 | super.mousePressed(me); |
| 144 | 0 | } |
| 145 | ||
| 146 | /* | |
| 147 | * Need to 'arm' the rerouting capability with mouseDragged(). | |
| 148 | * <p> | |
| 149 | * Don't arm if the edtior's current mode is a figedge create mode, | |
| 150 | * because once a new edge has been created it is not deselected, | |
| 151 | * therefore on the next create an unwanted reroute is performed. | |
| 152 | * | |
| 153 | * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent) | |
| 154 | */ | |
| 155 | public void mouseDragged(MouseEvent me) { | |
| 156 | ||
| 157 | 0 | Editor editor = Globals.curEditor(); |
| 158 | 0 | ModeManager modeMgr = editor.getModeManager(); |
| 159 | 0 | FigModifyingMode fMode = modeMgr.top(); |
| 160 | ||
| 161 | 0 | if (!(fMode instanceof ModeCreatePolyEdge)) { |
| 162 | 0 | armed = true; |
| 163 | } | |
| 164 | 0 | super.mouseDragged(me); |
| 165 | 0 | } |
| 166 | ||
| 167 | /* | |
| 168 | * Perform re-routing if src/dest nodes have changed. | |
| 169 | * | |
| 170 | * <p>This method needs to be 'armed' by a previous mouseDragged() | |
| 171 | * to avoid the situation where the user just clicks on the node | |
| 172 | * then clicks on some unrelated Fig, without moving the edge... | |
| 173 | * | |
| 174 | * <p>TODO: improve the fig finding algorithm to find the top most fig | |
| 175 | * in the layer. will be useful for nested states in a statechart. | |
| 176 | * | |
| 177 | * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent) | |
| 178 | */ | |
| 179 | public void mouseReleased(MouseEvent me) { | |
| 180 | // check pre-conds | |
| 181 | 0 | if (me.isConsumed() || !armed || pointIndex == -1) { |
| 182 | 0 | armed = false; |
| 183 | 0 | super.mouseReleased(me); |
| 184 | 0 | return; |
| 185 | } | |
| 186 | ||
| 187 | //Set-up: | |
| 188 | 0 | int x = me.getX(), y = me.getY(); |
| 189 | // the fig that was under the mouse when it was released | |
| 190 | 0 | FigNodeModelElement newFig = null; |
| 191 | //make a nice little target area: | |
| 192 | 0 | Rectangle mousePoint = new Rectangle(x - 5, y - 5, 5, 5); |
| 193 | // and find the Fig: | |
| 194 | 0 | Editor editor = Globals.curEditor(); |
| 195 | 0 | LayerManager lm = editor.getLayerManager(); |
| 196 | 0 | Layer active = lm.getActiveLayer(); |
| 197 | 0 | Enumeration figs = active.elementsIn(mousePoint); |
| 198 | // last is the top fig. | |
| 199 | 0 | while (figs.hasMoreElements()) { |
| 200 | 0 | Fig candidateFig = (Fig) figs.nextElement(); |
| 201 | 0 | if (candidateFig instanceof FigNodeModelElement |
| 202 | && candidateFig.isSelectable()) { | |
| 203 | 0 | newFig = (FigNodeModelElement) candidateFig; |
| 204 | } | |
| 205 | 0 | } |
| 206 | // check intermediate post-condition. | |
| 207 | 0 | if (newFig == null) { |
| 208 | 0 | armed = false; |
| 209 | 0 | super.mouseReleased(me); |
| 210 | 0 | return; |
| 211 | } | |
| 212 | ||
| 213 | 0 | UMLMutableGraphSupport mgm = |
| 214 | (UMLMutableGraphSupport) editor.getGraphModel(); | |
| 215 | 0 | FigNodeModelElement oldFig = null; |
| 216 | 0 | boolean isSource = false; |
| 217 | 0 | if (pointIndex == 0) { |
| 218 | 0 | oldFig = sourceFig; |
| 219 | 0 | isSource = true; |
| 220 | } | |
| 221 | else { | |
| 222 | 0 | oldFig = destFig; |
| 223 | } | |
| 224 | ||
| 225 | // delegate the re-routing to graphmodels. | |
| 226 | 0 | if (mgm.canChangeConnectedNode(newFig.getOwner(), |
| 227 | oldFig.getOwner(), | |
| 228 | this.getContent().getOwner())) { | |
| 229 | 0 | mgm.changeConnectedNode(newFig.getOwner(), |
| 230 | oldFig.getOwner(), | |
| 231 | this.getContent().getOwner(), | |
| 232 | isSource); | |
| 233 | } | |
| 234 | ||
| 235 | 0 | editor.getSelectionManager().deselect(getContent()); |
| 236 | 0 | armed = false; |
| 237 | // TODO: There is a cyclic dependency between SelectionRerouteEdge | |
| 238 | // and FigEdgeModelElement | |
| 239 | 0 | FigEdgeModelElement figEdge = (FigEdgeModelElement) getContent(); |
| 240 | 0 | figEdge.determineFigNodes(); |
| 241 | 0 | figEdge.computeRoute(); |
| 242 | 0 | super.mouseReleased(me); |
| 243 | 0 | return; |
| 244 | } | |
| 245 | ||
| 246 | ||
| 247 | } |