KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > actions > NewTaskAction


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.usertasks.actions;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.net.URL JavaDoc;
28 import javax.swing.ImageIcon JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import javax.swing.KeyStroke JavaDoc;
31 import javax.swing.event.ListSelectionEvent JavaDoc;
32 import javax.swing.tree.TreePath JavaDoc;
33 import org.netbeans.modules.tasklist.usertasks.EditTaskPanel;
34 import org.netbeans.modules.tasklist.usertasks.actions.UTViewAction;
35 import org.netbeans.modules.tasklist.usertasks.options.Settings;
36 import org.netbeans.modules.tasklist.usertasks.util.AWTThreadAnnotation;
37 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
38 import org.netbeans.modules.tasklist.usertasks.UserTaskViewRegistry;
39 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
40 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList;
41 import org.netbeans.modules.tasklist.usertasks.UserTaskTreeTableNode;
42 import org.netbeans.modules.tasklist.usertasks.UserTaskView;
43 import org.openide.DialogDescriptor;
44 import org.openide.DialogDisplayer;
45 import org.openide.NotifyDescriptor;
46 import org.openide.awt.Mnemonics;
47 import org.openide.filesystems.FileObject;
48 import org.openide.filesystems.URLMapper;
49 import org.openide.loaders.DataObject;
50 import org.openide.nodes.Node;
51 import org.openide.text.DataEditorSupport;
52 import org.openide.text.Line;
53 import org.openide.util.HelpCtx;
54 import org.openide.util.NbBundle;
55 import org.openide.util.Utilities;
56
57 /**
58  * Action which brings up a dialog where you can create
59  * a new subtask.
60  *
61  * @author Tor Norbye
62  * @author Trond Norbye
63  * @author tl
64  */

65 public class NewTaskAction extends UTViewAction {
66     private static final long serialVersionUID = 2;
67
68     private static EditTaskPanel panel;
69     
70     /**
71      * Constructor.
72      *
73      * @param utv a user task view.
74      */

75     public NewTaskAction(UserTaskView utv) {
76         super(utv, NbBundle.getMessage(NewTaskAction.class,
77                 "LBL_NewSubtask")); // NOI18N
78
putValue(SMALL_ICON, new ImageIcon JavaDoc(Utilities.loadImage(
79                 "org/netbeans/modules/tasklist/usertasks/" + // NOI18N
80
"actions/newTask.gif"))); // NOI18N
81
putValue(UTViewAction.ACCELERATOR_KEY,
82                 KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0));
83         setEnabled(true);
84     }
85     
86     public void valueChanged(ListSelectionEvent JavaDoc e) {
87     }
88     
89     /**
90      * Creates a panel for editing a task
91      *
92      * @return created panel
93      */

