1 19 20 package org.netbeans.modules.tasklist.suggestions; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Set ; 28 import java.util.Iterator ; 29 import javax.swing.JButton ; 30 import javax.swing.JEditorPane ; 31 import org.netbeans.modules.tasklist.client.SuggestionPerformer; 32 import org.netbeans.modules.tasklist.client.SuggestionManager; 33 import org.openide.cookies.EditorCookie; 34 import org.openide.ErrorManager; 35 import org.openide.DialogDescriptor; 36 import org.openide.DialogDisplayer; 37 import org.openide.loaders.DataObject; 38 import org.openide.NotifyDescriptor; 39 import org.openide.awt.Actions; 40 import org.openide.nodes.Node; 41 import org.openide.util.HelpCtx; 42 import org.openide.util.NbBundle; 43 import org.openide.util.actions.NodeAction; 44 import org.netbeans.modules.tasklist.core.*; 45 import org.openide.awt.Mnemonics; 46 47 56 57 public final class FixAction extends NodeAction { 58 59 private static final long serialVersionUID = 1; 60 61 protected boolean asynchronous() { 62 return false; 63 } 64 65 protected boolean enable(Node[] node) { 66 if ((node == null) || (node.length < 1)) { 67 return false; 68 } 69 boolean enabled = false; 70 for (int i = 0; i < node.length; i++) { 71 Task task = TaskNode.getTask(node[i]); 72 if ((task != null) && (task.getAction() != null)) { 73 enabled = true; 74 } 75 } 76 return enabled; 77 } 78 79 protected void performAction(Node[] node) { 80 SuggestionManagerImpl manager = 81 (SuggestionManagerImpl)SuggestionManager.getDefault(); 82 83 boolean skipConfirm = false; 84 85 assert node[0] instanceof SuggestionNode : "Need to be softened later on"; 86 TaskListView tlv = TaskListView.getCurrent(); 87 89 Collection originalModified = 90 new ArrayList (DataObject.getRegistry().getModifiedSet()); 91 boolean fixingStarted = false; 92 try { 93 94 for (int i = 0; i < node.length; i++) { 95 SuggestionImpl item = (SuggestionImpl)TaskNode.getTask(node[i]); 96 if (item == null) { 97 continue; 98 } 99 100 SuggestionPerformer performer = item.getAction(); 101 if (performer == null) { 102 continue; 103 } 104 105 boolean doConfirm = manager.isConfirm(item.getSType()); 106 Object confirmation = null; 107 if (doConfirm && !skipConfirm && performer.hasConfirmation()) { 108 confirmation = performer.getConfirmation(item); 109 } 110 if (confirmation != null) { 111 if (tlv != null) { 113 tlv.showTaskInEditor(item, new SuggestionAnno(item)); 114 tlv.select(item); 115 } 116 117 JButton fixButton = new JButton (); 118 Mnemonics.setLocalizedText(fixButton, NbBundle.getMessage(FixAction.class, "FixIt")); 119 120 JButton fixAllButton = null; 121 JButton skipButton = null; 122 if (node.length > 1) { 123 fixAllButton = new JButton (); 124 Mnemonics.setLocalizedText(fixAllButton, 125 NbBundle.getMessage(FixAction.class, 126 "FixAll")); 127 skipButton = new JButton (); 128 Mnemonics.setLocalizedText(skipButton, 129 NbBundle.getMessage(FixAction.class, 130 "Skip")); 131 fixAllButton.getAccessibleContext().setAccessibleDescription( 132 NbBundle.getMessage(FixAction.class, 133 "ACSD_FixAll")); skipButton.getAccessibleContext().setAccessibleDescription( 135 NbBundle.getMessage(FixAction.class, 136 "ACSD_Skip")); } 138 JButton cancelButton = new JButton (); 139 Mnemonics.setLocalizedText(cancelButton, 140 NbBundle.getMessage(FixAction.class, "Cancel")); 141 142 if (confirmation instanceof Component ) { 143 ((Component )confirmation).getAccessibleContext(). 144 setAccessibleDescription( 145 NbBundle.getMessage(FixAction.class, 146 "ACSD_Confirmation")); } 148 fixButton.getAccessibleContext().setAccessibleDescription( 149 NbBundle.getMessage(FixAction.class, 150 "ACSD_Fix")); cancelButton.getAccessibleContext().setAccessibleDescription( 152 NbBundle.getMessage(FixAction.class, 153 "ACSD_Cancel")); 155 String title = NbBundle.getMessage(FixAction.class, "TITLE_fixconfirm"); 156 DialogDescriptor dlg = new DialogDescriptor( 157 confirmation, 158 title, 159 true, 160 (node.length > 1) ? 161 new JButton [] { 162 fixButton, 163 skipButton, 164 fixAllButton, 165 cancelButton 166 } 167 : 168 new JButton [] { 169 fixButton, 170 cancelButton 171 }, 172 173 fixButton, 174 DialogDescriptor.DEFAULT_ALIGN, 175 null, 176 null); 177 dlg.setMessageType(NotifyDescriptor.PLAIN_MESSAGE); 178 197 198 dlg.setModal(true); 199 final Dialog dialog = DialogDisplayer.getDefault().createDialog(dlg); 200 dialog.pack(); 201 dialog.setVisible(true); 202 Object pressedButton = dlg.getValue(); 203 204 if (tlv != null) { 205 tlv.showTaskInEditor(null, null); 206 } 207 208 if (pressedButton == cancelButton) { 209 break; } else if (pressedButton == fixAllButton) { 211 skipConfirm = true; 212 } else if (pressedButton == skipButton) { 213 216 continue; 217 } else if (pressedButton != fixButton) { 218 continue; 221 } 222 223 228 } 229 if (!fixingStarted) { 230 fixingStarted = true; 231 manager.setFixing(true); 232 } 233 if (item.isZombie()) { 234 241 int matches = 0; 265 Iterator it = item.getParent().subtasksIterator(); 266 SuggestionImpl match = null; 267 boolean exact = false; 268 while (it.hasNext()) { 269 SuggestionImpl sm = (SuggestionImpl)it.next(); 270 if (sm.hasSubtasks()) { 271 Iterator it2 = sm.subtasksIterator(); 273 while (it2.hasNext()) { 274 SuggestionImpl sm2 = (SuggestionImpl)it2.next(); 275 if ((item.getSType() == sm2.getSType()) && 276 item.getSummary().equals(sm2.getSummary()) && 277 (item.getPriority() == sm2.getPriority()) && 278 (item.getPriority() == sm2.getPriority())) { 279 match = sm2; 280 matches++; 281 if (item.getLine().equals(sm2.getLine())) { 282 exact = true; 283 break; 284 } 285 } 286 } 287 if (exact) { 288 break; 289 } 290 } else { 291 if ((item.getSType() == sm.getSType()) && 292 item.getSummary().equals(sm.getSummary()) && 293 (item.getPriority() == sm.getPriority()) && 294 (item.getPriority() == sm.getPriority())) { 295 match = sm; 296 matches++; 297 if (item.getLine().equals(sm.getLine())) { 298 exact = true; 299 break; 300 } 301 } 302 } 303 } 304 if ((match != null) && (exact || (matches == 1))) { 305 item = match; 307 } else { 308 314 } 316 } 317 performer.perform(item); 318 319 } 325 } finally { 326 if (fixingStarted) { 327 manager.setFixing(false); 328 } 329 } 330 331 342 Set modifiedRO = DataObject.getRegistry().getModifiedSet(); 344 Set modified = new java.util.HashSet (modifiedRO); 345 modified.removeAll(originalModified); 346 347 boolean haveModified = false; 348 Iterator it = modified.iterator(); 349 while (it.hasNext()) { 350 DataObject dao = (DataObject)it.next(); 351 EditorCookie cookie = 352 (EditorCookie)dao.getCookie(EditorCookie.class); 353 if (cookie != null) { 354 JEditorPane [] panes = cookie.getOpenedPanes(); 355 if ((panes == null) || (panes.length == 0)) { 356 haveModified = true; 357 } 358 } 359 } 360 if (haveModified) { 361 JButton openFiles = new JButton (); 362 Mnemonics.setLocalizedText(openFiles, 363 NbBundle.getMessage(FixAction.class, 364 "ShowFiles")); 365 366 JButton selectFiles = new JButton (); 367 Mnemonics.setLocalizedText(selectFiles, 368 NbBundle.getMessage(FixAction.class, 369 "SelectFiles")); 370 371 JButton saveFiles = new JButton (); 372 Mnemonics.setLocalizedText(saveFiles, 373 NbBundle.getMessage(FixAction.class, 374 "SaveAllFiles")); 375 376 JButton cancelButton = new JButton (); 377 Mnemonics.setLocalizedText(cancelButton, 378 NbBundle.getMessage(FixAction.class, 379 "Cancel")); 380 381 String title = NbBundle.getMessage(FixAction.class, 382 "FixSavesTitle"); 383 DialogDescriptor dlg = new DialogDescriptor( 384 NbBundle.getMessage(FixAction.class, 385 "FixFileSaves"), title, 387 true, 388 (node.length > 1) ? 389 new JButton [] { 390 openFiles, 391 saveFiles, 393 cancelButton 394 } 395 : 396 new JButton [] { 397 openFiles, 398 saveFiles, 400 cancelButton 401 }, 402 403 openFiles, 404 DialogDescriptor.DEFAULT_ALIGN, 405 null, 406 null); 407 dlg.setMessageType(NotifyDescriptor.PLAIN_MESSAGE); 408 dlg.setModal(true); 409 final Dialog dialog = 410 DialogDisplayer.getDefault().createDialog(dlg); 411 dialog.pack(); 412 dialog.setVisible(true); 413 Object pressedButton = dlg.getValue(); 414 415 if (pressedButton == openFiles) { 416 it = modified.iterator(); 417 while (it.hasNext()) { 418 DataObject dao = (DataObject)it.next(); 419 EditorCookie cookie = 420 (EditorCookie)dao.getCookie(EditorCookie.class); 421 if (cookie != null) { 422 cookie.open(); 423 } 424 } 425 } else if (pressedButton == saveFiles) { 426 it = modified.iterator(); 427 while (it.hasNext()) { 428 DataObject dao = (DataObject)it.next(); 429 EditorCookie cookie = 430 (EditorCookie)dao.getCookie(EditorCookie.class); 431 if (cookie != null) { 432 try { 433 cookie.saveDocument(); 434 } catch (Exception e) { 435 ErrorManager.getDefault().notify( 436 ErrorManager.WARNING, e); 437 } 438 } 439 } 440 } else if (pressedButton == selectFiles) { 441 } 443 } 444 } 445 446 public String getName() { 447 return NbBundle.getMessage(FixAction.class, "LBL_FixConfirm"); } 449 450 protected String iconResource() { 451 return "org/netbeans/modules/tasklist/suggestions/fix.gif"; } 453 454 public HelpCtx getHelpCtx() { 455 return HelpCtx.DEFAULT_HELP; 456 } 459 460 } 461 | Popular Tags |