KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > transfer > MyTransferHandler


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.transfer;
21
22 import java.awt.datatransfer.Clipboard JavaDoc;
23 import java.awt.datatransfer.DataFlavor JavaDoc;
24 import java.awt.datatransfer.StringSelection JavaDoc;
25 import java.awt.datatransfer.Transferable JavaDoc;
26 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import javax.swing.Icon JavaDoc;
36 import javax.swing.ImageIcon JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.TransferHandler JavaDoc;
39 import javax.swing.tree.TreePath JavaDoc;
40 import org.netbeans.modules.tasklist.usertasks.*;
41 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
42 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList;
43 import org.netbeans.modules.tasklist.usertasks.model.UserTaskObjectList;
44 import org.netbeans.modules.tasklist.usertasks.options.Settings;
45 import org.netbeans.modules.tasklist.usertasks.renderers.UserTaskIconProvider;
46 import org.netbeans.modules.tasklist.usertasks.treetable.AdvancedTreeTableNode;
47 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
48
49 /**
50  * TransferHandler for the table.
51  *
52  * @author tl
53  */

54 public class MyTransferHandler extends TransferHandler JavaDoc {
55     private static DataFlavor JavaDoc X_MOZ_URL;
56     
57     static {
58         try {
59             X_MOZ_URL = new DataFlavor JavaDoc(
60                     "text/x-moz-url; class=\"[B\""); // NOI18N
61
} catch (ClassNotFoundException JavaDoc e) {
62             UTUtils.LOGGER.log(Level.WARNING, e.getMessage(), e);
63         }
64     }
65     
66     /**
67      * Extracts tasks from a Transferable.
68      *
69      * @return extracted tasks or null
70      */

71     private UserTask[] getTasks(Transferable JavaDoc t) {
72         UserTask[] tasks = null;
73         try {
74             if (t.isDataFlavorSupported(UserTasksTransferable.
75                     USER_TASKS_FLAVOR)) {
76                 ArrayList JavaDoc list = (ArrayList JavaDoc) t.getTransferData(
77                         UserTasksTransferable.USER_TASKS_FLAVOR);
78                 tasks = (UserTask[]) list.toArray(new UserTask[list.size()]);
79                 for (int i = 0; i < tasks.length; i++) {
80                     tasks[i] = tasks[i].cloneTask();
81                 }
82             } else if (t.isDataFlavorSupported(X_MOZ_URL)) {
83                 byte[] d = (byte[]) t.getTransferData(X_MOZ_URL);
84                 String JavaDoc s = new String JavaDoc(d, "UTF-16LE"); // NOI18N
85
/* DEBUG
86                 for (int i = 0; i < s.length(); i++) {
87                     UTUtils.LOGGER.fine(Integer.toString(s.charAt(i)));
88                 }*/

89                 int index = s.indexOf("\n");
90                 String JavaDoc url, title;
91                 if (index < 0)
92                     index = s.indexOf(" ");
93                 if (index < 0)
94                     index = s.indexOf("\u0000");
95                 if (index >= 0) {
96                     url = s.substring(0, index);
97                     title = s.substring(index + 1).trim();
98                 } else {
99                     url = s;
100                     title = null;
101                 }
102                 
103                 // DEBUG UTUtils.LOGGER.fine("'" + url + "'"); // NOI18N
104
UserTask ut = new UserTask(title == null ? url : title, null);
105                 ut.setUrl(new URL JavaDoc(url.trim()));
106                 tasks = new UserTask[] {ut};
107             } else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
108                 String JavaDoc text = (String JavaDoc) t.getTransferData(
109                         DataFlavor.stringFlavor);
110                 tasks = UserTask.parse(new StringReader JavaDoc(text));
111             }
112         } catch (IOException JavaDoc e) {
113             UTUtils.LOGGER.log(Level.WARNING, "", e); // NOI18N
114
} catch (UnsupportedFlavorException JavaDoc e) {
115             UTUtils.LOGGER.log(Level.WARNING, "", e); // NOI18N
116
}
117         return tasks;
118     }
119     
120     private UserTask[] transferredTasks;
121     
122     public int getSourceActions(JComponent JavaDoc c) {
123     return COPY_OR_MOVE;
124     }
125
126     protected Transferable JavaDoc createTransferable(JComponent JavaDoc c) {
127         UserTasksTreeTable tt = (UserTasksTreeTable) c;
128         TreePath JavaDoc[] paths = tt.getSelectedPaths();
129         List JavaDoc<UserTask> tasks = new ArrayList JavaDoc<UserTask>();
130         for (int i = 0; i < paths.length; i++) {
131             Object JavaDoc last = paths[i].getLastPathComponent();
132             if (last instanceof UserTaskTreeTableNode) {
133                 tasks.add(((UserTaskTreeTableNode) last).getUserTask());
134             }
135         }
136         UserTask[] t = tasks.toArray(new UserTask[tasks.size()]);
137         t = UserTask.reduce(t);
138         if (t.length == 0)
139             return null;
140         
141         transferredTasks = t;
142         
143         UserTask[] cloned = new UserTask[t.length];
144         for (int i = 0; i < t.length; i++) {
145             cloned[i] = t[i].cloneTask();
146         }
147         return new UserTasksTransferable(cloned);
148     }
149
150     public Icon JavaDoc getVisualRepresentation(Transferable JavaDoc t) {
151         return new ImageIcon JavaDoc(UserTaskIconProvider.getUserTaskImage(
152                 new UserTask("", new UserTaskList()), false));
153     }
154
155     public boolean canImport(JComponent JavaDoc comp, DataFlavor JavaDoc[] transferFlavors) {
156         for (int i = 0; i < transferFlavors.length; i++) {
157             if (transferFlavors[i].equals(
158                     UserTasksTransferable.USER_TASKS_FLAVOR) ||
159                     transferFlavors[i].equals(DataFlavor.stringFlavor) ||
160                     transferFlavors[i].equals(X_MOZ_URL)) {
161                 return true;
162             }
163         }
164         return false;
165     }
166
167     /**
168      * Imports data.
169      *
170      * @param comp TreeTable
171      * @param t dragged data
172      * @param topLevel true = the tasks will be pasted at top level. Otherwise
173      * current selected node will be the parent.
174      */

