KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DataTransferSupport


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  * DataTransferSupport.java
21  *
22  * Created on June 18, 2001, 1:26 PM
23  */

24
25 package org.openide.loaders;
26
27
28 import java.awt.datatransfer.Transferable JavaDoc;
29 import java.io.*;
30 import java.util.Arrays JavaDoc;
31 import java.util.logging.*;
32 import org.openide.*;
33 import org.openide.cookies.InstanceCookie;
34 import org.openide.filesystems.*;
35 import org.openide.util.*;
36 import org.openide.util.datatransfer.*;
37
38 /** Support for data transfer Paste operation.
39  * @author Vita Stejskal
40  */

41 abstract class DataTransferSupport {
42
43     /** Defines array of classes implementing paste for specified clipboard operation.
44      * @param op clopboard operation to specify paste types for
45      * @return array of classes extending PasteTypeExt class
46      */

47     protected abstract PasteTypeExt [] definePasteTypes (int op);
48     /** Defines array of data clipboard operations recognized by this paste support.
49      * @return array of DataFlavors
50      */

51     protected abstract int [] defineOperations ();
52     /** Override in order to support additional paste types.
53      * @param t clipboard Transferable object, list of transfered DataObjects with their flavors
54      * @param s list of paste types supported for transfered objects
55      */

56     protected void handleCreatePasteTypes (Transferable JavaDoc t, java.util.List JavaDoc<PasteType> s) {
57     }
58     /** Fills in the list of paste types available for given set to transfered
59      * DataObjects.
60      * @param t clipboard Transferable object, list of transfered DataObjects with their flavors
61      * @param s list of paste types supported for transfered objects
62      */

63     public final void createPasteTypes (Transferable JavaDoc t, java.util.List JavaDoc<PasteType> s) {
64         /** All supported operations. */
65         int [] ops = defineOperations ();
66
67         for (int i = 0; i < ops.length; i++) {
68             DataObject objs [] = LoaderTransfer.getDataObjects (t, ops[i]);
69             PasteTypeExt pts [];
70
71             if (objs == null || objs.length == 0)
72                 continue;
73
74             pts = definePasteTypes (ops[i]);
75
76             for (int j = 0; j < pts.length; j++) {
77                 pts[j].setDataObjects (objs);
78                 if (pts[j].canPaste ())
79                     s.add (pts[j]);
80             }
81         }
82
83         handleCreatePasteTypes (t, s);
84     }
85     
86     private static final Logger err = Logger.getLogger("org.openide.loaders.DataTransferSupport"); //NOI18N
87

88     /** Supports paste of multiple DataObject at once.
89      */

90     static abstract class PasteTypeExt extends PasteType {
91         /** All DataObjects being pasted. */
92         private DataObject objs [];
93         /** Create paste type. */
94         public PasteTypeExt () {
95         }
96         /** Can DataObject be pasted.
97          * @param obj DataObject to be pasted
98          * @return result of the test
99          */

100         protected abstract boolean handleCanPaste (DataObject obj);
101         /** Handles the paste action
102         * @param obj pasted DataObject
103         */

104         protected abstract void handlePaste (DataObject obj) throws IOException;
105         /** Could be clipboard cleand up after the paste operation is finished or
106          * should its content be preserved.
107          * @return default implementation returns <code>false</code>
108          */

109         protected boolean cleanClipboard () {
110             return false;
111         }
112         /** Paste all DataObjects */
113         public final boolean canPaste () {
114             for (int i = 0; i < objs.length; i++) {
115                 if (!handleCanPaste (objs[i]))
116                     return false;
117             }
118             return true;
119         }
120         /** Paste all DataObjects */
121         public final Transferable JavaDoc paste() throws IOException {
122             if (javax.swing.SwingUtilities.isEventDispatchThread()) {
123                 org.openide.util.RequestProcessor.getDefault().post(new java.lang.Runnable JavaDoc() {
124
125                                                                         public void run() {
126                                                                             java.lang.String JavaDoc n = org.openide.awt.Actions.cutAmpersand(getName());
127                                                                             org.netbeans.api.progress.ProgressHandle h = org.netbeans.api.progress.ProgressHandleFactory.createHandle(n);
128
129                                                                             h.start();
130                                                                             h.switchToIndeterminate();
131                                                                             try {
132                                                                                 doPaste();
133                                                                             }
134                                                                             catch (java.io.IOException JavaDoc ioe) {
135                                                                                 Exceptions.printStackTrace(ioe);
136                                                                             }
137                                                                             finally {
138                                                                                 h.finish();
139                                                                             }
140                                                                         }
141                                                                     });
142             } else {
143                 doPaste();
144             }
145             // clear clipboard or preserve content
146
return cleanClipboard() ? ExTransferable.EMPTY : null;
147         }
148         
149         private void doPaste () throws IOException {
150         if (err.isLoggable (Level.FINE)) {
151         err.log(Level.FINE, null, new Throwable JavaDoc ("Issue #58666: Called " + this + " doPaste() on objects " + Arrays.asList (objs))); // NOI18N
152
}
153             for (int i = 0; i < objs.length; i++)
154                 handlePaste (objs[i]);
155         }
156     
157         public final void setDataObjects (DataObject objs []) {
158             this.objs = objs;
159         }
160     }
161
162     /** Paste types for data objects.
163     */

164     static class SerializePaste extends PasteType {
165         private InstanceCookie cookie;
166         private DataFolder target;
167         
168         /**
169         * @param obj object to work with
170         */

171         public SerializePaste (DataFolder target, InstanceCookie cookie) {
172             this.cookie = cookie;
173             this.target = target;
174         }
175
176         /** The name is obtained from the bundle.
177         * @return the name
178         */

179         public String JavaDoc getName () {
180             return DataObject.getString ("PT_serialize");
181         }
182
183         public HelpCtx getHelpCtx () {
184             return new HelpCtx (SerializePaste.class);
185         }
186
187         /** Paste.
188         */

189         public final Transferable JavaDoc paste () throws IOException {
190             final DataFolder trg = getTargetFolder();
191             String JavaDoc name = cookie.instanceName ();
192             int i = name.lastIndexOf ('.') + 1;
193             if (i != 0 && i != name.length ()) {
194                 name = name.substring (i);
195             }
196
197             name = FileUtil.findFreeFileName (trg.getPrimaryFile (), name, "ser"); // NOI18N
198

199
200             final NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine (
201                                                       DataObject.getString ("SerializeBean_Text"),
202                                                       DataObject.getString ("SerializeBean_Title")
203                                                   );
204             nd.setInputText (name);
205
206             if (NotifyDescriptor.OK_OPTION == DialogDisplayer.getDefault ().notify (nd)) {
207                 DataObjectPool.getPOOL().runAtomicAction (trg.getPrimaryFile (), new FileSystem.AtomicAction () {
208                             public void run () throws IOException {
209                                 FileObject fo = trg.getPrimaryFile ().createData (nd.getInputText (), "ser"); // NOI18N
210
FileLock lock = fo.lock ();
211                                 ObjectOutputStream oos = null;
212                                 try {
213                                     oos = new ObjectOutputStream (
214                                               new java.io.BufferedOutputStream JavaDoc (fo.getOutputStream (lock))
215                                           );
216                                     oos.writeObject (cookie.instanceCreate ());
217                                 } catch (ClassNotFoundException JavaDoc e) {
218                                     throw new IOException (e.getMessage ());
219                                 } finally {
220                                     if (oos != null) oos.close ();
221                                     lock.releaseLock ();
222                                 }
223                             }
224                         });
225             }
226
227             // preserve clipboard
228
return null;
229         }
230
231         protected DataFolder getTargetFolder() throws IOException {
232             return target;
233         }
234     }
235
236     /** Paste types for data objects.
237     */

238     static class InstantiatePaste extends PasteType {
239         private InstanceCookie cookie;
240         private DataFolder target;
241         
242         /**
243         * @param obj object to work with
244         */

245         public InstantiatePaste (DataFolder target, InstanceCookie cookie) {
246             this.cookie = cookie;
247             this.target = target;
248         }
249
250         /** The name is obtained from the bundle.
251         * @return the name
252         */

253         public String JavaDoc getName () {
254             return DataObject.getString ("PT_instance");
255         }
256
257         public HelpCtx getHelpCtx () {
258             return new HelpCtx (InstantiatePaste.class);
259         }
260
261         /** Paste.
262         */

263         public final Transferable JavaDoc paste () throws IOException {
264             try {
265                 Class JavaDoc clazz = cookie.instanceClass ();
266                 
267                 // create the instance
268
InstanceDataObject.create(getTargetFolder(), null, clazz);
269             } catch (ClassNotFoundException JavaDoc ex) {
270                 throw new IOException (ex.getMessage ());
271             }
272
273             // preserve clipboard
274
return null;
275         }
276
277         protected DataFolder getTargetFolder() throws IOException {
278             return target;
279         }
280     }
281 }
282
Popular Tags