Coverage Report - org.argouml.notation.providers.uml.CallStateNotationUml
 
Classes in this File Line Coverage Branch Coverage Complexity
CallStateNotationUml
0%
0/91
0%
0/68
7.833
 
 1  
 /* $Id: CallStateNotationUml.java 17828 2010-01-12 18:55:12Z 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  
  *    mvw
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2006-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.notation.providers.uml;
 40  
 import java.text.ParseException;
 41  
 import java.util.Collection;
 42  
 import java.util.Iterator;
 43  
 import java.util.StringTokenizer;
 44  
 
 45  
 import org.argouml.application.events.ArgoEventPump;
 46  
 import org.argouml.application.events.ArgoEventTypes;
 47  
 import org.argouml.application.events.ArgoHelpEvent;
 48  
 import org.argouml.i18n.Translator;
 49  
 import org.argouml.kernel.ProjectManager;
 50  
 import org.argouml.model.Model;
 51  
 import org.argouml.notation.NotationSettings;
 52  
 import org.argouml.notation.providers.CallStateNotation;
 53  
 
 54  
 
 55  
 
 56  
 /**
 57  
  * The UML notation for a CallState. <p>
 58  
  * 
 59  
  * A call state is shown with the name of the operation being called 
 60  
  * in the symbol, along with the name of the classifier 
 61  
  * that hosts the operation in parentheses under it. <p>
 62  
  * 
 63  
  * Despite being shown on 2 lines, this is considered 1 text. 
 64  
  * The user may enter the text in 1 or 2 lines, but ArgoUML 
 65  
  * shows it as 2 lines.  
 66  
  *
 67  
  * @author Michiel
 68  
  */
 69  
 public class CallStateNotationUml extends CallStateNotation {
 70  
 
 71  
     /**
 72  
      * The constructor.
 73  
      *
 74  
      * @param callState the UML CallState
 75  
      */
 76  
     public CallStateNotationUml(Object callState) {
 77  0
         super(callState);
 78  0
     }
 79  
 
 80  
     /*
 81  
      * @see org.argouml.notation.providers.NotationProvider#parse(java.lang.Object, java.lang.String)
 82  
      */
 83  
     public void parse(Object modelElement, String text) {
 84  
         try {
 85  0
             parseCallState(modelElement, text);
 86  0
         } catch (ParseException pe) {
 87  0
             String msg = "statusmsg.bar.error.parsing.callstate";
 88  0
             Object[] args = {pe.getLocalizedMessage(),
 89  
                              Integer.valueOf(pe.getErrorOffset()), };
 90  0
             ArgoEventPump.fireEvent(new ArgoHelpEvent(
 91  
                     ArgoEventTypes.HELP_CHANGED, this,
 92  
                     Translator.messageFormat(msg, args)));
 93  0
         }
 94  0
     }
 95  
 
 96  
     protected Object parseCallState(Object callState, String s1)
 97  
         throws ParseException {
 98  
 
 99  0
         String s = s1.trim();
 100  
 
 101  0
         int a = s.indexOf("(");
 102  0
         int b = s.indexOf(")");
 103  0
         if (((a < 0) && (b >= 0)) || ((b < 0) && (a >= 0)) || (b < a)) {
 104  0
             throw new ParseException("No matching brackets () found.", 0);
 105  
         }
 106  
 
 107  
         /* First we decode the string: */
 108  0
         String newClassName = null;
 109  0
         String newOperationName = null;
 110  0
         StringTokenizer tokenizer = new StringTokenizer(s, "(");
 111  0
         while (tokenizer.hasMoreTokens()) {
 112  0
             String nextToken = tokenizer.nextToken().trim();
 113  0
             if (nextToken.endsWith(")")) {
 114  0
                 newClassName = nextToken.substring(0, nextToken.length() - 1);
 115  
             } else {
 116  0
                 newOperationName = nextToken.trim();
 117  
             }
 118  0
         }
 119  
 
 120  
         /* Secondly we check the previous model structure: */
 121  0
         String oldOperationName = null;
 122  0
         String oldClassName = null;
 123  0
         Object entry = Model.getFacade().getEntry(callState);
 124  0
         Object operation = null;
 125  0
         Object clazz = null;
 126  0
         if (Model.getFacade().isACallAction(entry)) {
 127  0
             operation = Model.getFacade().getOperation(entry);
 128  0
             if (Model.getFacade().isAOperation(operation)) {
 129  0
                 oldOperationName = Model.getFacade().getName(operation);
 130  0
                 clazz = Model.getFacade().getOwner(operation);
 131  0
                 oldClassName = Model.getFacade().getName(clazz);
 132  
             }
 133  
         }
 134  
         
 135  
         /* And 3rd, we adapt the model: */
 136  0
         boolean found = false;
 137  0
         if ((newClassName != null) 
 138  
                 && newClassName.equals(oldClassName)
 139  
                 && (newOperationName != null) 
 140  
                 && !newOperationName.equals(oldOperationName)) {
 141  
             // Same class, other operation
 142  0
             for ( Object op : Model.getFacade().getOperations(clazz)) {
 143  0
                 if (newOperationName.equals(
 144  
                         Model.getFacade().getName(op))) {
 145  0
                     Model.getCommonBehaviorHelper().setOperation(entry, op);
 146  0
                     found = true;
 147  0
                     break;
 148  
                 }
 149  
             }
 150  0
             if (!found) {
 151  0
                 throw new ParseException(
 152  
                         "Operation " + newOperationName 
 153  
                         + " not found in " + newClassName + ".", 0);
 154  
             }
 155  
 
 156  0
         } else if ((newClassName != null) 
 157  
                 && !newClassName.equals(oldClassName)
 158  
                 && (newOperationName != null)) {
 159  
             // Other class
 160  0
             Object model = 
 161  
                 ProjectManager.getManager().getCurrentProject().getRoot();
 162  0
             Collection c =
 163  
                 Model.getModelManagementHelper().
 164  
                     getAllModelElementsOfKind(model,
 165  
                                 Model.getMetaTypes().getClassifier());
 166  0
             Iterator i = c.iterator();
 167  0
             Object classifier = null;
 168  0
             while (i.hasNext()) {
 169  0
                 Object cl = i.next();
 170  0
                 String cn = Model.getFacade().getName(cl);
 171  0
                 if (cn.equals(newClassName)) {
 172  0
                     classifier = cl;
 173  0
                     break;
 174  
                 }
 175  0
             }
 176  0
             if (classifier == null) {
 177  0
                 throw new ParseException(
 178  
                         "Classifier " + newClassName + " not found.", 0);
 179  
             }
 180  
             // We found the classifier, now go find the operation:
 181  0
             if (classifier != null) {
 182  0
                 Collection ops = Model.getFacade().getOperations(classifier);
 183  0
                 Iterator io = ops.iterator();
 184  0
                 while (io.hasNext()) {
 185  0
                     Object op = io.next();
 186  0
                     if (newOperationName.equals(
 187  
                             Model.getFacade().getName(op))) {
 188  
                         /* Here we located the new classifier 
 189  
                          * with its operation. */
 190  0
                         found = true;
 191  0
                         if (!Model.getFacade().isACallAction(entry)) {
 192  0
                             entry = Model.getCommonBehaviorFactory()
 193  
                                 .buildCallAction(op, "ca");
 194  0
                             Model.getStateMachinesHelper().setEntry(
 195  
                                     callState, entry);
 196  
                         } else {
 197  0
                             Model.getCommonBehaviorHelper().setOperation(
 198  
                                     entry, op);
 199  
                         }
 200  0
                         break;
 201  
                     }
 202  0
                 }
 203  0
                 if (!found) {
 204  0
                     throw new ParseException(
 205  
                             "Operation " + newOperationName 
 206  
                             + " not found in " + newClassName + ".", 0);
 207  
                 }
 208  
             }
 209  
         }
 210  0
         if (!found) {
 211  0
             throw new ParseException(
 212  
                     "Incompatible input found.", 0);
 213  
         }
 214  
 
 215  0
         return callState;
 216  
     }
 217  
 
 218  
     /*
 219  
      * @see org.argouml.notation.providers.NotationProvider#getParsingHelp()
 220  
      */
 221  
     public String getParsingHelp() {
 222  0
         return "parsing.help.fig-callstate";
 223  
     }
 224  
 
 225  
     private String toString(Object modelElement) {
 226  0
         String ret = "";
 227  0
         Object action = Model.getFacade().getEntry(modelElement);
 228  0
         if (Model.getFacade().isACallAction(action)) {
 229  0
             Object operation = Model.getFacade().getOperation(action);
 230  0
             if (operation != null) {
 231  0
                 Object n = Model.getFacade().getName(operation);
 232  0
                 if (n != null) {
 233  0
                     ret = (String) n;
 234  
                 }
 235  0
                 n =
 236  
                     Model.getFacade().getName(
 237  
                         Model.getFacade().getOwner(operation));
 238  0
                 if (n != null && !n.equals("")) {
 239  0
                     ret += "\n(" + (String) n + ")";
 240  
                 }
 241  
             }
 242  
         }
 243  
 
 244  0
         if (ret == null) {
 245  0
             return "";
 246  
         }
 247  0
         return ret;
 248  
     }
 249  
 
 250  
     @Override
 251  
     public String toString(Object modelElement, NotationSettings settings) {
 252  0
         return toString(modelElement);
 253  
     }
 254  
 
 255  
 }