KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > suggestions > ui > SuggestionsView


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.ui;
21
22 import javax.swing.ButtonGroup JavaDoc;
23 import org.netbeans.modules.tasklist.core.table.ChooseColumnsPanel;
24 import org.openide.util.NbBundle;
25 import org.openide.util.Utilities;
26 import org.openide.nodes.Node;
27 import org.openide.util.actions.SystemAction;
28 import org.openide.text.Line;
29 import org.netbeans.modules.tasklist.core.*;
30 import org.netbeans.modules.tasklist.core.filter.Filter;
31 import org.netbeans.modules.tasklist.core.filter.FilterAction;
32 import org.netbeans.modules.tasklist.core.filter.RemoveFilterAction;
33 import org.netbeans.modules.tasklist.client.*;
34
35 import java.util.Iterator JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.awt.*;
40 import javax.swing.BorderFactory JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.JTabbedPane JavaDoc;
44 import javax.swing.JTable JavaDoc;
45 import javax.swing.JToggleButton JavaDoc;
46 import javax.swing.JToolBar JavaDoc;
47 import javax.swing.SwingUtilities JavaDoc;
48 import javax.swing.border.Border JavaDoc;
49 import org.netbeans.modules.tasklist.core.export.ExportAction;
50 import org.netbeans.modules.tasklist.suggestions.settings.ManagerSettings;
51
52 import org.netbeans.modules.tasklist.suggestions.*;
53
54 /**
55  * View showing the todo list items
56  *
57  * @author Tor Norbye
58  */

