Coverage Report - org.argouml.uml.diagram.ui.ActionList
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionList
0%
0/46
0%
0/48
5.143
 
 1  
 /* $Id: ActionList.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  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 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.util.List;
 42  
 import java.util.Vector;
 43  
 
 44  
 import javax.swing.Action;
 45  
 import javax.swing.JMenu;
 46  
 import javax.swing.JMenuItem;
 47  
 
 48  
 import org.argouml.kernel.UmlModelMutator;
 49  
 
 50  
 /**
 51  
  * A specialist collection used for filtering the return from getPopUpActions
 52  
  * in FigNodeModelElement and FigEdgeModelElement.
 53  
  * This class should remain package scope as I imagine its lifetime to be
 54  
  * short (we need a better way of registering Actions with Figs)
 55  
  *
 56  
  * @author Bob Tarling
 57  
  */
 58  
 class ActionList<E> extends Vector<E> {
 59  
     
 60  
     private final boolean readonly;
 61  
     
 62  
     /**
 63  
      * Construct an ActionList which filters modifying actions when
 64  
      * the readonly flag is set using initialList as the initial
 65  
      * content.
 66  
      * 
 67  
      * @param initialList initial contents to use for list.
 68  
      * @param readOnly true if mutating actions should be filtered
 69  
      */
 70  
     ActionList(List<? extends E> initialList, boolean readOnly) {
 71  0
         super(initialList);
 72  0
         this.readonly = readOnly;
 73  0
     }
 74  
     
 75  
     @Override
 76  
     public boolean add(E o) {
 77  0
         if (readonly) {
 78  0
             if (isUmlMutator(o) ) {
 79  0
                 return false;
 80  0
             } else if (o instanceof JMenu) {
 81  0
                 o = (E) trimMenu((JMenu) o);
 82  
             }
 83  
         }
 84  0
         if (o != null) {
 85  0
             return super.add(o);
 86  
         } else {
 87  0
             return false;
 88  
         }
 89  
     }
 90  
     
 91  
     @Override
 92  
     public void addElement(E o) {
 93  0
         if (readonly) {
 94  0
             if (isUmlMutator(o)) {
 95  0
                 return;
 96  0
             } else if (o instanceof JMenu) {
 97  0
                 o = (E) trimMenu((JMenu) o);
 98  
             }
 99  
         }
 100  0
         if (o != null) {
 101  0
             super.addElement(o);
 102  
         }
 103  0
     }
 104  
     
 105  
     @Override
 106  
     public void add(int index, E o) {
 107  0
         if (readonly) {
 108  0
             if (isUmlMutator(o)) {
 109  0
                 return;
 110  0
             } else if (o instanceof JMenu) {
 111  0
                 o = (E) trimMenu((JMenu) o);
 112  
             }
 113  
         }
 114  0
         if (o != null) {
 115  0
             super.add(index, o);
 116  
         }
 117  0
     }
 118  
     
 119  
     @Override
 120  
     public void insertElementAt(E o, int index) {
 121  0
         if (readonly) {
 122  0
             if (isUmlMutator(o)) {
 123  0
                 return;
 124  0
             } else if (o instanceof JMenu) {
 125  0
                 o = (E) trimMenu((JMenu) o);
 126  
             }
 127  
         }
 128  0
         if (o != null) {
 129  0
             super.insertElementAt(o, index);
 130  
         }
 131  0
     }
 132  
     
 133  
     /**
 134  
      * Trim out any menu items that have a UML model mutating action
 135  
      * @param menu the menu to be filtered
 136  
      * @return The trimmed menu or null if all contents trimmed out.
 137  
      */
 138  
     private JMenu trimMenu(JMenu menu) {
 139  0
         for (int i = menu.getItemCount() - 1; i >= 0; --i) {
 140  0
             JMenuItem menuItem = menu.getItem(i);
 141  0
             Action action = menuItem.getAction();
 142  0
             if (action == null
 143  
                     && menuItem.getActionListeners().length > 0
 144  
                     && menuItem.getActionListeners()[0] instanceof Action) {
 145  0
                 action = (Action) menuItem.getActionListeners()[0];
 146  
             }
 147  0
             if (isUmlMutator(action)) {
 148  0
                 menu.remove(i);
 149  
             }
 150  
         }
 151  0
         if (menu.getItemCount() == 0) {
 152  0
             return null;
 153  
         }
 154  0
         return menu;
 155  
     }
 156  
     
 157  
     /**
 158  
      * @param a An object (typically an Action) to test if its a UML model
 159  
      * mutator
 160  
      * @return true if the given action mutates the UML model.
 161  
      */
 162  
     private boolean isUmlMutator(Object a) {
 163  0
         return a instanceof UmlModelMutator 
 164  
             || a.getClass().isAnnotationPresent(UmlModelMutator.class);
 165  
     }
 166  
 }