Coverage Report - org.argouml.uml.diagram.ui.ActionAddConcurrentRegion
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionAddConcurrentRegion
0%
0/60
0%
0/18
4.667
 
 1  
 /* $Id: ActionAddConcurrentRegion.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-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  
 
 40  
 package org.argouml.uml.diagram.ui;
 41  
 
 42  
 import java.awt.Rectangle;
 43  
 import java.awt.event.ActionEvent;
 44  
 import java.util.List;
 45  
 
 46  
 import javax.swing.Action;
 47  
 
 48  
 import org.apache.log4j.Logger;
 49  
 import org.argouml.application.helpers.ResourceLoaderWrapper;
 50  
 import org.argouml.i18n.Translator;
 51  
 import org.argouml.model.Model;
 52  
 import org.argouml.model.StateMachinesFactory;
 53  
 import org.argouml.ui.targetmanager.TargetManager;
 54  
 import org.argouml.uml.diagram.DiagramSettings;
 55  
 import org.argouml.uml.diagram.state.StateDiagramGraphModel;
 56  
 import org.argouml.uml.diagram.state.ui.FigCompositeState;
 57  
 import org.argouml.uml.diagram.state.ui.FigConcurrentRegion;
 58  
 import org.argouml.uml.diagram.state.ui.FigStateVertex;
 59  
 import org.tigris.gef.base.Editor;
 60  
 import org.tigris.gef.base.Globals;
 61  
 import org.tigris.gef.base.LayerDiagram;
 62  
 import org.tigris.gef.graph.GraphModel;
 63  
 import org.tigris.gef.graph.MutableGraphModel;
 64  
 import org.tigris.gef.presentation.Fig;
 65  
 import org.argouml.ui.UndoableAction;
 66  
 
 67  
 //TODO: There is a cyclic dependency between this class and FigConcurrentRegion
 68  
 
 69  
 /**
 70  
  * Add a concurrent region to a concurrent composite state
 71  
  * <p>
 72  
  * This action can be executed with either 
 73  
  * the composite concurrent state selected,
 74  
  * or one of its concurrent regions.
 75  
  * <p>
 76  
  * TODO: Move all the magic numbers to constants
 77  
  * 
 78  
  * @author pepargouml@yahoo.es
 79  
  */
 80  
 public class ActionAddConcurrentRegion extends UndoableAction {
 81  
 
 82  
 
 83  
     /** logger */
 84  0
     private static final Logger LOG =
 85  
         Logger.getLogger(ActionAddConcurrentRegion.class);
 86  
 
 87  
     /**
 88  
      * Constructor
 89  
      */
 90  
     public ActionAddConcurrentRegion() {
 91  0
         super(Translator.localize("action.add-concurrent-region"),
 92  
                 ResourceLoaderWrapper.lookupIcon(
 93  
                         "action.add-concurrent-region"));
 94  
         // Set the tooltip string:
 95  0
         putValue(Action.SHORT_DESCRIPTION, 
 96  
                 Translator.localize("action.add-concurrent-region"));
 97  0
     }
 98  
 
 99  
     ////////////////////////////////////////////////////////////////
 100  
     // main methods
 101  
 
 102  
     /*
 103  
      * @see javax.swing.Action#isEnabled()
 104  
      */
 105  
     public boolean isEnabled() {
 106  0
         Object target = TargetManager.getInstance().getModelTarget();
 107  0
         if (Model.getStateMachinesHelper().isTopState(target)) {
 108  0
             return false;
 109  
         }
 110  0
         return TargetManager.getInstance().getModelTargets().size() < 2;
 111  
     }
 112  
 
 113  
     /*
 114  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 115  
      */
 116  
     public void actionPerformed(ActionEvent ae) {
 117  0
         super.actionPerformed(ae);
 118  
         try {
 119  
             /*Here the actions to divide a region*/
 120  0
             Fig f = TargetManager.getInstance().getFigTarget();
 121  
 
 122  0
             if (Model.getFacade().isAConcurrentRegion(f.getOwner())) {
 123  0
                 f = f.getEnclosingFig();
 124  
             }
 125  
 
 126  0
             final FigCompositeState figCompositeState = (FigCompositeState) f;
 127  
 
 128  0
             final List<FigConcurrentRegion> regionFigs = 
 129  
                 ((List<FigConcurrentRegion>) f.getEnclosedFigs().clone());
 130  0
             final Object umlCompositeState = figCompositeState.getOwner();
 131  0
             Editor editor = Globals.curEditor();
 132  0
             GraphModel gm = editor.getGraphModel();
 133  0
             LayerDiagram lay =
 134  
                 ((LayerDiagram) editor.getLayerManager().getActiveLayer());
 135  
 
 136  0
             Rectangle rName =
 137  
                 ((FigNodeModelElement) f).getNameFig().getBounds();
 138  0
             Rectangle rFig = f.getBounds();
 139  0
             if (!(gm instanceof MutableGraphModel)) {
 140  0
                 return;
 141  
             }
 142  
 
 143  0
             StateDiagramGraphModel mgm = (StateDiagramGraphModel) gm;
 144  
 
 145  0
             final StateMachinesFactory factory =
 146  
                 Model.getStateMachinesFactory();
 147  
             
 148  0
             if (!Model.getFacade().isConcurrent(umlCompositeState)) {
 149  
 
 150  0
                 final Object umlRegion1 =
 151  
                     factory.buildCompositeState(umlCompositeState);
 152  0
                 Rectangle bounds = new Rectangle(
 153  
                         f.getX() + FigConcurrentRegion.INSET_HORZ, 
 154  
                         f.getY() + rName.height 
 155  
                             + FigConcurrentRegion.INSET_VERT, 
 156  
                         rFig.width - 2 * FigConcurrentRegion.INSET_HORZ, 
 157  
                         rFig.height - rName.height 
 158  
                             - 2 * FigConcurrentRegion.INSET_VERT);
 159  0
                 DiagramSettings settings = figCompositeState.getSettings();
 160  0
                 final FigConcurrentRegion firstRegionFig =
 161  
                     new FigConcurrentRegion(
 162  
                         umlRegion1, bounds, settings);
 163  
                 /* The 1st region has an invisible divider line 
 164  
                  * (the box is always invisible): */
 165  0
                 firstRegionFig.setLineColor(ArgoFig.INVISIBLE_LINE_COLOR);
 166  
 
 167  0
                 firstRegionFig.setEnclosingFig(figCompositeState);
 168  0
                 firstRegionFig.setLayer(lay);
 169  0
                 lay.add(firstRegionFig);
 170  
 
 171  0
                 if (mgm.canAddNode(umlRegion1)) {
 172  0
                     mgm.getNodes().add(umlRegion1);
 173  0
                     mgm.fireNodeAdded(umlRegion1);
 174  
                 }
 175  
 
 176  
                 /* Throw out any previous elements that were 
 177  
                  * enclosed but are not a concurrent region;
 178  
                  * let's move them onto the first region: */
 179  0
                 if (!regionFigs.isEmpty()) {
 180  0
                     for (int i = 0; i < regionFigs.size(); i++) {
 181  0
                         FigStateVertex curFig = regionFigs.get(i);
 182  0
                         curFig.setEnclosingFig(firstRegionFig);
 183  0
                         firstRegionFig.addEnclosedFig(curFig);
 184  0
                         curFig.redrawEnclosedFigs();
 185  
                     }
 186  
                 }
 187  
             }
 188  
 
 189  0
             final Object umlRegion2 = 
 190  
                 factory.buildCompositeState(umlCompositeState);
 191  
             // TODO: What are these magic numbers?
 192  0
             Rectangle bounds = new Rectangle(
 193  
                     f.getX() + FigConcurrentRegion.INSET_HORZ, 
 194  
                     f.getY() + rFig.height - 1, //linewidth?
 195  
                     rFig.width - 2 * FigConcurrentRegion.INSET_HORZ, 
 196  
                     126);
 197  0
             DiagramSettings settings = figCompositeState.getSettings();
 198  0
             final FigConcurrentRegion newRegionFig =
 199  
                 new FigConcurrentRegion(umlRegion2, bounds, settings);
 200  
             /* The divider line should be visible, so no need to change its color. */
 201  
 
 202  
             /* Make the composite state 1 region higher: */
 203  0
             figCompositeState.setCompositeStateHeight(
 204  
                     rFig.height + newRegionFig.getInitialHeight());
 205  0
             newRegionFig.setEnclosingFig(figCompositeState);
 206  0
             figCompositeState.addEnclosedFig(newRegionFig);
 207  0
             newRegionFig.setLayer(lay);
 208  0
             lay.add(newRegionFig);
 209  0
             editor.getSelectionManager().select(f);
 210  0
             if (mgm.canAddNode(umlRegion2)) {
 211  0
                 mgm.getNodes().add(umlRegion2);
 212  0
                 mgm.fireNodeAdded(umlRegion2);
 213  
             }
 214  
 
 215  
             /* TODO: Verify this. 
 216  
              * IIUC, then this triggers the CompountStateFig 
 217  
              * to draw itself correctly.
 218  
              * Hence, there was a reason to wait this long 
 219  
              * to make the state concurrent. */
 220  0
             Model.getStateMachinesHelper().setConcurrent(
 221  
                     umlCompositeState, true);
 222  
 
 223  0
         } catch (Exception ex) {
 224  0
             LOG.error("Exception caught", ex);
 225  0
         }
 226  0
     }
 227  
 
 228  
 }