KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Code 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.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.Serializable JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
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 /**
40  * Transferable for user tasks.
41  *
42  * @author Tor Norbye
43  * @author tl
44  */

45 public final class UserTasksTransferable implements Transferable JavaDoc, Serializable JavaDoc {
46     private static final long serialVersionUID = 1;
47     
48     /** Flavor for tasks on the clipboard */
49     public static final DataFlavor JavaDoc USER_TASKS_FLAVOR = new DataFlavor JavaDoc(
50         DataFlavor.javaJVMLocalObjectMimeType + "; class=" +
51         java.util.ArrayList JavaDoc.class.getName(),
52         NbBundle.getMessage(UserTasksTransferable.class,
53             "UserTasks")); // NOI18N
54

55     private transient UserTask[] tasks;
56     
57     /**
58      * Construct a task transfer object
59      *
60      * @param tasks dragged tasks
61      */

62     public UserTasksTransferable(UserTask[] tasks) {
63         this.tasks = tasks;
64     }
65
66     public boolean isDataFlavorSupported(DataFlavor JavaDoc flavor) {
67         return flavor.equals(DataFlavor.stringFlavor) ||
68                 flavor.equals(USER_TASKS_FLAVOR) ||
69                 flavor.equals(DataFlavor.getTextPlainUnicodeFlavor());
70     }
71
72     public Object JavaDoc getTransferData(DataFlavor JavaDoc flavor) throws
73             UnsupportedFlavorException JavaDoc, IOException JavaDoc {
74         if (flavor.equals(DataFlavor.stringFlavor)) {
75             return toString(tasks);
76         } else if (flavor.equals(USER_TASKS_FLAVOR)) {
77             ArrayList JavaDoc al = new ArrayList JavaDoc();
78             al.addAll(Arrays.asList(tasks));
79             return al;
80         } else if (flavor.equals(DataFlavor.getTextPlainUnicodeFlavor())) {
81             String JavaDoc charset = DataFlavor.getTextPlainUnicodeFlavor().
82                     getParameter("charset");
83             if (charset == null)
84                 return new ReaderInputStream(
85                         new StringReader JavaDoc(toString(tasks)));
86             else
87                 return new ReaderInputStream(
88                         new StringReader JavaDoc(toString(tasks)), charset);
89         } else {
90             throw new UnsupportedFlavorException JavaDoc(flavor);
91         }
92     }
93
94     private String JavaDoc toString(UserTask[] tasks) {
95         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
96         String JavaDoc lf = System.getProperty("line.separator"); // NOI18N
97
for (int i = 0; i < tasks.length; i++) {
98             if (i != 0)
99                 sb.append(lf);
100             sb.append("- ").append(tasks[i].getSummary()); // NOI18N
101
}
102         return sb.toString();
103     }
104     
105     public DataFlavor JavaDoc[] getTransferDataFlavors() {
106         return new DataFlavor JavaDoc[] {
107             DataFlavor.stringFlavor,
108             USER_TASKS_FLAVOR
109         };
110     }
111 }
112
Popular Tags