Coverage Report - org.argouml.uml.diagram.ui.ActionCompartmentDisplay
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionCompartmentDisplay
0%
0/97
0%
0/50
9.333
 
 1  
 /* $Id: ActionCompartmentDisplay.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  
 package org.argouml.uml.diagram.ui;
 40  
 
 41  
 import java.awt.event.ActionEvent;
 42  
 import java.util.ArrayList;
 43  
 import java.util.Collection;
 44  
 import java.util.List;
 45  
 
 46  
 import javax.swing.Action;
 47  
 
 48  
 import org.argouml.i18n.Translator;
 49  
 import org.argouml.model.Model;
 50  
 import org.tigris.gef.base.Editor;
 51  
 import org.tigris.gef.base.Globals;
 52  
 import org.tigris.gef.base.Selection;
 53  
 import org.tigris.gef.presentation.Fig;
 54  
 import org.argouml.ui.UndoableAction;
 55  
 
 56  
 
 57  
 /**
 58  
  * A class to implement the actions involved in hiding and showing
 59  
  * compartments on interfaces, classes and use cases.<p>
 60  
  *
 61  
  * This implementation may easily be extended for other
 62  
  * compartments of other figs.<p>
 63  
  *
 64  
  * The class declares a number of static instances, each with an
 65  
  * actionPerformed method that performs the required action.
 66  
  */
 67  
 public class ActionCompartmentDisplay extends UndoableAction {
 68  
 
 69  
     /**
 70  
      * A flag to indicate whether the action should show or hide the
 71  
      * relevant compartment.
 72  
      */
 73  0
     private boolean display = false;
 74  
 
 75  
     /**
 76  
      * Compartment type(s) field.
 77  
      * Bitfield of flags with a bit for each compartment type
 78  
      */
 79  
     private int cType;
 80  
 
 81  
     private static final int COMPARTMENT_ATTRIBUTE = 1;
 82  
     private static final int COMPARTMENT_OPERATION = 2;
 83  
     private static final int COMPARTMENT_EXTENSIONPOINT = 4;
 84  
     private static final int COMPARTMENT_ENUMLITERAL = 8;
 85  
 
 86  
 
 87  
     /**
 88  
      * Static instance to show the attribute compartment of a class.
 89  
      */
 90  0
     private static final UndoableAction SHOW_ATTR_COMPARTMENT =
 91  
         new ActionCompartmentDisplay(true,
 92  
                 "action.show-attribute-compartment", COMPARTMENT_ATTRIBUTE);
 93  
 
 94  
     /**
 95  
      * Static instance to hide the attribute compartment of a class.
 96  
      */
 97  0
     private static final UndoableAction HIDE_ATTR_COMPARTMENT =
 98  
         new ActionCompartmentDisplay(false,
 99  
                 "action.hide-attribute-compartment", COMPARTMENT_ATTRIBUTE);
 100  
 
 101  
     /**
 102  
      * Static instance to show the operation compartment of a class.
 103  
      */
 104  0
     private static final UndoableAction SHOW_OPER_COMPARTMENT =
 105  
         new ActionCompartmentDisplay(true,
 106  
                 "action.show-operation-compartment", COMPARTMENT_OPERATION);
 107  
 
 108  
     /**
 109  
      * Static instance to hide the operation compartment of a class.
 110  
      */
 111  0
     private static final UndoableAction HIDE_OPER_COMPARTMENT =
 112  
         new ActionCompartmentDisplay(false,
 113  
                 "action.hide-operation-compartment", COMPARTMENT_OPERATION);
 114  
 
 115  
     /**
 116  
      * Static instance to show the extension point compartment of a use
 117  
      * case.
 118  
      */
 119  0
     private static final UndoableAction SHOW_EXTPOINT_COMPARTMENT =
 120  
         new ActionCompartmentDisplay(true,
 121  
                 "action.show-extension-point-compartment", 
 122  
                 COMPARTMENT_EXTENSIONPOINT);
 123  
 
 124  
     /**
 125  
      * Static instance to hide the extension point compartment of a use
 126  
      * case.
 127  
      */
 128  0
     private static final UndoableAction HIDE_EXTPOINT_COMPARTMENT =
 129  
         new ActionCompartmentDisplay(false,
 130  
                 "action.hide-extension-point-compartment", 
 131  
                 COMPARTMENT_EXTENSIONPOINT);
 132  
 
 133  
     /**
 134  
      * Static instance to show both compartments of a class or enumeration.
 135  
      */
 136  0
     private static final UndoableAction SHOW_ALL_COMPARTMENTS =
 137  
         new ActionCompartmentDisplay(true, "action.show-all-compartments", 
 138  
                 COMPARTMENT_ATTRIBUTE 
 139  
                 | COMPARTMENT_OPERATION 
 140  
                 | COMPARTMENT_ENUMLITERAL);
 141  
 
 142  
     /**
 143  
      * Static instance to hide both compartments of a class or enumeration.
 144  
      */
 145  0
     private static final UndoableAction HIDE_ALL_COMPARTMENTS =
 146  
         new ActionCompartmentDisplay(false, "action.hide-all-compartments", 
 147  
                 COMPARTMENT_ATTRIBUTE 
 148  
                 | COMPARTMENT_OPERATION
 149  
                 | COMPARTMENT_ENUMLITERAL);
 150  
 
 151  
     /**
 152  
      * Static instance to show the enumeration literals compartment of an
 153  
      * enumeration.
 154  
      */
 155  0
     private static final UndoableAction SHOW_ENUMLITERAL_COMPARTMENT =
 156  
         new ActionCompartmentDisplay(true,
 157  
                 "action.show-enumeration-literal-compartment", 
 158  
                 COMPARTMENT_ENUMLITERAL);
 159  
 
 160  
     /**
 161  
      * Static instance to hide the enumeration literals compartment of an
 162  
      * enumeration.
 163  
      */
 164  0
     private static final UndoableAction HIDE_ENUMLITERAL_COMPARTMENT =
 165  
         new ActionCompartmentDisplay(false,
 166  
                 "action.hide-enumeration-literal-compartment", 
 167  
                 COMPARTMENT_ENUMLITERAL);
 168  
 
 169  
 
 170  
 
 171  
     /**
 172  
      * Constructor for a new instance. Can only be called by this class or
 173  
      * its children, since used to create static instances only.
 174  
      *
 175  
      * @param d    <code>true</code> if the compartment is to be shown,
 176  
      *             <code>false</code> if it is to be hidden
 177  
      *
 178  
      * @param c    the text to be displayed for this action
 179  
      * @param type the type of compartment. See definition at {@link #cType}
 180  
      */
 181  
     protected ActionCompartmentDisplay(boolean d, String c, int type) {
 182  0
         super(Translator.localize(c));
 183  0
         display = d;
 184  0
         cType = type;
 185  0
     }
 186  
 
 187  
 
 188  
     /**
 189  
      * Return the compartment show and/or hide actions needed for the selected
 190  
      * Figs.
 191  
      * 
 192  
      * @return Only returns the actions for the menu-items that make sense for
 193  
      *         the current selection.
 194  
      */
 195  
     public static Collection<Action> getActions() {
 196  0
         Collection<Action> actions = new ArrayList<Action>();
 197  0
         Editor ce = Globals.curEditor();
 198  
 
 199  0
         int present = 0;
 200  0
         int visible = 0;
 201  
         
 202  0
         boolean operPresent = false;
 203  0
         boolean operVisible = false;
 204  
         
 205  0
         boolean attrPresent = false;
 206  0
         boolean attrVisible = false;
 207  
         
 208  0
         boolean epPresent = false;
 209  0
         boolean epVisible = false;
 210  
         
 211  0
         boolean enumPresent = false;
 212  0
         boolean enumVisible = false;
 213  
 
 214  0
         List<Fig> figs = ce.getSelectionManager().getSelectedFigs();
 215  0
         for (Fig f : figs) {
 216  0
             final FigCompartmentBox fcb = (FigCompartmentBox) f;
 217  
 
 218  0
             final FigCompartment attributeCompartment =
 219  
                 fcb.getCompartment(Model.getMetaTypes().getAttribute());
 220  0
             if (attributeCompartment != null) {
 221  0
                 present++;
 222  0
                 attrPresent = true;
 223  0
                 attrVisible = attributeCompartment.isVisible();
 224  0
                 if (attrVisible) {
 225  0
                     visible++;
 226  
                 }
 227  
             }
 228  0
             final FigCompartment operationCompartment =
 229  
                 fcb.getCompartment(Model.getMetaTypes().getOperation());
 230  0
             if (operationCompartment != null) {
 231  0
                 present++;
 232  0
                 operPresent = true;
 233  0
                 operVisible = operationCompartment.isVisible();
 234  0
                 if (operVisible) {
 235  0
                     visible++;
 236  
                 }
 237  
             }
 238  0
             final FigCompartment epCompartment =
 239  
                 fcb.getCompartment(Model.getMetaTypes().getExtensionPoint());
 240  0
             if (epCompartment != null) {
 241  0
                 present++;
 242  0
                 epPresent = true;
 243  0
                 epVisible = epCompartment.isVisible();
 244  0
                 if (epVisible) {
 245  0
                     visible++;
 246  
                 }
 247  
             }
 248  0
             final FigCompartment elCompartment =
 249  
                 fcb.getCompartment(Model.getMetaTypes().getEnumerationLiteral());
 250  0
             if (elCompartment != null) {
 251  0
                 present++;
 252  0
                 enumPresent = true;
 253  0
                 enumVisible = elCompartment.isVisible();
 254  0
                 if (enumVisible) {
 255  0
                     visible++;
 256  
                 }
 257  
             }
 258  0
         }
 259  
 
 260  
         // Set up hide all / show all
 261  0
         if (present > 1) {
 262  0
             if (visible > 0) {
 263  0
                 actions.add(HIDE_ALL_COMPARTMENTS);
 264  
             }
 265  0
             if (present - visible > 0) {
 266  0
                 actions.add(SHOW_ALL_COMPARTMENTS);
 267  
             }
 268  
         }
 269  
 
 270  0
         if (attrPresent) {
 271  0
             if (attrVisible) {
 272  0
                 actions.add(HIDE_ATTR_COMPARTMENT);                
 273  
             } else {
 274  0
                 actions.add(SHOW_ATTR_COMPARTMENT);               
 275  
             }
 276  
         }
 277  
 
 278  0
         if (enumPresent) {
 279  0
             if (enumVisible) {
 280  0
                 actions.add(HIDE_ENUMLITERAL_COMPARTMENT);
 281  
             } else {
 282  0
                 actions.add(SHOW_ENUMLITERAL_COMPARTMENT);
 283  
             }
 284  
         }
 285  
         
 286  0
         if (operPresent) {
 287  0
             if (operVisible) {
 288  0
                 actions.add(HIDE_OPER_COMPARTMENT);                
 289  
             } else {
 290  0
                 actions.add(SHOW_OPER_COMPARTMENT);               
 291  
             }
 292  
         }
 293  
 
 294  
 
 295  0
         if (epPresent) {
 296  0
             if (epVisible) {
 297  0
                 actions.add(HIDE_EXTPOINT_COMPARTMENT);
 298  
             } else {
 299  0
                 actions.add(SHOW_EXTPOINT_COMPARTMENT);               
 300  
             }
 301  
         }
 302  
 
 303  0
         return actions;
 304  
     }
 305  
 
 306  
 
 307  
     /**
 308  
      * Action method invoked when an event triggers this action.<p>
 309  
      *
 310  
      * The {@link #cType} instance variable defines the action to
 311  
      * take, and the {@link #display} instance variable whether it should
 312  
      * set visibility or not.<p>
 313  
      *
 314  
      * @param ae  The event that triggered us.
 315  
      */
 316  
     @Override
 317  
     public void actionPerformed(ActionEvent ae) {
 318  0
         for (Selection sel : Globals.curEditor().getSelectionManager().getSelections()) {
 319  0
             final Fig f = sel.getContent();
 320  
 
 321  
             // Perform the action
 322  0
             if ((cType & COMPARTMENT_ATTRIBUTE) != 0) {
 323  0
                 final FigCompartmentBox fcb = (FigCompartmentBox) f;
 324  0
                 fcb.showCompartment(
 325  
                         Model.getMetaTypes().getAttribute(),
 326  
                         display);
 327  
                 
 328  
             }
 329  0
             if ((cType & COMPARTMENT_OPERATION) != 0) {
 330  0
                 final FigCompartmentBox fcb = (FigCompartmentBox) f;
 331  0
                 fcb.showCompartment(
 332  
                         Model.getMetaTypes().getOperation(),
 333  
                         display);
 334  
             }
 335  
 
 336  0
             if ((cType & COMPARTMENT_EXTENSIONPOINT) != 0) {
 337  0
                 final FigCompartmentBox fcb = (FigCompartmentBox) f;
 338  0
                 fcb.showCompartment(
 339  
                         Model.getMetaTypes().getExtensionPoint(),
 340  
                         display);
 341  
             }
 342  0
             if ((cType & COMPARTMENT_ENUMLITERAL) != 0) {
 343  0
                 final FigCompartmentBox fcb = (FigCompartmentBox) f;
 344  0
                 fcb.showCompartment(
 345  
                         Model.getMetaTypes().getEnumerationLiteral(),
 346  
                         display);
 347  
             }
 348  0
         }
 349  0
     }
 350  
 
 351  
 }
 352  
 
 353