KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > suggestions > SuggestionList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.suggestions;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ListIterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.netbeans.modules.tasklist.core.TaskList;
31 import org.netbeans.modules.tasklist.core.TaskListView;
32 import org.netbeans.modules.tasklist.core.TaskNode;
33 import org.netbeans.modules.tasklist.core.Task;
34 import org.netbeans.modules.tasklist.client.SuggestionManager;
35
36 import org.openide.nodes.Node;
37 import org.openide.util.NbBundle;
38
39
40 /**
41  * A list of suggestions adding category grouping capabilities to
42  * orginary tasklist.
43  *
44  * @author Tor Norbye
45  */

46 public class SuggestionList extends TaskList {
47
48     /** Number of tasks we allow before for a type before dropping
49      * the task into a sublevel with a category task containing the
50      * tasks of that type.
51      */

52     private static final int MAX_INLINE = 20;
53
54     static final Object JavaDoc CATEGORY_NODE_SEED = new Object JavaDoc();
55
56     private final int groupTreshold;
57
58     /** Construct a new SuggestionManager instance. */
59     public SuggestionList() {
60         this(MAX_INLINE);
61     }
62
63     public SuggestionList(int groupTreshold) {
64         this.groupTreshold = groupTreshold;
65     }
66
67     synchronized SuggestionImpl getCategoryTask(SuggestionType type,
68                                                 boolean create) {
69         SuggestionImpl category = null;
70         if (categoryTasks != null) {
71             category = (SuggestionImpl)categoryTasks.get(type);
72         }
73         if (create && (category == null)) {
74             category = new SuggestionImpl(null,
75                 type.getLocalizedName(),type, null, CATEGORY_NODE_SEED);
76             category.setType(type.getName());
77             category.setIcon(type.getIconImage());
78             category.setVisitable(false);
79             // Don't duplicate the provider field! We don't want
80
// SMI.stuffCache to keep category task nodes stashed...
81
SuggestionManagerImpl manager =
82                 (SuggestionManagerImpl)SuggestionManager.getDefault();
83             if (manager.isExpandedType(type)) {
84                 // TODO it's callers reponsibity
85
// SuggestionsView view;
86
// if (getView() instanceof SuggestionsView) {
87
// view = (SuggestionsView)getView();
88
// } else {
89
// view = SuggestionsView.getCurrentView();
90
// }
91
// if (view != null) {
92
// manager.scheduleNodeExpansion(view,
93
// category);
94
// }
95
}
96
97             if (categoryTasks == null) {
98                 categoryTasks = new HashMap JavaDoc(20);
99             }
100             categoryTasks.put(type, category);
101             // Add the category in the given position
102
SuggestionImpl after = findAfter(type);
103             if (after != null) {
104                 addCategory(category, after);
105             } else {
106                 addCategory(category, false);
107             }
108         }
109         return category;
110     }
111
112     /** Add a task to the task list.
113      * @param task The task to be added.
114      * @param after The task which will be immediately before
115      * the new subtask after the addition (e.g. add
116      * this subtask directly AFTER the specified
117      * task)
118      * @deprecated use Task.addSubtask(Task subtask, Task after) instead
119      */

120     private void addCategory(Task task, Task after) {
121         if (task.getParent() == null) { //XXX null more often than before
122
appendTask(task);
123         } else {
124             Task parent = task.getParent();
125             // User insert: prepend to the list
126
parent.removeSubtask(task);
127             parent.addSubtask(task, after);
128         }
129     }
130
131
132     /** Add a task to the task list.
133      * @param task The task to be added.
134      * @param append If true, append the item to the list, otherwise prepend
135      * @deprecated use Task.addSubtask(Task subtask, boolean append) instead
136      */

137     private void addCategory(Task task, boolean append) { //XXX null more often than before
138
if (task.getParent() == null) {
139             appendTask(task);
140         } else {
141             // it's really funny contruct (XXX probably want to add it to list end)
142
Task parent = task.getParent();
143             parent.removeSubtask(task);
144             parent.addSubtask(task, append);
145         }
146     }
147
148
149     private Map JavaDoc categoryTasks = null;
150
151     /** Return the task that we need to put this new category type
152      * immadiately following. */

153     SuggestionImpl findAfter(SuggestionType type) {
154         SuggestionImpl after = null;
155         int pos = type.getPosition();
156         Iterator JavaDoc it = getTasks().iterator();
157         int i = 0;
158         while (it.hasNext()) {
159             i++;
160             SuggestionImpl s = (SuggestionImpl)it.next();
161             int spos = s.getSType().getPosition();
162             if (spos > pos) {
163                 break;
164             } else {
165                 after = s;
166             }
167         }
168
169         // #42618 hotfix
170
if (i == getTasks().size()) {
171             return null;
172         } else {
173             return after;
174         }
175     }
176
177     /** Remove the given category node, if unused.
178         @param force If true, remove the category node even if it has subtasks
179     */

180     synchronized void removeCategory(SuggestionImpl category, boolean force) {
181         //SuggestionImpl category = (SuggestionImpl)s.getParent();
182
if ((category != null) && (force || !category.hasSubtasks())) {
183             category.getParent().removeSubtask(category);
184             categoryTasks.remove(category.getSType());
185         }
186     }
187
188     synchronized void removeCategory(SuggestionType type) {
189         if (getTasks().size() == 0) {
190             categoryTasks = null;
191             return;
192         }
193         Iterator JavaDoc ti = getTasks().iterator();
194         ArrayList JavaDoc removeTasks = new ArrayList JavaDoc(50);
195         while (ti.hasNext()) {
196             SuggestionImpl suggestion = (SuggestionImpl)ti.next();
197             if (suggestion.getSType() == type) {
198                 removeTasks.add(suggestion);
199             }
200         }
201         addRemove(null, removeTasks, false, null, null);
202         if (categoryTasks != null) {
203             categoryTasks.remove(type);
204         }
205    }
206
207
208     /** Return the set of category tasks (SuggestionImpl objects) */
209     public Collection JavaDoc getCategoryTasks() {
210         if (categoryTasks != null) {
211             return categoryTasks.values();
212         }
213         return null;
214     }
215
216     void clearCategoryTasks() {
217         categoryTasks = null; // recreate such that they get reinserted etc.
218
}
219
220
221     /**
222      * Gets number of items then displays inlined once
223      * exceeded it's grouped (by category).
224      * @return
225      */

226     final int getGroupTreshold() {
227         return groupTreshold; // TODO #37068 turn into user's option
228
}
229
230 }
231
Popular Tags