| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ModeLabelDrag |
|
| 1.8333333333333333;1.833 |
| 1 | /* $Id: ModeLabelDrag.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 | * dthompson | |
| 11 | ***************************************************************************** | |
| 12 | * | |
| 13 | * Some portions of this file was previously release using the BSD License: | |
| 14 | */ | |
| 15 | ||
| 16 | // Copyright (c) 2008 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 java.util.List; | |
| 42 | import java.awt.Point; | |
| 43 | import java.awt.event.MouseEvent; | |
| 44 | ||
| 45 | import org.tigris.gef.base.Editor; | |
| 46 | import org.tigris.gef.base.FigModifyingModeImpl; | |
| 47 | import org.tigris.gef.base.PathItemPlacementStrategy; | |
| 48 | import org.tigris.gef.presentation.Fig; | |
| 49 | import org.tigris.gef.presentation.FigEdge; | |
| 50 | ||
| 51 | /** | |
| 52 | * Mode for dragging (i.e. user repositioning) of Text labels connected to | |
| 53 | * FigEdges. | |
| 54 | * @author Dave Thompson | |
| 55 | */ | |
| 56 | public class ModeLabelDrag extends FigModifyingModeImpl { | |
| 57 | ||
| 58 | /** | |
| 59 | * The text label that is currently being dragged, or | |
| 60 | * null if nothing is being dragged. | |
| 61 | */ | |
| 62 | 900 | private Fig dragFig = null; |
| 63 | ||
| 64 | /** | |
| 65 | * The FigEdge that contains the label that is currently | |
| 66 | * being dragged. We have to keep track of this because | |
| 67 | * the FigEdge is the only object that holds references to the | |
| 68 | * PathItems that place it's child figs. The child Figs do not | |
| 69 | * have these references. | |
| 70 | * figEdge = null if nothing is being dragged. | |
| 71 | */ | |
| 72 | 900 | private FigEdge figEdge = null; |
| 73 | ||
| 74 | /** | |
| 75 | * Point for tracking a mouse drag, indicates the last recorded mouse | |
| 76 | * point. When a new mouse drag event occurs, the difference between | |
| 77 | * the current point and dragBasePoint is the delta in position. | |
| 78 | */ | |
| 79 | 900 | private Point dragBasePoint = new Point(0, 0); |
| 80 | ||
| 81 | /** | |
| 82 | * X-offset for storing the location of the mouse click on a label | |
| 83 | * relative to the centre of the label. Used while dragging to | |
| 84 | * ensure that the user can click anywhere to start the drag, and things | |
| 85 | * still behave sensibly. X component. | |
| 86 | */ | |
| 87 | 900 | private int deltax = 0; |
| 88 | ||
| 89 | /** | |
| 90 | * Y-offset for storing the location of the mouse click on a label | |
| 91 | * relative to the centre of the label. Used while dragging to | |
| 92 | * ensure that the user can click anywhere to start the drag, and things | |
| 93 | * still behave sensibly. X component. | |
| 94 | */ | |
| 95 | 900 | private int deltay = 0; |
| 96 | ||
| 97 | /** | |
| 98 | * Constructor for creating the ModeLabelDrag instance. | |
| 99 | * @param editor The editor which will own this mode. | |
| 100 | */ | |
| 101 | public ModeLabelDrag(Editor editor) { | |
| 102 | 900 | super(editor); |
| 103 | 900 | } |
| 104 | ||
| 105 | /** | |
| 106 | * Constructor for creating the ModeLabelDrag instance. | |
| 107 | */ | |
| 108 | public ModeLabelDrag() { | |
| 109 | 0 | super(); |
| 110 | 0 | } |
| 111 | ||
| 112 | /** | |
| 113 | * Reply a string of instructions that should be shown in the | |
| 114 | * statusbar when this mode starts. | |
| 115 | * @return The text to display in the status bar. | |
| 116 | * @override | |
| 117 | */ | |
| 118 | public String instructions() { | |
| 119 | 900 | return " "; |
| 120 | } | |
| 121 | /** | |
| 122 | * Grabs label to begin movement. Turns cursor into a hand. | |
| 123 | * @param me The mouse event to process. | |
| 124 | * @override | |
| 125 | * @see org.tigris.gef.base.ModeImpl#mousePressed(java.awt.event.MouseEvent) | |
| 126 | */ | |
| 127 | public void mousePressed(MouseEvent me) { | |
| 128 | 0 | Point clickPoint = me.getPoint(); |
| 129 | 0 | Fig underMouse = editor.hit(clickPoint); |
| 130 | 0 | if (underMouse instanceof FigEdge) { |
| 131 | 0 | List<Fig> figList = ((FigEdge)underMouse).getPathItemFigs(); |
| 132 | 0 | for (Fig fig : figList) { |
| 133 | 0 | if (fig.contains(clickPoint)) { |
| 134 | // Consume to stop other modes from trying to take over | |
| 135 | 0 | me.consume(); |
| 136 | 0 | dragFig = fig; |
| 137 | 0 | dragBasePoint = fig.getCenter(); |
| 138 | 0 | deltax = clickPoint.x - dragBasePoint.x; |
| 139 | 0 | deltay = clickPoint.y - dragBasePoint.y; |
| 140 | 0 | figEdge = (FigEdge) underMouse; |
| 141 | 0 | break; |
| 142 | } | |
| 143 | } | |
| 144 | } | |
| 145 | 0 | } |
| 146 | ||
| 147 | /** | |
| 148 | * Handle mouseReleased events. | |
| 149 | * @param me The mouse event to process. | |
| 150 | * @see org.tigris.gef.base.ModeImpl#mouseReleased(java.awt.event.MouseEvent) | |
| 151 | */ | |
| 152 | public void mouseReleased(MouseEvent me) { | |
| 153 | 0 | if (dragFig != null) { |
| 154 | 0 | dragFig = null; |
| 155 | } | |
| 156 | 0 | } |
| 157 | ||
| 158 | /** | |
| 159 | * Handle mouseDragged events. | |
| 160 | * @param me The mouse event to process. | |
| 161 | * @see org.tigris.gef.base.ModeImpl#mouseDragged(java.awt.event.MouseEvent) | |
| 162 | */ | |
| 163 | public void mouseDragged(MouseEvent me) { | |
| 164 | 0 | if (dragFig != null) { |
| 165 | 0 | me = editor.translateMouseEvent(me); |
| 166 | 0 | Point newPoint = me.getPoint(); |
| 167 | // Subtract the the offset of the click, to take account of user | |
| 168 | // having not initially clicked in the centre. | |
| 169 | 0 | newPoint.translate(-deltax, -deltay); |
| 170 | 0 | PathItemPlacementStrategy pips |
| 171 | = figEdge.getPathItemPlacementStrategy(dragFig); | |
| 172 | 0 | pips.setPoint(newPoint); |
| 173 | 0 | newPoint = pips.getPoint(); |
| 174 | 0 | int dx = newPoint.x - dragBasePoint.x; |
| 175 | 0 | int dy = newPoint.y - dragBasePoint.y; |
| 176 | 0 | dragBasePoint.setLocation(newPoint); |
| 177 | 0 | dragFig.translate(dx, dy); |
| 178 | 0 | me.consume(); |
| 179 | 0 | editor.damaged(dragFig); |
| 180 | } | |
| 181 | 0 | } |
| 182 | ||
| 183 | } |