Coverage Report - org.argouml.uml.ui.ActionNewDiagram
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionNewDiagram
66%
30/45
36%
8/22
2.429
 
 1  
 /* $Id: ActionNewDiagram.java 18451 2010-06-21 11:57:41Z 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  
  *    bobtarling
 11  
  *    mvw
 12  
  *****************************************************************************
 13  
  *
 14  
  * Some portions of this file was previously release using the BSD License:
 15  
  */
 16  
 
 17  
 // Copyright (c) 2006-2008 The Regents of the University of California. All
 18  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 19  
 // software and its documentation without fee, and without a written
 20  
 // agreement is hereby granted, provided that the above copyright notice
 21  
 // and this paragraph appear in all copies. This software program and
 22  
 // documentation are copyrighted by The Regents of the University of
 23  
 // California. The software program and documentation are supplied "AS
 24  
 // IS", without any accompanying services from The Regents. The Regents
 25  
 // does not warrant that the operation of the program will be
 26  
 // uninterrupted or error-free. The end-user understands that the program
 27  
 // was developed for research purposes and is advised not to rely
 28  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 29  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 30  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 31  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 32  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 33  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 34  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 35  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 36  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 37  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 38  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 39  
 
 40  
 package org.argouml.uml.ui;
 41  
 
 42  
 import java.awt.event.ActionEvent;
 43  
 
 44  
 import javax.swing.Action;
 45  
 
 46  
 import org.apache.log4j.Logger;
 47  
 import org.argouml.application.helpers.ResourceLoaderWrapper;
 48  
 import org.argouml.i18n.Translator;
 49  
 import org.argouml.kernel.Project;
 50  
 import org.argouml.kernel.ProjectManager;
 51  
 import org.argouml.model.Model;
 52  
 import org.argouml.ui.UndoableAction;
 53  
 import org.argouml.ui.explorer.ExplorerEventAdaptor;
 54  
 import org.argouml.ui.targetmanager.TargetManager;
 55  
 import org.argouml.uml.diagram.ArgoDiagram;
 56  
 import org.argouml.uml.diagram.DiagramSettings;
 57  
 
 58  
 /**
 59  
  * Abstract action to trigger creation of a new diagram. <p>
 60  
  * 
 61  
  * ArgoUML shall never create a diagram for a read-only modelelement.
 62  
  * <p>
 63  
  * TODO: Bobs says, can we merge ActionAddDiagram with this class?
 64  
  * 
 65  
  * @author michiel
 66  
  */
 67  900
 public abstract class ActionNewDiagram extends UndoableAction {
 68  
 
 69  900
     private static final Logger LOG = Logger.getLogger(ActionNewDiagram.class);
 70  
 
 71  
     /**
 72  
      * The constructor.
 73  
      * @param name the i18n key for this action name.
 74  
      */
 75  
     protected ActionNewDiagram(String name) {
 76  18000
         super(Translator.localize(name),
 77  
                 ResourceLoaderWrapper.lookupIcon(name));
 78  
         // Set the tooltip string:
 79  18000
         putValue(Action.SHORT_DESCRIPTION, 
 80  
                 Translator.localize(name));
 81  18000
     }
 82  
 
 83  
     /*
 84  
      * @see java.awt.event.ActionListener#actionPerformed(
 85  
      *      java.awt.event.ActionEvent)
 86  
      */
 87  
     @Override
 88  
     public void actionPerformed(ActionEvent e) {
 89  
 
 90  
         // TODO: Get Project or other necessary context from source??
 91  
         // e.getSource();
 92  
         
 93  
         // TODO: Since there may be multiple top level elements in
 94  
         // a project, this should be using the default Namespace (currently
 95  
         // undefined) or something similar 
 96  96
         Project p = ProjectManager.getManager().getCurrentProject();
 97  96
         Object ns = findNamespace();
 98  
         
 99  96
         if (ns != null && isValidNamespace(ns)) {
 100  96
             super.actionPerformed(e);
 101  96
             DiagramSettings settings = 
 102  
                 p.getProjectSettings().getDefaultDiagramSettings();
 103  96
             ArgoDiagram diagram = createDiagram(ns, settings);
 104  
             assert (diagram != null)
 105  96
             : "No diagram was returned by the concrete class";
 106  
 
 107  96
             p.addMember(diagram);
 108  
             //TODO: make the explorer listen to project member property
 109  
             //changes...  to eliminate coupling on gui.
 110  96
             ExplorerEventAdaptor.getInstance().modelElementAdded(
 111  
                     diagram.getNamespace());
 112  96
             TargetManager.getInstance().setTarget(diagram);
 113  96
         } else {
 114  0
             LOG.error("No valid namespace found");
 115  0
             throw new IllegalStateException("No valid namespace found");
 116  
         }
 117  96
     }
 118  
 
 119  
     /**
 120  
      * Find an alternative namespace for the diagram, only to be used 
 121  
      * if the target is not suitable.
 122  
      *
 123  
      * @return the namespace or null
 124  
      */
 125  
     protected Object findNamespace() {
 126  96
         Project p = ProjectManager.getManager().getCurrentProject();
 127  96
         return p.getRoot();
 128  
     }
 129  
 
 130  
     /**
 131  
      * @param namespace the namespace in which to create the diagram
 132  
      * @return the new diagram
 133  
      * @deprecated for 0.29.1 by tfmorris. Use
 134  
      *             {@link #createDiagram(Object, DiagramSettings)}/
 135  
      */
 136  
     @SuppressWarnings("deprecation")
 137  
     @Deprecated
 138  
     protected ArgoDiagram createDiagram(Object namespace) {
 139  0
         DiagramSettings settings = ProjectManager.getManager()
 140  
                 .getCurrentProject().getProjectSettings()
 141  
                 .getDefaultDiagramSettings();
 142  
 
 143  0
         return createDiagram(namespace, settings);
 144  
     }
 145  
 
 146  
     /**
 147  
      * @param namespace the namespace in which to create the diagram
 148  
      * @param settings the render settings for the diagram
 149  
      * @return the new diagram
 150  
      */
 151  
     protected abstract ArgoDiagram createDiagram(Object namespace, 
 152  
             DiagramSettings settings);
 153  
 
 154  
     /**
 155  
      * Test if the given namespace is a valid namespace to add the diagram to.
 156  
      * TODO: This method was created to facilitate the merge 
 157  
      * of this class with ActionAddDiagram.
 158  
      *
 159  
      * @param ns the namespace to check
 160  
      * @return Returns <code>true</code> if valid.
 161  
      */
 162  
     public boolean isValidNamespace(Object ns) {
 163  96
         return true;
 164  
     }
 165  
 
 166  
     /**
 167  
      * Utility function to create a collaboration.
 168  
      * 
 169  
      * @return a new collaboration
 170  
      * @param namespace the back-up namespace to put the collaboration in
 171  
      */
 172  
     protected static Object createCollaboration(Object namespace) {
 173  38
         Object target = TargetManager.getInstance().getModelTarget();
 174  38
         if (Model.getFacade().isAUMLElement(target) 
 175  
                 && Model.getModelManagementHelper().isReadOnly(target)) {
 176  0
             target = namespace;
 177  
         }
 178  38
         Object collaboration = null;
 179  38
         if (Model.getFacade().isAOperation(target)) {
 180  0
             Object ns = Model.getFacade().getNamespace(
 181  
                     Model.getFacade().getOwner(target));
 182  0
             collaboration =
 183  
                 Model.getCollaborationsFactory().buildCollaboration(ns, target);
 184  0
         } else if (Model.getFacade().isAClassifier(target)) {
 185  0
             Object ns = Model.getFacade().getNamespace(target);
 186  0
             collaboration =
 187  
                 Model.getCollaborationsFactory().buildCollaboration(ns, target);
 188  0
         } else {
 189  38
             collaboration =
 190  
                 Model.getCollaborationsFactory().createCollaboration();
 191  38
             if (Model.getFacade().isANamespace(target)) {
 192  
                 /* TODO: Not all namespaces are useful here - any WFRs? */
 193  0
                 namespace = target;
 194  
             } else {
 195  38
                 if (Model.getFacade().isAModelElement(target)) {
 196  0
                     Object ns = Model.getFacade().getNamespace(target);
 197  0
                     if (Model.getFacade().isANamespace(ns)) {
 198  0
                         namespace = ns;
 199  
                     }
 200  
                 }
 201  
             }
 202  38
             Model.getCoreHelper().setNamespace(collaboration, namespace);
 203  38
             Model.getCoreHelper().setName(collaboration, 
 204  
                     "unattachedCollaboration");
 205  
         }
 206  38
         return collaboration;
 207  
     }
 208  
 }