Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ToDoPerspective |
|
| 2.875;2.875 |
1 | /* $Id: ToDoPerspective.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 | * bobtarling | |
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.ui; | |
40 | ||
41 | import java.util.ArrayList; | |
42 | import java.util.List; | |
43 | ||
44 | import org.apache.log4j.Logger; | |
45 | import org.argouml.cognitive.ToDoItem; | |
46 | import org.argouml.ui.TreeModelComposite; | |
47 | ||
48 | /** | |
49 | * This class represents a todo tree model / perspective.<p> | |
50 | * | |
51 | * A todo tree model / perspective is a collection of GoRules. | |
52 | */ | |
53 | public abstract class ToDoPerspective extends TreeModelComposite { | |
54 | ||
55 | /** | |
56 | * Logger. | |
57 | */ | |
58 | 900 | private static final Logger LOG = Logger.getLogger(ToDoPerspective.class); |
59 | ||
60 | //////////////////////////////////////////////////////////////// | |
61 | // instance variables | |
62 | ||
63 | /** | |
64 | * todoList specific. | |
65 | */ | |
66 | private boolean flat; | |
67 | ||
68 | /** | |
69 | * todoList specific. | |
70 | */ | |
71 | private List<ToDoItem> flatChildren; | |
72 | ||
73 | /** | |
74 | * The constructor. | |
75 | * | |
76 | * @param name the name that will be localized | |
77 | */ | |
78 | public ToDoPerspective(String name) { | |
79 | ||
80 | 5400 | super(name); |
81 | 5400 | flatChildren = new ArrayList<ToDoItem>(); |
82 | 5400 | } |
83 | ||
84 | //////////////////////////////////////////////////////////////// | |
85 | // TreeModel implementation - todo specific stuff | |
86 | ||
87 | /** | |
88 | * Finds the each of the children of a parent in the tree. | |
89 | * | |
90 | * @param parent in the tree | |
91 | * @param index of child to find | |
92 | * @return the child found at index. Null if index is out of bounds. | |
93 | */ | |
94 | @Override | |
95 | public Object getChild(Object parent, int index) { | |
96 | 6791 | if (flat && parent == getRoot()) { |
97 | 0 | return flatChildren.get(index); |
98 | } | |
99 | 6791 | return super.getChild(parent, index); |
100 | } | |
101 | ||
102 | /* | |
103 | * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object) | |
104 | */ | |
105 | @Override | |
106 | public int getChildCount(Object parent) { | |
107 | 10223 | if (flat && parent == getRoot()) { |
108 | 0 | return flatChildren.size(); |
109 | } | |
110 | 10223 | return super.getChildCount(parent); |
111 | } | |
112 | ||
113 | /* | |
114 | * @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object, | |
115 | * java.lang.Object) | |
116 | */ | |
117 | @Override | |
118 | public int getIndexOfChild(Object parent, Object child) { | |
119 | 1967 | if (flat && parent == getRoot()) { |
120 | 0 | return flatChildren.indexOf(child); |
121 | } | |
122 | 1967 | return super.getIndexOfChild(parent, child); |
123 | } | |
124 | ||
125 | // ------------ other methods ------------ | |
126 | ||
127 | /** | |
128 | * todoList specific. | |
129 | * | |
130 | * @param b true if flat | |
131 | */ | |
132 | public void setFlat(boolean b) { | |
133 | 0 | flat = false; |
134 | 0 | if (b) { |
135 | 0 | calcFlatChildren(); |
136 | } | |
137 | 0 | flat = b; |
138 | 0 | } |
139 | ||
140 | /** | |
141 | * todoList specific. | |
142 | * | |
143 | * @return the flatness: true if flat | |
144 | */ | |
145 | public boolean getFlat() { | |
146 | 0 | return flat; |
147 | } | |
148 | ||
149 | /** | |
150 | * TodoList specific. | |
151 | */ | |
152 | public void calcFlatChildren() { | |
153 | 0 | flatChildren.clear(); |
154 | 0 | addFlatChildren(getRoot()); |
155 | 0 | } |
156 | ||
157 | /** | |
158 | * TodoList specific. | |
159 | * | |
160 | * @param node the object to be added | |
161 | */ | |
162 | public void addFlatChildren(Object node) { | |
163 | 0 | if (node == null) { |
164 | 0 | return; |
165 | } | |
166 | 0 | LOG.debug("addFlatChildren"); |
167 | // hack for to do items only, should check isLeaf(node), but that | |
168 | // includes empty folders. Really I need alwaysLeaf(node). | |
169 | 0 | if ((node instanceof ToDoItem) && !flatChildren.contains(node)) { |
170 | 0 | flatChildren.add((ToDoItem) node); |
171 | } | |
172 | ||
173 | 0 | int nKids = getChildCount(node); |
174 | 0 | for (int i = 0; i < nKids; i++) { |
175 | 0 | addFlatChildren(getChild(node, i)); |
176 | } | |
177 | 0 | } |
178 | ||
179 | } |