KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > config > filter > FilterTransferHandler


1 /*
2  * Created on 2003-nov-08
3  */

4 package org.columba.mail.gui.config.filter;
5
6 import java.awt.Component JavaDoc;
7 import java.awt.datatransfer.DataFlavor JavaDoc;
8 import java.awt.datatransfer.Transferable JavaDoc;
9
10 import javax.swing.JComponent JavaDoc;
11 import javax.swing.JScrollPane JavaDoc;
12 import javax.swing.TransferHandler JavaDoc;
13
14 import org.columba.core.facade.DialogFacade;
15 import org.columba.core.filter.Filter;
16
17 /**
18  * A <code>TransferHandler</code> that handles the transfering of filters
19  * between two filter dialogs.
20  * <p>
21  * Note that the transfer handler will only allow to move filters within a
22  * component, whereas it is possible to either move or copy filters between two
23  * different components.
24  *
25  * @author Erik Mattsson
26  */

27
28 public class FilterTransferHandler extends TransferHandler JavaDoc {
29
30     /**
31      * Creates a FilterTransferHandler.
32      *
33      * @param src
34      * the source component.
35      */

36     public FilterTransferHandler() {
37     }
38
39     /** {@inheritDoc} */
40     public boolean canImport(JComponent JavaDoc comp, DataFlavor JavaDoc[] transferFlavors) {
41         boolean canHandleOneOfDataFlavors = false;
42
43         if (canHandleImport(comp)) {
44             for (int k = 0; (k < transferFlavors.length)
45                     || (!canHandleOneOfDataFlavors); k++) {
46                 if (transferFlavors[k].equals(ObjectArrayTransfer.FLAVOR)) {
47                     canHandleOneOfDataFlavors = true;
48                 }
49             }
50         }
51
52         return canHandleOneOfDataFlavors;
53     }
54
55     /** {@inheritDoc} */
56     protected Transferable JavaDoc createTransferable(JComponent JavaDoc c) {
57         if (!canHandleExport(c)) {
58             return null;
59         }
60
61         Transferable JavaDoc transferable = null;
62         FilterListTable filterTable = getFilterListTable(c);
63
64         if (filterTable != null) {
65             FilterListDataModel model = (FilterListDataModel) filterTable
66                     .getModel();
67
68             int[] selectedRows = filterTable.getSelectedRows();
69             Object JavaDoc[] selectedFilters = new Object JavaDoc[selectedRows.length];
70
71             for (int i = 0; i < selectedFilters.length; i++) {
72                 selectedFilters[i] = model.getFilter(selectedRows[i]).clone();
73             }
74
75             transferable = new ObjectArrayTransfer(filterTable, selectedFilters);
76         }
77
78         return transferable;
79     }
80
81     /** {@inheritDoc} */
82     protected void exportDone(JComponent JavaDoc source, Transferable JavaDoc data, int action) {
83         if (!canHandleExport(source)) {
84             return;
85         }
86
87         if (!((action == COPY_OR_MOVE) || (action == MOVE))) {
88             return;
89         }
90
91         FilterListTable list = getFilterListTable(source);
92
93         if (list != null) {
94             try {
95                 Object JavaDoc transferable = data
96                         .getTransferData(ObjectArrayTransfer.FLAVOR);
97
98                 if (!(transferable instanceof ObjectArrayTransfer)) {
99                     return;
100                 }
101
102                 ObjectArrayTransfer arrayTransferable = (ObjectArrayTransfer) transferable;
103
104                 if (!source.equals(arrayTransferable.getSource())) {
105                     return;
106                 }
107
108                 Object JavaDoc[] filters = arrayTransferable.getData();
109                 FilterListDataModel model = (FilterListDataModel) list
110                         .getModel();
111
112                 for (int i = 0; i < filters.length; i++) {
113                     model.removeFilter((Filter) filters[i]);
114                 }
115             } catch (Exception JavaDoc e) {
116                 DialogFacade.showExceptionDialog(e);
117             }
118         }
119     }
120
121     /**
122      * Returns the source actions that the specified component can handle. If
123      * the source component is the same as the specified component, then there
124      * should only be possible to move the filter not copy it. {@inheritDoc}
125      */

126     public int getSourceActions(JComponent JavaDoc c) {
127         /*
128          * if ( c == transferSource ) return MOVE;
129          */

130         if (canHandleImport(c)) {
131             return COPY_OR_MOVE;
132         } else {
133             return NONE;
134         }
135     }
136
137     /** {@inheritDoc} */
138     public boolean importData(JComponent JavaDoc comp, Transferable JavaDoc t) {
139         if (!canHandleImport(comp)) {
140             return false;
141         }
142
143         FilterListTable list = getFilterListTable(comp);
144
145         if (list != null) {
146             FilterListDataModel model = (FilterListDataModel) list.getModel();
147
148             try {
149                 Object JavaDoc obj = t.getTransferData(ObjectArrayTransfer.FLAVOR);
150
151                 if (!(obj instanceof ObjectArrayTransfer)) {
152                     return false;
153                 }
154
155                 ObjectArrayTransfer at = (ObjectArrayTransfer) obj;
156                 Object JavaDoc[] filters = at.getData();
157
158                 // block transfer to self!
159
if (comp.equals(at.getSource())) {
160                     return false;
161                 }
162
163                 // if it is in a table, then we need to insert it at the
164
// selected position.
165
if (comp instanceof FilterListTable) {
166                     int selectedRow = list.getSelectedRow();
167
168                     int i;
169
170                     for (i = (filters.length - 1); i >= 0; i--) {
171                         model.insertFilter((Filter) filters[i], selectedRow);
172                     }
173
174                     list.getSelectionModel().clearSelection();
175                 }
176                 // if it is in a scroll pane, then we add to the end of the
177
// list.
178
else if (comp instanceof JScrollPane JavaDoc) {
179                     for (int i = 0; i < filters.length; i++) {
180                         model.addFilter((Filter) filters[i]);
181                     }
182                 }
183             } catch (Exception JavaDoc e) {
184                 DialogFacade.showExceptionDialog(e);
185             }
186         }
187
188         return true;
189     }
190
191     /**
192      * Returns true if this handler can import data from the specified
193      * component.
194      *
195      * @param component
196      * the component that is exporting the data.
197      * @return true if this handler can import data from the specified
198      * component.
199      */

200     private boolean canHandleImport(JComponent JavaDoc component) {
201         return ((component instanceof FilterListTable) || (component instanceof JScrollPane JavaDoc));
202     }
203
204     /**
205      * Returns true if this handler can export data to the specified component.
206      *
207      * @param component
208      * the component that is importing the data.
209      * @return true if this handler can export data to the specified component.
210      */

211     private boolean canHandleExport(JComponent JavaDoc component) {
212         return (component instanceof FilterListTable);
213     }
214
215     /**
216      * Returns the <code>FilterListTable</code> from the component. This is a
217      * utility method to simplify the retrieving of a FilterListTable. Since the
218      * table and the JScollPane is a valid importer, then we need a method that
219      * returns the table when adding filters to it.
220      *
221      * @param comp
222      * a JComponent that either is a <code>JScrollPane</code> or a
223      * <code>FilterListTable</code>
224      * @return a FilterListTable; or null if the component doesnt contain a
225      * <code>FilterListTable</code>.
226      */

227     private FilterListTable getFilterListTable(JComponent JavaDoc comp) {
228         Component JavaDoc tableComponent = null;
229
230         if (comp instanceof JScrollPane JavaDoc) {
231             tableComponent = ((JScrollPane JavaDoc) comp).getViewport().getView();
232         } else if (comp instanceof FilterListTable) {
233             tableComponent = comp;
234         }
235
236         return (FilterListTable) tableComponent;
237     }
238 }
239
Popular Tags