94     private static EditTaskPanel getEditTaskPanel() {
95         if (panel == null) {
96             panel = new EditTaskPanel(false);
97             panel.setPreferredSize(new Dimension JavaDoc(600,500));
98         }
99         return panel;
100     }
101     
102     public void actionPerformed(ActionEvent JavaDoc e) {
103         TreePath JavaDoc[] tp = utv.getTreeTable().getSelectedPaths();
104         final UserTask parent;
105         final UserTaskList utl;
106         Object JavaDoc last = tp.length > 0 ? tp[0].getLastPathComponent() : null;
107         if (last instanceof UserTaskTreeTableNode) {
108             parent = ((UserTaskTreeTableNode) last).getUserTask();
109             utl = parent.getList();
110         } else {
111             parent = null;
112             utl = utv.getUserTaskList();
113         }
114         
115         // Get the current filename and line number so we can initialize
116
// the filename:linenumber columns
117

118         // First try to get the editor window itself; if you right click
119
// on a node in the User Tasks Window, that node becomes the activated
120
// node (which is good - it makes the properties window show the
121
// todo item's properties, etc.) but that means that we can't
122
// find the editor position via the normal means.
123
// So, we go hunting for the topmosteditor tab, and when we find it,
124
// ask for its nodes.
125

126         // find cursor position
127
Line cursor = UTUtils.findCursorPosition(null); // TODO: null
128
if (cursor == null) {
129             Node[] editorNodes = UTUtils.getEditorNodes();
130             if (editorNodes != null)
131                 cursor = UTUtils.findCursorPosition(editorNodes);
132         }
133         int lineNumber;
134         URL JavaDoc url;
135         if (cursor != null) {
136             lineNumber = cursor.getLineNumber();
137             url = UTUtils.getExternalURLForLine(cursor);
138         } else {
139             url = null;
140             lineNumber = -1;
141         }
142         
143         // After the add - view the todo list as well!
144
final UserTaskView utv = UserTaskViewRegistry.getInstance().
145                 getLastActivated();
146
147         UserTask ut = new UserTask("", utl); // NOI18N
148

149         final EditTaskPanel panel = getEditTaskPanel();
150         panel.setTopLevel(parent == null);
151         panel.fillPanel(ut);
152         panel.setAssociatedFilePos(false);
153         panel.setUrl(url);
154         panel.setLineNumber(lineNumber);
155         panel.focusSummary();
156         
157         DialogDescriptor dd = new DialogDescriptor(getEditTaskPanel(),
158              NbBundle.getMessage(NewTaskAction.class,
159              "TITLE_add_todo")); // NOI18N
160
dd.setModal(true);
161         dd.setHelpCtx(new HelpCtx(
162                 "org.netbeans.modules.tasklist.usertasks.NewTaskDialog")); // NOI18N
163
dd.setMessageType(NotifyDescriptor.PLAIN_MESSAGE);
164
165         // dialog buttons
166
final JButton JavaDoc addAnotherButton = new JButton JavaDoc();
167         Mnemonics.setLocalizedText(addAnotherButton, NbBundle.getMessage(
168             NewTaskAction.class, "BTN_AddAnother")); // NOI18N
169
dd.setOptions(new Object JavaDoc[] {
170             DialogDescriptor.OK_OPTION,
171             DialogDescriptor.CANCEL_OPTION,
172             addAnotherButton
173         });
174         dd.setClosingOptions(new Object JavaDoc[] {
175             DialogDescriptor.OK_OPTION,
176             DialogDescriptor.CANCEL_OPTION
177         });
178         dd.setButtonListener(new ActionListener JavaDoc() {
179                 public void actionPerformed(ActionEvent JavaDoc e) {
180                     UTUtils.LOGGER.fine(""); // NOI18N
181
Object JavaDoc src = e.getSource();
182                     if (src == addAnotherButton) {
183                         UserTask ut = new UserTask("", utl); // NOI18N
184
panel.fillObject(ut);
185
186                         // See if the user wants to append or prepend
187
boolean append = panel.getAppend();
188                         if (parent != null && !panel.isTopLevel()) {
189                             if (append)
190                                 parent.getSubtasks().add(ut);
191                             else
192                                 parent.getSubtasks().add(0, ut);
193                             if (Settings.getDefault().getAutoSwitchToComputed()) {
194                                 parent.setValuesComputed(true);
195                             }
196                         } else {
197                             if (append)
198                                 utl.getSubtasks().add(ut);
199                             else
200                                 utl.getSubtasks().add(0, ut);
201                         }
202
203                         // After the add - view the todo list as well!
204
utv.showInMode();
205                         utv.select(ut);
206                         utv.scrollTo(ut);
207
208                         ut = new UserTask("", utl); // NOI18N
209
panel.fillPanel(ut);
210                         panel.focusSummary();
211                     }
212                 }
213             });
214             
215         Dialog JavaDoc d = DialogDisplayer.getDefault().createDialog(dd);
216         d.pack();
217         d.setVisible(true);
218
219         if (dd.getValue() == NotifyDescriptor.OK_OPTION) {
220             panel.fillObject(ut);
221
222             // See if the user wants to append or prepend
223
boolean append = panel.getAppend();
224             if (parent != null && !panel.isTopLevel()) {
225                 if (append)
226                     parent.getSubtasks().add(ut);
227                 else
228                     parent.getSubtasks().add(0, ut);
229                 if (Settings.getDefault().getAutoSwitchToComputed()) {
230                     parent.setValuesComputed(true);
231                 }
232             } else {
233                 if (append)
234                     utl.getSubtasks().add(ut);
235                 else
236                     utl.getSubtasks().add(0, ut);
237             }
238
239             assert utv != null;
240             
241             utv.showInMode();
242             utv.select(ut);
243             utv.scrollTo(ut);
244         }
245     }
246
247     /**
248      * Performs the action
249      *
250      * @param line the associated line
251      */

