Coverage Report - org.argouml.ui.ProjectActions
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectActions
37%
28/75
14%
6/42
3.231
ProjectActions$1
0%
0/10
0%
0/8
3.231
 
 1  
 /* $Id: ProjectActions.java 18968 2011-01-13 14:39:23Z tfmorris $
 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) 2007-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.ui;
 40  
 
 41  
 import java.beans.PropertyChangeEvent;
 42  
 import java.beans.PropertyChangeListener;
 43  
 import java.util.Collection;
 44  
 import java.util.List;
 45  
 
 46  
 import javax.swing.AbstractAction;
 47  
 import javax.swing.SwingUtilities;
 48  
 
 49  
 import org.argouml.application.helpers.ResourceLoaderWrapper;
 50  
 import org.argouml.i18n.Translator;
 51  
 import org.argouml.kernel.Project;
 52  
 import org.argouml.kernel.ProjectManager;
 53  
 import org.argouml.kernel.UndoManager;
 54  
 import org.argouml.ui.targetmanager.TargetEvent;
 55  
 import org.argouml.ui.targetmanager.TargetListener;
 56  
 import org.argouml.ui.targetmanager.TargetManager;
 57  
 import org.argouml.uml.diagram.ArgoDiagram;
 58  
 import org.argouml.uml.diagram.DiagramUtils;
 59  
 import org.argouml.uml.diagram.UMLMutableGraphSupport;
 60  
 import org.argouml.uml.diagram.ui.ActionRemoveFromDiagram;
 61  
 import org.tigris.gef.base.Editor;
 62  
 import org.tigris.gef.base.Globals;
 63  
 import org.tigris.gef.graph.GraphModel;
 64  
 import org.tigris.gef.presentation.Fig;
 65  
 
 66  
 /**
 67  
  * Class to manage Project related actions which need to be (or historically
 68  
  * have been) managed as singletons.
 69  
  * 
 70  
  * TODO: It's unclear to me whether all of these actually have to be managed as
 71  
  * singletons, but for now I've just moved them from ProjectBrowser as is. - tfm
 72  
  * 
 73  
  * @author Tom Morris
 74  
  */
 75  0
 public final class ProjectActions
 76  
         implements TargetListener, PropertyChangeListener {
 77  
 
 78  
     private static ProjectActions theInstance;
 79  
     
 80  
     private ProjectActions() {
 81  900
         super();
 82  
         
 83  900
         undoAction = new ActionUndo(
 84  
                 Translator.localize("action.undo"),
 85  
                 ResourceLoaderWrapper.lookupIcon("Undo"));
 86  900
         undoAction.setEnabled(false);
 87  
         
 88  900
         redoAction = new ActionRedo(
 89  
                 Translator.localize("action.redo"),
 90  
                 ResourceLoaderWrapper.lookupIcon("Redo"));
 91  900
         redoAction.setEnabled(false);
 92  
         
 93  900
         TargetManager.getInstance().addTargetListener(this);
 94  900
         Project p = ProjectManager.getManager().getCurrentProject();
 95  900
         if (p != null) {
 96  0
             p.getUndoManager().addPropertyChangeListener(this);
 97  
         }
 98  
             
 99  900
     }
 100  
 
 101  
     /**
 102  
      * The action to undo the last user interaction.
 103  
      */
 104  
     private final ActionUndo undoAction;
 105  
     /**
 106  
      * The action to redo the last undone action.
 107  
      */
 108  
     private final AbstractAction redoAction;
 109  
 
 110  
     /**
 111  
      * Singleton retrieval method for the projectbrowser. Lazely instantiates
 112  
      * the projectbrowser.
 113  
      * @return the singleton instance of the projectbrowser
 114  
      */
 115  
     public static synchronized ProjectActions getInstance() {
 116  3600
         if (theInstance == null) {
 117  900
             theInstance = new ProjectActions();
 118  
         }
 119  3600
         return theInstance;
 120  
     }
 121  
 
 122  
     /**
 123  
      * The action to remove the current selected Figs from the diagram.
 124  
      */
 125  900
     private final ActionRemoveFromDiagram removeFromDiagram =
 126  
         new ActionRemoveFromDiagram(
 127  
                 Translator.localize("action.remove-from-diagram"));
 128  
 
 129  
     /**
 130  
      * Get the action that can undo the last user interaction on this project.
 131  
      * @return the undo action.
 132  
      */
 133  
     public AbstractAction getUndoAction() {
 134  900
         return undoAction;
 135  
     }
 136  
 
 137  
     /**
 138  
      * Get the action that can redo the last undone action.
 139  
      * @return the redo action.
 140  
      */
 141  
     public AbstractAction getRedoAction() {
 142  900
         return redoAction;
 143  
     }
 144  
 
 145  
     /**
 146  
      * Get the action that removes selected figs from the diagram.
 147  
      * @return the remove from diagram action.
 148  
      */
 149  
     public AbstractAction getRemoveFromDiagramAction() {
 150  1800
         return removeFromDiagram;
 151  
     }
 152  
 
 153  
     /*
 154  
      * @see org.argouml.ui.targetmanager.TargetListener#targetAdded(org.argouml.ui.targetmanager.TargetEvent)
 155  
      */
 156  
     public void targetAdded(TargetEvent e) {
 157  0
         determineRemoveEnabled();
 158  0
     }
 159  
 
 160  
     /*
 161  
      * @see org.argouml.ui.targetmanager.TargetListener#targetRemoved(org.argouml.ui.targetmanager.TargetEvent)
 162  
      */
 163  
     public void targetRemoved(TargetEvent e) {
 164  88
         determineRemoveEnabled();
 165  88
     }
 166  
 
 167  
     /*
 168  
      * @see org.argouml.ui.targetmanager.TargetListener#targetSet(org.argouml.ui.targetmanager.TargetEvent)
 169  
      */
 170  
     public void targetSet(TargetEvent e) {
 171  1239
         determineRemoveEnabled();
 172  1239
     }
 173  
     
 174  
     /**
 175  
      * Enabled the remove action if an item is selected in anything other then
 176  
      * the activity or state diagrams.
 177  
      */
 178  
     private void determineRemoveEnabled() {
 179  1327
         Editor editor = Globals.curEditor();
 180  1327
         Collection figs = editor.getSelectionManager().getFigs();
 181  1327
         boolean removeEnabled = !figs.isEmpty();
 182  1327
         GraphModel gm = editor.getGraphModel();
 183  1327
         if (gm instanceof UMLMutableGraphSupport) {
 184  427
             removeEnabled =
 185  
                 ((UMLMutableGraphSupport) gm).isRemoveFromDiagramAllowed(figs);
 186  
         }
 187  1327
         removeFromDiagram.setEnabled(removeEnabled);
 188  1327
     }
 189  
 
 190  
     /**
 191  
      * Display the diagram which contains the given list of targets and scroll
 192  
      * to make them visible.
 193  
      *
 194  
      * @param targets Collection of targets to show
 195  
      */ 
 196  
      // TODO: Move to different class?
 197  
     public static void jumpToDiagramShowing(List targets) {
 198  
 
 199  0
         if (targets == null || targets.size() == 0) {
 200  0
             return;
 201  
         }
 202  0
         Object first = targets.get(0);
 203  0
         if (first instanceof ArgoDiagram && targets.size() > 1) {
 204  0
             setTarget(first);
 205  0
             setTarget(targets.get(1));
 206  0
             return;
 207  
         }
 208  0
         if (first instanceof ArgoDiagram && targets.size() == 1) {
 209  0
             setTarget(first);
 210  0
             return;
 211  
         }
 212  
         
 213  
         // TODO: This should get the containing project from the list of
 214  
         // targets, not from some global
 215  0
         Project project = ProjectManager.getManager().getCurrentProject();
 216  0
         if (project == null) {
 217  0
             return;
 218  
         }
 219  
         
 220  0
         List<ArgoDiagram> diagrams = project.getDiagramList();
 221  0
         Object target = TargetManager.getInstance().getTarget();
 222  0
         if ((target instanceof ArgoDiagram)
 223  
             && ((ArgoDiagram) target).countContained(targets) == targets.size()) {
 224  0
             setTarget(first);
 225  0
             return;
 226  
         }
 227  
 
 228  0
         ArgoDiagram bestDiagram = null;
 229  0
         int bestNumContained = 0;
 230  0
         for (ArgoDiagram d : diagrams) {
 231  0
             int nc = d.countContained(targets);
 232  0
             if (nc > bestNumContained) {
 233  0
                 bestNumContained = nc;
 234  0
                 bestDiagram = d;
 235  
             }
 236  0
             if (nc == targets.size()) {
 237  0
                 break;
 238  
             }
 239  0
         }
 240  0
         if (bestDiagram != null) {
 241  0
             if (!DiagramUtils.getActiveDiagram().equals(bestDiagram)) {
 242  0
                 setTarget(bestDiagram);
 243  
             }
 244  0
             setTarget(first);
 245  
         }
 246  
         // making it possible to jump to the modelroots
 247  0
         if (project.getRoots().contains(first)) {
 248  0
             setTarget(first);
 249  
         }
 250  
 
 251  
         // and finally, adjust the scrollbars to show the Fig
 252  0
         Object f = TargetManager.getInstance().getFigTarget();
 253  0
         if (f instanceof Fig) {
 254  0
             Globals.curEditor().scrollToShow((Fig) f);
 255  
         }
 256  
         
 257  0
     }
 258  
 
 259  
     private static void setTarget(Object o) {
 260  0
         TargetManager.getInstance().setTarget(o);
 261  0
     }
 262  
 
 263  
     public void propertyChange(final PropertyChangeEvent evt) {
 264  0
         if (evt.getSource() instanceof UndoManager) {
 265  0
             SwingUtilities.invokeLater(new Runnable() {
 266  
                 public void run() {
 267  0
                     if ("undoLabel".equals(evt.getPropertyName())) {
 268  0
                         undoAction.putValue(AbstractAction.NAME, evt
 269  
                                 .getNewValue());
 270  
                     }
 271  0
                     if ("redoLabel".equals(evt.getPropertyName())) {
 272  0
                         redoAction.putValue(AbstractAction.NAME, evt
 273  
                                 .getNewValue());
 274  
                     }
 275  0
                     if ("undoable".equals(evt.getPropertyName())) {
 276  0
                         undoAction.setEnabled((Boolean) evt.getNewValue());
 277  
                     }
 278  0
                     if ("redoable".equals(evt.getPropertyName())) {
 279  0
                         redoAction.setEnabled((Boolean) evt.getNewValue());
 280  
                     }
 281  0
                 }
 282  
             });
 283  
         }
 284  0
     }
 285  
 }