Coverage Report - org.argouml.model.mdr.UmlHelperMDRImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
UmlHelperMDRImpl
4%
6/131
1%
1/56
5.75
 
 1  
 /* $Id: UmlHelperMDRImpl.java 17765 2010-01-11 21:20:14Z 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-2006 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.model.mdr;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.Arrays;
 43  
 import java.util.Collection;
 44  
 import java.util.Iterator;
 45  
 import java.util.List;
 46  
 
 47  
 import org.argouml.model.Model;
 48  
 import org.argouml.model.UmlHelper;
 49  
 import org.omg.uml.behavioralelements.collaborations.Message;
 50  
 import org.omg.uml.behavioralelements.commonbehavior.Action;
 51  
 import org.omg.uml.behavioralelements.commonbehavior.ActionSequence;
 52  
 import org.omg.uml.behavioralelements.commonbehavior.Argument;
 53  
 import org.omg.uml.behavioralelements.commonbehavior.Link;
 54  
 import org.omg.uml.behavioralelements.commonbehavior.LinkEnd;
 55  
 import org.omg.uml.behavioralelements.statemachines.Event;
 56  
 import org.omg.uml.behavioralelements.statemachines.Transition;
 57  
 import org.omg.uml.behavioralelements.usecases.Extend;
 58  
 import org.omg.uml.behavioralelements.usecases.ExtensionPoint;
 59  
 import org.omg.uml.behavioralelements.usecases.UseCase;
 60  
 import org.omg.uml.foundation.core.AssociationEnd;
 61  
 import org.omg.uml.foundation.core.Attribute;
 62  
 import org.omg.uml.foundation.core.BehavioralFeature;
 63  
 import org.omg.uml.foundation.core.Classifier;
 64  
 import org.omg.uml.foundation.core.Enumeration;
 65  
 import org.omg.uml.foundation.core.EnumerationLiteral;
 66  
 import org.omg.uml.foundation.core.Feature;
 67  
 import org.omg.uml.foundation.core.Operation;
 68  
 import org.omg.uml.foundation.core.Parameter;
 69  
 import org.omg.uml.foundation.core.Relationship;
 70  
 import org.omg.uml.foundation.core.UmlAssociation;
 71  
 
 72  
 /**
 73  
  * Helper class for UML metamodel.
 74  
  * 
 75  
  * @since ARGO0.11.2
 76  
  * @author Thierry Lach
 77  
  */
 78  
 class UmlHelperMDRImpl implements UmlHelper {
 79  
 
 80  
     /**
 81  
      * The model implementation.
 82  
      */
 83  
     private MDRModelImplementation modelImpl;
 84  
 
 85  
     /**
 86  
      * Don't allow instantiation.
 87  
      * 
 88  
      * @param implementation
 89  
      *            To get other helpers and factories.
 90  
      */
 91  900
     UmlHelperMDRImpl(MDRModelImplementation implementation) {
 92  900
         modelImpl = implementation;
 93  900
     }
 94  
 
 95  
     public void addListenersToModel(Object model) {
 96  
         // Nothing to do - we get all events automatically
 97  0
     }
 98  
 
 99  
     /*
 100  
      * @see org.argouml.model.UmlHelper#deleteCollection(java.util.Collection)
 101  
      */
 102  
     public void deleteCollection(Collection col) {
 103  8
         Iterator it = col.iterator();
 104  8
         while (it.hasNext()) {
 105  0
             modelImpl.getUmlFactory().delete(it.next());
 106  
         }
 107  8
     }
 108  
 
 109  
     /*
 110  
      * @see org.argouml.model.UmlHelper#getSource(java.lang.Object)
 111  
      */
 112  
     public Object getSource(Object relationship) {
 113  0
         if (relationship instanceof Message) {
 114  0
             Message message = (Message) relationship;
 115  0
             return message.getSender();
 116  
         }
 117  0
         if (relationship instanceof Relationship) {
 118  
             // handles all children of relationship including extend and
 119  
             // include which are not members of core
 120  0
             return modelImpl.getCoreHelper().getSource(relationship);
 121  
         }
 122  0
         if (relationship instanceof Transition) {
 123  0
             return modelImpl.getStateMachinesHelper().getSource(relationship);
 124  
         }
 125  0
         if (relationship instanceof AssociationEnd) {
 126  0
             return modelImpl.getCoreHelper().getSource(relationship);
 127  
         }
 128  0
         throw new IllegalArgumentException();
 129  
     }
 130  
 
 131  
     /*
 132  
      * @see org.argouml.model.UmlHelper#getDestination(java.lang.Object)
 133  
      */
 134  
     public Object getDestination(Object relationship) {
 135  0
         if (relationship instanceof Message) {
 136  0
             Message message = (Message) relationship;
 137  0
             return message.getSender();
 138  
         }
 139  0
         if (relationship instanceof Relationship) {
 140  
             // handles all children of relationship including extend and
 141  
             // include which are not members of core
 142  0
             return modelImpl.getCoreHelper().getDestination(relationship);
 143  
         }
 144  0
         if (relationship instanceof Transition) {
 145  0
             return modelImpl.getStateMachinesHelper().
 146  
                     getDestination(relationship);
 147  
         }
 148  0
         if (relationship instanceof AssociationEnd) {
 149  0
             return modelImpl.getCoreHelper().getDestination(relationship);
 150  
         }
 151  0
         throw new IllegalArgumentException();
 152  
     }
 153  
     
 154  
 
 155  
     /*
 156  
      * @see org.argouml.model.UmlHelper#move(java.lang.Object, org.argouml.model.UmlHelper.Direction)
 157  
      */
 158  
     public void move(Object parent, Object element, Direction direction) {
 159  0
         if (element instanceof Argument) {
 160  0
             final Argument arg = (Argument) element;
 161  0
             final Action action = arg.getAction();
 162  0
             final List<Argument> f = action.getActualArgument();
 163  0
             final int oldIndex = f.indexOf(arg);
 164  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 165  0
             f.remove(arg);
 166  0
             f.add(newIndex, arg);
 167  0
         } else if (element instanceof Action) {
 168  0
             final Action act = (Action) element;
 169  0
             final ActionSequence actionSequence = act.getActionSequence();
 170  0
             final List<Action> f = actionSequence.getAction();
 171  0
             final int oldIndex = f.indexOf(act);
 172  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 173  0
             f.remove(actionSequence);
 174  0
             f.add(newIndex, actionSequence);
 175  0
         } else if (element instanceof AssociationEnd) {
 176  0
             final AssociationEnd assEnd = (AssociationEnd) element;
 177  0
             final UmlAssociation assoc = assEnd.getAssociation();
 178  0
             final List<AssociationEnd> f =  assoc.getConnection();
 179  0
             final int oldIndex = f.indexOf(assEnd);
 180  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 181  0
             f.remove(assEnd);
 182  0
             f.add(newIndex, assEnd);
 183  0
         } else if (element instanceof Attribute && parent instanceof AssociationEnd) {
 184  0
             final Attribute attr = (Attribute) element;
 185  0
             final AssociationEnd assocEnd = attr.getAssociationEnd();
 186  0
             final List<Attribute> f = assocEnd.getQualifier();
 187  0
             final int oldIndex = f.indexOf(assocEnd);
 188  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 189  0
             f.remove(attr);
 190  0
             f.add(newIndex, attr);
 191  0
         } else if (element instanceof Feature) {
 192  0
             final Feature att = (Feature) element;
 193  0
             final Classifier cls = att.getOwner();
 194  0
             final List f = Model.getFacade().getFeatures(cls);
 195  0
             final int oldIndex = f.indexOf(att);
 196  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 197  0
             Model.getCoreHelper().removeFeature(cls, att);
 198  0
             Model.getCoreHelper().addFeature(cls, newIndex, att);
 199  0
         } else if (element instanceof Parameter && parent instanceof Event) {
 200  0
             final Parameter param = (Parameter) element;
 201  0
             final Event event = (Event) parent;
 202  0
             final List<Parameter> f = event.getParameter();
 203  0
             final int oldIndex = f.indexOf(param);
 204  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 205  0
             f.remove(param);
 206  0
             f.add(newIndex, param);
 207  0
         } else if (element instanceof Parameter) {
 208  0
             final Parameter param = (Parameter) element;
 209  0
             final BehavioralFeature bf = param.getBehavioralFeature();
 210  0
             final List<Parameter> f = bf.getParameter();
 211  0
             final int oldIndex = f.indexOf(param);
 212  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 213  0
             f.remove(param);
 214  0
             f.add(newIndex, param);
 215  0
         } else if (element instanceof EnumerationLiteral) {
 216  0
             final EnumerationLiteral lit = (EnumerationLiteral) element;
 217  0
             final Enumeration enumeration = lit.getEnumeration();
 218  0
             final List<EnumerationLiteral> f = enumeration.getLiteral();
 219  0
             final int oldIndex = f.indexOf(lit);
 220  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 221  0
             f.remove(lit);
 222  0
             f.add(newIndex, lit);
 223  0
         } else if (element instanceof ExtensionPoint && parent instanceof Extend) {
 224  0
             final ExtensionPoint ep = (ExtensionPoint) element;
 225  0
             final Extend extend = (Extend) parent;
 226  0
             final List<ExtensionPoint> f = extend.getExtensionPoint();
 227  0
             final int oldIndex = f.indexOf(ep);
 228  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 229  0
             f.remove(ep);
 230  0
             f.add(newIndex, ep);
 231  0
         } else if (element instanceof LinkEnd) {
 232  0
             final LinkEnd le = (LinkEnd) element;
 233  0
             final Link link = le.getLink();
 234  0
             final List f = new ArrayList(Model.getFacade().getConnections(link));
 235  0
             final int oldIndex = f.indexOf(le);
 236  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 237  0
             f.remove(le);
 238  0
             f.add(newIndex, le);
 239  0
             Model.getCoreHelper().setConnections(link, f);
 240  0
         } else if (element instanceof ExtensionPoint && parent instanceof UseCase) {
 241  0
             final ExtensionPoint ep = (ExtensionPoint) element;
 242  0
             final UseCase extend = ep.getUseCase();
 243  0
             final List f = new ArrayList(Model.getFacade().getExtensionPoints(extend));
 244  0
             final int oldIndex = f.indexOf(ep);
 245  0
             final int newIndex = newPosition(oldIndex, f.size(), direction);
 246  0
             f.remove(ep);
 247  0
             f.add(newIndex, ep);
 248  0
             Model.getUseCasesHelper().setExtensionPoints(ep, f);
 249  
         }
 250  0
     }
 251  
     
 252  
     /*
 253  
      * @see org.argouml.model.UmlHelper#move(java.lang.Object, org.argouml.model.UmlHelper.Direction)
 254  
      */
 255  
     public boolean isMovable(Object metaType) {
 256  0
         final Class<?>[] movableMetaType = new Class<?> [] {
 257  
             Action.class, 
 258  
             Argument.class, 
 259  
             AssociationEnd.class, 
 260  
             Attribute.class, 
 261  
             EnumerationLiteral.class,
 262  
             Extend.class,
 263  
             ExtensionPoint.class,
 264  
             Feature.class, 
 265  
             LinkEnd.class,
 266  
             Operation.class, 
 267  
             Parameter.class};
 268  0
         return Arrays.asList(movableMetaType).contains(metaType);
 269  
     }
 270  
     
 271  
     
 272  
     private int newPosition(int index, int size, Direction direction) {
 273  
         final int posn;
 274  0
         if (direction == Direction.DOWN) {
 275  0
             posn = index + 1;
 276  0
         } else if (direction == Direction.UP) {
 277  0
             posn = index - 1;
 278  0
         } else if (direction == Direction.TOP) {
 279  0
             posn = 0;
 280  0
         } else if (direction == Direction.BOTTOM) {
 281  0
             posn = size - 1;
 282  
         } else {
 283  0
             posn = 0;
 284  
         }
 285  0
         return posn;
 286  
     }
 287  
 }