Coverage Report - org.argouml.cognitive.ui.GoListToDecisionsToItems
 
Classes in this File Line Coverage Branch Coverage Complexity
GoListToDecisionsToItems
1%
1/53
0%
0/38
3.7
 
 1  
 /* $Id: GoListToDecisionsToItems.java 17818 2010-01-12 18:39:46Z 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  
  *    linus
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2008 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.ui;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.List;
 43  
 
 44  
 import javax.swing.event.TreeModelListener;
 45  
 import javax.swing.tree.TreePath;
 46  
 
 47  
 import org.argouml.cognitive.Decision;
 48  
 import org.argouml.cognitive.Designer;
 49  
 import org.argouml.cognitive.ToDoItem;
 50  
 import org.argouml.cognitive.ToDoList;
 51  
 
 52  
 
 53  
 /**
 54  
  * Rule for sorting the ToDo list: Decision -> Item.
 55  
  *
 56  
  */
 57  1800
 public class GoListToDecisionsToItems extends AbstractGoList2 {
 58  
 
 59  
     ////////////////////////////////////////////////////////////////
 60  
     // TreeModel implementation
 61  
 
 62  
 
 63  
     /*
 64  
      * @see javax.swing.tree.TreeModel#getChild(java.lang.Object, int)
 65  
      */
 66  
     public Object getChild(Object parent, int index) {
 67  0
         if (parent instanceof ToDoList) {
 68  0
             return getDecisionList().get(index);
 69  
         }
 70  0
         if (parent instanceof Decision) {
 71  0
             Decision dec = (Decision) parent;
 72  0
             List<ToDoItem> itemList = 
 73  
                 Designer.theDesigner().getToDoList().getToDoItemList();
 74  0
             synchronized (itemList) {
 75  0
                 for (ToDoItem item : itemList) {
 76  0
                     if (item.getPoster().supports(dec)) {
 77  0
                         if (index == 0) {
 78  0
                             return item;
 79  
                         }
 80  0
                         index--;
 81  
                     }
 82  
                 }
 83  0
             }
 84  
         }
 85  
 
 86  0
         throw new IndexOutOfBoundsException("getChild shouldn't get here "
 87  
                                             + "GoListToDecisionsToItems");
 88  
     }
 89  
 
 90  
     private int getChildCountCond(Object parent, boolean stopafterone) {
 91  0
         if (parent instanceof ToDoList) {
 92  0
             return getDecisionList().size();
 93  
         }
 94  0
         if (parent instanceof Decision) {
 95  0
             Decision dec = (Decision) parent;
 96  0
             int count = 0;
 97  0
             List<ToDoItem> itemList = 
 98  
                 Designer.theDesigner().getToDoList().getToDoItemList();
 99  0
             synchronized (itemList) {
 100  0
                 for (ToDoItem item : itemList) {
 101  0
                     if (item.getPoster().supports(dec)) {
 102  0
                         count++;
 103  
                     }
 104  0
                     if (stopafterone && count > 0) {
 105  0
                         break;
 106  
                     }
 107  
                 }
 108  0
             }
 109  0
             return count;
 110  
         }
 111  0
         return 0;
 112  
     }
 113  
 
 114  
     /*
 115  
      * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
 116  
      */
 117  
     public int getChildCount(Object parent) {
 118  0
         return getChildCountCond(parent, false);
 119  
     }
 120  
 
 121  
     /**
 122  
      * @param parent the object to check its offspring
 123  
      * @return the nr of children
 124  
      */
 125  
     private boolean hasChildren(Object parent) {
 126  0
         return getChildCountCond(parent, true) > 0;
 127  
     }
 128  
 
 129  
 
 130  
     /*
 131  
      * @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object,
 132  
      * java.lang.Object)
 133  
      */
 134  
     public int getIndexOfChild(Object parent, Object child) {
 135  0
         if (parent instanceof ToDoList) {
 136  0
             return getDecisionList().indexOf(child);
 137  
         }
 138  0
         if (parent instanceof Decision) {
 139  
             // instead of making a new list, decrement index, return when
 140  
             // found and index == 0
 141  0
             List<ToDoItem> candidates = new ArrayList<ToDoItem>();
 142  0
             Decision dec = (Decision) parent;
 143  0
             List<ToDoItem> itemList = 
 144  
                 Designer.theDesigner().getToDoList().getToDoItemList();
 145  0
             synchronized (itemList) {
 146  0
                 for (ToDoItem item : itemList) {
 147  0
                     if (item.getPoster().supports(dec)) {
 148  0
                         candidates.add(item);
 149  
                     }
 150  
                 }
 151  0
             }
 152  0
             return candidates.indexOf(child);
 153  
         }
 154  0
         return -1;
 155  
     }
 156  
 
 157  
     /*
 158  
      * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
 159  
      */
 160  
     public boolean isLeaf(Object node) {
 161  0
         if (node instanceof ToDoList) {
 162  0
             return false;
 163  
         }
 164  0
         if (node instanceof Decision && hasChildren(node)) {
 165  0
             return false;
 166  
         }
 167  0
         return true;
 168  
     }
 169  
 
 170  
     /*
 171  
      * @see javax.swing.tree.TreeModel#valueForPathChanged(
 172  
      * javax.swing.tree.TreePath, java.lang.Object)
 173  
      */
 174  0
     public void valueForPathChanged(TreePath path, Object newValue) { }
 175  
 
 176  
     /*
 177  
      * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
 178  
      */
 179  0
     public void addTreeModelListener(TreeModelListener l) { }
 180  
 
 181  
     /*
 182  
      * @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener)
 183  
      */
 184  0
     public void removeTreeModelListener(TreeModelListener l) { }
 185  
 
 186  
     /**
 187  
      * @return the decisions
 188  
      */    
 189  
     public List<Decision> getDecisionList() {
 190  0
         return Designer.theDesigner().getDecisionModel().getDecisionList();
 191  
     }
 192  
 
 193  
 
 194  
 }