KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > UserTasksTreeTable


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  * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks;
21
22 import com.toedter.calendar.JDateChooserCellEditor;
23 import java.awt.Graphics JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.datatransfer.DataFlavor JavaDoc;
26 import java.awt.datatransfer.StringSelection JavaDoc;
27 import java.awt.datatransfer.Transferable JavaDoc;
28 import java.awt.dnd.DropTarget JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.MouseAdapter JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.io.Serializable JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import javax.swing.Action JavaDoc;
38 import javax.swing.ImageIcon JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.JPopupMenu JavaDoc;
41 import javax.swing.JScrollPane JavaDoc;
42 import javax.swing.ListSelectionModel JavaDoc;
43 import javax.swing.SwingUtilities JavaDoc;
44 import javax.swing.TransferHandler JavaDoc;
45 import javax.swing.event.ListSelectionEvent JavaDoc;
46 import javax.swing.event.ListSelectionListener JavaDoc;
47 import javax.swing.table.TableColumnModel JavaDoc;
48 import javax.swing.tree.TreePath JavaDoc;
49 import org.netbeans.modules.tasklist.core.export.ExportAction;
50 import org.netbeans.modules.tasklist.core.export.ImportAction;
51 import org.netbeans.modules.tasklist.core.filter.Filter;
52 import org.netbeans.modules.tasklist.core.filter.FilterAction;
53 import org.netbeans.modules.tasklist.core.filter.RemoveFilterAction;
54 import org.netbeans.modules.tasklist.usertasks.actions.CollapseAllAction;
55 import org.netbeans.modules.tasklist.usertasks.actions.ExpandAllUserTasksAction;
56 import org.netbeans.modules.tasklist.usertasks.actions.GoToUserTaskAction;
57 import org.netbeans.modules.tasklist.usertasks.actions.PauseAction;
58 import org.netbeans.modules.tasklist.usertasks.actions.ScheduleAction;
59 import org.netbeans.modules.tasklist.usertasks.actions.StartTaskAction;
60 import org.netbeans.modules.tasklist.usertasks.editors.CategoryTableCellEditor;
61 import org.netbeans.modules.tasklist.usertasks.editors.EffortTableCellEditor;
62 import org.netbeans.modules.tasklist.usertasks.editors.OwnerTableCellEditor;
63 import org.netbeans.modules.tasklist.usertasks.editors.PercentsTableCellEditor;
64 import org.netbeans.modules.tasklist.usertasks.editors.PriorityTableCellEditor;
65 import org.netbeans.modules.tasklist.usertasks.renderers.PriorityTableCellRenderer;
66 import org.netbeans.modules.tasklist.usertasks.renderers.SummaryTreeCellRenderer;
67 import org.netbeans.modules.tasklist.usertasks.treetable.DefaultMutableTreeTableNode;
68 import org.netbeans.modules.tasklist.usertasks.treetable.DefaultTreeTableModel;
69 import org.netbeans.modules.tasklist.usertasks.treetable.TreeTable;
70 import org.netbeans.modules.tasklist.usertasks.treetable.TreeTableDragGestureRecognizer;
71 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
72 import org.openide.awt.MouseUtils;
73 import org.openide.nodes.Node;
74 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
75 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList;
76 import org.netbeans.modules.tasklist.usertasks.model.UserTaskObjectList;
77 import org.netbeans.modules.tasklist.usertasks.options.Settings;
78 import org.netbeans.modules.tasklist.usertasks.renderers.CategoryTableCellRenderer;
79 import org.netbeans.modules.tasklist.usertasks.renderers.DateTableCellRenderer;
80 import org.netbeans.modules.tasklist.usertasks.renderers.DoneTreeTableCellRenderer;
81 import org.netbeans.modules.tasklist.usertasks.renderers.DueDateTableCellRenderer;
82 import org.netbeans.modules.tasklist.usertasks.renderers.DurationTableCellRenderer;
83 import org.netbeans.modules.tasklist.usertasks.renderers.EffortTableCellRenderer;
84 import org.netbeans.modules.tasklist.usertasks.renderers.LineTableCellRenderer;
85 import org.netbeans.modules.tasklist.usertasks.renderers.OwnerTableCellRenderer;
86 import org.netbeans.modules.tasklist.usertasks.renderers.PercentsTableCellRenderer;
87 import org.netbeans.modules.tasklist.usertasks.transfer.MyTransferHandler;
88 import org.netbeans.modules.tasklist.usertasks.treetable.AdvancedTreeTableNode;
89 import org.netbeans.modules.tasklist.usertasks.treetable.SortingHeaderRenderer;
90 import org.openide.actions.CopyAction;
91 import org.openide.actions.CutAction;
92 import org.openide.actions.DeleteAction;
93 import org.openide.actions.PasteAction;
94 import org.openide.util.Utilities;
95 import org.openide.util.actions.SystemAction;
96
97 /**
98  * TT for user tasks
99  *
100  * @author tl
101  */

