1 19 20 package org.netbeans.modules.tasklist.usertasks.actions; 21 22 import java.awt.Dialog ; 23 import java.awt.Dimension ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.KeyEvent ; 27 import java.net.URL ; 28 import javax.swing.ImageIcon ; 29 import javax.swing.JButton ; 30 import javax.swing.KeyStroke ; 31 import javax.swing.event.ListSelectionEvent ; 32 import javax.swing.tree.TreePath ; 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 65 public class NewTaskAction extends UTViewAction { 66 private static final long serialVersionUID = 2; 67 68 private static EditTaskPanel panel; 69 70 75 public NewTaskAction(UserTaskView utv) { 76 super(utv, NbBundle.getMessage(NewTaskAction.class, 77 "LBL_NewSubtask")); putValue(SMALL_ICON, new ImageIcon (Utilities.loadImage( 79 "org/netbeans/modules/tasklist/usertasks/" + "actions/newTask.gif"))); putValue(UTViewAction.ACCELERATOR_KEY, 82 KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0)); 83 setEnabled(true); 84 } 85 86 public void valueChanged(ListSelectionEvent e) { 87 } 88 89 94 private static EditTaskPanel getEditTaskPanel() { 95 if (panel == null) { 96 panel = new EditTaskPanel(false); 97 panel.setPreferredSize(new Dimension (600,500)); 98 } 99 return panel; 100 } 101 102 public void actionPerformed(ActionEvent e) { 103 TreePath [] tp = utv.getTreeTable().getSelectedPaths(); 104 final UserTask parent; 105 final UserTaskList utl; 106 Object 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 118 126 Line cursor = UTUtils.findCursorPosition(null); if (cursor == null) { 129 Node[] editorNodes = UTUtils.getEditorNodes(); 130 if (editorNodes != null) 131 cursor = UTUtils.findCursorPosition(editorNodes); 132 } 133 int lineNumber; 134 URL 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 final UserTaskView utv = UserTaskViewRegistry.getInstance(). 145 getLastActivated(); 146 147 UserTask ut = new UserTask("", utl); 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")); dd.setModal(true); 161 dd.setHelpCtx(new HelpCtx( 162 "org.netbeans.modules.tasklist.usertasks.NewTaskDialog")); dd.setMessageType(NotifyDescriptor.PLAIN_MESSAGE); 164 165 final JButton addAnotherButton = new JButton (); 167 Mnemonics.setLocalizedText(addAnotherButton, NbBundle.getMessage( 168 NewTaskAction.class, "BTN_AddAnother")); dd.setOptions(new Object [] { 170 DialogDescriptor.OK_OPTION, 171 DialogDescriptor.CANCEL_OPTION, 172 addAnotherButton 173 }); 174 dd.setClosingOptions(new Object [] { 175 DialogDescriptor.OK_OPTION, 176 DialogDescriptor.CANCEL_OPTION 177 }); 178 dd.setButtonListener(new ActionListener () { 179 public void actionPerformed(ActionEvent e) { 180 UTUtils.LOGGER.fine(""); Object src = e.getSource(); 182 if (src == addAnotherButton) { 183 UserTask ut = new UserTask("", utl); panel.fillObject(ut); 185 186 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 utv.showInMode(); 205 utv.select(ut); 206 utv.scrollTo(ut); 207 208 ut = new UserTask("", utl); panel.fillPanel(ut); 210 panel.focusSummary(); 211 } 212 } 213 }); 214 215 Dialog 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 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 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 url = URLMapper.findURL(fo, URLMapper.EXTERNAL); 260 if (url == null) 261 return; 262 263 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 UserTask parent = null; 273 TreePath 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); 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")); dd.setModal(true); 292 dd.setHelpCtx(new HelpCtx( 293 "org.netbeans.modules.tasklist.usertasks.NewTaskDialog")); dd.setMessageType(NotifyDescriptor.PLAIN_MESSAGE); 295 296 dd.setOptions(new Object [] { 298 DialogDescriptor.OK_OPTION, 299 DialogDescriptor.CANCEL_OPTION, 300 }); 301 dd.setClosingOptions(new Object [] { 302 DialogDescriptor.OK_OPTION, 303 DialogDescriptor.CANCEL_OPTION 304 }); 305 306 Dialog 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 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 |