Coverage Report - org.argouml.uml.diagram.ui.FigTextGroup
 
Classes in this File Line Coverage Branch Coverage Complexity
FigTextGroup
0%
0/57
0%
0/36
2.462
 
 1  
 /* $Id: FigTextGroup.java 17741 2010-01-10 03:50:08Z bobtarling $
 2  
  *******************************************************************************
 3  
  * Copyright (c) 2010 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  
  *    Bob Tarling
 11  
  *******************************************************************************
 12  
  *
 13  
  * Some portions of this file were previously release using the BSD License:
 14  
  */
 15  
 // $Id: FigTextGroup.java 17741 2010-01-10 03:50:08Z bobtarling $
 16  
 // Copyright (c) 2003-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.awt.Point;
 42  
 import java.awt.Rectangle;
 43  
 import java.awt.event.MouseEvent;
 44  
 import java.awt.event.MouseListener;
 45  
 import java.util.List;
 46  
 
 47  
 import org.argouml.uml.diagram.DiagramSettings;
 48  
 import org.tigris.gef.presentation.Fig;
 49  
 import org.tigris.gef.presentation.FigText;
 50  
 
 51  
 /**
 52  
  * Custom class to group FigTexts in such a way that they don't
 53  
  * overlap and that the group is shrinked to fit (no whitespace in
 54  
  * group).
 55  
  *
 56  
  * @author jaap.branderhorst@xs4all.nl
 57  
  */
 58  
 public class FigTextGroup extends ArgoFigGroup implements MouseListener {
 59  
 
 60  0
     private boolean supressCalcBounds = false;
 61  
 
 62  
     /**
 63  
      * Construct a FigGroup with the given render settings.
 64  
      * 
 65  
      * @param owner owning UML element
 66  
      * @param settings rendering settings.
 67  
      */
 68  
     public FigTextGroup(Object owner, DiagramSettings settings) {
 69  0
         super(owner, settings);
 70  0
     }
 71  
     
 72  
     /**
 73  
      * Adds a FigText to the list with figs. Makes sure that the
 74  
      * figtexts do not overlap.
 75  
      * {@inheritDoc}
 76  
      */
 77  
     @Override
 78  
     public void addFig(Fig f) {
 79  0
         super.addFig(f);
 80  0
         updateFigTexts();
 81  0
         calcBounds();
 82  0
     }
 83  
 
 84  
     /**
 85  
      * Updates the FigTexts. FigTexts without text (equals "") are not shown.
 86  
      * The rest of the figtexts are shown non-overlapping. The first figtext
 87  
      * added (via addFig) is shown at the bottom of the FigTextGroup.
 88  
      */
 89  
     private void updateFigTexts() {
 90  0
         int height = 0;
 91  0
         for (Fig fig : (List<Fig>) getFigs()) {
 92  0
             int figHeight = fig.getMinimumSize().height;
 93  0
             fig.setBounds(getX(), getY() + height, fig.getWidth(), figHeight);
 94  0
             fig.endTrans();
 95  0
             height += fig.getHeight();
 96  0
         }
 97  0
     }
 98  
 
 99  
 
 100  
     /*
 101  
      * @see org.tigris.gef.presentation.Fig#calcBounds()
 102  
      */
 103  
     @Override
 104  
     public void calcBounds() {
 105  0
             updateFigTexts();
 106  0
         if (!supressCalcBounds) {
 107  0
             super.calcBounds();
 108  
             // get the widest of all textfigs
 109  
             // calculate the total height
 110  0
             int maxWidth = 0;
 111  0
             int height = 0;
 112  0
             for (Fig fig : (List<Fig>) getFigs()) {
 113  
 //                fig.calcBounds();
 114  0
                 if (fig.getWidth() > maxWidth) {
 115  0
                     maxWidth = fig.getWidth();
 116  
                 }
 117  0
                 fig.setHeight(fig.getMinimumSize().height);
 118  0
                 height += fig.getHeight();
 119  
             }
 120  0
             _w = maxWidth;
 121  0
             _h = height;
 122  
         }
 123  0
     }
 124  
 
 125  
     /*
 126  
      * @see org.tigris.gef.presentation.Fig#removeFromDiagram()
 127  
      */
 128  
     @Override
 129  
     public void removeFromDiagram() {
 130  0
         for (Fig fig : (List<Fig>) getFigs()) {
 131  0
             fig.removeFromDiagram();
 132  
         }
 133  0
         super.removeFromDiagram();
 134  0
     }
 135  
 
 136  
     /*
 137  
      * @see org.tigris.gef.presentation.Fig#deleteFromModel()
 138  
      */
 139  
     @Override
 140  
     public void deleteFromModel() {
 141  0
         for (Fig fig : (List<Fig>) getFigs()) {
 142  0
             fig.deleteFromModel();
 143  
         }
 144  0
         super.deleteFromModel();
 145  0
     }
 146  
 
 147  
     ////////////////////////////////////////////////////////////////
 148  
     // event handlers - MouseListener implementation
 149  
 
 150  
     /*
 151  
      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
 152  
      */
 153  
     public void mousePressed(MouseEvent me) {
 154  
         // ignored
 155  0
     }
 156  
 
 157  
     /*
 158  
      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
 159  
      */
 160  
     public void mouseReleased(MouseEvent me) {
 161  
         // ignored
 162  0
     }
 163  
 
 164  
     /*
 165  
      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
 166  
      */
 167  
     public void mouseEntered(MouseEvent me) {
 168  
         // ignored
 169  0
     }
 170  
 
 171  
     /*
 172  
      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
 173  
      */
 174  
     public void mouseExited(MouseEvent me) {
 175  
         // ignored
 176  0
     }
 177  
 
 178  
     /**
 179  
      * Handle mouse click. If the user double clicks on any part of this
 180  
      * FigGroup, pass it down to one of the internal Figs. This allows the user
 181  
      * to initiate direct text editing.
 182  
      * 
 183  
      * {@inheritDoc}
 184  
      */
 185  
     public void mouseClicked(MouseEvent me) {
 186  0
         if (me.isConsumed()) {
 187  0
             return;
 188  
         }
 189  0
         if (me.getClickCount() >= 2) {
 190  0
             Fig f = hitFig(new Rectangle(me.getX() - 2, me.getY() - 2, 4, 4));
 191  0
             if (f instanceof MouseListener) {
 192  0
                 ((MouseListener) f).mouseClicked(me);
 193  
             }
 194  0
             if (me.isConsumed()) {
 195  0
                 return;
 196  
             }
 197  
             // If the mouse event hasn't been consumed, it means that the user
 198  
             // double clicked on an area that didn't contain an editable fig.
 199  
             // in this case, scan through the list and start editing the first 
 200  
             // fig with editable text.  This allows us to remove the editable 
 201  
             // box clarifier outline, and just outline the whole FigTextGroup, 
 202  
             // see issue 1048.
 203  0
             for (Object o : this.getFigs()) {
 204  0
                 f = (Fig) o;
 205  0
                 if (f instanceof MouseListener && f instanceof FigText) {
 206  0
                     if ( ((FigText) f).getEditable()) {
 207  0
                         ((MouseListener) f).mouseClicked(me);
 208  
                     }
 209  
                 }
 210  
             }
 211  
         }
 212  
         // TODO: 21/12/2008 dthompson mouseClicked(me) above consumes the 
 213  
         // mouse event internally, so I suspect that this line might not be 
 214  
         // necessary.
 215  0
         me.consume();
 216  0
     }
 217  
     
 218  
     /**
 219  
      * Checks to see if the given click point (Rectangle) hits
 220  
      * the FigTextGroup.
 221  
      * This overrides the FigGroup implementation of hit where the hit 
 222  
      * rectangle must hit a sub fig.
 223  
      * Now, we count a hit if the user clicks anywhere within the outer 
 224  
      * bounds of the FigTextGroup, see issue 5620.
 225  
      * This method is important for registering double clicks to edit text 
 226  
      * boxes in a FigTextGroup.
 227  
      * @param r The hit rectangle.
 228  
      * @return True if the given hit rectangle intersects or is contained by
 229  
      * the outer limits of this FigTextGroup.
 230  
      * @see org.tigris.gef.presentation.FigGroup#hit(java.awt.Rectangle)
 231  
      */
 232  
     public boolean hit(Rectangle r) {
 233  0
         return this.intersects(r);
 234  
     }
 235  
     
 236  
     /** 
 237  
      * Returns true if the bounds of the group contain the given point. 
 238  
      * Used to check if a mouse click occurs within the FigTextGroup.
 239  
      * Overrides the FigGroup implementation of contains where the
 240  
      * x, y point must hit a sub fig.
 241  
      * Now we count a hit if the user clicks anywhere within the outer
 242  
      * bounds of the FigTextGroup, see issue 5620.
 243  
      * This method is important for handling mousePressed events including
 244  
      * the start of mouse drags.
 245  
      * @param x The x coordinate of the point to test.
 246  
      * @param y The y coordinate of the point to test.
 247  
      * @return True if the given point is within the outer limits of this
 248  
      * FigTextGroup.
 249  
      * @see org.tigris.gef.presentation.FigGroup#contains(int, int)
 250  
      */
 251  
     public boolean contains(int x, int y) {
 252  0
         return (_x <= x) && (x <= _x + _w) && (_y <= y) && (y <= _y + _h);
 253  
     }
 254  
 }