KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > folders > ArrayListTransfertHandler


1 package SnowMailClient.view.folders;
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 /*
12  * ArrayListTransferHandler.java is used by the 1.4
13  * DragListDemo.java example.
14  */

15 @SuppressWarnings JavaDoc("unchecked")
16 public final class ArrayListTransfertHandler extends TransferHandler {
17     private DataFlavor localArrayListFlavor, serialArrayListFlavor;
18     private String JavaDoc localArrayListType = DataFlavor.javaJVMLocalObjectMimeType +
19                                 ";class=java.util.ArrayList";
20     JList source = null;
21     int[] indices = null;
22     int addIndex = -1; //Location where items were added
23
int addCount = 0; //Number of items added
24

25     ArrayListTransfertHandler() {
26         try {
27             localArrayListFlavor = new DataFlavor(localArrayListType);
28         } catch (ClassNotFoundException JavaDoc e) {
29             System.out.println(
30              "ArrayListTransferHandler: unable to create data flavor");
31         }
32         serialArrayListFlavor = new DataFlavor(ArrayList.class,
33                                               "ArrayList");
34     }
35
36     public boolean importData(JComponent c, Transferable t) {
37         JList target = null;
38         ArrayList<Object JavaDoc> alist = null;
39         if (!canImport(c, t.getTransferDataFlavors())) {
40             return false;
41         }
42         try {
43             target = (JList)c;
44             if (hasLocalArrayListFlavor(t.getTransferDataFlavors())) {
45                 alist = (ArrayList)t.getTransferData(localArrayListFlavor);
46             } else if (hasSerialArrayListFlavor(t.getTransferDataFlavors())) {
47                 alist = (ArrayList)t.getTransferData(serialArrayListFlavor);
48             } else {
49                 return false;
50             }
51         } catch (UnsupportedFlavorException ufe) {
52             System.out.println("importData: unsupported data flavor");
53             return false;
54         } catch (IOException ioe) {
55             System.out.println("importData: I/O exception");
56             return false;
57         }
58
59         //At this point we use the same code to retrieve the data
60
//locally or serially.
61

62         //We'll drop at the current selected index.
63
int index = target.getSelectedIndex();
64
65         //Prevent the user from dropping data back on itself.
66
//For example, if the user is moving items #4,#5,#6 and #7 and
67
//attempts to insert the items after item #5, this would
68
//be problematic when removing the original items.
69
//This is interpreted as dropping the same data on itself
70
//and has no effect.
71
if (source.equals(target)) {
72             if (indices != null && index >= indices[0] - 1 &&
73                   index <= indices[indices.length - 1]) {
74                 indices = null;
75                 return true;
76             }
77         }
78
79         DefaultListModel listModel = (DefaultListModel)target.getModel();
80         int max = listModel.getSize();
81         if (index < 0) {
82             index = max;
83         } else {
84             index++;
85             if (index > max) {
86                 index = max;
87             }
88         }
89         addIndex = index;
90         addCount = alist.size();
91         for (int i=0; i < alist.size(); i++) {
92             listModel.add(index++, alist.get(i));
93         }
94         return true;
95     }
96
97     protected void exportDone(JComponent c, Transferable data, int action)
98     {
99         if ((action == MOVE) && (indices != null)) {
100             DefaultListModel model = (DefaultListModel)source.getModel();
101
102             //If we are moving items around in the same list, we
103
//need to adjust the indices accordingly since those
104
//after the insertion point have moved.
105
if (addCount > 0) {
106                 for (int i = 0; i < indices.length; i++) {
107                     if (indices[i] > addIndex) {
108                         indices[i] += addCount;
109                     }
110                 }
111             }
112             for (int i = indices.length -1; i >= 0; i--)
113                 model.remove(indices[i]);
114         }
115         indices = null;
116         addIndex = -1;
117         addCount = 0;
118     }
119
120     private boolean hasLocalArrayListFlavor(DataFlavor[] flavors) {
121         if (localArrayListFlavor == null) {
122             return false;
123         }
124
125         for (int i = 0; i < flavors.length; i++) {
126             if (flavors[i].equals(localArrayListFlavor)) {
127                 return true;
128             }
129         }
130         return false;
131     }
132
133     private boolean hasSerialArrayListFlavor(DataFlavor[] flavors) {
134         if (serialArrayListFlavor == null) {
135             return false;
136         }
137
138         for (int i = 0; i < flavors.length; i++) {
139             if (flavors[i].equals(serialArrayListFlavor)) {
140                 return true;
141             }
142         }
143         return false;
144     }
145
146     public boolean canImport(JComponent c, DataFlavor[] flavors) {
147         if (hasLocalArrayListFlavor(flavors)) { return true; }
148         if (hasSerialArrayListFlavor(flavors)) { return true; }
149         return false;
150     }
151
152     protected Transferable createTransferable(JComponent c) {
153         if (c instanceof JList) {
154             source = (JList)c;
155             indices = source.getSelectedIndices();
156             Object JavaDoc[] values = source.getSelectedValues();
157             if (values == null || values.length == 0) {
158                 return null;
159             }
160             ArrayList alist = new ArrayList(values.length);
161             for (int i = 0; i < values.length; i++) {
162                 Object JavaDoc o = values[i];
163                 String JavaDoc str = o.toString();
164                 if (str == null) str = "";
165                 alist.add(str);
166             }
167             return new ArrayListTransferable(alist);
168         }
169         return null;
170     }
171
172     public int getSourceActions(JComponent c) {
173         return COPY_OR_MOVE;
174     }
175
176     public class ArrayListTransferable implements Transferable {
177         ArrayList<Object JavaDoc> data;
178
179         public ArrayListTransferable(ArrayList<Object JavaDoc> alist) {
180             data = alist;
181         }
182
183         public Object JavaDoc getTransferData(DataFlavor flavor)
184                                  throws UnsupportedFlavorException {
185             if (!isDataFlavorSupported(flavor)) {
186                 throw new UnsupportedFlavorException(flavor);
187             }
188             return data;
189         }
190
191         public DataFlavor[] getTransferDataFlavors() {
192             return new DataFlavor[] { localArrayListFlavor,
193                                       serialArrayListFlavor };
194         }
195
196         public boolean isDataFlavorSupported(DataFlavor flavor) {
197             if (localArrayListFlavor.equals(flavor)) {
198                 return true;
199             }
200             if (serialArrayListFlavor.equals(flavor)) {
201                 return true;
202             }
203             return false;
204         }
205     }
206 }
Popular Tags