1 package org.columba.addressbook.gui.list; 17 18 import java.awt.datatransfer.StringSelection ; 19 import java.awt.dnd.DnDConstants ; 20 import java.awt.dnd.DragGestureEvent ; 21 import java.awt.dnd.DragGestureListener ; 22 import java.awt.dnd.DragSource ; 23 import java.awt.dnd.DragSourceDragEvent ; 24 import java.awt.dnd.DragSourceDropEvent ; 25 import java.awt.dnd.DragSourceEvent ; 26 import java.awt.dnd.DragSourceListener ; 27 import java.awt.dnd.DropTarget ; 28 import java.awt.dnd.DropTargetDragEvent ; 29 import java.awt.dnd.DropTargetDropEvent ; 30 import java.awt.dnd.DropTargetEvent ; 31 import java.awt.dnd.DropTargetListener ; 32 33 import javax.swing.event.ListSelectionEvent ; 34 import javax.swing.event.ListSelectionListener ; 35 36 import org.columba.addressbook.facade.IHeaderItem; 37 import org.columba.addressbook.model.IBasicModelPartial; 38 39 42 46 @SuppressWarnings ({"serial","serial"}) 47 public class AddressbookDNDListView extends AddressbookListView implements 48 DropTargetListener , DragSourceListener , DragGestureListener , 49 ListSelectionListener { 50 51 54 DropTarget dropTarget = null; 55 56 59 DragSource dragSource = null; 60 61 boolean acceptDrop = true; 62 63 private IBasicModelPartial[] selection1; 64 65 private IBasicModelPartial[] selection2; 66 67 int index = -1; 68 69 private boolean dndAction = false; 70 71 public AddressbookDNDListView() { 73 super(); 74 75 addListSelectionListener(this); 76 77 dropTarget = new DropTarget (this, this); 78 dragSource = DragSource.getDefaultDragSource(); 79 80 if (acceptDrop == true) { 81 dragSource.createDefaultDragGestureRecognizer(this, 82 DnDConstants.ACTION_COPY_OR_MOVE, this); 83 } else { 84 dragSource.createDefaultDragGestureRecognizer(this, 85 DnDConstants.ACTION_COPY, this); 86 } 87 } 88 89 public AddressbookDNDListView(AddressbookListModel model) { 90 super(model); 91 92 addListSelectionListener(this); 93 94 dropTarget = new DropTarget (this, this); 95 dragSource = new DragSource (); 96 97 if (acceptDrop == true) { 98 dragSource.createDefaultDragGestureRecognizer(this, 99 DnDConstants.ACTION_COPY_OR_MOVE, this); 100 } else { 101 dragSource.createDefaultDragGestureRecognizer(this, 102 DnDConstants.ACTION_COPY, this); 103 } 104 } 105 106 public void setAcceptDrop(boolean b) { 107 acceptDrop = b; 108 } 109 110 114 public void dragEnter(DropTargetDragEvent event) { 115 if (acceptDrop == true) { 117 event.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); 118 } else { 119 event.acceptDrag(DnDConstants.ACTION_COPY); 120 } 121 } 122 123 127 public void dragExit(DropTargetEvent event) { 128 } 129 130 134 public void dragOver(DropTargetDragEvent event) { 135 } 136 137 141 public void drop(DropTargetDropEvent event) { 142 if (acceptDrop == false) { 143 event.rejectDrop(); 144 145 clearSelection(); 146 147 return; 148 } 149 150 IBasicModelPartial[] items = HeaderItemDNDManager.getInstance() 151 .getHeaderItemList(); 152 153 for (int i = 0; i < items.length; i++) { 154 addElement((IBasicModelPartial) ((IBasicModelPartial) items[i]).clone()); 155 } 156 157 event.getDropTargetContext().dropComplete(true); 158 159 clearSelection(); 160 } 161 162 166 public void dropActionChanged(DropTargetDragEvent event) { 167 } 168 169 173 public void dragGestureRecognized(DragGestureEvent event) { 174 if (dndAction == false) { 175 180 if (selection1 == null) { 181 IBasicModelPartial[] items = new IBasicModelPartial[1]; 182 items[0] = (IBasicModelPartial) getSelectedValue(); 183 184 HeaderItemDNDManager.getInstance().setHeaderItemList(items); 185 } else if (selection1.length != 0) { 186 IBasicModelPartial[] items = new IBasicModelPartial[selection1.length]; 187 items = selection1; 188 HeaderItemDNDManager.getInstance().setHeaderItemList(items); 189 } 190 191 198 } else { 199 204 if (selection2.length != 0) { 205 IBasicModelPartial[] items = new IBasicModelPartial[selection2.length]; 206 items = selection2; 207 HeaderItemDNDManager.getInstance().setHeaderItemList(items); 208 } else { 209 IBasicModelPartial[] items = new IBasicModelPartial[1]; 210 items[0] = (IBasicModelPartial) getSelectedValue(); 211 212 HeaderItemDNDManager.getInstance().setHeaderItemList(items); 213 } 214 } 215 216 221 StringSelection text = new StringSelection ("contact"); 222 223 dragSource.startDrag(event, DragSource.DefaultMoveDrop, text, this); 224 225 clearSelection(); 226 } 227 228 233 public void dragDropEnd(DragSourceDropEvent event) { 234 if (event.getDropSuccess()) { 235 if (acceptDrop == true) { 236 IBasicModelPartial[] items = HeaderItemDNDManager.getInstance() 237 .getHeaderItemList(); 238 239 for (int i = 0; i < items.length; i++) { 240 ((AddressbookListModel) getModel()).removeElement(items[i]); 241 } 242 243 } 245 } 246 } 247 248 253 public void dragEnter(DragSourceDragEvent event) { 254 } 255 256 261 public void dragExit(DragSourceEvent event) { 262 } 263 264 269 public void dragOver(DragSourceDragEvent event) { 270 } 271 272 276 public void dropActionChanged(DragSourceDragEvent event) { 277 } 278 279 283 286 public void removeElement() { 287 ((AddressbookListModel) getModel()) 288 .removeElement((IBasicModelPartial) getSelectedValue()); 289 } 290 291 public void valueChanged(ListSelectionEvent e) { 292 if (dndAction == true) { 293 Object [] list = getSelectedValues(); 294 295 selection1 = new IBasicModelPartial[list.length]; 296 297 for (int i = 0; i < list.length; i++) { 298 selection1[i] = (IBasicModelPartial) list[i]; 299 } 300 301 dndAction = false; 302 } else { 303 Object [] list = getSelectedValues(); 304 305 selection2 = new IBasicModelPartial[list.length]; 306 307 for (int i = 0; i < list.length; i++) { 308 selection2[i] = (IBasicModelPartial) list[i]; 309 } 310 311 dndAction = true; 312 } 313 } 314 } 315 | Popular Tags |