59 public class SuggestionsView extends TaskListView implements SuggestionView {
60
61     private static final long serialVersionUID = 1;
62
63     private static SuggestionsView theView;
64     
65     /**
66      * Create the live suggestion view.
67      * @return
68      */

69     public static SuggestionsView createSuggestionsView() {
70         if (theView == null) {
71             theView = new SuggestionsView();
72         }
73         return theView;
74     }
75
76     public final static String JavaDoc CATEGORY = "suggestions"; // NOI18N
77

78     private String JavaDoc originalName;
79     private String JavaDoc originalIcon;
80
81     /** Iff true live view otherwise snapshot. */
82     private boolean scan = false;
83     private SuggestionsBroker.Job broker;
84
85     private SuggestionType showingType = null;
86
87     /**
88      * Creates default suggestions view
89      */

90     public SuggestionsView() {
91         this(CATEGORY,
92             NbBundle.getMessage(SuggestionsView.class, "SuggestionsView"), // NOI18N
93
null,
94             true,
95             "org/netbeans/modules/tasklist/suggestions/suggestion.gif" // NOI18N
96
);
97         scan = true;
98     }
99
100     /**
101      * Construct a Scan view with the given window title, and the given
102      * list to show the contents in
103      *
104      * @param category The category of this window
105      * @param name The name of the window
106      * @param list The tasklist to store the scanned tasks in
107      * @param persistent should this view be reconstructed after NB restart?
108      * @param icon icon for this view
109      */

110     public SuggestionsView(String JavaDoc category, String JavaDoc name, TaskList list,
111         boolean persistent, String JavaDoc icon) {
112         super(
113               category,
114               name,
115               // I made a taskView.png, but it was larger (286 bytes) than the
116
// gif (186 bytes). More importantly, it had ugly display artifacts.
117
Utilities.loadImage(icon),
118               persistent,
119               list);
120
121         this.originalName = name;
122         this.originalIcon = icon;
123
124         // When the tab is alone in a container, don't show a tab;
125
// the category nodes provide enough feedback.
126
putClientProperty("TabPolicy", "HideWhenAlone"); // NOI18N
127
}
128     
129     static final String JavaDoc PROP_SUGG_DETAILS = "suggDetails"; // NOI18N
130
static final String JavaDoc PROP_SUGG_PRIO = "suggPrio"; // NOI18N
131
static final String JavaDoc PROP_SUGG_FILE = "suggFile"; // NOI18N
132
static final String JavaDoc PROP_SUGG_LINE = "suggLine"; // NOI18N
133
static final String JavaDoc PROP_SUGG_CAT = "suggCat"; // NOI18N
134
static final String JavaDoc PROP_SUGG_LOC = "suggLoc"; // NOI18N
135

136     protected ColumnProperty[] createColumns() {
137         // No point allowing other attributes of the task since that's
138
// all we support for scan items (they are not created by
139
// the user - and they are not persisted.
140
return new ColumnProperty[] {
141             getMainColumn(800),
142             getPriorityColumn(true, 100),
143             getDetailsColumn(false, 800),
144             getFileColumn(true, 150),
145             getLineColumn(true, 50),
146             getCategoryColumn(true, 150)
147         };
148     };
149
150     public ColumnProperty getMainColumn(int width) {
151         // Tree column
152
// NOTE: Task.getDisplayName() must also be kept in sync here
153
return new ColumnProperty(
154         0, // UID -- never change (part of serialization
155
SuggestionImplProperties.PROP_SUMMARY,
156         true,
157             width
158         );
159     }
160     
161     public ColumnProperty getPriorityColumn(boolean visible, int width) {
162         return new ColumnProperty(
163         1, // UID -- never change (part of serialization
164
SuggestionImplProperties.PROP_PRIORITY,
165             true,
166             visible,
167             width
168             );
169     }
170
171     public ColumnProperty getFileColumn(boolean visible, int width) {
172         return new ColumnProperty(
173         2, // UID -- never change (part of serialization
174
SuggestionImplProperties.PROP_FILENAME,
175             true,
176             visible,
177             width
178             );
179     }
180
181     public ColumnProperty getLineColumn(boolean visible, int width) {
182         return new ColumnProperty(
183         3, // UID -- never change (part of serialization
184
SuggestionImplProperties.PROP_LINE_NUMBER,
185             true,
186             visible,
187             width
188             );
189     }
190
191     public ColumnProperty getCategoryColumn(boolean visible, int width) {
192         return new ColumnProperty(
193         4, // UID -- never change (part of serialization
194
SuggestionImplProperties.PROP_CATEGORY,
195             true,
196             visible,
197             width
198             );
199     }
200
201     public ColumnProperty getDetailsColumn(boolean visible, int width) {
202         return new ColumnProperty(
203         5, // UID -- never change (part of serialization
204
SuggestionImplProperties.PROP_DETAILS,
205             true,
206             visible,
207             width
208             );
209     }
210
211     protected Component createNorthComponent() {
212         SystemAction[] actions = new SystemAction[] {
213             SystemAction.get(FixAction.class),
214             SystemAction.get(ShowSuggestionAction.class),
215             SystemAction.get(FilterAction.class),
216             null,
217             SystemAction.get(RemoveFilterAction.class),
218             SystemAction.get(ShowCategoryAction.class),
219             SystemAction.get(EditTypesAction.class),
220             SystemAction.get(DisableAction.class),
221             null,
222             SystemAction.get(ExportAction.class)
223         };
224
225         JToolBar JavaDoc toolbar = SystemAction.createToolbarPresenter(actions);
226         toolbar.setFloatable(false);
227         toolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE); // NOI18N
228

229         toolbar.setFloatable(false);
230         // TODO: toolbar.setLayout(new ToolbarLayout());
231

232         ButtonGroup JavaDoc bg = new ButtonGroup JavaDoc();
233         JToggleButton JavaDoc cur = new JToggleButton JavaDoc("Current File", true); // TODO: NOI18N
234
bg.add(cur);
235         JToggleButton JavaDoc openedFiles =
236                 new JToggleButton JavaDoc("Opened Projects", false); // TODO: NOI18N
237
bg.add(openedFiles);
238         JToggleButton JavaDoc openedProjects =
239                 new JToggleButton JavaDoc("Opened Files", false); // TODO: NOI18N
240
bg.add(openedProjects);
241         
242         toolbar.add(cur, 0);
243         toolbar.add(openedFiles, 1); // TODO: NOI18N
244
toolbar.add(openedProjects, 2); // TODO: NOI18N
245

246         return toolbar;
247     }
248    
249     protected String JavaDoc preferredID() {
250         return getClass().getName();
251     }
252     
253     public void readExternal(java.io.ObjectInput JavaDoc objectInput) throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
254         int version = objectInput.readInt(); // IOExceptions means even older unversioned serialization
255
if (version == 1) {
256             scan = objectInput.readBoolean();
257             theView = this;
258         }
259         super.readExternal(objectInput);
260         category = CATEGORY;
261     }
262
263     public void writeExternal(java.io.ObjectOutput JavaDoc objectOutput) throws java.io.IOException JavaDoc {
264         objectOutput.writeInt(1); // version
265
objectOutput.writeBoolean(this == theView); // theView
266
super.writeExternal(objectOutput);
267         ObservableList list = getModel();
268         if (list != null) {
269             flushExpansion();
270             ManagerSettings.getDefault().store();
271         }
272     }
273
274     protected void componentHidden() {
275         super.componentHidden();
276         // Update expansion state before we remove the nodes
277
ObservableList list = getModel();
278         if (list != null) {
279             flushExpansion();
280         }
281         getManager().dispatchStop();
282
283         if (scan) {
284             broker.stopBroker(); // created in componentShowing
285
setModel(null);
286         }
287     }
288
289     protected void componentShowing() {
290         super.componentShowing();
291         getManager().dispatchRun();
292         if (scan) {
293             broker = SuggestionsBroker.getDefault().startBroker(ProviderAcceptor.ALL);
294             setModel(broker.getSuggestionsList());
295         }
296     }
297
298     protected void componentOpened() {
299         super.componentOpened();
300         getManager().dispatchPrepare();
301     }
302     
303     protected void componentClosed() {
304         super.componentClosed();
305         getManager().notifyViewClosed();
306         theView = null;
307     }
308     
309     protected Node createRootNode() {
310         return new TaskListNode(getModel());
311     }
312
313     // Ensure that we clear the filter icon and label if a user-entered
314
// filter (not by the ShowCategoryAction) is added
315
public void setFilter(Filter f) {
316         super.setFilter(f);
317         notifyFiltered(null);
318     }
319
320     /**
321      * Notify view that a new filter for the given type is in effect
322      * This method is thread safe.
323      */

