KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.netbeans.modules.tasklist.usertasks.actions;
2
3 import java.awt.event.ActionEvent JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7 import javax.swing.AbstractAction JavaDoc;
8 import javax.swing.SwingUtilities JavaDoc;
9 import javax.swing.event.ListSelectionEvent JavaDoc;
10 import javax.swing.event.ListSelectionListener JavaDoc;
11 import javax.swing.tree.TreePath JavaDoc;
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 /**
24  * DeleteAction for UserTasks
25  *
26  * This class is not used.
27  */

28 public class UTDeleteAction extends AbstractAction JavaDoc
29 implements ListSelectionListener JavaDoc {
30     private UserTasksTreeTable tt;
31     
32     /**
33      * Konstruktor
34      *
35      * @param tt tree table
36      */

37     public UTDeleteAction(UserTasksTreeTable tt) {
38         this.tt = tt;
39         tt.getSelectionModel().addListSelectionListener(this);
40     }
41
42     public void valueChanged(ListSelectionEvent JavaDoc 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 JavaDoc 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 JavaDoc<UserTaskTreeTableNode> ret =
61                 new ArrayList JavaDoc<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 JavaDoc message, title;
83         if (sel.length == 1) {
84             message = NbBundle.getMessage(UTDeleteAction.class, "MSG_ConfirmDeleteObject", // NOI18N
85
sel[0].getUserTask().getSummary());
86             title = NbBundle.getMessage(UTDeleteAction.class, "MSG_ConfirmDeleteObjectTitle"); // NOI18N
87
} else {
88             message = NbBundle.getMessage(UTDeleteAction.class, "MSG_ConfirmDeleteObjects", // NOI18N
89
new Integer JavaDoc(sel.length));
90             title = NbBundle.getMessage(UTDeleteAction.class, "MSG_ConfirmDeleteObjectsTitle"); // NOI18N
91
}
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 JavaDoc event) {
97         if (SwingUtilities.isEventDispatchThread())
98             actionPerformed();
99         else {
100             try {
101                 SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
102                     public void run() {
103                         actionPerformed();
104                     }
105                 });
106             } catch (InterruptedException JavaDoc ex) {
107                 UTUtils.LOGGER.log(java.util.logging.Level.SEVERE,
108                         ex.getMessage(), ex);
109             } catch (InvocationTargetException JavaDoc ex) {
110                 UTUtils.LOGGER.log(java.util.logging.Level.SEVERE,
111                         ex.getMessage(), ex);
112             }
113         }
114     }
115     
116     /**
117      * Performs the action.
118      */

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 JavaDoc 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         // perform action if confirmed
148
if (doConfirm(tasks)) {
149             //tt.setPaintDisabled(true);
150
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 JavaDoc tp = new TreePath JavaDoc(n.getPathToRoot());
164                 tt.select(tp);
165                 tt.scrollTo(tp);
166             }
167             //tt.setPaintDisabled(false);
168
tt.repaint();
169         }
170     }
171 }
172
Popular Tags