Coverage Report - org.argouml.uml.ui.UMLMultiplicityPanel
 
Classes in this File Line Coverage Branch Coverage Complexity
UMLMultiplicityPanel
0%
0/29
0%
0/12
2.562
UMLMultiplicityPanel$MultiplicityCheckBox
0%
0/12
0%
0/2
2.562
UMLMultiplicityPanel$MultiplicityComboBox
0%
0/24
0%
0/8
2.562
UMLMultiplicityPanel$MultiplicityComboBoxModel
0%
0/28
0%
0/18
2.562
 
 1  
 /* $Id: UMLMultiplicityPanel.java 18760 2010-09-18 05:19:53Z 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) 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.ui;
 40  
 
 41  
 import java.awt.BorderLayout;
 42  
 import java.awt.Dimension;
 43  
 import java.awt.event.ItemEvent;
 44  
 import java.awt.event.ItemListener;
 45  
 import java.util.ArrayList;
 46  
 import java.util.List;
 47  
 
 48  
 import javax.swing.Action;
 49  
 import javax.swing.JCheckBox;
 50  
 import javax.swing.JComboBox;
 51  
 import javax.swing.JPanel;
 52  
 
 53  
 import org.argouml.model.Model;
 54  
 import org.argouml.ui.targetmanager.TargetEvent;
 55  
 import org.argouml.uml.ui.behavior.collaborations.ActionSetClassifierRoleMultiplicity;
 56  
 
 57  
 /**
 58  
  * A compound control containing all the visual controls for specifying
 59  
  * multiplicity.
 60  
  * @author Bob Tarling
 61  
  * @since 0.23 alpha2
 62  
  * @deprecated in 0.31.5 by Bob Tarling. Property panel controls are now
 63  
  * internal to the property panel component
 64  
  */
 65  0
 public class UMLMultiplicityPanel extends JPanel implements ItemListener {
 66  
 
 67  
     private JComboBox multiplicityComboBox;
 68  
     private JCheckBox checkBox;
 69  
     private MultiplicityComboBoxModel multiplicityComboBoxModel;
 70  
     
 71  0
     private static List<String> multiplicityList = new ArrayList<String>();
 72  
     
 73  
     static {
 74  0
         multiplicityList.add("1");
 75  0
         multiplicityList.add("0..1");
 76  0
         multiplicityList.add("0..*");
 77  0
         multiplicityList.add("1..*");
 78  0
     }
 79  
 
 80  
     /**
 81  
      * Constructor
 82  
      */
 83  
     public UMLMultiplicityPanel() {
 84  0
         super(new BorderLayout());
 85  
         
 86  0
         multiplicityComboBoxModel =
 87  
             new MultiplicityComboBoxModel("multiplicity");
 88  
         
 89  0
         checkBox = new MultiplicityCheckBox();
 90  0
         multiplicityComboBox =
 91  
                 new MultiplicityComboBox(
 92  
                         multiplicityComboBoxModel,
 93  
                         ActionSetClassifierRoleMultiplicity.getInstance());
 94  0
         multiplicityComboBox.setEditable(true);
 95  0
         multiplicityComboBox.addItemListener(this);
 96  0
         add(checkBox, BorderLayout.WEST);
 97  0
         add(multiplicityComboBox, BorderLayout.CENTER);
 98  0
     }
 99  
     
 100  
     /**
 101  
      * Enforce that the preferred height is the minimum height.
 102  
      * This works around a bug in Windows LAF of JRE5 where a change
 103  
      * in the preferred/min size of a combo has changed and has a knock
 104  
      * on effect here.
 105  
      * If the layout manager for prop panels finds the preferred
 106  
      * height is greater than the minimum height then it will allow
 107  
      * this component to resize in error.
 108  
      * See issue 4333 - Sun has now fixed this bug in JRE6 and so this
 109  
      * method can be removed once JRE5 is no longer supported.
 110  
      * @return the preferred dimension
 111  
      */
 112  
     @Override
 113  
     public Dimension getPreferredSize() {
 114  0
         return new Dimension(
 115  
                 super.getPreferredSize().width,
 116  
                 getMinimumSize().height);
 117  
     }
 118  
 
 119  
     public void itemStateChanged(ItemEvent event) {
 120  0
         if (event.getSource() == multiplicityComboBox && getTarget() != null) {
 121  0
             Object item = multiplicityComboBox.getSelectedItem();
 122  0
             Object target = multiplicityComboBoxModel.getTarget();
 123  0
             if (Model.getFacade().isAMultiplicity(item)) {
 124  0
                 Model.getCoreHelper().setMultiplicity(target, item);
 125  0
             } else if (item instanceof String) {
 126  0
                 Model.getCoreHelper().setMultiplicity(target, (String) item);
 127  
             }
 128  
         }
 129  0
     }
 130  
 
 131  
     private void delete(Object multiplicity) {
 132  0
         if ("1.4".equals(Model.getFacade().getUmlVersion()) 
 133  
                 && multiplicity != null) {
 134  
             // For UML 1.4 Multiplicities are value objects, but
 135  
             // for UML 2.x, the bounds are contained by the Property
 136  
             // so they shouldn't be cleaned up
 137  0
             Model.getUmlFactory().delete(multiplicity);
 138  
         }
 139  0
     }
 140  
     
 141  
     private Object getTarget() {
 142  0
         return multiplicityComboBoxModel.getTarget();
 143  
     }
 144  
 
 145  
     /**
 146  
      * An editable and searchable combobox to edit the multiplicity attribute of
 147  
      * some modelelement.
 148  
      *
 149  
      * @author jaap.branderhorst@xs4all.nl
 150  
      * @since Jan 5, 2003
 151  
      */
 152  
     private class MultiplicityComboBox extends UMLSearchableComboBox {
 153  
 
 154  
         /**
 155  
          * Constructor for UMLMultiplicityComboBox2.
 156  
          * @param arg0 the combobox model
 157  
          * @param selectAction the action
 158  
          */
 159  
         public MultiplicityComboBox(UMLComboBoxModel2 arg0,
 160  0
                 Action selectAction) {
 161  0
             super(arg0, selectAction);
 162  0
         }
 163  
 
 164  
         /**
 165  
          * On enter, the text the user has filled in the textfield is first
 166  
          * checked to see if it's a valid multiplicity. If so then that is the
 167  
          * multiplicity to be set. If not, the combobox searches for a
 168  
          * multiplicity starting with the given text. If there is no
 169  
          * multiplicity starting with the given text, the old value is reset
 170  
          * in the comboboxeditor.
 171  
          * 
 172  
          * {@inheritDoc}
 173  
          */
 174  
         @Override
 175  
         protected void doOnEdit(Object item) {
 176  0
             String text = (String) item;
 177  
             try {
 178  
                 // TODO: Add a parse for syntax method?
 179  0
                 Object multi = 
 180  
                     Model.getDataTypesFactory().createMultiplicity(text);
 181  0
                 if (multi != null) {
 182  0
                     setSelectedItem(text);
 183  0
                     delete(multi);
 184  0
                     return;
 185  
                 }
 186  0
             } catch (IllegalArgumentException e) {
 187  0
                 Object o = search(text);
 188  0
                 if (o != null ) {
 189  0
                     setSelectedItem(o);
 190  0
                     return;
 191  
                 }
 192  0
             }
 193  0
             getEditor().setItem(getSelectedItem());
 194  0
         }
 195  
 
 196  
         /**
 197  
          * When we change target make sure that the check box is only selected
 198  
          * if the multiplicity exists
 199  
          * @param e
 200  
          * @see org.argouml.uml.ui.UMLComboBox2#targetSet(org.argouml.ui.targetmanager.TargetEvent)
 201  
          */
 202  
         @Override
 203  
         public void targetSet(TargetEvent e) {
 204  0
             super.targetSet(e);
 205  0
             Object target = getTarget();
 206  0
             boolean exists = target != null 
 207  
                 && Model.getFacade().getMultiplicity(target) != null;
 208  0
             multiplicityComboBox.setEnabled(exists);
 209  0
             multiplicityComboBox.setEditable(exists);
 210  
             // This will cause itemStateChanged to be called because of
 211  
             // us rather than a user action, but we don't care because we're
 212  
             // going to check if the value is the same
 213  0
             checkBox.setSelected(exists);
 214  0
         }
 215  
     }
 216  
     
 217  
     
 218  
     /**
 219  
      * A model for multiplicities.
 220  
      */
 221  
     private class MultiplicityComboBoxModel
 222  
         extends UMLComboBoxModel2 {
 223  
 
 224  
         /**
 225  
          * Constructor for UMLMultiplicityComboBoxModel.
 226  
          *
 227  
          * @param propertySetName the name of the property set
 228  
          */
 229  0
         public MultiplicityComboBoxModel(String propertySetName) {
 230  0
             super(propertySetName, false);
 231  0
         }
 232  
     
 233  
         /*
 234  
          * @see org.argouml.uml.ui.UMLComboBoxModel2#isValidElement(Object)
 235  
          */
 236  
         protected boolean isValidElement(Object element) {
 237  0
             return element instanceof String;
 238  
         }
 239  
     
 240  
         /*
 241  
          * @see org.argouml.uml.ui.UMLComboBoxModel2#buildModelList()
 242  
          */
 243  
         protected void buildModelList() {
 244  0
             setElements(multiplicityList);
 245  0
             Object t = getTarget();
 246  0
             if (Model.getFacade().isAModelElement(t)) {
 247  0
                 addElement(Model.getFacade().getMultiplicity(t));
 248  
             }
 249  0
         }
 250  
     
 251  
         /*
 252  
          * @see org.argouml.uml.ui.UMLComboBoxModel2#addElement(java.lang.Object)
 253  
          */
 254  
         @Override
 255  
         public void addElement(Object o) {
 256  0
             if (o == null) {
 257  0
                 return;
 258  
             }
 259  
             String text;
 260  0
             if (Model.getFacade().isAMultiplicity(o)) {
 261  0
                 text = Model.getFacade().toString(o);
 262  0
                 if ("".equals(text)) {
 263  0
                     text = "1";
 264  
                 }
 265  0
             } else if (o instanceof String) {
 266  0
                 text = (String) o;
 267  
             } else {
 268  0
                 return;
 269  
             }
 270  0
             if (!multiplicityList.contains(text) && isValidElement(text)) {
 271  0
                 multiplicityList.add(text);
 272  
             }
 273  0
             super.addElement(text);
 274  0
         }
 275  
     
 276  
         /*
 277  
          * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
 278  
          */
 279  
         @Override
 280  
         public void setSelectedItem(Object anItem) {
 281  0
             addElement(anItem);
 282  0
             super.setSelectedItem((anItem == null) ? null 
 283  
                     : Model.getFacade().toString(anItem));
 284  0
         }
 285  
 
 286  
         protected Object getSelectedModelElement() {
 287  0
             if (getTarget() != null) {
 288  0
                 return Model.getFacade().toString(
 289  
                         Model.getFacade().getMultiplicity(getTarget()));
 290  
             }
 291  0
             return null;
 292  
         }
 293  
     }
 294  
     
 295  
     private class MultiplicityCheckBox extends JCheckBox
 296  
         implements ItemListener {
 297  
         
 298  0
         public MultiplicityCheckBox() {
 299  0
             addItemListener(this);
 300  0
         }
 301  
 
 302  
         public void itemStateChanged(ItemEvent e) {
 303  0
             Object target = getTarget();
 304  
             // Note: MultiplicityComboBox.targetSet() can cause this event
 305  
             // as well as user actions, so be sure to consider this in 
 306  
             // changing the following logic 
 307  0
             if (e.getStateChange() == ItemEvent.SELECTED) {
 308  0
                 Model.getCoreHelper().setMultiplicity(target,
 309  
                         (String) multiplicityComboBox.getSelectedItem());
 310  
 
 311  0
                 multiplicityComboBox.setEnabled(true);
 312  0
                 multiplicityComboBox.setEditable(true);
 313  
             } else {
 314  0
                 multiplicityComboBox.setEnabled(false);
 315  0
                 multiplicityComboBox.setEditable(false);
 316  0
                 Model.getCoreHelper().setMultiplicity(target, null);
 317  
             }
 318  0
         }
 319  
     }
 320  
 }