Coverage Report - org.argouml.uml.ui.TabTaggedValues
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionRemoveTaggedValue
50%
4/8
N/A
1.7
TabTaggedValues
64%
72/111
16%
4/24
1.7
 
 1  
 /* $Id: TabTaggedValues.java 19068 2011-02-24 09:07:34Z thn $
 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  
  *    bobtarling
 11  
  *    Thomas Neustupny
 12  
  *****************************************************************************
 13  
  *
 14  
  * Some portions of this file was previously release using the BSD License:
 15  
  */
 16  
 
 17  
 // Copyright (c) 1996-2007 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.BorderLayout;
 43  
 import java.awt.Font;
 44  
 import java.awt.event.ActionEvent;
 45  
 import java.awt.event.ComponentEvent;
 46  
 import java.awt.event.ComponentListener;
 47  
 import java.util.Collection;
 48  
 
 49  
 import javax.swing.Action;
 50  
 import javax.swing.DefaultCellEditor;
 51  
 import javax.swing.DefaultListSelectionModel;
 52  
 import javax.swing.JButton;
 53  
 import javax.swing.JLabel;
 54  
 import javax.swing.JPanel;
 55  
 import javax.swing.JScrollPane;
 56  
 import javax.swing.JTable;
 57  
 import javax.swing.JToolBar;
 58  
 import javax.swing.event.ListSelectionEvent;
 59  
 import javax.swing.event.ListSelectionListener;
 60  
 import javax.swing.table.TableCellEditor;
 61  
 import javax.swing.table.TableColumn;
 62  
 
 63  
 import org.apache.log4j.Logger;
 64  
 import org.argouml.application.api.AbstractArgoJPanel;
 65  
 import org.argouml.application.helpers.ResourceLoaderWrapper;
 66  
 import org.argouml.i18n.Translator;
 67  
 import org.argouml.model.InvalidElementException;
 68  
 import org.argouml.model.Model;
 69  
 import org.argouml.swingext.UpArrowIcon;
 70  
 import org.argouml.ui.LookAndFeelMgr;
 71  
 import org.argouml.ui.TabModelTarget;
 72  
 import org.argouml.ui.targetmanager.TargetEvent;
 73  
 import org.argouml.uml.ui.foundation.extension_mechanisms.ActionNewTagDefinition;
 74  
 import org.argouml.uml.ui.foundation.extension_mechanisms.UMLTagDefinitionComboBoxModel;
 75  
 import org.tigris.gef.presentation.Fig;
 76  
 import org.argouml.ui.UndoableAction;
 77  
 import org.tigris.toolbar.ToolBar;
 78  
 
 79  
 /**
 80  
  * Table view of a Model Element's Tagged Values.
 81  
  */
 82  
 public class TabTaggedValues extends AbstractArgoJPanel
 83  
     implements TabModelTarget, ListSelectionListener, ComponentListener {
 84  
     
 85  900
     private static final Logger LOG = Logger.getLogger(TabTaggedValues.class);
 86  
 
 87  
     /**
 88  
      * Serial version generated for rev 1.58
 89  
      */
 90  
     private static final long serialVersionUID = -8566948113385239423L;
 91  
 
 92  
     private Object target;
 93  900
     private boolean shouldBeEnabled = false;
 94  900
     private JTable table = new JTable(10, 2);
 95  
     private JLabel titleLabel;
 96  
     private JToolBar buttonPanel;
 97  
 
 98  
     private UMLComboBox2 tagDefinitionsComboBox;
 99  
 
 100  
     private UMLComboBoxModel2 tagDefinitionsComboBoxModel;
 101  
 
 102  
 
 103  
     /**
 104  
      * Construct a TaggedValues pane for the property panel
 105  
      */
 106  
     public TabTaggedValues() {
 107  900
         super("tab.tagged-values");
 108  900
         setIcon(new UpArrowIcon());
 109  900
         buttonPanel = new ToolBar();
 110  900
         buttonPanel.setName(getTitle());
 111  900
         buttonPanel.setFloatable(false);
 112  
 
 113  900
         JButton b = null;
 114  900
         if (Model.getFacade().getUmlVersion().charAt(0) == '1') {
 115  
             // in UML2, a td is a property of a stereotype in a profile
 116  900
             b = new JButton();
 117  900
             buttonPanel.add(b);
 118  900
             b.setAction(new ActionNewTagDefinition());
 119  900
             b.setText("");
 120  900
             b.setFocusable(false);
 121  
         }
 122  
 
 123  900
         b = new JButton();
 124  900
         buttonPanel.add(b);
 125  900
         b.setToolTipText(Translator.localize("button.delete"));
 126  900
         b.setAction(new ActionRemoveTaggedValue(table));
 127  900
         b.setText("");
 128  900
         b.setFocusable(false);
 129  
   
 130  900
         table.setModel(new TabTaggedValuesModel());
 131  900
         table.setRowSelectionAllowed(false);
 132  900
         tagDefinitionsComboBoxModel = new UMLTagDefinitionComboBoxModel();
 133  900
         tagDefinitionsComboBox = new UMLComboBox2(tagDefinitionsComboBoxModel);
 134  900
         Class tagDefinitionClass = (Class) Model.getMetaTypes()
 135  
                 .getTagDefinition();
 136  900
         tagDefinitionsComboBox.setRenderer(new UMLListCellRenderer2(false));
 137  900
         table.setDefaultEditor(tagDefinitionClass,
 138  
                 new DefaultCellEditor(tagDefinitionsComboBox));
 139  900
         table.setDefaultRenderer(tagDefinitionClass,
 140  
                 new UMLTableCellRenderer());
 141  900
         table.getSelectionModel().addListSelectionListener(this);
 142  
 
 143  900
         JScrollPane sp = new JScrollPane(table);
 144  900
         Font labelFont = LookAndFeelMgr.getInstance().getStandardFont();
 145  900
         table.setFont(labelFont);
 146  
 
 147  900
         titleLabel = new JLabel("none");
 148  900
         resizeColumns();
 149  900
         setLayout(new BorderLayout());
 150  900
         titleLabel.setLabelFor(buttonPanel);
 151  
 
 152  900
         JPanel topPane = new JPanel(new BorderLayout());
 153  900
         topPane.add(titleLabel, BorderLayout.WEST);
 154  900
         topPane.add(buttonPanel, BorderLayout.CENTER);
 155  
 
 156  900
         add(topPane, BorderLayout.NORTH);
 157  900
         add(sp, BorderLayout.CENTER);
 158  
         
 159  900
         addComponentListener(this);
 160  900
     }
 161  
 
 162  
     /**
 163  
      * Resize the columns.
 164  
      */
 165  
     public void resizeColumns() {
 166  900
         TableColumn keyCol = table.getColumnModel().getColumn(0);
 167  900
         TableColumn valCol = table.getColumnModel().getColumn(1);
 168  900
         keyCol.setMinWidth(50);
 169  900
         keyCol.setWidth(150);
 170  900
         keyCol.setPreferredWidth(150);
 171  900
         valCol.setMinWidth(250);
 172  900
         valCol.setWidth(550);
 173  900
         valCol.setPreferredWidth(550);
 174  900
         table.doLayout();
 175  900
     }
 176  
 
 177  
     /*
 178  
      * @see org.argouml.ui.TabTarget#setTarget(java.lang.Object)
 179  
      */
 180  
     public void setTarget(Object theTarget) {
 181  0
         stopEditing();
 182  
 
 183  0
         Object t = (theTarget instanceof Fig)
 184  
                     ? ((Fig) theTarget).getOwner() : theTarget;
 185  0
         if (!Model.getFacade().isAUMLElement(t)) {
 186  0
             target = null;
 187  0
             shouldBeEnabled = false;
 188  0
             return;
 189  
         }
 190  0
         target = t;
 191  0
         shouldBeEnabled = true;
 192  
 
 193  
         // Only update our model if we're visible
 194  0
         if (isVisible()) {
 195  0
             setTargetInternal(target);
 196  
         } 
 197  0
     }
 198  
 
 199  
     /**
 200  
      * Make sure any pending edits are flushed.
 201  
      */
 202  
     private void stopEditing() {
 203  900
         if (table.isEditing()) {
 204  0
             TableCellEditor ce = table.getCellEditor();
 205  
             try {
 206  0
                 if (ce != null && !ce.stopCellEditing()) {
 207  0
                     ce.cancelCellEditing();
 208  
                 }
 209  0
             } catch (InvalidElementException e) {
 210  
                 // Most likely cause of this is that someone deleted our
 211  
                 // target with the event pump turned off so we didn't
 212  
                 // get notification.  Nothing we can do about it now and
 213  
                 // we are changing targets anyway, so just log it.
 214  0
                 LOG.warn("failed to cancel editing - " 
 215  
                         + "model element deleted while edit in progress");
 216  0
             }
 217  
         }
 218  900
     }
 219  
 
 220  
     private void setTargetInternal(Object t) {
 221  900
         tagDefinitionsComboBoxModel.setTarget(t);
 222  
 
 223  900
         table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
 224  
 
 225  900
         ((TabTaggedValuesModel) table.getModel()).setTarget(t);
 226  900
         table.sizeColumnsToFit(0);
 227  
 
 228  900
         if (t != null) {
 229  0
             titleLabel.setText("Target: "
 230  
                     + Model.getFacade().getUMLClassName(t)
 231  
                     + " ("
 232  
                     + Model.getFacade().getName(t) + ")");
 233  
         } else {
 234  900
             titleLabel.setText("none");
 235  
         }
 236  900
         validate();
 237  900
     }
 238  
 
 239  
     /*
 240  
      * @see org.argouml.ui.TabTarget#getTarget()
 241  
      */
 242  
     public Object getTarget() {
 243  0
         return target;
 244  
     }
 245  
 
 246  
     /*
 247  
      * @see org.argouml.ui.TabTarget#refresh()
 248  
      */
 249  
     public void refresh() {
 250  0
         setTarget(target);
 251  0
     }
 252  
 
 253  
     /*
 254  
      * @see org.argouml.ui.TabTarget#shouldBeEnabled(java.lang.Object)
 255  
      */
 256  
     public boolean shouldBeEnabled(Object theTarget) {
 257  1327
         Object t = (theTarget instanceof Fig)
 258  
             ? ((Fig) theTarget).getOwner() : theTarget;
 259  1327
         shouldBeEnabled = Model.getFacade().isAUMLElement(t);
 260  1327
         return shouldBeEnabled;
 261  
     }
 262  
 
 263  
     /*
 264  
      * @see org.argouml.ui.targetmanager.TargetListener#targetAdded(
 265  
      *         org.argouml.ui.targetmanager.TargetEvent)
 266  
      */
 267  
     public void targetAdded(TargetEvent e) {
 268  0
         setTarget(e.getNewTarget());
 269  0
     }
 270  
 
 271  
     /*
 272  
      * @see org.argouml.ui.targetmanager.TargetListener#targetRemoved(
 273  
      *         org.argouml.ui.targetmanager.TargetEvent)
 274  
      */
 275  
     public void targetRemoved(TargetEvent e) {
 276  0
         setTarget(e.getNewTarget());
 277  0
     }
 278  
 
 279  
     /*
 280  
      * @see org.argouml.ui.targetmanager.TargetListener#targetSet(
 281  
      *         org.argouml.ui.targetmanager.TargetEvent)
 282  
      */
 283  
     public void targetSet(TargetEvent e) {
 284  0
         setTarget(e.getNewTarget());
 285  0
     }
 286  
 
 287  
     /**
 288  
      * @return Returns the tableModel.
 289  
      */
 290  
     protected TabTaggedValuesModel getTableModel() {
 291  0
         return (TabTaggedValuesModel) table.getModel();
 292  
     }
 293  
     /**
 294  
      * @return Returns the table.
 295  
      */
 296  
     protected JTable getTable() {
 297  0
         return table;
 298  
     }
 299  
 
 300  
     /*
 301  
      * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
 302  
      */
 303  
     public void valueChanged(ListSelectionEvent e) {
 304  0
         if (!e.getValueIsAdjusting()) {
 305  0
             DefaultListSelectionModel sel = 
 306  
                 (DefaultListSelectionModel) e.getSource();
 307  0
             Collection tvs =
 308  
                     Model.getFacade().getTaggedValuesCollection(target);
 309  0
             int index = sel.getLeadSelectionIndex();
 310  0
             if (index >= 0 && index < tvs.size()) {
 311  0
                 Object tagDef = Model.getFacade().getTagDefinition(
 312  
                         TabTaggedValuesModel.getFromCollection(tvs, index));
 313  0
                 tagDefinitionsComboBoxModel.setSelectedItem(tagDef);
 314  
             }
 315  
         }
 316  0
     }
 317  
 
 318  
     /*
 319  
      * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
 320  
      */
 321  
     public void componentShown(ComponentEvent e) {
 322  
         // Update our model with our saved target
 323  0
         setTargetInternal(target);
 324  0
     }
 325  
     
 326  
     /*
 327  
      * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
 328  
      */
 329  
     public void componentHidden(ComponentEvent e) {
 330  
         // Stop updating model when we're not visible
 331  900
         stopEditing();
 332  900
         setTargetInternal(null);
 333  900
     }
 334  
 
 335  
     public void componentMoved(ComponentEvent e) {
 336  
         // ignored
 337  900
     }
 338  
 
 339  
     public void componentResized(ComponentEvent e) {
 340  
         // ignored
 341  900
     }
 342  
 
 343  
 
 344  
 } /* end class TabTaggedValues */
 345  
 
 346  
 class ActionRemoveTaggedValue extends UndoableAction {
 347  
 
 348  
     /**
 349  
      * Serial version generated for rev 1.58
 350  
      */
 351  
     private static final long serialVersionUID = 8276763533039642549L;
 352  
     
 353  
     /**
 354  
      * The table we are bound to.
 355  
      */
 356  
     private JTable table;
 357  
 
 358  
     /**
 359  
      * Construct an Action to remove a TaggedValue from the table.
 360  
      * 
 361  
      * @param tableTv A JTable backed by a TabTaggedValuesModel
 362  
      */
 363  
     public ActionRemoveTaggedValue(JTable tableTv) {
 364  900
         super(Translator.localize("button.delete"),
 365  
                 ResourceLoaderWrapper.lookupIcon("Delete"));
 366  
         // Set the tooltip string:
 367  900
         putValue(Action.SHORT_DESCRIPTION, 
 368  
                 Translator.localize("button.delete"));
 369  900
         table = tableTv;
 370  900
     }
 371  
 
 372  
     /*
 373  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 374  
      */
 375  
     @Override
 376  
     public void actionPerformed(ActionEvent e) {
 377  0
         super.actionPerformed(e);
 378  0
         TabTaggedValuesModel model = (TabTaggedValuesModel) table.getModel();
 379  0
         model.removeRow(table.getSelectedRow());
 380  0
     }
 381  
 }