Coverage Report - org.argouml.uml.ui.UMLExpressionModel2
 
Classes in this File Line Coverage Branch Coverage Complexity
UMLExpressionModel2
0%
0/77
0%
0/52
2.647
UMLExpressionModel2$1
0%
0/3
N/A
2.647
 
 1  
 /* $Id: UMLExpressionModel2.java 17881 2010-01-12 21:09:28Z 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  
  *    bobtarling
 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.beans.PropertyChangeEvent;
 42  
 import java.beans.PropertyChangeListener;
 43  
 
 44  
 import javax.swing.SwingUtilities;
 45  
 
 46  
 import org.argouml.model.Model;
 47  
 import org.argouml.ui.TabTarget;
 48  
 import org.argouml.ui.targetmanager.TargetEvent;
 49  
 import org.argouml.ui.targetmanager.TargetListener;
 50  
 import org.tigris.gef.presentation.Fig;
 51  
 
 52  
 /**
 53  
  * The model for an expression. 
 54  
  * An expression consists of a body and a language.
 55  
  * 
 56  
  * @author mkl
 57  
  */
 58  0
 public abstract class UMLExpressionModel2  
 59  
     implements TargetListener, PropertyChangeListener {
 60  
     private UMLUserInterfaceContainer container;
 61  
     private String propertyName;
 62  
     private Object expression;
 63  
     private boolean mustRefresh;
 64  
     private static final String EMPTYSTRING = "";
 65  
     
 66  0
     private Object target = null;
 67  
 
 68  
     /**
 69  
      * The constructor.
 70  
      *
 71  
      * @param c the container of UML user interface components
 72  
      * @param name the name of the property
 73  
      */
 74  0
     public UMLExpressionModel2(UMLUserInterfaceContainer c, String name) {
 75  0
         container = c;
 76  0
         propertyName = name;
 77  0
         mustRefresh = true;
 78  0
     }
 79  
 
 80  
     /**
 81  
      * When the target is changed, we must refresh.
 82  
      */
 83  
     public void targetChanged() {
 84  0
         mustRefresh = true;
 85  0
         expression = null;
 86  0
     }
 87  
 
 88  
 
 89  
     /**
 90  
      * @return the expression
 91  
      */
 92  
     public abstract Object getExpression();
 93  
 
 94  
     /**
 95  
      * @param expr the expression
 96  
      */
 97  
     public abstract void setExpression(Object expr);
 98  
 
 99  
     /**
 100  
      * @return a new expression
 101  
      */
 102  
     public abstract Object newExpression();
 103  
 
 104  
 
 105  
     /**
 106  
      * @return the language of the expression
 107  
      */
 108  
     public String getLanguage() {
 109  0
         if (mustRefresh) {
 110  0
             expression = getExpression();
 111  
         }
 112  0
         if (expression == null) {
 113  0
             return EMPTYSTRING;
 114  
         }
 115  0
         return Model.getDataTypesHelper().getLanguage(expression);
 116  
     }
 117  
 
 118  
     /**
 119  
      * @return The body text of the expression.
 120  
      */
 121  
     public String getBody() {
 122  0
         if (mustRefresh) {
 123  0
             expression = getExpression();
 124  
         }
 125  0
         if (expression == null) {
 126  0
             return EMPTYSTRING;
 127  
         }
 128  0
         return Model.getDataTypesHelper().getBody(expression);
 129  
     }
 130  
 
 131  
     /**
 132  
      * @param lang the language of the expression
 133  
      */
 134  
     public void setLanguage(String lang) {
 135  
 
 136  0
         boolean mustChange = true;
 137  0
         if (expression != null) {
 138  0
             String oldValue =
 139  
                 Model.getDataTypesHelper().getLanguage(expression);
 140  0
             if (oldValue != null && oldValue.equals(lang)) {
 141  0
                 mustChange = false;
 142  
             }
 143  
         }
 144  0
         if (mustChange) {
 145  0
             String body = EMPTYSTRING;
 146  0
             if (expression != null
 147  
                     && Model.getDataTypesHelper().getBody(expression) != null) {
 148  0
                 body = Model.getDataTypesHelper().getBody(expression);
 149  
             }
 150  
 
 151  0
             setExpression(lang, body);
 152  
         }
 153  0
     }
 154  
 
 155  
     /**
 156  
      * @param body the body text of the expression
 157  
      */
 158  
     public void setBody(String body) {
 159  0
         boolean mustChange = true;
 160  0
         if (expression != null) {
 161  0
             Object oldValue = Model.getDataTypesHelper().getBody(expression);
 162  0
             if (oldValue != null && oldValue.equals(body)) {
 163  0
                 mustChange = false;
 164  
             }
 165  
         }
 166  0
         if (mustChange) {
 167  0
             String lang = null;
 168  0
             if (expression != null) {
 169  0
                 lang = Model.getDataTypesHelper().getLanguage(expression);
 170  
             }
 171  0
             if (lang == null) {
 172  0
                 lang = EMPTYSTRING;
 173  
             }
 174  
 
 175  0
             setExpression(lang, body);
 176  
         }
 177  0
     }
 178  
 
 179  
     /**
 180  
      * @param lang the language of the expression
 181  
      * @param body the body text of the expression
 182  
      */
 183  
     private void setExpression(String lang, String body) {
 184  
         // Expressions are DataTypes, not independent model elements
 185  
         // be careful not to reuse them
 186  0
         Object oldExpression = null;
 187  0
         if (mustRefresh || expression == null) {
 188  0
             oldExpression = expression;
 189  0
             expression = newExpression();
 190  
         }
 191  0
         expression = Model.getDataTypesHelper().setLanguage(expression, lang);
 192  0
         expression = Model.getDataTypesHelper().setBody(expression, body);
 193  0
         setExpression(expression);
 194  0
         if (oldExpression != null) {
 195  0
             Model.getUmlFactory().delete(oldExpression);
 196  
         }
 197  0
     }
 198  
 
 199  
     /**
 200  
      * @return the container
 201  
      */
 202  
     protected UMLUserInterfaceContainer getContainer() {
 203  0
         return container;
 204  
     }
 205  
 
 206  
     /**
 207  
      * TODO: The next text was copied - to adapt.
 208  
      * 
 209  
      * Sets the target. If the old target is an UML Element, it also removes
 210  
      * the model from the element listener list of the target. If the new target
 211  
      * is an UML Element, the model is added as element listener to the
 212  
      * new target. <p>
 213  
      *
 214  
      * This function is called when the user changes the target. 
 215  
      * Hence, this shall not result in any UML model changes.<p>
 216  
      * 
 217  
      * This function looks a lot like the one in UMLComboBoxModel2.
 218  
      * <p>
 219  
      * As a possible future extension, we could allow listening to 
 220  
      * other model elements.
 221  
      * 
 222  
      * @param theNewTarget the new target
 223  
      */
 224  
     public void setTarget(Object theNewTarget) {
 225  0
         theNewTarget = theNewTarget instanceof Fig
 226  
             ? ((Fig) theNewTarget).getOwner() : theNewTarget;
 227  0
         if (Model.getFacade().isAUMLElement(target)) {
 228  0
             Model.getPump().removeModelEventListener(this, target,
 229  
                     propertyName);
 230  
             // Allow listening to other elements:
 231  
             //                removeOtherModelEventListeners(listTarget);
 232  
         }
 233  
 
 234  0
         if (Model.getFacade().isAUMLElement(theNewTarget)) {
 235  0
             target = theNewTarget;
 236  0
             Model.getPump().addModelEventListener(this, target,
 237  
                     propertyName);
 238  
             // Allow listening to other elements:
 239  
             //                addOtherModelEventListeners(listTarget);
 240  
 
 241  0
             if (container instanceof TabTarget) {
 242  0
                 ((TabTarget) container).refresh();
 243  
             }
 244  
         } else {
 245  0
             target = null;
 246  
         }
 247  0
     }
 248  
     
 249  
     public void propertyChange(PropertyChangeEvent e) {
 250  0
         if (target != null && target == e.getSource()) {
 251  0
             mustRefresh = true;
 252  0
             expression = null;
 253  
             /* This works - we do get an event - and now 
 254  
              * refresh the UI: */
 255  0
             if (container instanceof TabTarget) {
 256  0
                 SwingUtilities.invokeLater(new Runnable() {
 257  
                     public void run() {
 258  0
                         ((TabTarget) container).refresh();
 259  
                         /* TODO: The above statement also refreshes when 
 260  
                          * we are not shown (to be verified) - hence 
 261  
                          * not entirely correct. */
 262  0
                     }
 263  
                 });
 264  
             }
 265  
         }
 266  0
     }
 267  
 
 268  
     public void targetAdded(TargetEvent e) {
 269  0
         setTarget(e.getNewTarget());
 270  0
     }
 271  
 
 272  
     public void targetRemoved(TargetEvent e) {
 273  0
         setTarget(e.getNewTarget());
 274  0
     }
 275  
 
 276  
     public void targetSet(TargetEvent e) {
 277  0
         setTarget(e.getNewTarget());
 278  0
     }
 279  
 
 280  
 }