1 19 20 package org.netbeans.modules.tasklist.usertasks.transfer; 21 22 import java.awt.datatransfer.Clipboard ; 23 import java.awt.datatransfer.DataFlavor ; 24 import java.awt.datatransfer.StringSelection ; 25 import java.awt.datatransfer.Transferable ; 26 import java.awt.datatransfer.UnsupportedFlavorException ; 27 import java.io.IOException ; 28 import java.io.StringReader ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.Arrays ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.logging.Level ; 35 import javax.swing.Icon ; 36 import javax.swing.ImageIcon ; 37 import javax.swing.JComponent ; 38 import javax.swing.TransferHandler ; 39 import javax.swing.tree.TreePath ; 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 54 public class MyTransferHandler extends TransferHandler { 55 private static DataFlavor X_MOZ_URL; 56 57 static { 58 try { 59 X_MOZ_URL = new DataFlavor ( 60 "text/x-moz-url; class=\"[B\""); } catch (ClassNotFoundException e) { 62 UTUtils.LOGGER.log(Level.WARNING, e.getMessage(), e); 63 } 64 } 65 66 71 private UserTask[] getTasks(Transferable t) { 72 UserTask[] tasks = null; 73 try { 74 if (t.isDataFlavorSupported(UserTasksTransferable. 75 USER_TASKS_FLAVOR)) { 76 ArrayList list = (ArrayList ) 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 s = new String (d, "UTF-16LE"); 89 int index = s.indexOf("\n"); 90 String 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 UserTask ut = new UserTask(title == null ? url : title, null); 105 ut.setUrl(new URL (url.trim())); 106 tasks = new UserTask[] {ut}; 107 } else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) { 108 String text = (String ) t.getTransferData( 109 DataFlavor.stringFlavor); 110 tasks = UserTask.parse(new StringReader (text)); 111 } 112 } catch (IOException e) { 113 UTUtils.LOGGER.log(Level.WARNING, "", e); } catch (UnsupportedFlavorException e) { 115 UTUtils.LOGGER.log(Level.WARNING, "", e); } 117 return tasks; 118 } 119 120 private UserTask[] transferredTasks; 121 122 public int getSourceActions(JComponent c) { 123 return COPY_OR_MOVE; 124 } 125 126 protected Transferable createTransferable(JComponent c) { 127 UserTasksTreeTable tt = (UserTasksTreeTable) c; 128 TreePath [] paths = tt.getSelectedPaths(); 129 List <UserTask> tasks = new ArrayList <UserTask>(); 130 for (int i = 0; i < paths.length; i++) { 131 Object 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 getVisualRepresentation(Transferable t) { 151 return new ImageIcon (UserTaskIconProvider.getUserTaskImage( 152 new UserTask("", new UserTaskList()), false)); 153 } 154 155 public boolean canImport(JComponent comp, DataFlavor [] 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 175 public boolean importData(JComponent comp, Transferable t, 176 boolean topLevel) { 177 if (UTUtils.LOGGER.isLoggable(Level.FINE)) { 178 DataFlavor [] dfs = t.getTransferDataFlavors(); 179 for (DataFlavor df: dfs) { 180 UTUtils.LOGGER.fine(df.getMimeType()); 181 } 182 } 183 184 UserTask[] tasks = getTasks(t); 186 if (tasks == null) 187 return false; 188 189 UserTasksTreeTable tt = (UserTasksTreeTable) comp; 191 UserTask target = null; 192 UserTaskObjectList list = null; 193 194 TreePath 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 <UserTask> tasks_ = new ArrayList <UserTask>(Arrays.asList(tasks)); 215 Iterator <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 [] paths = new TreePath [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 comp, Transferable t) { 249 return importData(comp, t, false); 250 } 251 252 protected void exportDone(JComponent source, Transferable data, int action) { 253 if (action == MOVE && transferredTasks != null) { 254 UserTasksTreeTable tt = (UserTasksTreeTable) source; 255 256 TreePath [] 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 comp, Clipboard clip, int action) { 275 boolean exportSuccess = false; 276 Transferable 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 next = null; 291 for (int i = 0; i < transferredTasks.length; i++) { 292 UserTask ut = transferredTasks[i]; 293 if (i == transferredTasks.length - 1) { 294 TreePath sel = tt.findPath(ut); 295 AdvancedTreeTableNode ttn = (AdvancedTreeTableNode) 296 ((AdvancedTreeTableNode) sel.getLastPathComponent()). 297 findNextNodeAfterDelete(); 298 next = new TreePath (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 |