324     void notifyFiltered(SuggestionType type) {
325         showingType = type;
326         final Image icon;
327         final String JavaDoc title;
328         if (type != null) {
329             icon = type.getIconImage();
330             title = type.getLocalizedName();
331         } else {
332             title = originalName;
333             icon = Utilities.loadImage(originalIcon);
334         }
335         
336         SwingUtilities.invokeLater(new Runnable JavaDoc() {
337             public void run() {
338                 setName(title);
339                 setIcon(icon);
340             }
341         });
342     }
343
344     /**
345      * Returns the live view managed by manager.
346      * <p>
347      * If you need to locate scanned view
348      * you must use retrieve it from context (e.g. SuggestionNode).
349      *
350      * @return the view or null
351      */

352     public static SuggestionsView getCurrentView() {
353         return theView;
354     }
355
356     protected TaskAnnotation getAnnotation(Task task) {
357         return new SuggestionAnno(task, this);
358     }
359
360     /**
361      * Set the scanning status of the window. While the view is scanning,
362      * show that it's busy.
363      * This method is thread safe.
364      */

365     void setScanning(boolean scanning) {
366         final Image icon;
367         if (scanning) {
368             icon = Utilities.loadImage(
369                 "org/netbeans/modules/tasklist/suggestions/scanning.gif");
370         } else {
371             if (showingType != null) {
372                 icon = showingType.getIconImage();
373             } else {
374                 icon = Utilities.loadImage(originalIcon);
375             }
376         }
377         SwingUtilities.invokeLater(new Runnable JavaDoc() {
378             public void run() {
379                 setIcon(icon);
380             }
381         });
382     }
383
384     public String JavaDoc toString() {
385         return "SuggestionsView@" + hashCode();
386     }
387
388     private SuggestionList getSuggestionList() {
389         return (SuggestionList) getList();
390     }
391
392     // SuggestionView interface ~~~~~~~~~~~~~~~~~~~~~~~~~~~
393

