Coverage Report - org.argouml.uml.ui.foundation.extension_mechanisms.UMLTagDefinitionComboBoxModel
 
Classes in this File Line Coverage Branch Coverage Complexity
UMLTagDefinitionComboBoxModel
5%
3/51
0%
0/26
2.091
 
 1  
 /* $Id: UMLTagDefinitionComboBoxModel.java 18442 2010-06-10 20:01:30Z thn $
 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  
  *    Thomas Neustupny
 12  
  *****************************************************************************
 13  
  *
 14  
  * Some portions of this file was previously release using the BSD License:
 15  
  */
 16  
 
 17  
 // Copyright (c) 2005-2009 The Regents of the University of California. All
 18  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 19  
 // software and its documentation without fee, and without a written
 20  
 // agreement is hereby granted, provided that the above copyright notice
 21  
 // and this paragraph appear in all copies.  This software program and
 22  
 // documentation are copyrighted by The Regents of the University of
 23  
 // California. The software program and documentation are supplied "AS
 24  
 // IS", without any accompanying services from The Regents. The Regents
 25  
 // does not warrant that the operation of the program will be
 26  
 // uninterrupted or error-free. The end-user understands that the program
 27  
 // was developed for research purposes and is advised not to rely
 28  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 29  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 30  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 31  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 32  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 33  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 34  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 35  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 36  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 37  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 38  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 39  
 
 40  
 package org.argouml.uml.ui.foundation.extension_mechanisms;
 41  
 
 42  
 import java.util.ArrayList;
 43  
 import java.util.Collection;
 44  
 import java.util.HashSet;
 45  
 import java.util.List;
 46  
 import java.util.Set;
 47  
 import java.util.TreeSet;
 48  
 
 49  
 import org.apache.log4j.Logger;
 50  
 import org.argouml.kernel.Project;
 51  
 import org.argouml.kernel.ProjectManager;
 52  
 import org.argouml.model.Model;
 53  
 import org.argouml.model.UmlChangeEvent;
 54  
 import org.argouml.uml.ui.UMLComboBoxModel2;
 55  
 import org.argouml.uml.util.PathComparator;
 56  
 
 57  
 /**
 58  
  * A model for tagdefinitions.
 59  
  * @author lmaitre
 60  
  * @since October 27, 2005
 61  
  */
 62  
 public class UMLTagDefinitionComboBoxModel  extends UMLComboBoxModel2 {
 63  
     
 64  900
     private static final Logger LOG = 
 65  
         Logger.getLogger(UMLTagDefinitionComboBoxModel.class);
 66  
 
 67  
     /**
 68  
      * Constructor for UMLTagDefinitionComboBoxModel.
 69  
      */
 70  
     public UMLTagDefinitionComboBoxModel() {
 71  
         // stereotypes applied to the target mostly control which TDs
 72  
         // (but see below for other listeners too)
 73  900
         super("stereotype", false);
 74  900
     }
 75  
 
 76  
     @Override
 77  
     protected void addOtherModelEventListeners(Object target) {
 78  
         // Ask to be notified of any changes to TagDefinitions so that we
 79  
         // can track new ones, name changes, etc
 80  0
         Model.getPump().addClassModelEventListener(this, 
 81  
                 Model.getMetaTypes().getTagDefinition(), (String[]) null);
 82  0
     }    
 83  
 
 84  
     @Override
 85  
     protected void removeOtherModelEventListeners(Object target) {
 86  0
         Model.getPump().addClassModelEventListener(this, 
 87  
                 Model.getMetaTypes().getTagDefinition(), (String[]) null);
 88  0
     }
 89  
 
 90  
     @Override
 91  
     public void modelChanged(UmlChangeEvent evt) {
 92  
         // because we're listening for stereotypes being added and removed
 93  
         // but we're really interested in their owned tag definitions,
 94  
         // the default implementation won't work for us
 95  
         
 96  0
         if (Model.getFacade().isATagDefinition(evt.getSource())) {
 97  0
             LOG.debug("Got TagDefinition event " + evt.toString());
 98  
             // Just mark for rebuild next time since we use lazy loading
 99  0
             setModelInvalid();
 100  0
         } else if ("stereotype".equals(evt.getPropertyName())) {
 101  0
             LOG.debug("Got stereotype event " + evt.toString());
 102  
             // A stereotype got applied or removed
 103  
             // Just mark for rebuild next time since we use lazy loading
 104  0
             setModelInvalid();
 105  
         } else {
 106  0
             LOG.debug("Got other event " + evt.toString());
 107  
         }    
 108  0
     }
 109  
 
 110  
 
 111  
     @Override
 112  
     public boolean isLazy() {
 113  0
         return true;
 114  
     }
 115  
 
 116  
 
 117  
     /*
 118  
      * @see org.argouml.uml.ui.UMLComboBoxModel2#isValidElement(Object)
 119  
      */
 120  
     protected boolean isValidElement(Object element) {
 121  0
         Object owner = Model.getFacade().getOwner(element);
 122  0
         return (Model.getFacade().isATagDefinition(element)
 123  
                 && (owner == null || Model
 124  
                 .getFacade().getStereotypes(getTarget()).contains(owner)));
 125  
     }
 126  
 
 127  
     /*
 128  
      * @see org.argouml.uml.ui.UMLComboBoxModel2#setSelectedItem(java.lang.Object)
 129  
      */
 130  
     @Override
 131  
     public void setSelectedItem(Object o) {
 132  0
         setFireListEvents(false);
 133  0
         super.setSelectedItem(o);
 134  0
         setFireListEvents(true);
 135  0
     }
 136  
 
 137  
 
 138  
     /*
 139  
      * @see org.argouml.uml.ui.UMLComboBoxModel2#buildModelList()
 140  
      */
 141  
     protected void buildModelList() {
 142  0
         setElements(getApplicableTagDefinitions(getTarget()));
 143  0
     }
 144  
 
 145  
     /*
 146  
      * @see org.argouml.uml.ui.UMLComboBoxModel2#getSelectedModelElement()
 147  
      */
 148  
     protected Object getSelectedModelElement() {
 149  0
         return getSelectedItem();
 150  
     }
 151  
 
 152  
     Collection getApplicableTagDefinitions(Object element) {
 153  0
         Set<List<String>> paths = new HashSet<List<String>>();
 154  0
         Set<Object> availableTagDefs = 
 155  
             new TreeSet<Object>(new PathComparator());
 156  0
         Collection stereotypes = Model.getFacade().getStereotypes(element);
 157  0
         Project project = ProjectManager.getManager().getCurrentProject();
 158  0
         if (Model.getFacade().getUmlVersion().charAt(0) == '1') {
 159  0
             for (Object model : project.getModels()) {
 160  
                 // TODO: Won't our use of PathComparator take care of uniqueness?
 161  0
                 addAllUniqueModelElementsFrom(availableTagDefs, paths,
 162  
                         Model.getModelManagementHelper().getAllModelElementsOfKind(
 163  
                                 model,
 164  
                                 Model.getMetaTypes().getTagDefinition()));
 165  
             }
 166  
             // TODO: Won't our use of PathComparator take care of uniqueness?
 167  0
             addAllUniqueModelElementsFrom(availableTagDefs, paths, project
 168  
                     .getProfileConfiguration().findByMetaType(
 169  
                             Model.getMetaTypes().getTagDefinition()));
 170  
                             
 171  0
             List notValids = new ArrayList();
 172  0
             for (Object tagDef : availableTagDefs) {
 173  0
                 Object owner = Model.getFacade().getOwner(tagDef);
 174  0
                 if (owner != null && !stereotypes.contains(owner)) {
 175  0
                     notValids.add(tagDef);
 176  
                 }
 177  0
             }
 178  0
             availableTagDefs.removeAll(notValids);
 179  0
         } else {
 180  
             // since UML2 it's easier: TDs only via stereotypes
 181  0
             for (Object st : stereotypes) {
 182  0
                 availableTagDefs.addAll(Model.getFacade().getAttributes(st));
 183  
             }
 184  
         }
 185  0
         return availableTagDefs;
 186  
     }
 187  
 
 188  
     /**
 189  
      * Helper method for buildModelList.<p>
 190  
      *
 191  
      * Adds those elements from source that do not have the same path as any
 192  
      * path in paths to elements, and its path to paths. Thus elements will
 193  
      * never contain two objects with the same path, unless they are added by
 194  
      * other means.
 195  
      */
 196  
     private static void addAllUniqueModelElementsFrom(Set elements,
 197  
             Set<List<String>> paths, Collection sources) {
 198  
         
 199  0
         for (Object source : sources) {
 200  0
             List<String> path = Model.getModelManagementHelper().getPathList(
 201  
                     source);
 202  0
             if (!paths.contains(path)) {
 203  0
                 paths.add(path);
 204  0
                 elements.add(source);
 205  
             }
 206  0
         }
 207  0
     }
 208  
 
 209  
     /**
 210  
      * The UID.
 211  
      */
 212  
     private static final long serialVersionUID = -4194727034416788372L;
 213  
 }