| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CompartmentFigText |
|
| 1.4615384615384615;1.462 |
| 1 | /* $Id: CompartmentFigText.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 | * mvw | |
| 11 | ***************************************************************************** | |
| 12 | * | |
| 13 | * Some portions of this file was previously release using the BSD License: | |
| 14 | */ | |
| 15 | ||
| 16 | // Copyright (c) 1996-2009 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.awt.Color; | |
| 42 | import java.awt.Graphics; | |
| 43 | import java.awt.Rectangle; | |
| 44 | import java.util.Arrays; | |
| 45 | ||
| 46 | import org.argouml.ui.targetmanager.TargetEvent; | |
| 47 | import org.argouml.ui.targetmanager.TargetListener; | |
| 48 | import org.argouml.ui.targetmanager.TargetManager; | |
| 49 | import org.argouml.uml.diagram.DiagramSettings; | |
| 50 | import org.tigris.gef.base.Globals; | |
| 51 | import org.tigris.gef.presentation.Fig; | |
| 52 | import org.tigris.gef.presentation.FigGroup; | |
| 53 | import org.tigris.gef.presentation.FigText; | |
| 54 | ||
| 55 | /** | |
| 56 | * A single line FigText class extension for editable | |
| 57 | * FigClass/FigInterface/FigUseCase | |
| 58 | * compartments that use notation. | |
| 59 | * When selected, this compartment is highlighted.<p> | |
| 60 | * | |
| 61 | * This implementation now supports the extension point compartment in | |
| 62 | * a use case. | |
| 63 | * | |
| 64 | * @author thn | |
| 65 | */ | |
| 66 | public abstract class CompartmentFigText extends FigSingleLineTextWithNotation | |
| 67 | implements TargetListener { | |
| 68 | ||
| 69 | private static final int MARGIN = 3; | |
| 70 | ||
| 71 | /** | |
| 72 | * The bounding figure of the compartment containing this fig text. | |
| 73 | * @deprecated for 0.27.3 by tfmorris. Only used for line color which we | |
| 74 | * can get from the render settings or GEF. | |
| 75 | */ | |
| 76 | @Deprecated | |
| 77 | private Fig refFig; | |
| 78 | ||
| 79 | /** | |
| 80 | * Set if the user has selected this component Fig inside the FigNode. | |
| 81 | */ | |
| 82 | private boolean highlighted; | |
| 83 | ||
| 84 | /** | |
| 85 | * Construct a CompartmentFigText. | |
| 86 | * | |
| 87 | * @param element owning uml element | |
| 88 | * @param bounds position and size | |
| 89 | * @param settings render settings | |
| 90 | */ | |
| 91 | public CompartmentFigText(Object element, Rectangle bounds, | |
| 92 | DiagramSettings settings) { | |
| 93 | 0 | super(element, bounds, settings, true); |
| 94 | 0 | TargetManager.getInstance().addTargetListener(this); |
| 95 | ||
| 96 | 0 | setJustification(FigText.JUSTIFY_LEFT); |
| 97 | 0 | setRightMargin(MARGIN); |
| 98 | 0 | setLeftMargin(MARGIN); |
| 99 | // TODO: We'd like these to not be filled, but GEF won't let us | |
| 100 | // select them if we do that. | |
| 101 | // setFilled(false); | |
| 102 | 0 | } |
| 103 | ||
| 104 | /** | |
| 105 | * Build a new compartment figText of the given dimensions, within the | |
| 106 | * compartment described by <code>aFig</code>. | |
| 107 | * <p> | |
| 108 | * Invoke the parent constructor, then set the reference to the associated | |
| 109 | * compartment figure. The associated FigText is marked as expand only. | |
| 110 | * <p> | |
| 111 | * @param owner owning UML element | |
| 112 | * @param bounds position and size | |
| 113 | * @param settings render settings | |
| 114 | * @param property The property this Fig should listen for | |
| 115 | */ | |
| 116 | public CompartmentFigText(Object owner, Rectangle bounds, | |
| 117 | DiagramSettings settings, String property) { | |
| 118 | 0 | this(owner, bounds, settings, new String[] {property}); |
| 119 | 0 | } |
| 120 | ||
| 121 | /** | |
| 122 | * Build a new compartment figText of the given dimensions, within the | |
| 123 | * compartment described by <code>aFig</code>. | |
| 124 | * <p> | |
| 125 | * Invoke the parent constructor, then set the reference to the associated | |
| 126 | * compartment figure. The associated FigText is marked as expand only. | |
| 127 | * <p> | |
| 128 | * @param owner owning UML element | |
| 129 | * @param bounds position and size | |
| 130 | * @param settings render settings | |
| 131 | * @param properties The properties this Fig should listen for | |
| 132 | */ | |
| 133 | public CompartmentFigText(Object owner, Rectangle bounds, | |
| 134 | DiagramSettings settings, String[] properties) { | |
| 135 | 0 | super(owner, bounds, settings, true, properties); |
| 136 | 0 | TargetManager.getInstance().addTargetListener(this); |
| 137 | 0 | } |
| 138 | ||
| 139 | /* | |
| 140 | * @see org.argouml.uml.diagram.ui.FigSingleLineText#removeFromDiagram() | |
| 141 | */ | |
| 142 | @Override | |
| 143 | public void removeFromDiagram() { | |
| 144 | 0 | super.removeFromDiagram(); |
| 145 | 0 | Fig fg = getGroup(); |
| 146 | 0 | if (fg instanceof FigGroup) { |
| 147 | 0 | ((FigGroup) fg).removeFig(this); |
| 148 | 0 | setGroup(null); |
| 149 | } | |
| 150 | 0 | TargetManager.getInstance().removeTargetListener(this); |
| 151 | 0 | } |
| 152 | ||
| 153 | /** | |
| 154 | * @return Current fill status—always <code>false</code>. | |
| 155 | */ | |
| 156 | @Override | |
| 157 | public boolean isFilled() { | |
| 158 | 0 | return false; |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * Override for correct graphical behaviour.<p> | |
| 163 | * | |
| 164 | * @return Current fill colour—always the fill colour of the | |
| 165 | * associated compartment fig. | |
| 166 | */ | |
| 167 | @Override | |
| 168 | public Color getLineColor() { | |
| 169 | 0 | if (refFig != null) { |
| 170 | 0 | return refFig.getLineColor(); |
| 171 | } else { | |
| 172 | // Get the right color from our settings | |
| 173 | 0 | return super.getLineColor(); |
| 174 | } | |
| 175 | } | |
| 176 | ||
| 177 | /** | |
| 178 | * This is actually used to mark this Fig as selected, however setSelected | |
| 179 | * is set final in GEF. | |
| 180 | * TODO: Can setSelected be used without side-effect if GEF is adjusted? | |
| 181 | * Otherwise consider renaming as setSelectedChild and try to make | |
| 182 | * protected. | |
| 183 | * @param flag <code>true</code> if the entry is to be highlighted, | |
| 184 | * <code>false</code> otherwise. | |
| 185 | */ | |
| 186 | public void setHighlighted(boolean flag) { | |
| 187 | 0 | highlighted = flag; |
| 188 | 0 | } |
| 189 | ||
| 190 | /** | |
| 191 | * Extends the normal paint function in order to display a similar | |
| 192 | * selection-box to that given for a non-resizable FigNode. | |
| 193 | * @param g the graphics object | |
| 194 | * @see org.tigris.gef.presentation.FigText#paint(java.awt.Graphics) | |
| 195 | */ | |
| 196 | @Override | |
| 197 | public void paint(Graphics g) { | |
| 198 | 0 | super.paint(g); |
| 199 | 0 | if (highlighted) { |
| 200 | 0 | final int x = getX(); |
| 201 | 0 | final int y = getY(); |
| 202 | 0 | final int w = getWidth(); |
| 203 | 0 | final int h = getHeight(); |
| 204 | 0 | g.setColor(Globals.getPrefs().handleColorFor(this)); |
| 205 | ||
| 206 | 0 | g.drawRect(x - 1, y - 1, w + 2, h + 2); |
| 207 | 0 | g.drawRect(x, y, w, h); |
| 208 | } | |
| 209 | 0 | } |
| 210 | ||
| 211 | /** | |
| 212 | * Return whether this item is highlighted.<p> | |
| 213 | * | |
| 214 | * @return <code>true</code> if the entry is highlighted, | |
| 215 | * <code>false</code> otherwise. | |
| 216 | */ | |
| 217 | public boolean isHighlighted() { | |
| 218 | 0 | return highlighted; |
| 219 | } | |
| 220 | ||
| 221 | /** | |
| 222 | * Called when text editing has completed on this Fig. | |
| 223 | */ | |
| 224 | protected void textEdited() { | |
| 225 | 0 | setHighlighted(true); |
| 226 | 0 | super.textEdited(); |
| 227 | 0 | } |
| 228 | ||
| 229 | public void targetAdded(TargetEvent e) { | |
| 230 | 0 | if (Arrays.asList(e.getNewTargets()).contains(getOwner())) { |
| 231 | 0 | setHighlighted(true); |
| 232 | 0 | this.damage(); |
| 233 | } | |
| 234 | 0 | } |
| 235 | ||
| 236 | public void targetRemoved(TargetEvent e) { | |
| 237 | 0 | if (e.getRemovedTargetCollection().contains(getOwner())) { |
| 238 | 0 | setHighlighted(false); |
| 239 | 0 | this.damage(); |
| 240 | } | |
| 241 | 0 | } |
| 242 | ||
| 243 | public void targetSet(TargetEvent e) { | |
| 244 | /* This is needed for when the selection changes from | |
| 245 | * one compartment fig to an other object. | |
| 246 | * Without this, the selection indicators would stay on the screen. | |
| 247 | * See issue 5681. */ | |
| 248 | 0 | setHighlighted((Arrays.asList(e.getNewTargets()).contains(getOwner()))); |
| 249 | 0 | } |
| 250 | } |