KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > contact > list > ContactDNDListView


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.mail.gui.contact.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.facade.IModelFacade;
38 import org.columba.api.exception.ServiceNotFoundException;
39 import org.columba.mail.connector.ServiceConnector;
40
41 public class ContactDNDListView extends ContactList implements
42         DropTargetListener JavaDoc, DragSourceListener JavaDoc, DragGestureListener JavaDoc,
43         ListSelectionListener JavaDoc {
44
45     /**
46      * enables this component to be a dropTarget
47      */

48     DropTarget JavaDoc dropTarget = null;
49
50     /**
51      * enables this component to be a Drag Source
52      */

53     DragSource JavaDoc dragSource = null;
54
55     boolean acceptDrop = true;
56
57     private IHeaderItem[] selection1;
58
59     private IHeaderItem[] selection2;
60
61     int index = -1;
62
63     private boolean dndAction = false;
64
65     // Where, in the drag image, the mouse was clicked
66
public ContactDNDListView() {
67         super();
68
69         addListSelectionListener(this);
70
71         dropTarget = new DropTarget JavaDoc(this, this);
72         dragSource = DragSource.getDefaultDragSource();
73
74         if (acceptDrop == true) {
75             dragSource.createDefaultDragGestureRecognizer(this,
76                     DnDConstants.ACTION_COPY_OR_MOVE, this);
77         } else {
78             dragSource.createDefaultDragGestureRecognizer(this,
79                     DnDConstants.ACTION_COPY, this);
80         }
81     }
82
83     public ContactDNDListView(ContactListModel model) {
84         super(model);
85
86         addListSelectionListener(this);
87
88         dropTarget = new DropTarget JavaDoc(this, this);
89         dragSource = new DragSource JavaDoc();
90
91         if (acceptDrop == true) {
92             dragSource.createDefaultDragGestureRecognizer(this,
93                     DnDConstants.ACTION_COPY_OR_MOVE, this);
94         } else {
95             dragSource.createDefaultDragGestureRecognizer(this,
96                     DnDConstants.ACTION_COPY, this);
97         }
98     }
99
100     public void setAcceptDrop(boolean b) {
101         acceptDrop = b;
102     }
103
104     /**
105      * is invoked when you are dragging over the DropSite
106      *
107      */

108     public void dragEnter(DropTargetDragEvent JavaDoc event) {
109         // debug messages for diagnostics
110
if (acceptDrop == true) {
111             event.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
112         } else {
113             event.acceptDrag(DnDConstants.ACTION_COPY);
114         }
115     }
116
117     /**
118      * is invoked when you are exit the DropSite without dropping
119      *
120      */

121     public void dragExit(DropTargetEvent JavaDoc event) {
122     }
123
124     /**
125      * is invoked when a drag operation is going on
126      *
127      */

128     public void dragOver(DropTargetDragEvent JavaDoc event) {
129     }
130
131     /**
132      * a drop has occurred
133      *
134      */

135     public void drop(DropTargetDropEvent JavaDoc event) {
136         if (acceptDrop == false) {
137             event.rejectDrop();
138
139             clearSelection();
140
141             return;
142         }
143
144         IHeaderItem[] items = HeaderItemDNDManager.getInstance()
145                 .getHeaderItemList();
146
147         for (int i = 0; i < items.length; i++) {
148             IHeaderItem old = items[i];
149             IHeaderItem newItem = null;
150             try {
151                 IModelFacade c = ServiceConnector.getModelFacade();
152                 newItem = c.createHeaderItem();
153                 newItem.setName(old.getName());
154                 newItem.setDescription(old.getDescription());
155             } catch (ServiceNotFoundException e) {
156                 e.printStackTrace();
157             }
158             
159             addElement(newItem);
160         }
161
162         event.getDropTargetContext().dropComplete(true);
163
164         clearSelection();
165     }
166
167     /**
168      * is invoked if the use modifies the current drop gesture
169      *
170      */

171     public void dropActionChanged(DropTargetDragEvent JavaDoc event) {
172     }
173
174     /**
175      * a drag gesture has been initiated
176      *
177      */

178     public void dragGestureRecognized(DragGestureEvent JavaDoc event) {
179         if (dndAction == false) {
180             /*
181              * HeaderItem[] items = new HeaderItem[selection1.length]; items =
182              * selection1;
183              * HeaderItemDNDManager.getInstance().setHeaderItemList(items);
184              */

185             if (selection1 == null) {
186                 IHeaderItem[] items = new IHeaderItem[1];
187                 items[0] = (IHeaderItem) getSelectedValue();
188
189                 HeaderItemDNDManager.getInstance().setHeaderItemList(items);
190             } else if (selection1.length != 0) {
191                 IHeaderItem[] items = new IHeaderItem[selection1.length];
192                 items = selection1;
193                 HeaderItemDNDManager.getInstance().setHeaderItemList(items);
194             }
195
196             /*
197              * else {
198              *
199              * HeaderItem[] items = new HeaderItem[1]; items[0] = (HeaderItem)
200              * getSelectedValue();
201              * HeaderItemDNDManager.getInstance().setHeaderItemList(items); }
202              */

203         } else {
204             /*
205              * HeaderItem[] items = new HeaderItem[selection2.length]; items =
206              * selection2;
207              * HeaderItemDNDManager.getInstance().setHeaderItemList(items);
208              */

209             if (selection2.length != 0) {
210                 IHeaderItem[] items = new IHeaderItem[selection2.length];
211                 items = selection2;
212                 HeaderItemDNDManager.getInstance().setHeaderItemList(items);
213             } else {
214                 IHeaderItem[] items = new IHeaderItem[1];
215                 items[0] = (IHeaderItem) getSelectedValue();
216
217                 HeaderItemDNDManager.getInstance().setHeaderItemList(items);
218             }
219         }
220
221         /*
222          * dragSource.startDrag( event, new Cursor(Cursor.DEFAULT_CURSOR),
223          * ImageLoader.getImageIcon("contact_small","Add16").getImage(), new
224          * Point(5, 5), new StringSelection("contact"), this);
225          */

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

238     public void dragDropEnd(DragSourceDropEvent JavaDoc event) {
239         if (event.getDropSuccess()) {
240             if (acceptDrop == true) {
241                 IHeaderItem[] items = HeaderItemDNDManager.getInstance()
242                         .getHeaderItemList();
243
244                 for (int i = 0; i < items.length; i++) {
245                     ((ContactListModel) getModel()).removeElement(items[i]);
246                 }
247
248                 // removeElement();
249
}
250         }
251     }
252
253     /**
254      * this message goes to DragSourceListener, informing it that the dragging
255      * has entered the DropSite
256      *
257      */

258     public void dragEnter(DragSourceDragEvent JavaDoc event) {
259     }
260
261     /**
262      * this message goes to DragSourceListener, informing it that the dragging
263      * has exited the DropSite
264      *
265      */

266     public void dragExit(DragSourceEvent JavaDoc event) {
267     }
268
269     /**
270      * this message goes to DragSourceListener, informing it that the dragging
271      * is currently ocurring over the DropSite
272      *
273      */

274     public void dragOver(DragSourceDragEvent JavaDoc event) {
275     }
276
277     /**
278      * is invoked when the user changes the dropAction
279      *
280      */

281     public void dropActionChanged(DragSourceDragEvent JavaDoc event) {
282     }
283
284     /**
285      * adds elements to itself
286      *
287      */

288     /**
289      * removes an element from itself
290      */

291     public void removeElement() {
292         ((ContactListModel) getModel())
293                 .removeElement((IHeaderItem) getSelectedValue());
294     }
295
296     public void valueChanged(ListSelectionEvent JavaDoc e) {
297         if (dndAction == true) {
298             Object JavaDoc[] list = getSelectedValues();
299
300             selection1 = new IHeaderItem[list.length];
301
302             for (int i = 0; i < list.length; i++) {
303                 selection1[i] = (IHeaderItem) list[i];
304             }
305
306             dndAction = false;
307         } else {
308             Object JavaDoc[] list = getSelectedValues();
309
310             selection2 = new IHeaderItem[list.length];
311
312             for (int i = 0; i < list.length; i++) {
313                 selection2[i] = (IHeaderItem) list[i];
314             }
315
316             dndAction = true;
317         }
318     }
319 }
320
Popular Tags