Coverage Report - org.argouml.uml.diagram.ui.PropPanelDiagram
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionNavigateUpFromDiagram
27%
3/11
0%
0/4
1.933
ActionSetDiagramHomeModel
16%
2/12
0%
0/6
1.933
PropPanelDiagram
85%
12/14
50%
1/2
1.933
UMLDiagramHomeModelComboBoxModel
37%
10/27
10%
1/10
1.933
 
 1  
 /* $Id: PropPanelDiagram.java 18022 2010-02-16 08:24:42Z bobtarling $
 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) 1996-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 static org.argouml.model.Model.getModelManagementFactory;
 42  
 
 43  
 import java.awt.event.ActionEvent;
 44  
 import java.util.ArrayList;
 45  
 import java.util.Collection;
 46  
 import java.util.Collections;
 47  
 import java.util.List;
 48  
 
 49  
 import javax.swing.ImageIcon;
 50  
 import javax.swing.JComboBox;
 51  
 import javax.swing.JComponent;
 52  
 import javax.swing.JTextField;
 53  
 
 54  
 import org.argouml.i18n.Translator;
 55  
 import org.argouml.ui.TabModelTarget;
 56  
 import org.argouml.ui.UndoableAction;
 57  
 import org.argouml.ui.targetmanager.TargetManager;
 58  
 import org.argouml.uml.diagram.ArgoDiagram;
 59  
 import org.argouml.uml.diagram.Relocatable;
 60  
 import org.argouml.uml.ui.AbstractActionNavigate;
 61  
 import org.argouml.uml.ui.ActionDeleteModelElements;
 62  
 import org.argouml.uml.ui.PropPanel;
 63  
 import org.argouml.uml.ui.UMLComboBox2;
 64  
 import org.argouml.uml.ui.UMLComboBoxModel2;
 65  
 import org.argouml.uml.ui.UMLComboBoxNavigator;
 66  
 import org.argouml.uml.ui.UMLSearchableComboBox;
 67  
 import org.argouml.uml.util.PathComparator;
 68  
 
 69  
 /**
 70  
  * This class represents the properties panel for a Diagram.
 71  
  *
 72  
  */
 73  
 public class PropPanelDiagram extends PropPanel implements TabModelTarget {
 74  
 
 75  
     private JComboBox homeModelSelector;
 76  2289
     private UMLDiagramHomeModelComboBoxModel homeModelComboBoxModel =
 77  
         new UMLDiagramHomeModelComboBoxModel();
 78  
 
 79  
     /**
 80  
      * Construct a property panel with a given name and icon.
 81  
      * 
 82  
      * @param diagramName the diagram name to use as the title of the panel
 83  
      * @param icon an icon to display on the panel
 84  
      */
 85  
     protected PropPanelDiagram(String diagramName, ImageIcon icon) {
 86  2289
         super(diagramName, icon);
 87  
 
 88  2289
         JTextField field = new JTextField();
 89  
         // TODO: This should probably only update the project when the user
 90  
         // presses Return or focus is lost
 91  2289
         field.getDocument().addDocumentListener(new DiagramNameDocument(field));
 92  2289
         addField("label.name", field);
 93  
 
 94  2289
         addField("label.home-model", getHomeModelSelector());
 95  
 
 96  2289
         addAction(new ActionNavigateUpFromDiagram());
 97  2289
         addAction(ActionDeleteModelElements.getTargetFollower());
 98  2289
     }
 99  
 
 100  
     /**
 101  
      * Default constructor if there is no child of this class that can show the
 102  
      * diagram.
 103  
      */
 104  
     public PropPanelDiagram() {
 105  0
         this("Diagram", null);
 106  0
     }
 107  
 
 108  
     /**
 109  
      * Returns the home-model selector. This is a component which allows the
 110  
      * user to select a single item as the home-model, 
 111  
      * i.e. the "owner" of the diagram.
 112  
      *
 113  
      * @return a component for selecting the home-model
 114  
      */
 115  
     protected JComponent getHomeModelSelector() {
 116  2289
         if (homeModelSelector == null) {
 117  2289
             homeModelSelector = new UMLSearchableComboBox(
 118  
                     homeModelComboBoxModel,
 119  
                     new ActionSetDiagramHomeModel(), true);
 120  
         }
 121  2289
         return new UMLComboBoxNavigator(
 122  
                 Translator.localize("label.namespace.navigate.tooltip"),
 123  
                 homeModelSelector);
 124  
     }
 125  
 
 126  
 }
 127  
 
 128  
 class UMLDiagramHomeModelComboBoxModel extends UMLComboBoxModel2 {
 129  
 
 130  
     public UMLDiagramHomeModelComboBoxModel() {
 131  2289
         super(ArgoDiagram.NAMESPACE_KEY, false);
 132  2289
     }
 133  
 
 134  
     @Override
 135  
     protected void buildModelList() {
 136  0
         Object target = getTarget();
 137  0
         List list = new ArrayList();
 138  0
         if (target instanceof Relocatable) {
 139  0
             Relocatable diagram = (Relocatable) target;
 140  0
             for (Object obj : diagram.getRelocationCandidates(
 141  
                     getModelManagementFactory().getRootModel())) {
 142  0
                 if (diagram.isRelocationAllowed(obj)) {
 143  0
                     list.add(obj);
 144  
                 }
 145  
             }
 146  
         }
 147  
         /* This should not be needed if the above is correct, 
 148  
          * but let's be sure: */
 149  0
         list.add(getSelectedModelElement());
 150  0
         Collections.sort(list, new PathComparator());
 151  0
         setElements(list);
 152  0
     }
 153  
     
 154  
     @Override
 155  
     protected void buildMinimalModelList() {
 156  2949
         Collection list = new ArrayList(1);
 157  2949
         list.add(getSelectedModelElement());
 158  2949
         setElements(list);
 159  2949
         setModelInvalid();
 160  2949
     }
 161  
     
 162  
     @Override
 163  
     protected boolean isLazy() {
 164  0
         return true;
 165  
     }
 166  
 
 167  
     @Override
 168  
     protected Object getSelectedModelElement() {
 169  5898
         Object t = getTarget();
 170  5898
         if (t instanceof ArgoDiagram) {
 171  5898
             return ((ArgoDiagram) t).getOwner();
 172  
         }
 173  0
         return null;
 174  
     }
 175  
 
 176  
     @Override
 177  
     protected boolean isValidElement(Object element) {
 178  0
         Object t = getTarget();
 179  0
         if (t instanceof Relocatable) {
 180  0
             return ((Relocatable) t).isRelocationAllowed(element);
 181  
         }
 182  0
         return false;
 183  
     }
 184  
 }
 185  
 
 186  
 class ActionSetDiagramHomeModel extends UndoableAction {
 187  
     protected ActionSetDiagramHomeModel() {
 188  2289
         super();
 189  2289
     }
 190  
 
 191  
     public void actionPerformed(ActionEvent e) {
 192  0
         Object source = e.getSource();
 193  0
         if (source instanceof UMLComboBox2) {
 194  0
             UMLComboBox2 box = (UMLComboBox2) source;
 195  0
             Object diagram = box.getTarget();
 196  0
             Object homeModel = box.getSelectedItem();
 197  0
             if (diagram instanceof Relocatable) {
 198  0
                 Relocatable d = (Relocatable) diagram;
 199  0
                 if (d.isRelocationAllowed(homeModel)) {
 200  0
                     d.relocate(homeModel);
 201  
                 }
 202  
             }
 203  
         }
 204  0
     }
 205  
 }
 206  
 
 207  
 class ActionNavigateUpFromDiagram extends AbstractActionNavigate {
 208  
 
 209  
     /**
 210  
      * The constructor.
 211  
      */
 212  
     public ActionNavigateUpFromDiagram() {
 213  2289
         super("button.go-up", true);
 214  2289
     }
 215  
 
 216  
     /*
 217  
      * @see org.argouml.uml.ui.AbstractActionNavigate#navigateTo(java.lang.Object)
 218  
      */
 219  
     protected Object navigateTo(Object source) {
 220  0
         if (source instanceof ArgoDiagram) {
 221  0
             return ((ArgoDiagram) source).getNamespace();
 222  
         }
 223  0
         return null;
 224  
     }
 225  
     
 226  
     /*
 227  
      * @see javax.swing.Action#isEnabled()
 228  
      */
 229  
     @Override
 230  
     public boolean isEnabled() {
 231  14347
         return true;
 232  
     }
 233  
 
 234  
     /*
 235  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 236  
      */
 237  
     @Override
 238  
     public void actionPerformed(ActionEvent e) {
 239  0
         Object target = TargetManager.getInstance().getTarget();
 240  0
         Object destination = navigateTo(target);
 241  0
         if (destination != null) {
 242  0
             TargetManager.getInstance().setTarget(destination);
 243  
         }
 244  0
     }
 245  
 }