KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > TaskTransfer


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.core;
21
22 import java.awt.datatransfer.DataFlavor JavaDoc;
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
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 /**
38  * Utilities dealing with data transfer operations on todo items.
39  *
40  * @author Tor Norbye
41  */

42 public final class TaskTransfer implements ExClipboard.Convertor {
43     private static final Logger JavaDoc LOGGER = TLUtils.getLogger(TaskNode.class);
44     
45     static {
46         LOGGER.setLevel(Level.OFF);
47     }
48     
49     /** Flavor for tasks on the clipboard */
50     public static final DataFlavor JavaDoc TODO_FLAVOR = new DataFlavor JavaDoc(
51         Task.class, NbBundle.getMessage(TaskTransfer.class, "LBL_todo_flavor")); // NOI18N
52

53     /** Construct a task transfer object */
54     public TaskTransfer() {}
55     
56     /** Convert a transferable.
57      * If it has just a text selection, make a corresponding task
58      * by parsing it.
59      * If just a task, make a corresponding text selection by writing it out.
60      * If it has a multiple selection all of which are todo items, make a text
61      * selection with all of them concatenated (and leave the todoitems there too
62      * obviously).
63      * Otherwise leave it alone.
64      * @param t The transferable to convert
65      * @return The converted transferable */

66     public Transferable JavaDoc convert(final Transferable JavaDoc t) {
67         boolean supportsString = t.isDataFlavorSupported(DataFlavor.stringFlavor);
68         boolean supportsTodo = t.isDataFlavorSupported(TODO_FLAVOR);
69         if (supportsString && !supportsTodo) {
70         // Return a new TodoItem from a string
71
ExTransferable t2 = ExTransferable.create(t);
72             t2.put(new ExTransferable.Single(TODO_FLAVOR) {
73                 protected Object JavaDoc getData() throws IOException JavaDoc, UnsupportedFlavorException JavaDoc {
74                     String JavaDoc text = (String JavaDoc)t.getTransferData(DataFlavor.stringFlavor);
75             return Task.parse(new StringReader JavaDoc(text));
76                 }
77             });
78             return t2;
79         } else if (!supportsString && supportsTodo) {
80         // Return a new string from a todo item
81
ExTransferable t2 = ExTransferable.create(t);
82             t2.put(new ExTransferable.Single(DataFlavor.stringFlavor) {
83                 protected Object JavaDoc getData() throws IOException JavaDoc, UnsupportedFlavorException JavaDoc {
84                     Task item = (Task)t.getTransferData(TODO_FLAVOR);
85                     StringWriter JavaDoc wr = new StringWriter JavaDoc();
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         // Multiselection
94
try {
95                 final MultiTransferObject mto = (MultiTransferObject)
96                     t.getTransferData(ExTransferable.multiFlavor);
97                 boolean allSupportTodo = mto.areDataFlavorsSupported(
98                     new DataFlavor JavaDoc[] {TODO_FLAVOR});
99                 if (allSupportTodo) {
100                     LOGGER.fine("multi selection all supports todo");
101                     ExTransferable t2 = ExTransferable.create(t);
102                     if (!supportsString) {
103             // Create string representation
104
t2.put(new ExTransferable.Single(DataFlavor.stringFlavor) {
105                             protected Object JavaDoc getData() throws IOException JavaDoc, UnsupportedFlavorException JavaDoc {
106                                 StringWriter JavaDoc wr = new StringWriter JavaDoc();
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                 } // else: not all support todoitems - so don't do anything
118
} catch (Exception JavaDoc e) {
119                 // Should not happen: IOException, UnsupportedFlavorException
120
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
121             }
122         }
123         return t;
124     }
125
126 }
127
Popular Tags