252     @AWTThreadAnnotation
253     public static void performAction(Line line) {
254         DataObject dob = DataEditorSupport.findDataObject(line);
255         if (dob == null)
256             return;
257         
258         FileObject fo = dob.getPrimaryFile();
259         URL JavaDoc url = URLMapper.findURL(fo, URLMapper.EXTERNAL);
260         if (url == null)
261             return;
262
263         // After the add - view the todo list as well!
264
UserTaskView utv = UserTaskViewRegistry.getInstance().getLastActivated();
265         if (utv == null) {
266             utv = UserTaskViewRegistry.getInstance().getDefault();
267             utv.showInMode();
268         }
269         UserTaskList utl = utv.getUserTaskList();
270         
271         // find parent task
272
UserTask parent = null;
273         TreePath JavaDoc tp = utv.getTreeTable().getSelectedPath();
274         if (tp != null) {
275             parent = ((UserTaskTreeTableNode) tp.getLastPathComponent()).
276                     getUserTask();
277         }
278         
279         UserTask ut = new UserTask(line.getText(), utl); // NOI18N
280

281         EditTaskPanel panel = getEditTaskPanel();
282         panel.fillPanel(ut);
283         panel.setAssociatedFilePos(true);
284         panel.setUrl(url);
285         panel.setLineNumber(line.getLineNumber());
286         panel.focusSummary();
287         
288         DialogDescriptor dd = new DialogDescriptor(getEditTaskPanel(),
289              NbBundle.getMessage(NewTaskAction.class,
290              "TITLE_add_todo")); // NOI18N
291
dd.setModal(true);
292         dd.setHelpCtx(new HelpCtx(
293                 "org.netbeans.modules.tasklist.usertasks.NewTaskDialog")); // NOI18N
294
dd.setMessageType(NotifyDescriptor.PLAIN_MESSAGE);
295
296         // dialog buttons
297
dd.setOptions(new Object JavaDoc[] {
298             DialogDescriptor.OK_OPTION,
299             DialogDescriptor.CANCEL_OPTION,
300         });
301         dd.setClosingOptions(new Object JavaDoc[] {
302             DialogDescriptor.OK_OPTION,
303             DialogDescriptor.CANCEL_OPTION
304         });
305             
306         Dialog JavaDoc d = DialogDisplayer.getDefault().createDialog(dd);
307         d.pack();
308         d.setVisible(true);
309
310         if (dd.getValue() == NotifyDescriptor.OK_OPTION) {
311             panel.fillObject(ut);
312
313             // See if the user wants to append or prepend
314
boolean append = panel.getAppend();
315             if (parent != null) {
316                 if (append)
317                     parent.getSubtasks().add(ut);
318                 else
319                     parent.getSubtasks().add(0, ut);
320             } else {
321                 if (append)
322                     utl.getSubtasks().add(ut);
323                 else
324                     utl.getSubtasks().add(0, ut);
325             }
326
327             assert utv != null;
328             
329             utv.showInMode();
330             utv.select(ut);
331             utv.scrollTo(ut);
332         }
333     }
334 }
335
Popular Tags