Coverage Report - org.argouml.cognitive.critics.Wizard
 
Classes in this File Line Coverage Branch Coverage Complexity
Wizard
23%
11/47
0%
0/20
1.409
 
 1  
 /* $Id: Wizard.java 17816 2010-01-12 18:34:29Z 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.cognitive.critics;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.List;
 43  
 
 44  
 import javax.swing.JPanel;
 45  
 
 46  
 
 47  
 /**
 48  
  * "Abstract" base class for non-modal wizards.  Each subclass should
 49  
  * define its own makeNextPanel methods. Because most
 50  
  * wizards will not be run to completion, the panels are constructed
 51  
  * only as needed. This implies that Wizards should not initialize
 52  
  * many instance variables in their constructors.<p>
 53  
  *
 54  
  * By convention step 0 is the problem description of the ToDoItem,
 55  
  * step 1 is the first panel displayed after the user presses
 56  
  * "Next>", and so on.  The problem description panel is not stored in
 57  
  * this wizard, only the panels that are specific to the wizard are
 58  
  * stored. If the user presses "Back>" enough times to get back to the
 59  
  * problem description, backPanel should return null.  A null panel
 60  
  * indicates that the problem description should be shown. <p>
 61  
  *
 62  
  * Several of the comments in this class refer to "context".  Context
 63  
  * is the data about this execution of this wizard, for example, values
 64  
  * that the user enters in step 1 is part of the context of later steps,
 65  
  * and the ToDoItem with its offenders Set is always context.  Most
 66  
  * context should be stored in instance variables of Wizard subclasses.
 67  
  *
 68  
  * @author jrobbins
 69  
  */
 70  
 public abstract class Wizard implements java.io.Serializable {
 71  
 
 72  
     /** User interface panels displayed so far. */
 73  47491
     private List<JPanel> panels = new ArrayList<JPanel>();
 74  
 
 75  
     /** The current step that the Wizard is on.  Zero indicates that the
 76  
      *  wizard has not yet begun. */
 77  47491
     private int step = 0;
 78  
 
 79  
     /** True when the wizard has done everything it can. */
 80  47491
     private boolean finished = false;
 81  47491
     private boolean started = false;
 82  
 
 83  47491
     private WizardItem item = null;
 84  
 
 85  
     /** Construct a new wizard to help the user repair a design flaw. */
 86  47491
     public Wizard() {
 87  47491
     }
 88  
 
 89  
     /**
 90  
      * @param s the step number of the panel to be removed
 91  
      */
 92  
     protected void removePanel(int s) {
 93  0
         panels.remove(s);
 94  0
     }
 95  
 
 96  
     /**
 97  
      * Setter for the todoitem.
 98  
      *
 99  
      * @param i the todoitem
 100  
      */
 101  
     public void setToDoItem(WizardItem i) {
 102  47491
         item = i;
 103  47491
     }
 104  
 
 105  
     /**
 106  
      * @return the todoitem
 107  
      */
 108  
     public WizardItem getToDoItem() {
 109  47491
         return item;
 110  
     }
 111  
 
 112  
     /** An integer between 0 and 100, shows percent done. The current
 113  
      *  ArgoUML user interface shows different PostIt note icons for
 114  
      *  0, 1-25, 26-50. 51-75, and 76-100.
 115  
      *  @return the percentage done.
 116  
      */
 117  
     public int getProgress() {
 118  0
         return step * 100 / getNumSteps();
 119  
     }
 120  
 
 121  
     /** Get the number of steps in this wizard.  Subclasses should
 122  
      *  override to return a constant, or compute based on context.
 123  
      *  @return the number of steps in this wizard.
 124  
      */
 125  
     public abstract int getNumSteps();
 126  
 
 127  
     /** Get the panel that should be displayed now.  Usually called
 128  
      *  after the user pressed "Next&gt;" and next() has returned, or after
 129  
      *  the user pressed "&lt;Back" and back() has returned.  Also called
 130  
      *  when the user turns away from the wizard to do something else and
 131  
      *  then returns his or her attention to the wizard.
 132  
      *  @return the panel that should be displayed now.
 133  
      */
 134  
     public JPanel getCurrentPanel() {
 135  0
         return getPanel(step);
 136  
     }
 137  
 
 138  
 
 139  
     /** Get the exising panel at step s. Step 1 is the first wizard
 140  
      *  panel.
 141  
      * @param s the step
 142  
      * @return the panel for step s or null if none.
 143  
      */
 144  
     public JPanel getPanel(int s) {
 145  0
         if (s > 0 && s <= panels.size()) {
 146  0
             return panels.get(s - 1);
 147  
         }
 148  0
         return null;
 149  
     }
 150  
 
 151  
     ////////////////////////////////////////////////////////////////
 152  
     // wizard actions
 153  
 
 154  
     /** Return true iff the "Next&gt;" button should be enabled.
 155  
      *  Subclasses should override to first check super.nextEnabled()
 156  
      *  and then check for legal context values.
 157  
      *  @return <code>true</code> iff the "Next&gt;" button should be enabled.
 158  
      */
 159  
     public boolean canGoNext() {
 160  0
         return step < getNumSteps();
 161  
     }
 162  
 
 163  
     /**
 164  
      * The next step of the wizard.
 165  
      */
 166  
     public void next() {
 167  0
         doAction(step);
 168  0
         step++;
 169  0
         JPanel p = makePanel(step);
 170  0
         if (p != null) {
 171  0
             panels.add(p);
 172  
         }
 173  0
         started = true;
 174  0
         if (item != null) {
 175  0
             item.changed();
 176  
         }
 177  0
     }
 178  
 
 179  
     /**
 180  
      * @return true if we can step back
 181  
      */
 182  
     public boolean canGoBack() {
 183  0
         return step > 0;
 184  
     }
 185  
 
 186  
     /**
 187  
      * Step back.
 188  
      */
 189  
     public void back() {
 190  0
         step--;
 191  0
         if (step < 0) step = 0;
 192  0
         undoAction(step);
 193  0
         if (item != null) item.changed();
 194  0
     }
 195  
 
 196  
     /**
 197  
      * @return true if we can finish (i.e. the finish button is not downlighted)
 198  
      */
 199  
     public boolean canFinish() {
 200  0
         return true;
 201  
     }
 202  
 
 203  
     /**
 204  
      * @return true if the wizard is started
 205  
      */
 206  
     public boolean isStarted() {
 207  12133
         return started;
 208  
     }
 209  
 
 210  
     /**
 211  
      * @return true if the wizard is finished
 212  
      */
 213  
     public boolean isFinished() {
 214  0
         return finished;
 215  
     }
 216  
 
 217  
     /**
 218  
      * Finish the wizard.
 219  
      */
 220  
     public void finish() {
 221  0
         started = true;
 222  0
         int numSteps = getNumSteps();
 223  0
         for (int i = step; i <= numSteps; i++) {
 224  0
             doAction(i);
 225  0
             if (item != null) item.changed();
 226  
         }
 227  
         // TODO: do all following steps
 228  
         // TODO: resolve item from ToDoList
 229  0
         finished = true;
 230  0
     }
 231  
 
 232  
     /** Create a new panel for the given step. For example, When the
 233  
      *  given step is 1, create the first step of the wizard. <p>
 234  
      *
 235  
      *  TODO: It might be convient to make a reusable
 236  
      *  subclass of Wizard that shows all textual steps to guide the
 237  
      *  user without any automation.  Such a Wizard could be easily
 238  
      *  authored, stored in an XML file, and efficiently presented by
 239  
      *  reusing a single panel with a single JTextArea.
 240  
      *
 241  
      *  @param newStep the number of the step to make a panel for.
 242  
      *  @return a new panel for the given step
 243  
      */
 244  
     public abstract JPanel makePanel(int newStep);
 245  
 
 246  
     /** Take action at the completion of a step. For example, when the
 247  
      *  given step is 0, do nothing; and when the given step is 1, do
 248  
      *  the first action.  Argo non-modal wizards should take action as
 249  
      *  they do along, as soon as possible, they should not wait until
 250  
      *  the final step. Also, if the user pressed "Finish" doAction may
 251  
      *  be called for steps that never constructored or displayed their
 252  
      *  panels.
 253  
      *
 254  
      * @param oldStep the given step
 255  
      */
 256  
     public abstract void doAction(int oldStep);
 257  
 
 258  
     /**
 259  
      * Do the action of this wizard.
 260  
      */
 261  0
     public void doAction() { doAction(step); }
 262  
 
 263  
     /** Undo the action done after the given step. For example, when the
 264  
      *  given step is 0, nothing was done, so nothing can be undone; and
 265  
      *  when the given step is 1, undo the first action.  Undo allows
 266  
      *  users to work part way through fixing a problem, see the partial
 267  
      *  result, and explore a different alternative.
 268  
      *
 269  
      * @param oldStep the given step
 270  
      */
 271  
     public void undoAction(int oldStep) {
 272  0
     }
 273  
 
 274  
     /**
 275  
      * Undo the action.
 276  
      */
 277  0
     public void undoAction() { undoAction(step); }
 278  
 
 279  
     /**
 280  
      * @return Returns the step.
 281  
      */
 282  
     protected int getStep() {
 283  0
         return step;
 284  
     }
 285  
 }