394     public boolean isObserved(String JavaDoc category) {
395         // XXX filters
396
return isShowing();
397     }
398
399     public SuggestionList getSuggestionsModel() {
400         return getSuggestionList();
401     }
402
403     /** For the category tasks, update the expansion state setting. */
404     private void flushExpansion() {
405         Collection JavaDoc categoryTasks = getSuggestionList().getCategoryTasks();
406         if (categoryTasks == null) {
407             return;
408         }
409         SuggestionManagerImpl manager =
410             (SuggestionManagerImpl)SuggestionManager.getDefault();
411         Node root = getEffectiveRoot();
412         Iterator JavaDoc it = categoryTasks.iterator();
413         while (it.hasNext()) {
414             SuggestionImpl s = (SuggestionImpl)it.next();
415             Node n = TaskNode.find(root, s);
416             if (n == null) {
417                 continue;
418             }
419             SuggestionType type = s.getSType();
420             boolean expanded = isExpanded(n);
421             if (expanded) {
422                 manager.setExpandedType(type, true);
423             } else if (manager.isExpandedType(type)) {
424                 // Only set it to false if it's already recorded to be true
425
manager.setExpandedType(type, false);
426             }
427         }
428     }
429
430     // XXX unused was attached to caret listener
431

432     Line prevLine = null;
433     List JavaDoc erase = null;
434     List JavaDoc origIcon = null;
435
436     /**
437      * Set the current cursor line to the given line position.
438      * Suggestions on the given line will be highlighted.
439      *
440      * @param line The current line of the cursor.
441      */

442     private void setCursorLine(Line line) {
443         if (line == prevLine) {
444             return;
445         }
446         prevLine = line;
447
448         // Clear out previously highlighted items
449
if (erase != null) {
450             Iterator JavaDoc it = erase.iterator();
451             Iterator JavaDoc itorig = origIcon.iterator();
452             while (it.hasNext()) {
453                 SuggestionImpl s = (SuggestionImpl) it.next();
454                 Image icon = (Image) itorig.next();
455                 s.setIcon(icon);
456                 //s.setHighlighted(false);
457
}
458         }
459         erase = null;
460         origIcon = null;
461
462
463         if (line == null) {
464             // Prevent line==null from highlighting all suggestions
465
// without an associated line position...
466
return;
467         }
468
469         SuggestionsView view = SuggestionsView.getCurrentView();
470         if (view != null) {
471             Node node = view.getEffectiveRoot();
472             highlightNode(node, line);
473         }
474     }
475
476     /** Set a series of suggestions as highlighted. Or, clear the current
477      * selection of highlighted nodes.
478      * <p>
479      * @param suggestions List of suggestions that should be highlighted.
480      * If null, the selection is cleared.
481      * @param type The type for which this selection applies. Each type
482      * has its own, independent set of highlighted suggestions. If
483      * null, it applies to all types.
484      *
485      */

486     private void highlightNode(Node node, Line line) {
487         SuggestionImpl s = (SuggestionImpl) TaskNode.getTask(node);
488         if (s.getLine() == line) {
489             if (erase == null) {
490                 origIcon = new ArrayList JavaDoc(20);
491                 erase = new ArrayList JavaDoc(20);
492             }
493             origIcon.add(s.getIcon());
494             //s.setHighlighted(true);
495
Image badge = Utilities.loadImage(
496                 "org/netbeans/modules/tasklist/suggestions/badge.gif"); // NOI18N
497
Image image = Utilities.mergeImages(s.getIcon(), badge,
498                     0, 0);
499             s.setIcon(image);
500             erase.add(s);
501         }
502
503         // Recurse?
504
if (s.hasSubtasks() && (isExpanded(node))) {
505             Node[] nodes = node.getChildren().getNodes();
506             int n = (nodes != null) ? nodes.length : 0;
507             for (int i = 0; i < n; i++) {
508                 highlightNode(nodes[i], line);
509             }
510         }
511     }
512
513     private SuggestionManagerImpl getManager() {
514         return (SuggestionManagerImpl)SuggestionManager.getDefault();
515     }
516     
517     public Filter createFilter() {
518         return new SuggestionFilter("Simple"); // NOI18N
519
}
520     
521     protected Component createCenterComponent() {
522         Component cmp = super.createCenterComponent();
523         JTabbedPane JavaDoc tp = new JTabbedPane JavaDoc();
524         tp.addTab("Current File", cmp); // TODO: i18n
525
JTable JavaDoc t = new SuggestionsTable();
526         JScrollPane JavaDoc sp = new JScrollPane JavaDoc(t);
527         ChooseColumnsPanel.installChooseColumnsButton(sp);
528         tp.addTab("Open Projects", sp);// TODO: i18n
529
return tp;
530     }
531
532 }
533
Popular Tags