Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GoListToOffenderToItem |
|
| 2.4444444444444446;2.444 |
1 | /* $Id: GoListToOffenderToItem.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 | * tfmorris | |
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.Collections; | |
43 | import java.util.List; | |
44 | ||
45 | import javax.swing.event.TreeModelListener; | |
46 | import javax.swing.tree.TreePath; | |
47 | ||
48 | import org.argouml.cognitive.Designer; | |
49 | import org.argouml.cognitive.ListSet; | |
50 | import org.argouml.cognitive.ToDoItem; | |
51 | import org.argouml.cognitive.ToDoList; | |
52 | import org.argouml.uml.PredicateNotInTrash; | |
53 | ||
54 | ||
55 | /** | |
56 | * Rule for sorting the ToDo list: Offender -> Item. | |
57 | * | |
58 | */ | |
59 | public class GoListToOffenderToItem extends AbstractGoList2 { | |
60 | ||
61 | private Object lastParent; | |
62 | ||
63 | private List<ToDoItem> cachedChildrenList; | |
64 | ||
65 | /** | |
66 | * The constructor. | |
67 | */ | |
68 | 1800 | public GoListToOffenderToItem() { |
69 | 1800 | setListPredicate((org.argouml.util.Predicate) new PredicateNotInTrash()); |
70 | 1800 | } |
71 | ||
72 | //////////////////////////////////////////////////////////////// | |
73 | // TreeModel implementation | |
74 | ||
75 | /* | |
76 | * @see javax.swing.tree.TreeModel#getChild(java.lang.Object, int) | |
77 | */ | |
78 | public Object getChild(Object parent, int index) { | |
79 | // TODO: This should only be building list up to 'index' | |
80 | 0 | return getChildrenList(parent).get(index); |
81 | } | |
82 | ||
83 | /* | |
84 | * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object) | |
85 | */ | |
86 | public int getChildCount(Object parent) { | |
87 | 0 | return getChildrenList(parent).size(); |
88 | } | |
89 | ||
90 | /* | |
91 | * @see javax.swing.tree.TreeModel#getIndexOfChild( | |
92 | * java.lang.Object, java.lang.Object) | |
93 | */ | |
94 | public int getIndexOfChild(Object parent, Object child) { | |
95 | 0 | return getChildrenList(parent).indexOf(child); |
96 | } | |
97 | ||
98 | /* | |
99 | * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object) | |
100 | */ | |
101 | public boolean isLeaf(Object node) { | |
102 | 0 | if (node instanceof ToDoList) { |
103 | 0 | return false; |
104 | } | |
105 | // TODO: This is a very expensive way to do this | |
106 | // if (getChildCount(node) > 0) { | |
107 | // return false; | |
108 | // } | |
109 | ||
110 | 0 | List<ToDoItem> itemList = |
111 | Designer.theDesigner().getToDoList().getToDoItemList(); | |
112 | 0 | synchronized (itemList) { |
113 | 0 | for (ToDoItem item : itemList) { |
114 | 0 | if (item.getOffenders().contains(node)) { |
115 | 0 | return false; |
116 | } | |
117 | } | |
118 | 0 | } |
119 | ||
120 | 0 | return true; |
121 | } | |
122 | ||
123 | ||
124 | /** | |
125 | * Get a list of children. Note that unlike its predecessor getChildren(), | |
126 | * this never returns null. If there are no children, it will return an | |
127 | * empty list. | |
128 | * | |
129 | * @param parent the parent object to check | |
130 | * @return a list of children for the given object | |
131 | */ | |
132 | public List<ToDoItem> getChildrenList(Object parent) { | |
133 | 0 | if (parent.equals(lastParent)) { |
134 | 0 | return cachedChildrenList; |
135 | } | |
136 | 0 | lastParent = parent; |
137 | 0 | ListSet<ToDoItem> allOffenders = new ListSet<ToDoItem>(); |
138 | 0 | ListSet<ToDoItem> designerOffenders = |
139 | Designer.theDesigner().getToDoList().getOffenders(); | |
140 | 0 | synchronized (designerOffenders) { |
141 | 0 | allOffenders.addAllElementsSuchThat(designerOffenders, |
142 | getPredicate()); | |
143 | 0 | } |
144 | ||
145 | 0 | if (parent instanceof ToDoList) { |
146 | 0 | cachedChildrenList = allOffenders; |
147 | 0 | return cachedChildrenList; |
148 | } | |
149 | ||
150 | //otherwise parent must be an offending design material | |
151 | 0 | if (allOffenders.contains(parent)) { |
152 | 0 | List<ToDoItem> result = new ArrayList<ToDoItem>(); |
153 | 0 | List<ToDoItem> itemList = |
154 | Designer.theDesigner().getToDoList().getToDoItemList(); | |
155 | 0 | synchronized (itemList) { |
156 | 0 | for (ToDoItem item : itemList) { |
157 | 0 | ListSet offs = new ListSet(); |
158 | 0 | offs.addAllElementsSuchThat(item.getOffenders(), |
159 | getPredicate()); | |
160 | 0 | if (offs.contains(parent)) { |
161 | 0 | result.add(item); |
162 | } | |
163 | 0 | } |
164 | 0 | } |
165 | 0 | cachedChildrenList = result; |
166 | 0 | return cachedChildrenList; |
167 | } | |
168 | 0 | cachedChildrenList = Collections.emptyList(); |
169 | 0 | return cachedChildrenList; |
170 | } | |
171 | ||
172 | /* | |
173 | * @see javax.swing.tree.TreeModel#valueForPathChanged( | |
174 | * javax.swing.tree.TreePath, java.lang.Object) | |
175 | */ | |
176 | public void valueForPathChanged(TreePath path, Object newValue) { | |
177 | 0 | } |
178 | ||
179 | /* | |
180 | * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener) | |
181 | */ | |
182 | public void addTreeModelListener(TreeModelListener l) { | |
183 | 0 | } |
184 | ||
185 | /* | |
186 | * @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener) | |
187 | */ | |
188 | public void removeTreeModelListener(TreeModelListener l) { | |
189 | 0 | } |
190 | ||
191 | ||
192 | } |