KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > gui > list > AddressbookDNDListView


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.addressbook.gui.list;
17
18 import java.awt.datatransfer.StringSelection JavaDoc;
19 import java.awt.dnd.DnDConstants JavaDoc;
20 import java.awt.dnd.DragGestureEvent JavaDoc;
21 import java.awt.dnd.DragGestureListener JavaDoc;
22 import java.awt.dnd.DragSource JavaDoc;
23 import java.awt.dnd.DragSourceDragEvent JavaDoc;
24 import java.awt.dnd.DragSourceDropEvent JavaDoc;
25 import java.awt.dnd.DragSourceEvent JavaDoc;
26 import java.awt.dnd.DragSourceListener JavaDoc;
27 import java.awt.dnd.DropTarget JavaDoc;
28 import java.awt.dnd.DropTargetDragEvent JavaDoc;
29 import java.awt.dnd.DropTargetDropEvent JavaDoc;
30 import java.awt.dnd.DropTargetEvent JavaDoc;
31 import java.awt.dnd.DropTargetListener JavaDoc;
32
33 import javax.swing.event.ListSelectionEvent JavaDoc;
34 import javax.swing.event.ListSelectionListener JavaDoc;
35
36 import org.columba.addressbook.facade.IHeaderItem;
37 import org.columba.addressbook.model.IBasicModelPartial;
38
39 //import sun.security.krb5.internal.i;
40
//import sun.security.krb5.internal.crypto.b;
41

42 /**
43  * @version 1.0
44  * @author
45  */

46 @SuppressWarnings JavaDoc({"serial","serial"})
47 public class AddressbookDNDListView extends AddressbookListView implements
48         DropTargetListener JavaDoc, DragSourceListener JavaDoc, DragGestureListener JavaDoc,
49         ListSelectionListener JavaDoc {
50
51     /**
52      * enables this component to be a dropTarget
53      */

54     DropTarget JavaDoc dropTarget = null;
55
56     /**
57      * enables this component to be a Drag Source
58      */

59     DragSource JavaDoc 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     // Where, in the drag image, the mouse was clicked
72
public AddressbookDNDListView() {
73         super();
74
75         addListSelectionListener(this);
76
77         dropTarget = new DropTarget JavaDoc(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 JavaDoc(this, this);
95         dragSource = new DragSource JavaDoc();
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     /**
111      * is invoked when you are dragging over the DropSite
112      *
113      */

114     public void dragEnter(DropTargetDragEvent JavaDoc event) {
115         // debug messages for diagnostics
116
if (acceptDrop == true) {
117             event.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
118         } else {
119             event.acceptDrag(DnDConstants.ACTION_COPY);
120         }
121     }
122
123     /**
124      * is invoked when you are exit the DropSite without dropping
125      *
126      */

127     public void dragExit(DropTargetEvent JavaDoc event) {
128     }
129
130     /**
131      * is invoked when a drag operation is going on
132      *
133      */

134     public void dragOver(DropTargetDragEvent JavaDoc event) {
135     }
136
137     /**
138      * a drop has occurred
139      *
140      */

141     public void drop(DropTargetDropEvent JavaDoc 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     /**
163      * is invoked if the use modifies the current drop gesture
164      *
165      */

166     public void dropActionChanged(DropTargetDragEvent JavaDoc event) {
167     }
168
169     /**
170      * a drag gesture has been initiated
171      *
172      */

173     public void dragGestureRecognized(DragGestureEvent JavaDoc event) {
174         if (dndAction == false) {
175             /*
176              * HeaderItem[] items = new HeaderItem[selection1.length]; items =
177              * selection1;
178              * HeaderItemDNDManager.getInstance().setHeaderItemList(items);
179              */

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             /*
192              * else {
193              *
194              * HeaderItem[] items = new HeaderItem[1]; items[0] = (HeaderItem)
195              * getSelectedValue();
196              * HeaderItemDNDManager.getInstance().setHeaderItemList(items); }
197              */

198         } else {
199             /*
200              * HeaderItem[] items = new HeaderItem[selection2.length]; items =
201              * selection2;
202              * HeaderItemDNDManager.getInstance().setHeaderItemList(items);
203              */

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         /*
217          * dragSource.startDrag( event, new Cursor(Cursor.DEFAULT_CURSOR),
218          * ImageLoader.getImageIcon("contact_small","Add16").getImage(), new
219          * Point(5, 5), new StringSelection("contact"), this);
220          */

221         StringSelection JavaDoc text = new StringSelection JavaDoc("contact");
222
223         dragSource.startDrag(event, DragSource.DefaultMoveDrop, text, this);
224
225         clearSelection();
226     }
227
228     /**
229      * this message goes to DragSourceListener, informing it that the dragging
230      * has ended
231      *
232      */

233     public void dragDropEnd(DragSourceDropEvent JavaDoc 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                 // removeElement();
244
}
245         }
246     }
247
248     /**
249      * this message goes to DragSourceListener, informing it that the dragging
250      * has entered the DropSite
251      *
252      */

253     public void dragEnter(DragSourceDragEvent JavaDoc event) {
254     }
255
256     /**
257      * this message goes to DragSourceListener, informing it that the dragging
258      * has exited the DropSite
259      *
260      */

261     public void dragExit(DragSourceEvent JavaDoc event) {
262     }
263
264     /**
265      * this message goes to DragSourceListener, informing it that the dragging
266      * is currently ocurring over the DropSite
267      *
268      */

269     public void dragOver(DragSourceDragEvent JavaDoc event) {
270     }
271
272     /**
273      * is invoked when the user changes the dropAction
274      *
275      */

276     public void dropActionChanged(DragSourceDragEvent JavaDoc event) {
277     }
278
279     /**
280      * adds elements to itself
281      *
282      */

283     /**
284      * removes an element from itself
285      */

286     public void removeElement() {
287         ((AddressbookListModel) getModel())
288                 .removeElement((IBasicModelPartial) getSelectedValue());
289     }
290
291     public void valueChanged(ListSelectionEvent JavaDoc e) {
292         if (dndAction == true) {
293             Object JavaDoc[] 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 JavaDoc[] 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