Coverage Report - org.argouml.model.euml.UMLUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
UMLUtil
0%
0/53
0%
0/34
5.167
UMLUtil$1
0%
0/3
N/A
5.167
 
 1  
 // $Id: UMLUtil.java 19105 2011-03-05 01:04:10Z thn $
 2  
 /*******************************************************************************
 3  
  * Copyright (c) 2007,2010 Bogdan Pistol and other contributors
 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  
  *    Bogdan Pistol - initial API and implementation
 11  
  *******************************************************************************/
 12  
 package org.argouml.model.euml;
 13  
 
 14  
 import org.eclipse.emf.common.command.Command;
 15  
 import org.eclipse.emf.common.command.StrictCompoundCommand;
 16  
 import org.eclipse.emf.common.util.EList;
 17  
 import org.eclipse.emf.common.util.URI;
 18  
 import org.eclipse.emf.ecore.resource.Resource;
 19  
 import org.eclipse.emf.edit.command.CopyToClipboardCommand;
 20  
 import org.eclipse.emf.edit.command.PasteFromClipboardCommand;
 21  
 import org.eclipse.uml2.uml.Association;
 22  
 import org.eclipse.uml2.uml.AssociationClass;
 23  
 import org.eclipse.uml2.uml.Element;
 24  
 import org.eclipse.uml2.uml.NamedElement;
 25  
 import org.eclipse.uml2.uml.Operation;
 26  
 import org.eclipse.uml2.uml.Property;
 27  
 import org.eclipse.uml2.uml.Type;
 28  
 
 29  
 /**
 30  
  * This class exposes protected methods from
 31  
  * {@link org.eclipse.uml2.uml.UMLUtil} and adds additional util methods
 32  
  * 
 33  
  * @author Bogdan Pistol
 34  
  */
 35  0
 public class UMLUtil extends org.eclipse.uml2.uml.util.UMLUtil {
 36  
 
 37  
     /**
 38  
      * The default URI used for eUML
 39  
      */
 40  0
     public static final URI DEFAULT_URI = 
 41  
         URI.createURI("http://argouml.tigris.org/euml/resource/default_uri.xmi"); //$NON-NLS-1$
 42  
         
 43  
     /**
 44  
      * Getter for the attributes of a Type
 45  
      * 
 46  
      * @param type
 47  
      *                The Type that owns the attributes
 48  
      * @return the attributes or null
 49  
      */
 50  
     public static EList<Property> getOwnedAttributes(Type type) {
 51  0
         if (type instanceof AssociationClass) {
 52  0
             return ((AssociationClass) type).getOwnedAttributes();
 53  0
         } else if (type instanceof Association) {
 54  0
             return ((Association) type).getOwnedEnds();
 55  
         } else {
 56  0
             return org.eclipse.uml2.uml.util.UMLUtil.getOwnedAttributes(type);
 57  
         }
 58  
     }
 59  
 
 60  
     /**
 61  
      * Getter for the operations of a Type
 62  
      * 
 63  
      * @param type
 64  
      *                The Type that owns the operations
 65  
      * @return the operations or null
 66  
      */
 67  
     public static EList<Operation> getOwnedOperations(Type type) {
 68  0
         return org.eclipse.uml2.uml.util.UMLUtil.getOwnedOperations(type);
 69  
     }
 70  
 
 71  
     /**
 72  
      * Copy a tree of UML elements into a destination location
 73  
      * 
 74  
      * @param modelImplementation
 75  
      *                the eUML model implementation
 76  
      * @param source
 77  
      *                the tree of UML elements to be copied
 78  
      * @param destination
 79  
      *                the destination container
 80  
      * @return the root of the newly copied tree of UML elements or null
 81  
      */
 82  
     public static Element copy(EUMLModelImplementation modelImplementation,
 83  
             Element source, Element destination) {
 84  0
         Command copyToClipboard = CopyToClipboardCommand.create(
 85  
                 modelImplementation.getEditingDomain(), source);
 86  0
         Command pasteFromClipboard = PasteFromClipboardCommand.create(
 87  
                 modelImplementation.getEditingDomain(), destination, null);
 88  0
         StrictCompoundCommand copyCommand = new StrictCompoundCommand() {
 89  
             {
 90  0
                 isPessimistic = true;
 91  0
             }
 92  
         };
 93  0
         copyCommand.append(copyToClipboard);
 94  0
         copyCommand.append(pasteFromClipboard);
 95  0
         copyCommand.setLabel("Copy a tree of UML elements to a destination"); //$NON-NLS-1$
 96  0
         if (copyCommand.canExecute()) {
 97  0
             modelImplementation.getModelEventPump().getRootContainer().setHoldEvents(
 98  
                     true);
 99  0
             modelImplementation.getEditingDomain().getCommandStack().execute(
 100  
                     copyCommand);
 101  0
             if (modelImplementation.getEditingDomain().getCommandStack().getMostRecentCommand().getAffectedObjects().size() == 1) {
 102  0
                 modelImplementation.getModelEventPump().getRootContainer().setHoldEvents(
 103  
                         false);
 104  0
                 return (Element) modelImplementation.getEditingDomain().getCommandStack().getMostRecentCommand().getAffectedObjects().iterator().next();
 105  
             } else {
 106  0
                 modelImplementation.getEditingDomain().getCommandStack().undo();
 107  0
                 modelImplementation.getModelEventPump().getRootContainer().clearHeldEvents();
 108  0
                 modelImplementation.getModelEventPump().getRootContainer().setHoldEvents(
 109  
                         false);
 110  
             }
 111  
         }
 112  0
         return null;
 113  
     }
 114  
     
 115  
     /**
 116  
      * Returns information about an Object.
 117  
      * <p>
 118  
      * If it's an Element it returns its metaclass name, and if it's a
 119  
      * NamedElement it appends its name.
 120  
      * 
 121  
      * @param o
 122  
      *                The object
 123  
      * @return the String description
 124  
      */
 125  
     public static String toString(Object o) {
 126  0
         if (o == null) {
 127  0
             return "null"; //$NON-NLS-1$
 128  
         }
 129  0
         if (!(o instanceof Element)) {
 130  0
             return o.toString();
 131  
         }
 132  
         
 133  0
         StringBuilder sb = new StringBuilder("'"); //$NON-NLS-1$
 134  0
         boolean named = false;
 135  
         
 136  0
         if (o instanceof NamedElement && ((NamedElement) o).getName() != null
 137  
                 && !((NamedElement) o).getName().equals("")) { //$NON-NLS-1$
 138  0
             named = true;
 139  0
             sb.append(((NamedElement) o).getName() + " ["); //$NON-NLS-1$
 140  
         }
 141  
         
 142  0
         sb.append(((Element) o).eClass().getName());
 143  
         
 144  0
         if (named) {
 145  0
             sb.append("]"); //$NON-NLS-1$
 146  
         }
 147  
         
 148  0
         sb.append("'"); //$NON-NLS-1$
 149  
         
 150  0
         return sb.toString();
 151  
     }
 152  
     
 153  
     /**
 154  
      * Returns the Resource associated with an URI or creates the resource if it
 155  
      * does not exist
 156  
      * 
 157  
      * @param modelImplementation
 158  
      *                the eUML implementation
 159  
      * @param uri
 160  
      *                the URI which identifies the resource
 161  
      * @return the retrieved or created resource
 162  
      */
 163  
     static Resource getResource(
 164  
             EUMLModelImplementation modelImplementation, URI uri,
 165  
             boolean readOnly) {
 166  0
         if (!"xmi".equals(uri.fileExtension()) //$NON-NLS-1$
 167  
                 && !"uml".equals(uri.fileExtension())) { //$NON-NLS-1$
 168  
             // Make sure we have a recognized file extension
 169  0
             uri = uri.appendFileExtension("xmi"); //$NON-NLS-1$
 170  
         }
 171  0
         Resource r = modelImplementation.getEditingDomain().getResourceSet()
 172  
                 .getResource(uri, false);
 173  0
         if (r == null) {
 174  0
             r = modelImplementation.getEditingDomain().getResourceSet()
 175  
                     .createResource(uri);
 176  
         }
 177  0
         if (r == null) {
 178  0
             throw new NullPointerException("Failed to create resource for URI "  //$NON-NLS-1$
 179  
                     + uri);
 180  
         }
 181  0
         modelImplementation.getReadOnlyMap().put(r, Boolean.valueOf(readOnly));
 182  0
         return r;
 183  
     }
 184  
 
 185  
 
 186  
     @SuppressWarnings("unchecked")
 187  
     static void checkArgs(Object[] args, Class[] types) {
 188  0
         if (args.length != types.length) {
 189  0
             throw new IllegalArgumentException(
 190  
                     "Mismatched array lengths for args and types"); //$NON-NLS-1$
 191  
         }
 192  0
         for (int i = 0; i < args.length; i++) {
 193  0
             if (!types[i].isAssignableFrom(args[i].getClass())) {
 194  0
                 throw new IllegalArgumentException(
 195  
                         "Parameter " + i + " must be of type " + types[i]);  //$NON-NLS-1$//$NON-NLS-2$
 196  
             }
 197  
         }
 198  0
     }
 199  
 }