Coverage Report - org.argouml.uml.diagram.state.ui.FigStubState
 
Classes in this File Line Coverage Branch Coverage Complexity
FigStubState
0%
0/157
0%
0/80
2.826
 
 1  
 /* $Id: FigStubState.java 18728 2010-09-10 09:29:47Z mvw $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009-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  
  *    Michiel van der Wulp
 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.state.ui;
 40  
 
 41  
 import java.awt.Color;
 42  
 import java.awt.Font;
 43  
 import java.awt.Rectangle;
 44  
 import java.beans.PropertyChangeEvent;
 45  
 import java.util.Iterator;
 46  
 
 47  
 import org.apache.log4j.Logger;
 48  
 import org.argouml.model.Facade;
 49  
 import org.argouml.model.Model;
 50  
 import org.argouml.model.StateMachinesHelper;
 51  
 import org.argouml.uml.diagram.DiagramSettings;
 52  
 import org.argouml.uml.diagram.ui.SelectionMoveClarifiers;
 53  
 import org.tigris.gef.base.Selection;
 54  
 import org.tigris.gef.presentation.Fig;
 55  
 import org.tigris.gef.presentation.FigLine;
 56  
 import org.tigris.gef.presentation.FigRect;
 57  
 import org.tigris.gef.presentation.FigText;
 58  
 
 59  
 /**
 60  
  * Class to display graphics for a UML StubState in a diagram.
 61  
  *
 62  
  * @author pepargouml@yahoo.es
 63  
  */
 64  
 
 65  
 public class FigStubState extends FigStateVertex {
 66  
     
 67  0
     private static final Logger LOG = Logger.getLogger(FigStubState.class);
 68  
 
 69  
     private static final int X = 0;
 70  
     private static final int Y = 0;
 71  
     private static final int WIDTH = 45;
 72  
     private static final int HEIGHT = 20;
 73  
 
 74  
     private FigText referenceFig;
 75  
     private FigLine stubline;
 76  
     
 77  
     private Facade facade;
 78  
     private StateMachinesHelper stateMHelper;
 79  
 
 80  
     
 81  
     /**
 82  
      * Construct a new FigStubState.
 83  
      * 
 84  
      * @param owner owning UML element
 85  
      * @param bounds position and size
 86  
      * @param settings rendering settings
 87  
      */
 88  
     public FigStubState(Object owner, Rectangle bounds,
 89  
             DiagramSettings settings) {
 90  0
         super(owner, bounds, settings);
 91  0
         initFigs();
 92  0
     }
 93  
 
 94  
     @Override
 95  
     protected Fig createBigPortFig() {
 96  0
         FigRect fr = new FigRect(X, Y, WIDTH, HEIGHT);
 97  0
         fr.setLineWidth(0);
 98  0
         fr.setFilled(false);
 99  0
         return fr;
 100  
     }
 101  
 
 102  
     private void initFigs() {
 103  0
         facade = Model.getFacade();
 104  0
         stateMHelper = Model.getStateMachinesHelper();
 105  
 
 106  0
         stubline = new FigLine(X,
 107  
                 Y,
 108  
                 WIDTH,
 109  
                 Y,
 110  
                 TEXT_COLOR);
 111  
 
 112  0
         referenceFig = new FigText(0, 0, WIDTH, HEIGHT, true);
 113  0
         referenceFig.setFont(getSettings().getFontPlain());
 114  0
         referenceFig.setTextColor(TEXT_COLOR);
 115  0
         referenceFig.setReturnAction(FigText.END_EDITING);
 116  0
         referenceFig.setTabAction(FigText.END_EDITING);
 117  0
         referenceFig.setJustification(FigText.JUSTIFY_CENTER);
 118  0
         referenceFig.setLineWidth(0);
 119  0
         referenceFig.setBounds(X, Y,
 120  
                 WIDTH, referenceFig.getBounds().height);
 121  0
         referenceFig.setFilled(false);
 122  0
         referenceFig.setEditable(false);
 123  
 
 124  
 
 125  0
         addFig(getBigPort());
 126  0
         addFig(referenceFig);
 127  0
         addFig(stubline);
 128  
 
 129  0
         setShadowSize(0);
 130  0
         setBlinkPorts(false); //make port invisible unless mouse enters
 131  0
     }
 132  
 
 133  
     /*
 134  
      * @see java.lang.Object#clone()
 135  
      */
 136  
     @Override
 137  
     public Object clone() {
 138  0
         FigStubState figClone = (FigStubState) super.clone();
 139  0
         Iterator it = figClone.getFigs().iterator();
 140  0
         figClone.setBigPort((FigRect) it.next());
 141  0
         figClone.referenceFig = (FigText) it.next();
 142  0
         figClone.stubline = (FigLine) it.next();
 143  0
         return figClone;
 144  
     }
 145  
 
 146  
     ////////////////////////////////////////////////////////////////
 147  
     // Fig accessors
 148  
 
 149  
     /**
 150  
      * Synch states are fixed size.
 151  
      * @return false
 152  
      */
 153  
     @Override
 154  
     public boolean isResizable() {
 155  0
         return false;
 156  
     }
 157  
 
 158  
     /*
 159  
      * @see org.tigris.gef.presentation.Fig#makeSelection()
 160  
      */
 161  
     @Override
 162  
     public Selection makeSelection() {
 163  0
         return new SelectionMoveClarifiers(this);
 164  
     }
 165  
 
 166  
     /*
 167  
      * @see org.tigris.gef.presentation.Fig#setLineColor(java.awt.Color)
 168  
      */
 169  
     @Override
 170  
     public void setLineColor(Color col) {
 171  0
         stubline.setLineColor(col);
 172  0
     }
 173  
 
 174  
     /*
 175  
      * @see org.tigris.gef.presentation.Fig#getLineColor()
 176  
      */
 177  
     @Override
 178  
     public Color getLineColor() {
 179  0
         return stubline.getLineColor();
 180  
     }
 181  
 
 182  
     /*
 183  
      * @see org.tigris.gef.presentation.Fig#setFillColor(java.awt.Color)
 184  
      */
 185  
     @Override
 186  
     public void setFillColor(Color col) {
 187  0
         referenceFig.setFillColor(col);
 188  0
     }
 189  
 
 190  
     /*
 191  
      * @see org.tigris.gef.presentation.Fig#getFillColor()
 192  
      */
 193  
     @Override
 194  
     public Color getFillColor() {
 195  0
         return referenceFig.getFillColor();
 196  
     }
 197  
 
 198  
     /*
 199  
      * @see org.tigris.gef.presentation.Fig#setFilled(boolean)
 200  
      */
 201  
     @Override
 202  
     public void setFilled(boolean f) {
 203  0
         referenceFig.setFilled(f);
 204  0
     }
 205  
 
 206  
     @Override
 207  
     public boolean isFilled() {
 208  0
         return referenceFig.isFilled();
 209  
     }
 210  
 
 211  
     /*
 212  
      * @see org.tigris.gef.presentation.Fig#setLineWidth(int)
 213  
      */
 214  
     @Override
 215  
     public void setLineWidth(int w) {
 216  0
         stubline.setLineWidth(w);
 217  0
     }
 218  
 
 219  
     /*
 220  
      * @see org.tigris.gef.presentation.Fig#getLineWidth()
 221  
      */
 222  
     @Override
 223  
     public int getLineWidth() {
 224  0
         return stubline.getLineWidth();
 225  
     }
 226  
 
 227  
     /*
 228  
      * @see org.tigris.gef.presentation.Fig#setBoundsImpl(int, int, int, int)
 229  
      */
 230  
     @Override
 231  
     protected void setStandardBounds(int theX, int theY, int theW, int theH) {
 232  0
         Rectangle oldBounds = getBounds();
 233  0
         theW = 60;
 234  
 
 235  0
         referenceFig.setBounds(theX, theY, theW,
 236  
                 referenceFig.getBounds().height);
 237  0
         stubline.setShape(theX, theY,
 238  
                 theX + theW, theY);
 239  
 
 240  0
         getBigPort().setBounds(theX, theY, theW, theH);
 241  
 
 242  0
         calcBounds(); //_x = x; _y = y; _w = w; _h = h;
 243  0
         updateEdges();
 244  0
         firePropChange("bounds", oldBounds, getBounds());
 245  0
     }
 246  
 
 247  
     ////////////////////////////////////////////////////////////////
 248  
     // event processing
 249  
 
 250  
     /*
 251  
      * Update the text labels.
 252  
      *
 253  
      * @see org.argouml.uml.diagram.ui.FigNodeModelElement#modelChanged(java.beans.PropertyChangeEvent)
 254  
      */
 255  
     @Override
 256  
     protected void modelChanged(PropertyChangeEvent mee) {
 257  0
         super.modelChanged(mee);
 258  
         // TODO: Rather than specifically ignore some item maybe it would be better
 259  
         // to specifically state what items are of interest. Otherwise we may still
 260  
         // be acting on other events we don't need
 261  0
         if (!Model.getFacade().isATransition(mee.getNewValue())
 262  
                 && getOwner() != null) {
 263  0
             Object container = facade.getContainer(getOwner());
 264  
 
 265  
             //The event source is the owner stub state
 266  0
             if ((mee.getSource().equals(getOwner()))) {
 267  0
                 if (mee.getPropertyName().equals("referenceState")) {
 268  0
                     updateReferenceText();
 269  
                     final Object oldRef;
 270  0
                     if (container != null && facade.isASubmachineState(container)
 271  
                             && facade.getSubmachine(container) != null) {
 272  
                         final Object top;
 273  0
                         top = facade.getTop(facade.getSubmachine(container));
 274  0
                         oldRef = stateMHelper.getStatebyName(
 275  
                                 (String) mee.getOldValue(), top);
 276  0
                     } else {
 277  0
                         oldRef = null;
 278  
                     }
 279  0
                     updateListeners(oldRef, getOwner());
 280  0
                 } else if ((mee.getPropertyName().equals("container")
 281  
                         && facade.isASubmachineState(container))) {
 282  0
                     removeListeners();
 283  0
                     Object o = mee.getOldValue();
 284  0
                     if (o != null && facade.isASubmachineState(o)) {
 285  0
                         removeElementListener(o);
 286  
                     }
 287  0
                     stateMHelper.setReferenceState(getOwner(), null);
 288  0
                     updateListeners(getOwner(), getOwner());
 289  0
                     updateReferenceText();
 290  0
                 }
 291  
             } else {
 292  
                 /*The event source is the submachine state*/
 293  0
                 if (container != null
 294  
                         && mee.getSource().equals(container)
 295  
                         && facade.isASubmachineState(container)
 296  
                         && facade.getSubmachine(container) != null) {
 297  
                     /* The submachine has got a new name*/
 298  
                     // This indicates a change in association, not name - tfm
 299  0
                     if (mee.getPropertyName().equals("submachine")) {
 300  
                         final Object oldRef;
 301  0
                         if (mee.getOldValue() != null) {
 302  
                             final Object top;
 303  0
                             top = facade.getTop(mee.getOldValue());
 304  0
                             oldRef = stateMHelper.getStatebyName(facade
 305  
                                     .getReferenceState(getOwner()), top);
 306  0
                         } else {
 307  0
                             oldRef = null;
 308  
                         }
 309  0
                         stateMHelper.setReferenceState(getOwner(), null);
 310  0
                         updateListeners(oldRef, getOwner());
 311  0
                         updateReferenceText();
 312  0
                     }
 313  
 
 314  
                 } else {
 315  
                     // The event source is the stub state's referenced state
 316  
                     // or one of the referenced state's path.
 317  
                     final Object top;
 318  0
                     if (facade.getSubmachine(container) != null) {
 319  0
                         top = facade.getTop(facade.getSubmachine(container));
 320  
                     } else {
 321  0
                         top = null;
 322  
                     }
 323  0
                     String path = facade.getReferenceState(getOwner());
 324  0
                     Object refObject = stateMHelper.getStatebyName(path, top);
 325  
                     String ref;
 326  0
                     if (refObject == null) {
 327  
                         // The source was the referenced state that has got
 328  
                         // a new name.
 329  0
                         ref = stateMHelper.getPath(mee.getSource());
 330  
                     } else {
 331  
                         //The source was one of the referenced state's path which
 332  
                         // has got a new name.
 333  0
                         ref = stateMHelper.getPath(refObject);
 334  
                     }
 335  
                     // The Referenced State or one of his path's states has got
 336  
                     // a new name
 337  0
                     stateMHelper.setReferenceState(getOwner(), ref);
 338  0
                     updateReferenceText();
 339  
                 }
 340  
             }
 341  
         }
 342  0
     }
 343  
 
 344  
     /**
 345  
      * Rerender the whole figure.
 346  
      * Call superclass then add reference text
 347  
      */
 348  
     @Override
 349  
     public void renderingChanged() {
 350  0
         super.renderingChanged();
 351  0
         updateReferenceText();
 352  0
     }
 353  
     
 354  
     /**
 355  
      * Update the reference text.
 356  
      */
 357  
     public void updateReferenceText() {
 358  0
         Object text = null;
 359  
         try {
 360  0
             text = facade.getReferenceState(getOwner());
 361  0
         } catch (Exception e) {
 362  0
             LOG.error("Exception caught and ignored!!", e);
 363  0
         }
 364  0
         if (text != null) {
 365  0
             referenceFig.setText((String) text);
 366  
         } else {
 367  0
             referenceFig.setText("");
 368  
         }
 369  0
         calcBounds();
 370  0
         setBounds(getBounds());
 371  0
         damage();
 372  0
     }
 373  
 
 374  
     /**
 375  
      * @param newOwner
 376  
      */
 377  
     private void addListeners(Object newOwner) {
 378  
         Object container;
 379  
         Object top;
 380  
         Object reference;
 381  0
         container = facade.getContainer(newOwner);
 382  
         //The new submachine container is added as listener
 383  0
         if (container != null
 384  
                 && facade.isASubmachineState(container)) {
 385  0
             addElementListener(container);
 386  
         }
 387  
         
 388  
         //All states in the new reference state's path are added
 389  
         // as listeners
 390  0
         if (container != null
 391  
                 && facade.isASubmachineState(container)
 392  
                 && facade.getSubmachine(container) != null) {
 393  0
             top = facade.getTop(facade.getSubmachine(container));
 394  0
             reference = stateMHelper.getStatebyName(facade
 395  
                     .getReferenceState(newOwner), top);
 396  0
             String[] properties = {"name", "container"};
 397  0
             container = reference;
 398  
             while (container != null
 399  0
                     && !container.equals(top)) {
 400  0
                 addElementListener(container);
 401  0
                 container = facade.getContainer(container);
 402  
             }
 403  
         }
 404  0
     }
 405  
 
 406  
     /**
 407  
      * Remove all the existing listeners
 408  
      */
 409  
     private void removeListeners() {
 410  
         Object container;
 411  
         Object top;
 412  
         Object reference;
 413  0
         Object owner = getOwner();
 414  0
         if (owner == null) {
 415  0
             return;
 416  
         }
 417  0
         container = facade.getContainer(owner);
 418  
         //The old submachine container is deleted as listener
 419  0
         if (container != null
 420  
                 && facade.isASubmachineState(container)) {
 421  0
             removeElementListener(container);
 422  
         }
 423  
         //All states in the old reference state's path are deleted
 424  
         // as listeners
 425  0
         if (container != null
 426  
                 && facade.isASubmachineState(container)
 427  
                 && facade.getSubmachine(container) != null) {
 428  
             
 429  0
             top = facade.getTop(facade.getSubmachine(container));
 430  0
             reference = stateMHelper.getStatebyName(facade
 431  
                     .getReferenceState(owner), top);
 432  0
             if (reference != null) {
 433  0
                 removeElementListener(reference);
 434  0
                 container = facade.getContainer(reference);
 435  0
                 while (container != null && !facade.isTop(container)) {
 436  0
                     removeElementListener(container);
 437  0
                     container = facade.getContainer(container);
 438  
                 }
 439  
             }
 440  
         }
 441  0
     }
 442  
 
 443  
     /**
 444  
      * @param newOwner the new owner UML object
 445  
      * @param oldV the old owner UML object
 446  
      * @deprecated for 0.27.2 by tfmorris. Use
 447  
      *             {@link #updateListeners(Object, Object)} with the argument
 448  
      *             order swapper. There are no internal users of this method, so
 449  
      *             the only potential users are people who've subclassed this
 450  
      *             Fig.
 451  
      */
 452  
     protected void updateListenersX(Object newOwner, Object oldV) {
 453  
         // Just swap order of arguments to get to new form
 454  0
         updateListeners(oldV, newOwner);
 455  0
     }
 456  
 
 457  
     @Override
 458  
     protected void updateListeners(Object oldV, Object newOwner) {
 459  0
         if (oldV != null) {
 460  0
             if (oldV != newOwner) {
 461  0
                 removeElementListener(oldV);
 462  
             }
 463  0
             Object container = facade.getContainer(oldV);
 464  0
             while (container != null && !facade.isTop(container)) {
 465  0
                 removeElementListener(container);
 466  0
                 container = facade.getContainer(container);
 467  
             }
 468  
         }
 469  0
         super.updateListeners(getOwner(), newOwner);
 470  0
     }
 471  
     
 472  
     @Override
 473  
     protected void updateFont() {
 474  0
         super.updateFont();
 475  0
         Font f = getSettings().getFont(Font.PLAIN);
 476  0
         referenceFig.setFont(f);
 477  0
     }
 478  
 
 479  
 }