1 4 package org.columba.mail.gui.config.filter; 5 6 import java.awt.Component ; 7 import java.awt.datatransfer.DataFlavor ; 8 import java.awt.datatransfer.Transferable ; 9 10 import javax.swing.JComponent ; 11 import javax.swing.JScrollPane ; 12 import javax.swing.TransferHandler ; 13 14 import org.columba.core.facade.DialogFacade; 15 import org.columba.core.filter.Filter; 16 17 27 28 public class FilterTransferHandler extends TransferHandler { 29 30 36 public FilterTransferHandler() { 37 } 38 39 40 public boolean canImport(JComponent comp, DataFlavor [] 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 56 protected Transferable createTransferable(JComponent c) { 57 if (!canHandleExport(c)) { 58 return null; 59 } 60 61 Transferable 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 [] selectedFilters = new Object [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 82 protected void exportDone(JComponent source, Transferable 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 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 [] 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 e) { 116 DialogFacade.showExceptionDialog(e); 117 } 118 } 119 } 120 121 126 public int getSourceActions(JComponent c) { 127 130 if (canHandleImport(c)) { 131 return COPY_OR_MOVE; 132 } else { 133 return NONE; 134 } 135 } 136 137 138 public boolean importData(JComponent comp, Transferable 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 obj = t.getTransferData(ObjectArrayTransfer.FLAVOR); 150 151 if (!(obj instanceof ObjectArrayTransfer)) { 152 return false; 153 } 154 155 ObjectArrayTransfer at = (ObjectArrayTransfer) obj; 156 Object [] filters = at.getData(); 157 158 if (comp.equals(at.getSource())) { 160 return false; 161 } 162 163 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 else if (comp instanceof JScrollPane ) { 179 for (int i = 0; i < filters.length; i++) { 180 model.addFilter((Filter) filters[i]); 181 } 182 } 183 } catch (Exception e) { 184 DialogFacade.showExceptionDialog(e); 185 } 186 } 187 188 return true; 189 } 190 191 200 private boolean canHandleImport(JComponent component) { 201 return ((component instanceof FilterListTable) || (component instanceof JScrollPane )); 202 } 203 204 211 private boolean canHandleExport(JComponent component) { 212 return (component instanceof FilterListTable); 213 } 214 215 227 private FilterListTable getFilterListTable(JComponent comp) { 228 Component tableComponent = null; 229 230 if (comp instanceof JScrollPane ) { 231 tableComponent = ((JScrollPane ) comp).getViewport().getView(); 232 } else if (comp instanceof FilterListTable) { 233 tableComponent = comp; 234 } 235 236 return (FilterListTable) tableComponent; 237 } 238 } 239 | Popular Tags |