Coverage Report - org.argouml.uml.cognitive.critics.WizAssocComposite
 
Classes in this File Line Coverage Branch Coverage Complexity
WizAssocComposite
0%
0/77
0%
0/46
5.571
 
 1  
 /* $Id: WizAssocComposite.java 17849 2010-01-12 19:50:34Z 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  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2007 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.cognitive.critics;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.Iterator;
 43  
 import java.util.List;
 44  
 
 45  
 import javax.swing.JPanel;
 46  
 
 47  
 import org.apache.log4j.Logger;
 48  
 
 49  
 import org.argouml.cognitive.ui.WizStepChoice;
 50  
 import org.argouml.i18n.Translator;
 51  
 import org.argouml.model.Model;
 52  
 
 53  
 /**
 54  
  * A non-modal wizard to assist the user in changing aggregation of an
 55  
  * association.
 56  
  * <p>
 57  
  * 
 58  
  * Earlier version always imposed composite aggregation. This version allows the
 59  
  * user to choose.
 60  
  * <p>
 61  
  * 
 62  
  * <em>Note</em>. This only applies to binary associations. A separate wizard
 63  
  * is needed for 3-way (or more) associations.
 64  
  * <p>
 65  
  * 
 66  
  * @see "ArgoUML User Manual: Two Aggregate ends (roles) in binary Association"
 67  
  * @author jrobbins@ics.uci.edu
 68  
  */
 69  
 public class WizAssocComposite extends UMLWizard {
 70  
     /**
 71  
      * Logger.
 72  
      */
 73  0
     private static final Logger LOG = Logger.getLogger(WizAssocComposite.class);
 74  
 
 75  
     /**
 76  
      * The initial instructions on the Step 1 screen. May be set to a different
 77  
      * string through {@link #setInstructions(String)}.
 78  
      * <p>
 79  
      */
 80  0
     private String instructions = Translator
 81  
             .localize("critics.WizAssocComposite-ins");
 82  
 
 83  
     /**
 84  
      * Contains the {@link WizStepChoice} that is used to get the user's desired
 85  
      * options. Not created until we get to that step.
 86  
      * <p>
 87  
      */
 88  0
     private WizStepChoice step1Choice = null;
 89  
 
 90  
     /**
 91  
      * The Association {@link WizStepChoice} that triggered the critic. Null
 92  
      * until set when it is first needed.
 93  
      * <p>
 94  
      */
 95  0
     private Object triggerAssociation = null;
 96  
 
 97  
     /**
 98  
      * Constructor for the wizard. Currently does nothing.
 99  
      * <p>
 100  
      */
 101  0
     public WizAssocComposite() {
 102  0
     }
 103  
 
 104  
     /**
 105  
      * Tries to identify the Association that triggered the critic.
 106  
      * <p>
 107  
      * 
 108  
      * The first time it is called, it will initialise the trigger from the
 109  
      * ToDoItem. If there, it is assumed to be the first trigger of the ToDoItem
 110  
      * and to be an association. If found, the value is stored in the private
 111  
      * field {@link #triggerAssociation}.
 112  
      * <p>
 113  
      * 
 114  
      * On all subsequent calls, if a non-null value is found in {@link
 115  
      * #triggerAssociation} that is returned.
 116  
      * <p>
 117  
      * 
 118  
      * @return the Association that triggered the critic, or <code>null</code>
 119  
      *         if there was none.
 120  
      */
 121  
     private Object getTriggerAssociation() {
 122  
 
 123  
         // If we don't have it, find the trigger. If this fails it will keep
 124  
         // its default value of null
 125  
 
 126  0
         if ((triggerAssociation == null) && (getToDoItem() != null)) {
 127  0
             triggerAssociation = getModelElement();
 128  
         }
 129  
 
 130  0
         return triggerAssociation;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Returns a list of options to be used in creating a {@link
 135  
      * WizStepChoice} that will exercise the options.
 136  
      * <p>
 137  
      * 
 138  
      * We provide five options, shared aggregation in each direction, composite
 139  
      * aggregation in each direction and no aggregation at all.
 140  
      * <p>
 141  
      * 
 142  
      * It is possible that a very malicious user could delete the triggering
 143  
      * association just before we get to this point. For now we don't bother to
 144  
      * trap this. It will raise an exception, and then everything will carry on
 145  
      * happily.
 146  
      * <p>
 147  
      * 
 148  
      * @return A {@link List} of the options or <code>null</code> if the
 149  
      *         association that triggered the critic is no longer there.
 150  
      */
 151  
     private List<String> buildOptions() {
 152  
 
 153  
         // The association that triggered the critic. Its just possible the
 154  
         // association is no longer there, in which case we return null
 155  
 
 156  0
         Object asc = getTriggerAssociation();
 157  
 
 158  0
         if (asc == null) {
 159  0
             return null;
 160  
         }
 161  
 
 162  0
         List<String> result = new ArrayList<String>();
 163  
 
 164  
         // Get the ends from the association (we know there are two), and the
 165  
         // types associated with them.
 166  
 
 167  0
         Iterator iter = Model.getFacade().getConnections(asc).iterator();
 168  
 
 169  0
         Object ae0 = iter.next();
 170  0
         Object ae1 = iter.next();
 171  
 
 172  0
         Object cls0 = Model.getFacade().getType(ae0);
 173  0
         Object cls1 = Model.getFacade().getType(ae1);
 174  
 
 175  
         // Get the names of the two ends. If there are none (i.e they are
 176  
         // currently anonymous), use the ArgoUML convention of "(anon)" for the
 177  
         // names
 178  
 
 179  0
         String start = Translator.localize("misc.name.anon");
 180  0
         String end = Translator.localize("misc.name.anon");
 181  
 
 182  0
         if ((cls0 != null) && (Model.getFacade().getName(cls0) != null)
 183  
                 && (!(Model.getFacade().getName(cls0).equals("")))) {
 184  0
             start = Model.getFacade().getName(cls0);
 185  
         }
 186  
 
 187  0
         if ((cls1 != null) && (Model.getFacade().getName(cls1) != null)
 188  
                 && (!(Model.getFacade().getName(cls1).equals("")))) {
 189  0
             end = Model.getFacade().getName(cls1);
 190  
         }
 191  
 
 192  
         // Now create the five options
 193  
 
 194  0
         result.add(start
 195  
                 + Translator.localize("critics.WizAssocComposite-option1")
 196  
                 + end);
 197  0
         result.add(start
 198  
                 + Translator.localize("critics.WizAssocComposite-option2")
 199  
                 + end);
 200  
 
 201  0
         result.add(end
 202  
                 + Translator.localize("critics.WizAssocComposite-option1")
 203  
                 + start);
 204  0
         result.add(end
 205  
                 + Translator.localize("critics.WizAssocComposite-option2")
 206  
                 + start);
 207  
 
 208  0
         result.add(Translator.localize("critics.WizAssocComposite-option3"));
 209  
 
 210  0
         return result;
 211  
     }
 212  
 
 213  
     /**
 214  
      * Set the initial instruction string for the choice. May be called by the
 215  
      * creator of the wizard to override the default.
 216  
      * <p>
 217  
      * 
 218  
      * @param s
 219  
      *            The new instructions.
 220  
      */
 221  
     public void setInstructions(String s) {
 222  0
         instructions = s;
 223  0
     }
 224  
 
 225  
     /**
 226  
      * Create a {@link JPanel} for the given step.
 227  
      * <p>
 228  
      * We use a {@link WizStepChoice} to handle the choice selection for the
 229  
      * user. We only create the panel once, saving it in a private field 
 230  
      * (<code>_step1Choice</code>) for subsequent use.
 231  
      * <p>
 232  
      * <em>Note</em>. If the association has been deleted, then we may not be
 233  
      * able to create a list of options. Under these circumstances we also
 234  
      * return null.
 235  
      * <p>
 236  
      * 
 237  
      * @param newStep The index of the step for which a panel is needed.
 238  
      * @return The created {@link JPanel} or <code>null</code> if no options
 239  
      *         were available.
 240  
      * @see org.argouml.cognitive.critics.Wizard
 241  
      */
 242  
     public JPanel makePanel(int newStep) {
 243  
 
 244  0
         switch (newStep) {
 245  
 
 246  
         case 1:
 247  
 
 248  
             // First step. Create the panel if not already done and options are
 249  
             // available. Otherwise it retains its default value of null.
 250  
 
 251  0
             if (step1Choice == null) {
 252  0
                 List<String> opts = buildOptions();
 253  
 
 254  0
                 if (opts != null) {
 255  0
                     step1Choice = new WizStepChoice(this, instructions, opts);
 256  0
                     step1Choice.setTarget(getToDoItem());
 257  
                 }
 258  
             }
 259  
 
 260  0
             return step1Choice;
 261  
 
 262  
         default:
 263  
         }
 264  
 
 265  
         // Default (any other step) is to return nothing
 266  
 
 267  0
         return null;
 268  
     }
 269  
 
 270  
     /**
 271  
      * Take action at the completion of a step.
 272  
      * <p>
 273  
      * 
 274  
      * The guideline for ArgoUML non-modal wizards is to act immediately, not
 275  
      * wait for the finish. This method may also be invoked when finish is
 276  
      * triggered for any steps whose panels didn't get created.
 277  
      * <p>
 278  
      * 
 279  
      * The observation is that this seems to be trigged when there is any change
 280  
      * on the panel (e.g choosing an option), not just when "next" is pressed.
 281  
      * Coded accordingly
 282  
      * <p>
 283  
      * 
 284  
      * We allow for the association that caused the problem having by now been
 285  
      * deleted, and hence an exception may be raised. We catch this politely.
 286  
      * <p>
 287  
      * 
 288  
      * @param oldStep
 289  
      *            The index of the step just completed (0 for the first
 290  
      *            information panel)
 291  
      * 
 292  
      * @see org.argouml.cognitive.critics.Wizard
 293  
      */
 294  
     public void doAction(int oldStep) {
 295  
 
 296  0
         switch (oldStep) {
 297  
 
 298  
         case 1:
 299  
 
 300  
             // Just completed the first step where we make our choices. First
 301  
             // see if we have a choice. We always should, so print a rude
 302  
             // message if we don't
 303  
 
 304  0
             int choice = -1;
 305  
 
 306  0
             if (step1Choice != null) {
 307  0
                 choice = step1Choice.getSelectedIndex();
 308  
             }
 309  
 
 310  0
             if (choice == -1) {
 311  0
                 LOG.warn("WizAssocComposite: nothing selected, "
 312  
                         + "should not get here");
 313  0
                 return;
 314  
             }
 315  
 
 316  
             // It is quite possible that the cause of the problem has by now
 317  
             // been deleted, in which case we will throw an exception if we try
 318  
             // to change things. Catch this tidily.
 319  
 
 320  
             try {
 321  
 
 322  
                 // Set the appropriate aggregation on each end
 323  
 
 324  0
                 Iterator iter = Model.getFacade().getConnections(
 325  
                         getTriggerAssociation()).iterator();
 326  
 
 327  0
                 Object ae0 = iter.next();
 328  0
                 Object ae1 = iter.next();
 329  
 
 330  0
                 switch (choice) {
 331  
 
 332  
                 case 0:
 333  
 
 334  
                     // Start is a composite aggregation of end
 335  
 
 336  0
                     Model.getCoreHelper().setAggregation(ae0,
 337  
                             Model.getAggregationKind().getComposite());
 338  0
                     Model.getCoreHelper().setAggregation(ae1,
 339  
                             Model.getAggregationKind().getNone());
 340  0
                     break;
 341  
 
 342  
                 case 1:
 343  
 
 344  
                     // Start is a shared aggregation of end
 345  
 
 346  0
                     Model.getCoreHelper().setAggregation(ae0,
 347  
                             Model.getAggregationKind().getAggregate());
 348  0
                     Model.getCoreHelper().setAggregation(ae1,
 349  
                             Model.getAggregationKind().getNone());
 350  0
                     break;
 351  
 
 352  
                 case 2:
 353  
 
 354  
                     // End is a composite aggregation of start
 355  
 
 356  0
                     Model.getCoreHelper().setAggregation(ae0,
 357  
                             Model.getAggregationKind().getNone());
 358  0
                     Model.getCoreHelper().setAggregation(ae1,
 359  
                             Model.getAggregationKind().getComposite());
 360  0
                     break;
 361  
 
 362  
                 case 3:
 363  
 
 364  
                     // End is a shared aggregation of start
 365  0
                     Model.getCoreHelper().setAggregation(ae0,
 366  
                             Model.getAggregationKind().getNone());
 367  0
                     Model.getCoreHelper().setAggregation(ae1,
 368  
                             Model.getAggregationKind().getAggregate());
 369  0
                     break;
 370  
 
 371  
                 case 4:
 372  
 
 373  
                     // No aggregation
 374  0
                     Model.getCoreHelper().setAggregation(ae0,
 375  
                             Model.getAggregationKind().getNone());
 376  0
                     Model.getCoreHelper().setAggregation(ae1,
 377  
                             Model.getAggregationKind().getNone());
 378  0
                     break;
 379  
 
 380  
                 default:
 381  
                 }
 382  0
             } catch (Exception pve) {
 383  
 
 384  
                 // Someone took our association away.
 385  
 
 386  0
                 LOG.error("WizAssocComposite: could not set " + "aggregation.",
 387  
                         pve);
 388  0
             }
 389  
 
 390  
         default:
 391  
         }
 392  0
     }
 393  
 
 394  
     /**
 395  
      * Determine if we have sufficient information to finish.
 396  
      * <p>
 397  
      * We can't finish if our parent
 398  
      * {@link org.argouml.cognitive.critics.Wizard} can't finish.
 399  
      * <p>
 400  
      * We can finish if we're on step 0.
 401  
      * <p>
 402  
      * We can finish if we're on step 1 and have made a choice.
 403  
      * <p>
 404  
      * 
 405  
      * @return <code>true</code> if we can finish, otherwise
 406  
      *         <code>false</code>.
 407  
      * @see org.argouml.cognitive.critics.Wizard
 408  
      */
 409  
     @Override
 410  
     public boolean canFinish() {
 411  
 
 412  
         // Can't finish if our parent can't
 413  
 
 414  0
         if (!super.canFinish()) {
 415  0
             return false;
 416  
         }
 417  
 
 418  
         // Can finish if it's step 0
 419  
 
 420  0
         if (getStep() == 0) {
 421  0
             return true;
 422  
         }
 423  
 
 424  
         // Can finish if we're on step1 and have actually made a choice
 425  
 
 426  0
         if ((getStep() == 1) && (step1Choice != null)
 427  
                 && (step1Choice.getSelectedIndex() != -1)) {
 428  0
             return true;
 429  
         }
 430  
 
 431  
         // Otherwise we can't finish
 432  
 
 433  0
         return false;
 434  
     }
 435  
 
 436  
 }