1 19 20 package org.netbeans.modules.tasklist.core; 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.StringReader ; 27 import java.io.StringWriter ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import org.openide.ErrorManager; 31 import org.openide.util.NbBundle; 32 33 import org.openide.util.datatransfer.ExTransferable; 34 import org.openide.util.datatransfer.MultiTransferObject; 35 import org.openide.util.datatransfer.ExClipboard; 36 37 42 public final class TaskTransfer implements ExClipboard.Convertor { 43 private static final Logger LOGGER = TLUtils.getLogger(TaskNode.class); 44 45 static { 46 LOGGER.setLevel(Level.OFF); 47 } 48 49 50 public static final DataFlavor TODO_FLAVOR = new DataFlavor ( 51 Task.class, NbBundle.getMessage(TaskTransfer.class, "LBL_todo_flavor")); 53 54 public TaskTransfer() {} 55 56 66 public Transferable convert(final Transferable t) { 67 boolean supportsString = t.isDataFlavorSupported(DataFlavor.stringFlavor); 68 boolean supportsTodo = t.isDataFlavorSupported(TODO_FLAVOR); 69 if (supportsString && !supportsTodo) { 70 ExTransferable t2 = ExTransferable.create(t); 72 t2.put(new ExTransferable.Single(TODO_FLAVOR) { 73 protected Object getData() throws IOException , UnsupportedFlavorException { 74 String text = (String )t.getTransferData(DataFlavor.stringFlavor); 75 return Task.parse(new StringReader (text)); 76 } 77 }); 78 return t2; 79 } else if (!supportsString && supportsTodo) { 80 ExTransferable t2 = ExTransferable.create(t); 82 t2.put(new ExTransferable.Single(DataFlavor.stringFlavor) { 83 protected Object getData() throws IOException , UnsupportedFlavorException { 84 Task item = (Task)t.getTransferData(TODO_FLAVOR); 85 StringWriter wr = new StringWriter (); 86 Task.generate(item, wr); 87 return wr.toString(); 88 } 89 }); 90 return t2; 91 } else if (t.isDataFlavorSupported(ExTransferable.multiFlavor)) { 92 LOGGER.fine("multi selection"); 93 try { 95 final MultiTransferObject mto = (MultiTransferObject) 96 t.getTransferData(ExTransferable.multiFlavor); 97 boolean allSupportTodo = mto.areDataFlavorsSupported( 98 new DataFlavor [] {TODO_FLAVOR}); 99 if (allSupportTodo) { 100 LOGGER.fine("multi selection all supports todo"); 101 ExTransferable t2 = ExTransferable.create(t); 102 if (!supportsString) { 103 t2.put(new ExTransferable.Single(DataFlavor.stringFlavor) { 105 protected Object getData() throws IOException , UnsupportedFlavorException { 106 StringWriter wr = new StringWriter (); 107 for (int i = 0; i < mto.getCount(); i++) { 108 Task item = 109 (Task)mto.getTransferData(i, TODO_FLAVOR); 110 Task.generate(item, wr); 111 } 112 return wr.toString(); 113 } 114 }); 115 } 116 return t2; 117 } } catch (Exception e) { 119 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 121 } 122 } 123 return t; 124 } 125 126 } 127 | Popular Tags |