175     public boolean importData(JComponent JavaDoc comp, Transferable JavaDoc t,
176             boolean topLevel) {
177         if (UTUtils.LOGGER.isLoggable(Level.FINE)) {
178             DataFlavor JavaDoc[] dfs = t.getTransferDataFlavors();
179             for (DataFlavor JavaDoc df: dfs) {
180                 UTUtils.LOGGER.fine(df.getMimeType());
181             }
182         }
183         
184         // dragged tasks
185
UserTask[] tasks = getTasks(t);
186         if (tasks == null)
187             return false;
188         
189         // target
190
UserTasksTreeTable tt = (UserTasksTreeTable) comp;
191         UserTask target = null;
192         UserTaskObjectList list = null;
193
194         TreePath JavaDoc tp = tt.getSelectedPath();
195         if (!topLevel && tp != null && tp.getLastPathComponent()
196                 instanceof UserTaskTreeTableNode) {
197             UserTaskTreeTableNode obj =
198                     (UserTaskTreeTableNode) tp.getLastPathComponent();
199             target = obj.getUserTask();
200             list = target.getSubtasks();
201         } else {
202             list = ((UserTasksTreeTableModel) tt.getTreeTableModel()).
203                     getUserTaskList().getSubtasks();
204         }
205         
206         if (transferredTasks != null && target != null) {
207             for (int i = 0; i < transferredTasks.length; i++) {
208                 if (transferredTasks[i].isAncestorOf(target)) {
209                     return false;
210                 }
211             }
212         }
213         
214         List JavaDoc<UserTask> tasks_ = new ArrayList JavaDoc<UserTask>(Arrays.asList(tasks));
215         Iterator JavaDoc<UserTask> it = tasks_.iterator();
216         while (it.hasNext()) {
217             UserTask ut = it.next();
218             if (UTUtils.identityIndexOf(list, ut) >= 0) {
219                 it.remove();
220             }
221         }
222         
223         if (tasks_.size() == 0)
224             return false;
225
226         if (Settings.getDefault().getAppend())
227             list.addAll(tasks_);
228         else
229             list.addAll(0, tasks_);
230         if (target != null && Settings.getDefault().getAutoSwitchToComputed()) {
231             target.setValuesComputed(true);
232         }
233
234         UserTasksTreeTable uttt = (UserTasksTreeTable) comp;
235         if (target != null) {
236             uttt.expandPath(uttt.findPath(target));
237         }
238         TreePath JavaDoc[] paths = new TreePath JavaDoc[tasks_.size()];
239         for (int i = 0; i < paths.length; i++) {
240             paths[i] = uttt.findPath((UserTask) tasks_.get(i));
241         }
242         uttt.select(paths);
243         uttt.scrollTo(paths[0]);
244         
245         return true;
246     }
247     
248     public boolean importData(JComponent JavaDoc comp, Transferable JavaDoc t) {
249         return importData(comp, t, false);
250     }
251
252     protected void exportDone(JComponent JavaDoc source, Transferable JavaDoc data, int action) {
253         if (action == MOVE && transferredTasks != null) {
254             UserTasksTreeTable tt = (UserTasksTreeTable) source;
255             
256             // TODO: any delete in the tree leads to
257
// clearing the selection in the table. This is a workaround:
258
TreePath JavaDoc[] sel = tt.getSelectedPaths();
259             
260             for (int i = 0; i < transferredTasks.length; i++) {
261                 UserTask ut = transferredTasks[i];
262                 if (ut.getParent() != null)
263                     ut.getParent().getSubtasks().remove(ut);
264                 else
265                     ut.getList().getSubtasks().remove(ut);
266                 ut.destroy();
267             }
268             
269             tt.select(sel);
270         }
271         transferredTasks = null;
272     }
273
274     public void exportToClipboard(JComponent JavaDoc comp, Clipboard JavaDoc clip, int action) {
275         boolean exportSuccess = false;
276         Transferable JavaDoc t = null;
277
278     int clipboardAction = getSourceActions(comp) & action;
279     if (clipboardAction != NONE) {
280             t = createTransferable(comp);
281             if (t != null) {
282                 clip.setContents(t, null);
283                 exportSuccess = true;
284             }
285         }
286
287         if (exportSuccess && action == MOVE && transferredTasks != null) {
288             UserTasksTreeTable tt = (UserTasksTreeTable) comp;
289             tt.clearSelection();
290             TreePath JavaDoc next = null;
291             for (int i = 0; i < transferredTasks.length; i++) {
292                 UserTask ut = transferredTasks[i];
293                 if (i == transferredTasks.length - 1) {
294                     TreePath JavaDoc sel = tt.findPath(ut);
295                     AdvancedTreeTableNode ttn = (AdvancedTreeTableNode)
296                             ((AdvancedTreeTableNode) sel.getLastPathComponent()).
297                             findNextNodeAfterDelete();
298                     next = new TreePath JavaDoc(ttn.getPathToRoot());
299                 }
300                 if (ut.getParent() != null)
301                     ut.getParent().getSubtasks().remove(ut);
302                 else
303                     ut.getList().getSubtasks().remove(ut);
304                 ut.destroy();
305             }
306             tt.select(next);
307         }
308         
309         transferredTasks = null;
310     }
311 }
312
Popular Tags