KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > MailDnDTransferHandler


1 package SnowMailClient.view;
2
3 /*
4  * from ArrayListTransferHandler.java is used by the 1.4 DragListDemo.java example.
5  */

6
7 import snow.utils.gui.VectorTransferable;
8
9 import java.util.*;
10 import java.io.*;
11 import java.awt.*;
12 import java.awt.event.*;
13 import java.awt.datatransfer.*;
14 import java.awt.dnd.*;
15 import javax.swing.*;
16 import javax.swing.table.*;
17 import javax.swing.tree.*;
18
19 /** for the originator JTables
20       table.setDragEnabled(true);
21       table.setTransferHandler(new TableExportTransferHandler());
22 */

23 public final class MailDnDTransferHandler extends TransferHandler
24 {
25     final String JavaDoc id;
26     final FolderView transferHandlerSource;
27
28     Object JavaDoc source = null;
29     int[] indices = null;
30
31     public MailDnDTransferHandler(String JavaDoc id, FolderView transferHandlerSource)
32     {
33        this.transferHandlerSource = transferHandlerSource;
34        this.id = id;
35     }
36
37     /** import on tree
38     */

39 @SuppressWarnings JavaDoc("unchecked")
40     public boolean importData(JComponent c, Transferable t)
41     {
42         System.out.println("Import data on "+c);
43
44         if(c instanceof JTable) return false;
45
46         Vector<Object JavaDoc> transferedVector = null;
47         if (!canImport(c, t.getTransferDataFlavors()))
48         {
49             return false;
50         }
51
52         try
53         {
54
55             if (hasLocalVectorFlavor(t.getTransferDataFlavors()))
56             {
57                 transferedVector = (Vector) t.getTransferData(VectorTransferable.localVectorFlavor);
58             }
59             else if (hasSerialVectorFlavor(t.getTransferDataFlavors()))
60             {
61                 transferedVector = (Vector) t.getTransferData(VectorTransferable.serialVectorFlavor);
62             }
63             else
64             {
65                 return false;
66             }
67
68         } catch (UnsupportedFlavorException ufe) {
69             System.out.println("importData: unsupported data flavor");
70             return false;
71         } catch (IOException ioe) {
72             System.out.println("importData: I/O exception");
73             return false;
74         }
75
76
77         if(c instanceof JTree)
78         {
79            return importData_to_JTree((JTree) c, transferedVector);
80         }
81
82         return false;
83    }
84
85    private boolean importData_to_JTree(JTree target, Vector<Object JavaDoc> transferedVector)
86    {
87         System.out.println("Import data to JTree");
88         System.out.println("model = "+target.getModel().getClass().getName());
89
90         System.out.println(""+this.transferHandlerSource.getSelectedMessages().length+" messages");
91
92         /*if(!(target.getSelectionRows().length==1)) return false;
93         TreePath selPath = target.getSelectionPath();
94         DefaultTreeModel dtm = (DefaultTreeModel) target.getModel();
95         MutableTreeNode mtn = new DefaultMutableTreeNode("Imported: "+transferedVector);
96         MutableTreeNode parent = (MutableTreeNode) selPath.getLastPathComponent();
97         dtm.insertNodeInto(mtn, parent, parent.getChildCount());*/

98
99         return true;
100    }
101
102
103
104     /** called when the export has been accepted and worked
105     */

106     protected final void exportDone(JComponent c, Transferable data, int action)
107     {
108
109         if ((action == MOVE) && (indices != null))
110         {
111            if(source instanceof JTable)
112            {
113               JTable table = (JTable) source;
114            }
115            else if(source instanceof JTree)
116            {
117               JTree tree = (JTree) source;
118            }
119            else
120            {
121               System.out.println("not recognized source "+source);
122            }
123
124         }
125         indices = null;
126     }
127
128     private boolean hasLocalVectorFlavor(DataFlavor[] flavors) {
129         if (VectorTransferable.localVectorFlavor == null) {
130             return false;
131         }
132
133         for (int i = 0; i < flavors.length; i++) {
134             if (flavors[i].equals(VectorTransferable.localVectorFlavor)) {
135                 return true;
136             }
137         }
138         return false;
139     }
140
141     private boolean hasSerialVectorFlavor(DataFlavor[] flavors) {
142         if (VectorTransferable.serialVectorFlavor == null) {
143             return false;
144         }
145
146         for (int i = 0; i < flavors.length; i++)
147         {
148             if (flavors[i].equals(VectorTransferable.serialVectorFlavor))
149             {
150                 return true;
151             }
152         }
153         return false;
154     }
155
156     /** this is called
157     */

158     public boolean canImport(JComponent c, DataFlavor[] flavors)
159     {
160         // otherwise it destroys the selection on the table view (?)
161
if(c instanceof JTable) return false;
162         if (hasLocalVectorFlavor(flavors)) { return true; }
163         if (hasSerialVectorFlavor(flavors)) { return true; }
164         return false;
165     }
166
167     /** Transfer the selected table source row indices
168     */

169     protected Transferable createTransferable(JComponent c)
170     {
171         if (c instanceof JTable)
172         {
173             JTable table = (JTable) c;
174             source = table;
175             indices = table.getSelectedRows();
176             if(indices.length==0) return null;
177
178             Vector<Object JavaDoc> v = new Vector<Object JavaDoc>();
179             v.addElement(id);
180             v.addElement("JTable");
181             v.addElement("selectedRows");
182             v.addElement(indices);
183             v.addElement(this); // test
184
return new VectorTransferable(v);
185         }
186
187         else if (c instanceof JTree)
188         {
189             JTree tree = (JTree) c;
190             //source = (JTable) c;
191
//indices = source.getSelectedRows();
192
//if(indices.length==0) return null;
193

194             Vector<Object JavaDoc> v = new Vector<Object JavaDoc>();
195             v.addElement(id);
196             v.addElement("JTree");
197             v.addElement("selectionRows");
198             v.addElement(tree.getSelectionRows());
199             return new VectorTransferable(v);
200         }
201         else
202         {
203
204             return null;
205         }
206     }
207
208     public int getSourceActions(JComponent c)
209     {
210         return COPY_OR_MOVE;
211     }
212
213
214  /**
215   * Static main method
216   */

217   public static void main( String JavaDoc[] arguments )
218   {
219      //DnDTest dt = new DnDTest();
220
} // main
221

222 } // MailDnDTransferHandler
Popular Tags