Coverage Report - org.argouml.uml.diagram.ui.DiagramNameDocument
 
Classes in this File Line Coverage Branch Coverage Complexity
DiagramNameDocument
51%
24/47
30%
3/10
1.889
 
 1  
 /* $Id: DiagramNameDocument.java 17865 2010-01-12 20:45:26Z linus $
 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) 2004-2007 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 java.beans.PropertyVetoException;
 42  
 
 43  
 import javax.swing.JTextField;
 44  
 import javax.swing.event.DocumentEvent;
 45  
 import javax.swing.event.DocumentListener;
 46  
 import javax.swing.text.BadLocationException;
 47  
 import javax.swing.text.DefaultHighlighter;
 48  
 
 49  
 import org.apache.log4j.Logger;
 50  
 import org.argouml.ui.targetmanager.TargetEvent;
 51  
 import org.argouml.ui.targetmanager.TargetListener;
 52  
 import org.argouml.ui.targetmanager.TargetManager;
 53  
 import org.argouml.uml.diagram.ArgoDiagram;
 54  
 
 55  
 /**
 56  
  * This is the model for the diagram name text box (JTextField)
 57  
  * shown on the property panel of a Diagram. <p>
 58  
  *
 59  
  * It handles changes by the user in the text-entry field,
 60  
  * by updating the name of the diagram.
 61  
  * And it handles target changes (i.e. when the user selects another diagram)
 62  
  * by updating the name shown in the namefield.
 63  
  *
 64  
  * @author Michiel
 65  
  */
 66  
 class DiagramNameDocument implements DocumentListener, TargetListener {
 67  
 
 68  900
     private static final Logger LOG = 
 69  
         Logger.getLogger(DiagramNameDocument.class);
 70  
     
 71  
     private JTextField field;
 72  2289
     private boolean stopEvents = false;
 73  
 
 74  2289
     private Object highlightTag = null;
 75  
 
 76  
     /**
 77  
      * The constructor.
 78  
      * @param theField the input text field
 79  
      */
 80  2289
     public DiagramNameDocument(JTextField theField) {
 81  2289
         field = theField;
 82  2289
         TargetManager tm = TargetManager.getInstance();
 83  2289
         tm.addTargetListener(this);
 84  2289
         setTarget(tm.getTarget());
 85  2289
     }
 86  
 
 87  
     /**
 88  
      * If the currently selected object is a diagram,
 89  
      * then update the name-field. <p>
 90  
      *
 91  
      * MVW: I added the stopEvents mechanism, because otherwise
 92  
      * updating the field causes the UML model to be adapted!
 93  
      *
 94  
      * @param t the currently selected object
 95  
      */
 96  
     private void setTarget(Object t) {
 97  3208
         if (t instanceof ArgoDiagram) {
 98  2949
             stopEvents = true;
 99  2949
             field.setText(((ArgoDiagram) t).getName());
 100  2949
             stopEvents = false;
 101  
         }
 102  3208
     }
 103  
 
 104  
     /*
 105  
      * @see org.argouml.ui.targetmanager.TargetListener#targetAdded(org.argouml.ui.targetmanager.TargetEvent)
 106  
      */
 107  
     public void targetAdded(TargetEvent e) {
 108  0
         setTarget(e.getNewTarget());
 109  0
     }
 110  
 
 111  
     /*
 112  
      * @see org.argouml.ui.targetmanager.TargetListener#targetRemoved(org.argouml.ui.targetmanager.TargetEvent)
 113  
      */
 114  
     public void targetRemoved(TargetEvent e) {
 115  176
         setTarget(e.getNewTarget());
 116  176
     }
 117  
 
 118  
     /*
 119  
      * @see org.argouml.ui.targetmanager.TargetListener#targetSet(org.argouml.ui.targetmanager.TargetEvent)
 120  
      */
 121  
     public void targetSet(TargetEvent e) {
 122  743
         setTarget(e.getNewTarget());
 123  743
     }
 124  
 
 125  
     /*
 126  
      * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
 127  
      */
 128  
     public void insertUpdate(DocumentEvent e) {
 129  660
         update(e);
 130  660
     }
 131  
 
 132  
     /*
 133  
      * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
 134  
      */
 135  
     public void removeUpdate(DocumentEvent e) {
 136  660
         update(e);
 137  660
     }
 138  
 
 139  
     /*
 140  
      * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
 141  
      */
 142  
     public void changedUpdate(DocumentEvent e) {
 143  0
         update(e);
 144  0
     }
 145  
 
 146  
     /**
 147  
      * If a new name has been typed by the user, then
 148  
      * let's update the name of the diagram.
 149  
      *
 150  
      * @param e the documentevent from the Documentlistener interface
 151  
      */
 152  
     private void update(DocumentEvent e) {
 153  1320
         if (!stopEvents) {
 154  0
             Object target = TargetManager.getInstance().getTarget();
 155  0
             if (target instanceof ArgoDiagram) {
 156  0
                 ArgoDiagram d = (ArgoDiagram) target;
 157  
                 try {
 158  0
                     int documentLength = e.getDocument().getLength();
 159  0
                     String newName = e.getDocument().getText(0, documentLength);
 160  0
                     String oldName = d.getName();
 161  
                     /* Prevent triggering too many events by setName(). */
 162  0
                     if (!oldName.equals(newName)) {
 163  0
                         d.setName(newName);
 164  0
                         if (highlightTag != null) {
 165  0
                             field.getHighlighter()
 166  
                                     .removeHighlight(highlightTag);
 167  0
                             highlightTag = null;
 168  
                         }
 169  
                     }
 170  0
                 } catch (PropertyVetoException pe) {
 171  
                     // Provide feedback to the user that their name was
 172  
                     // not accepted
 173  
                     try {
 174  0
                         highlightTag  = field.getHighlighter().addHighlight(0, 
 175  
                                 field.getText().length(), 
 176  
                                 DefaultHighlighter.DefaultPainter);
 177  0
                     } catch (BadLocationException e1) {
 178  0
                         LOG.debug("Nested exception", e1);
 179  0
                     }
 180  0
                 } catch (BadLocationException ble) {
 181  0
                     LOG.debug(ble);
 182  0
                 }
 183  
             }
 184  
         }
 185  1320
     }
 186  
 
 187  
 }