1 package org.netbeans.modules.tasklist.usertasks.actions; 2 3 import java.awt.event.ActionEvent ; 4 import java.lang.reflect.InvocationTargetException ; 5 import java.util.ArrayList ; 6 import java.util.List ; 7 import javax.swing.AbstractAction ; 8 import javax.swing.SwingUtilities ; 9 import javax.swing.event.ListSelectionEvent ; 10 import javax.swing.event.ListSelectionListener ; 11 import javax.swing.tree.TreePath ; 12 import org.netbeans.modules.tasklist.usertasks.UserTaskTreeTableNode; 13 import org.netbeans.modules.tasklist.usertasks.UserTasksTreeTable; 14 import org.netbeans.modules.tasklist.usertasks.model.UserTask; 15 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList; 16 import org.netbeans.modules.tasklist.usertasks.treetable.TreeTableNode; 17 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 18 import org.openide.DialogDisplayer; 19 import org.openide.NotifyDescriptor; 20 import org.openide.util.NbBundle; 21 22 23 28 public class UTDeleteAction extends AbstractAction 29 implements ListSelectionListener { 30 private UserTasksTreeTable tt; 31 32 37 public UTDeleteAction(UserTasksTreeTable tt) { 38 this.tt = tt; 39 tt.getSelectionModel().addListSelectionListener(this); 40 } 41 42 public void valueChanged(ListSelectionEvent event) { 43 int[] rows = tt.getSelectedRows(); 44 if (rows.length == 0) 45 return; 46 47 boolean e = true; 48 for (int i = 0; i < rows.length; i++) { 49 Object obj = tt.getNodeForRow(rows[i]); 50 if (!(obj instanceof UserTaskTreeTableNode)) { 51 e = false; 52 break; 53 } 54 } 55 56 setEnabled(e); 57 } 58 59 private UserTaskTreeTableNode[] normalize(UserTaskTreeTableNode[] tasks) { 60 List <UserTaskTreeTableNode> ret = 61 new ArrayList <UserTaskTreeTableNode>(); 62 outer: 63 for (int i = 0; i < tasks.length; i++) { 64 for (int j = 0; j < ret.size(); j++) { 65 UserTaskTreeTableNode ut = (UserTaskTreeTableNode) ret.get(j); 66 if (ut.getUserTask().isAncestorOf(tasks[i].getUserTask())) 67 continue outer; 68 } 69 for (int j = 0; j < ret.size(); ) { 70 UserTaskTreeTableNode ut = (UserTaskTreeTableNode) ret.get(j); 71 if (tasks[i].getUserTask().isAncestorOf(ut.getUserTask())) 72 ret.remove(j); 73 else 74 j++; 75 } 76 ret.add(tasks[i]); 77 } 78 return ret.toArray(new UserTaskTreeTableNode[ret.size()]); 79 } 80 81 private boolean doConfirm(UserTaskTreeTableNode[] sel) { 82 String message, title; 83 if (sel.length == 1) { 84 message = NbBundle.getMessage(UTDeleteAction.class, "MSG_ConfirmDeleteObject", sel[0].getUserTask().getSummary()); 86 title = NbBundle.getMessage(UTDeleteAction.class, "MSG_ConfirmDeleteObjectTitle"); } else { 88 message = NbBundle.getMessage(UTDeleteAction.class, "MSG_ConfirmDeleteObjects", new Integer (sel.length)); 90 title = NbBundle.getMessage(UTDeleteAction.class, "MSG_ConfirmDeleteObjectsTitle"); } 92 NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message, title, NotifyDescriptor.YES_NO_OPTION); 93 return NotifyDescriptor.YES_OPTION.equals(DialogDisplayer.getDefault().notify(desc)); 94 } 95 96 public void actionPerformed(ActionEvent event) { 97 if (SwingUtilities.isEventDispatchThread()) 98 actionPerformed(); 99 else { 100 try { 101 SwingUtilities.invokeAndWait(new Runnable () { 102 public void run() { 103 actionPerformed(); 104 } 105 }); 106 } catch (InterruptedException ex) { 107 UTUtils.LOGGER.log(java.util.logging.Level.SEVERE, 108 ex.getMessage(), ex); 109 } catch (InvocationTargetException ex) { 110 UTUtils.LOGGER.log(java.util.logging.Level.SEVERE, 111 ex.getMessage(), ex); 112 } 113 } 114 } 115 116 119 private void actionPerformed() { 120 int[] rows = tt.getSelectedRows(); 121 if (rows.length == 0) 122 return; 123 124 UserTaskTreeTableNode[] tasks = new UserTaskTreeTableNode[rows.length]; 125 boolean e = true; 126 for (int i = 0; i < tasks.length; i++) { 127 Object obj = tt.getNodeForRow(rows[i]); 128 if (obj instanceof UserTaskTreeTableNode) { 129 tasks[i] = (UserTaskTreeTableNode) obj; 130 } else { 131 e = false; 132 break; 133 } 134 } 135 if (!e) 136 return; 137 138 tasks = normalize(tasks); 139 140 UserTaskTreeTableNode n = tasks[tasks.length - 1]; 141 TreeTableNode ttn = n.findNextNodeAfterDelete(); 142 if (ttn instanceof UserTaskTreeTableNode) 143 n = (UserTaskTreeTableNode) ttn; 144 else 145 n = null; 146 147 if (doConfirm(tasks)) { 149 tt.clearSelection(); 151 for (int i = 0; i < tasks.length; i++) { 152 UserTask item = tasks[i].getUserTask(); 153 if (item.isStarted()) 154 item.stop(); 155 UserTaskList utl = item.getList(); 156 item.destroy(); 157 if (item.getParent() != null) 158 item.getParent().getSubtasks().remove(item); 159 else 160 utl.getSubtasks().remove(item); 161 } 162 if (n != null) { 163 TreePath tp = new TreePath (n.getPathToRoot()); 164 tt.select(tp); 165 tt.scrollTo(tp); 166 } 167 tt.repaint(); 169 } 170 } 171 } 172 | Popular Tags |