1 19 20 package org.netbeans.modules.tasklist.usertasks.transfer; 21 22 import java.awt.datatransfer.DataFlavor ; 23 import java.awt.datatransfer.Transferable ; 24 import java.awt.datatransfer.UnsupportedFlavorException ; 25 import java.io.IOException ; 26 import java.io.Serializable ; 27 import java.io.StringReader ; 28 import java.io.StringWriter ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import org.netbeans.modules.tasklist.usertasks.*; 32 import org.openide.util.NbBundle; 33 import org.openide.util.datatransfer.ExTransferable; 34 import org.openide.util.datatransfer.MultiTransferObject; 35 import org.netbeans.modules.tasklist.usertasks.model.UserTask; 36 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 37 import org.openide.util.io.ReaderInputStream; 38 39 45 public final class UserTasksTransferable implements Transferable , Serializable { 46 private static final long serialVersionUID = 1; 47 48 49 public static final DataFlavor USER_TASKS_FLAVOR = new DataFlavor ( 50 DataFlavor.javaJVMLocalObjectMimeType + "; class=" + 51 java.util.ArrayList .class.getName(), 52 NbBundle.getMessage(UserTasksTransferable.class, 53 "UserTasks")); 55 private transient UserTask[] tasks; 56 57 62 public UserTasksTransferable(UserTask[] tasks) { 63 this.tasks = tasks; 64 } 65 66 public boolean isDataFlavorSupported(DataFlavor flavor) { 67 return flavor.equals(DataFlavor.stringFlavor) || 68 flavor.equals(USER_TASKS_FLAVOR) || 69 flavor.equals(DataFlavor.getTextPlainUnicodeFlavor()); 70 } 71 72 public Object getTransferData(DataFlavor flavor) throws 73 UnsupportedFlavorException , IOException { 74 if (flavor.equals(DataFlavor.stringFlavor)) { 75 return toString(tasks); 76 } else if (flavor.equals(USER_TASKS_FLAVOR)) { 77 ArrayList al = new ArrayList (); 78 al.addAll(Arrays.asList(tasks)); 79 return al; 80 } else if (flavor.equals(DataFlavor.getTextPlainUnicodeFlavor())) { 81 String charset = DataFlavor.getTextPlainUnicodeFlavor(). 82 getParameter("charset"); 83 if (charset == null) 84 return new ReaderInputStream( 85 new StringReader (toString(tasks))); 86 else 87 return new ReaderInputStream( 88 new StringReader (toString(tasks)), charset); 89 } else { 90 throw new UnsupportedFlavorException (flavor); 91 } 92 } 93 94 private String toString(UserTask[] tasks) { 95 StringBuffer sb = new StringBuffer (); 96 String lf = System.getProperty("line.separator"); for (int i = 0; i < tasks.length; i++) { 98 if (i != 0) 99 sb.append(lf); 100 sb.append("- ").append(tasks[i].getSummary()); } 102 return sb.toString(); 103 } 104 105 public DataFlavor [] getTransferDataFlavors() { 106 return new DataFlavor [] { 107 DataFlavor.stringFlavor, 108 USER_TASKS_FLAVOR 109 }; 110 } 111 } 112 | Popular Tags |