102 public class UserTasksTreeTable extends TreeTable {
103     private UserTask selected;
104     private UserTaskView utv;
105     
106     /**
107      * Creates a new instance of UserTasksTreeTable
108      *
109      * @param em ExplorerManager associated with this tree table
110      * @param utl list with user tasks
111      * @param filter used filter or null
112      */

113     public UserTasksTreeTable(UserTaskView utv, UserTaskList utl,
114     Filter filter) {
115         super(new DefaultTreeTableModel(
116             new DefaultMutableTreeTableNode(), new String JavaDoc[] {""})); // NOI18N
117
this.utv = utv;
118         // this disables automatic scrolling if using keyboard
119
// setAutoscrolls(false);
120
setTreeTableModel(
121                 new UserTasksTreeTableModel(utl, getSortingModel(), filter));
122         setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
123         setShowHorizontalLines(true);
124         setShowVerticalLines(true);
125         setAutoResizeMode(AUTO_RESIZE_OFF);
126         getTree().setCellRenderer(new SummaryTreeCellRenderer());
127         getTree().setShowsRootHandles(true);
128         getTree().setToggleClickCount(3);
129         getTree().setRootVisible(false);
130         
131         setAutoCreateColumnsFromModel(false);
132         
133         /* DEBUG
134         if (UTUtils.LOGGER.isLoggable(Level.FINER)) {
135             getSelectionModel().addListSelectionListener(
136                 new ListSelectionListener() {
137                     public void valueChanged(ListSelectionEvent e) {
138                         UTUtils.LOGGER.fine(e.getFirstIndex() + " " + // NOI18N
139                             e.getLastIndex() + " " + // NOI18N
140                             e.getValueIsAdjusting() + " " + // NOI18N
141                             UserTasksTreeTable.this.getSelectedRow());
142                         if (UTUtils.LOGGER.isLoggable(Level.FINER))
143                             Thread.dumpStack();
144                     }
145                 }
146             );
147         }
148          */

149         
150         getSelectionModel().addListSelectionListener(
151             new ListSelectionListener JavaDoc() {
152                 public void valueChanged(ListSelectionEvent JavaDoc e) {
153                     int row = getSelectedRow();
154                     UserTask ut = null;
155                     if (row >= 0) {
156                         Object JavaDoc node = getNodeForRow(row);
157                         if (node instanceof UserTaskTreeTableNode) {
158                             ut = ((UserTaskTreeTableNode) node).getUserTask();
159                         }
160                     }
161                     if (selected != null && selected.getAnnotation() != null)
162                         selected.getAnnotation().setHighlight(false);
163                     selected = ut;
164                     if (selected != null && selected.getAnnotation() != null)
165                         selected.getAnnotation().setHighlight(true);
166                 }
167             }
168         );
169         
170         addMouseListener(new MouseUtils.PopupMouseAdapter() {
171             public void showPopup(MouseEvent JavaDoc e) {
172                 int row = rowAtPoint(e.getPoint());
173                 int col = columnAtPoint(e.getPoint());
174                 Action JavaDoc[] actions;
175
176                 if (row < 0 || col < 0)
177                     return;
178                 
179                 if (!getSelectionModel().isSelectedIndex(row)) {
180                     setRowSelectionInterval(row, row);
181                 }
182                 actions = getActions_();
183
184                 JPopupMenu JavaDoc pm = Utilities.actionsToPopup(actions,
185                         UserTasksTreeTable.this);
186
187                 if (pm != null)
188                     pm.show(UserTasksTreeTable.this, e.getX(), e.getY());
189             }
190         });
191         
192         addMouseListener(new MouseAdapter JavaDoc() {
193             @Override JavaDoc
194             public void mouseClicked(MouseEvent JavaDoc e) {
195                 if (e.getClickCount() == 2) {
196                     int row = rowAtPoint(e.getPoint());
197                     if (!getSelectionModel().isSelectedIndex(row)) {
198                         setRowSelectionInterval(row, row);
199                     }
200                     UserTasksTreeTable.this.utv.showTaskAction.
201                             actionPerformed(new ActionEvent JavaDoc(
202                             UserTasksTreeTable.this.utv.getTreeTable(), 0, ""));
203                 }
204             }
205         });
206         
207         setColumnsConfig(createDefaultColumnsConfig());
208         
209         TreeTableDragGestureRecognizer.enableDnD(this);
210         setTransferHandler(new MyTransferHandler());
211         setAutoscrolls(true);
212     }
213
214     @Override JavaDoc
215     protected void configureEnclosingScrollPane() {
216         super.configureEnclosingScrollPane();
217         final JScrollPane JavaDoc sp = (JScrollPane JavaDoc) SwingUtilities.getAncestorOfClass(
218                 JScrollPane JavaDoc.class, this);
219         sp.addMouseListener(new MouseUtils.PopupMouseAdapter() {
220             public void showPopup(MouseEvent JavaDoc e) {
221                 JPopupMenu JavaDoc pm = Utilities.actionsToPopup(getActions_(),
222                         UserTasksTreeTable.this);
223
224                 if (pm != null)
225                     pm.show(sp, e.getX(), e.getY());
226             }
227         });
228     }
229
230     /**
231      * Creates default columns configuration
232      *
233      * @return default columns
234      */

235     private TreeTable.ColumnsConfig createDefaultColumnsConfig() {
236         TreeTable.ColumnsConfig ret = new TreeTable.ColumnsConfig();
237         ret.ascending = false;
238         ret.columnWidths = new int[] {18, 18, 400, 60, 60, 60, 80, 80, 80};
239         ret.sortedColumn = UserTasksTreeTableModel.PRIORITY;
240         ret.columns = new int[] {
241             UserTasksTreeTableModel.DONE,
242             UserTasksTreeTableModel.PRIORITY,
243             UserTasksTreeTableModel.SUMMARY,
244             UserTasksTreeTableModel.CATEGORY,
245             UserTasksTreeTableModel.OWNER,
246             UserTasksTreeTableModel.PERCENT_COMPLETE,
247             UserTasksTreeTableModel.EFFORT,
248             UserTasksTreeTableModel.REMAINING_EFFORT,
249             UserTasksTreeTableModel.SPENT_TIME
250         };
251         return ret;
252     }
253     
254     public Node createNode(Object JavaDoc obj) {
255         UserTaskList utl = ((UserTasksTreeTableModel) getTreeTableModel()).
256             getUserTaskList();
257         UserTaskTreeTableNode node = (UserTaskTreeTableNode) obj;
258         UserTask ut = node.getUserTask();
259         return new UserTaskNode(node, ut, utl, this);
260     }
261
262     public void createDefaultColumnsFromModel() {
263         super.createDefaultColumnsFromModel();
264             
265         TableColumnModel JavaDoc tcm = getColumnModel();
266         if (tcm.getColumnCount() < 14)
267             return;
268         
269         JDateChooserCellEditor dc = new JDateChooserCellEditor();
270         
271         SortingHeaderRenderer r = new SortingHeaderRenderer();
272         r.setIcon(new ImageIcon JavaDoc(
273             UserTasksTreeTable.class.getResource("checkbox.gif"))); // NOI18N
274
tcm.getColumn(UserTasksTreeTableModel.DONE).setHeaderRenderer(r);
275         tcm.getColumn(UserTasksTreeTableModel.DONE).setCellRenderer(
276             new DoneTreeTableCellRenderer());
277         tcm.getColumn(UserTasksTreeTableModel.DONE).setMinWidth(17);
278         
279         tcm.getColumn(UserTasksTreeTableModel.PERCENT_COMPLETE).
280             setCellEditor(new PercentsTableCellEditor());
281         tcm.getColumn(UserTasksTreeTableModel.PERCENT_COMPLETE).setCellRenderer(
282             new PercentsTableCellRenderer());
283
284         DurationTableCellRenderer dr = new DurationTableCellRenderer();
285         tcm.getColumn(UserTasksTreeTableModel.REMAINING_EFFORT).setCellRenderer(dr);
286
287         tcm.getColumn(UserTasksTreeTableModel.SPENT_TIME).setCellRenderer(dr);
288         tcm.getColumn(UserTasksTreeTableModel.SPENT_TIME).setCellEditor(
289                 new EffortTableCellEditor());
290
291         tcm.getColumn(UserTasksTreeTableModel.LINE_NUMBER).setCellRenderer(
292             new LineTableCellRenderer());
293
294         DateTableCellRenderer dcr = new DateTableCellRenderer();
295         tcm.getColumn(UserTasksTreeTableModel.CREATED).setCellRenderer(dcr);
296
297         tcm.getColumn(UserTasksTreeTableModel.LAST_EDITED).setCellRenderer(dcr);
298
299         tcm.getColumn(UserTasksTreeTableModel.DUE_DATE).setCellRenderer(dcr);
300
301         tcm.getColumn(UserTasksTreeTableModel.COMPLETED_DATE)
302             .setCellRenderer(dcr);
303
304         tcm.getColumn(UserTasksTreeTableModel.CATEGORY).
305             setCellEditor(new CategoryTableCellEditor());
306         tcm.getColumn(UserTasksTreeTableModel.CATEGORY).
307             setCellRenderer(new CategoryTableCellRenderer());
308         
309         SortingHeaderRenderer priorityRenderer = new SortingHeaderRenderer();
310         priorityRenderer.setIcon(new ImageIcon JavaDoc(
311             UserTasksTreeTable.class.getResource("priority.gif"))); // NOI18N
312
tcm.getColumn(UserTasksTreeTableModel.PRIORITY).setHeaderRenderer(
313                 priorityRenderer);
314         tcm.getColumn(UserTasksTreeTableModel.PRIORITY).setCellRenderer(
315             new PriorityTableCellRenderer());
316         tcm.getColumn(UserTasksTreeTableModel.PRIORITY).setCellEditor(
317             new PriorityTableCellEditor());
318         tcm.getColumn(UserTasksTreeTableModel.PRIORITY).setCellRenderer(
319             new PriorityTableCellRenderer());
320         
321         tcm.getColumn(UserTasksTreeTableModel.EFFORT).setCellRenderer(
322             new EffortTableCellRenderer());
323         tcm.getColumn(UserTasksTreeTableModel.EFFORT).setCellEditor(
324                 new EffortTableCellEditor());
325
326         tcm.getColumn(UserTasksTreeTableModel.OWNER).
327             setCellEditor(new OwnerTableCellEditor());
328         tcm.getColumn(UserTasksTreeTableModel.OWNER).
329             setCellRenderer(new OwnerTableCellRenderer());
330         
331         tcm.getColumn(UserTasksTreeTableModel.DUE_DATE).setCellRenderer(
332             new DueDateTableCellRenderer());
333         tcm.getColumn(UserTasksTreeTableModel.DUE_DATE).setWidth(100);
334         tcm.getColumn(UserTasksTreeTableModel.DUE_DATE).setCellEditor(dc);
335         
336         tcm.getColumn(UserTasksTreeTableModel.START).setCellRenderer(dcr);
337         tcm.getColumn(UserTasksTreeTableModel.START).setCellEditor(dc);
338
339         tcm.getColumn(UserTasksTreeTableModel.SPENT_TIME_TODAY).setCellRenderer(dr);
340     }
341
342     /**
343      * Finds the path to the specified task
344      *
345      * @return found path or null
346      */

347     public TreePath JavaDoc findPath(UserTask task) {
348         List JavaDoc<UserTask> l = new ArrayList JavaDoc<UserTask>();
349         while (task != null) {
350             l.add(0, task);
351             task = task.getParent();
352         }
353         AdvancedTreeTableNode n =
354             (AdvancedTreeTableNode) getTreeTableModel().getRoot();
355         
356         for (int i = 0; i < l.size(); i++) {
357             int index = n.getIndexOfObject(l.get(i));
358             if (index == -1)
359                 return null;
360             n = (AdvancedTreeTableNode) n.getChildAt(index);
361         }
362         return new TreePath JavaDoc(n.getPathToRoot());
363     }
364     
365     public javax.swing.Action JavaDoc[] getActions_() {
366         return new Action JavaDoc[] {
367             utv.newTaskAction,
368             //SystemAction.get(ShowScheduleViewAction.class),
369
null,
370             new StartTaskAction(utv),
371             PauseAction.getInstance(),
372             null,
373             utv.showTaskAction,
374             new GoToUserTaskAction(utv),
375             null,
376             SystemAction.get(CutAction.class),
377             SystemAction.get(CopyAction.class),
378             SystemAction.get(PasteAction.class),
379             utv.pasteAtTopLevelAction,
380             null,
381             SystemAction.get(DeleteAction.class),
382             null,
383             utv.moveUpAction,
384             utv.moveDownAction,
385             utv.moveLeftAction,
386             utv.moveRightAction,
387             null,
388             SystemAction.get(FilterAction.class),
389             SystemAction.get(RemoveFilterAction.class),
390             null,
391             utv.purgeTasksAction,
392             utv.clearCompletedAction,
393             SystemAction.get(ScheduleAction.class),
394             null,
395             SystemAction.get(ExpandAllUserTasksAction.class),
396             SystemAction.get(CollapseAllAction.class),
397             null,
398             SystemAction.get(ImportAction.class),
399             SystemAction.get(ExportAction.class),
400
401             // Property: node specific, but by convention last in menu
402
null,
403             utv.propertiesAction
404         };
405     }
406
407     protected Serializable JavaDoc writeReplaceNode(Object JavaDoc node) {
408         return ((UserTaskTreeTableNode) node).getUserTask().getUID();
409     }
410
411     protected Object JavaDoc readResolveNode(Object JavaDoc parent, Object JavaDoc node) {
412         AdvancedTreeTableNode p = (AdvancedTreeTableNode) parent;
413         String JavaDoc uid = (String JavaDoc) node;
414         
415         for (int i = 0; i < p.getChildCount(); i++) {
416             UserTaskTreeTableNode ch =
417                 (UserTaskTreeTableNode) p.getChildAt(i);
418             UserTask ut = ch.getUserTask();
419             if (uid.equals(ut.getUID()))
420                 return ch;
421         }
422         
423         return null;
424     }
425
426     public String JavaDoc getToolTipText(java.awt.event.MouseEvent JavaDoc event) {
427         Point JavaDoc point = event.getPoint();
428         int row = rowAtPoint(point);
429         
430         String JavaDoc result = null;
431         if (row >= 0) {
432             Object JavaDoc node = getNodeForRow(row);
433
434             if (node instanceof UserTaskTreeTableNode) {
435                 result = ((UserTaskTreeTableNode) node).getUserTask().getDetails();
436                 result = UTUtils.prepareForTooltip(result);
437                 if (result.length() == 0)
438                     result = null;
439             }
440         }
441         return result;
442     }
443
444     /* DEBUG*/
445     public void paint(Graphics JavaDoc g) {
446         super.paint(g);
447     }
448      /* */
449 }
450
451
452         
453         
454         
Popular Tags