KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > VectorTransferable


1 package snow.utils.gui;
2
3 import java.util.*;
4 import java.io.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.awt.datatransfer.*;
8 import java.awt.dnd.*;
9 import javax.swing.*;
10
11 /** a transferable java Vector object. Useful in DnD operations.
12 */

13 public class VectorTransferable implements Transferable
14 {
15
16     public static DataFlavor localVectorFlavor, serialVectorFlavor;
17     public static String JavaDoc localVectorType = DataFlavor.javaJVMLocalObjectMimeType + ";class=java.util.Vector";
18
19
20     static
21     {
22         try {
23             localVectorFlavor = new DataFlavor(localVectorType);
24         } catch (ClassNotFoundException JavaDoc e) {
25             System.out.println(
26              "VectorTransferable: unable to create data flavor");
27         }
28         serialVectorFlavor = new DataFlavor(Vector.class, "Vector");
29
30     }
31
32     // transferred data
33
public Vector v;
34     public DataFlavor transferedDataFlavor;
35
36
37     public VectorTransferable(Vector v)
38     {
39         this.v = v;
40     }
41
42     public Object JavaDoc getTransferData(DataFlavor flavor)
43                              throws UnsupportedFlavorException {
44         if (!isDataFlavorSupported(flavor))
45         {
46             throw new UnsupportedFlavorException(flavor);
47         }
48
49         // this allow transferring to a text area or to the native system !
50
if( flavor.equals( DataFlavor.stringFlavor ))
51         {
52           transferedDataFlavor = DataFlavor.stringFlavor;
53           return ""+v;
54         }
55
56         transferedDataFlavor = flavor;
57         return v;
58     }
59
60     public DataFlavor[] getTransferDataFlavors() {
61         return new DataFlavor[] { localVectorFlavor,
62                                   serialVectorFlavor,
63                                   DataFlavor.stringFlavor
64                                 };
65     }
66
67     public boolean isDataFlavorSupported(DataFlavor flavor) {
68         if (localVectorFlavor.equals(flavor)) return true;
69         if (serialVectorFlavor.equals(flavor)) return true;
70         if (DataFlavor.stringFlavor.equals(flavor)) return true;
71         return false;
72     }
73
74
75   public static void main( String JavaDoc[] arguments )
76   {
77 // DnDTest dt = new DnDTest();
78
} // main
79

80
81 } // VectorTransferable
Popular Tags