001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.gui.contact.list;
017:
018: import java.awt.datatransfer.StringSelection;
019: import java.awt.dnd.DnDConstants;
020: import java.awt.dnd.DragGestureEvent;
021: import java.awt.dnd.DragGestureListener;
022: import java.awt.dnd.DragSource;
023: import java.awt.dnd.DragSourceDragEvent;
024: import java.awt.dnd.DragSourceDropEvent;
025: import java.awt.dnd.DragSourceEvent;
026: import java.awt.dnd.DragSourceListener;
027: import java.awt.dnd.DropTarget;
028: import java.awt.dnd.DropTargetDragEvent;
029: import java.awt.dnd.DropTargetDropEvent;
030: import java.awt.dnd.DropTargetEvent;
031: import java.awt.dnd.DropTargetListener;
032:
033: import javax.swing.event.ListSelectionEvent;
034: import javax.swing.event.ListSelectionListener;
035:
036: import org.columba.addressbook.facade.IHeaderItem;
037: import org.columba.addressbook.facade.IModelFacade;
038: import org.columba.api.exception.ServiceNotFoundException;
039: import org.columba.mail.connector.ServiceConnector;
040:
041: public class ContactDNDListView extends ContactList implements
042: DropTargetListener, DragSourceListener, DragGestureListener,
043: ListSelectionListener {
044:
045: /**
046: * enables this component to be a dropTarget
047: */
048: DropTarget dropTarget = null;
049:
050: /**
051: * enables this component to be a Drag Source
052: */
053: DragSource dragSource = null;
054:
055: boolean acceptDrop = true;
056:
057: private IHeaderItem[] selection1;
058:
059: private IHeaderItem[] selection2;
060:
061: int index = -1;
062:
063: private boolean dndAction = false;
064:
065: // Where, in the drag image, the mouse was clicked
066: public ContactDNDListView() {
067: super ();
068:
069: addListSelectionListener(this );
070:
071: dropTarget = new DropTarget(this , this );
072: dragSource = DragSource.getDefaultDragSource();
073:
074: if (acceptDrop == true) {
075: dragSource.createDefaultDragGestureRecognizer(this ,
076: DnDConstants.ACTION_COPY_OR_MOVE, this );
077: } else {
078: dragSource.createDefaultDragGestureRecognizer(this ,
079: DnDConstants.ACTION_COPY, this );
080: }
081: }
082:
083: public ContactDNDListView(ContactListModel model) {
084: super (model);
085:
086: addListSelectionListener(this );
087:
088: dropTarget = new DropTarget(this , this );
089: dragSource = new DragSource();
090:
091: if (acceptDrop == true) {
092: dragSource.createDefaultDragGestureRecognizer(this ,
093: DnDConstants.ACTION_COPY_OR_MOVE, this );
094: } else {
095: dragSource.createDefaultDragGestureRecognizer(this ,
096: DnDConstants.ACTION_COPY, this );
097: }
098: }
099:
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 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 event) {
122: }
123:
124: /**
125: * is invoked when a drag operation is going on
126: *
127: */
128: public void dragOver(DropTargetDragEvent event) {
129: }
130:
131: /**
132: * a drop has occurred
133: *
134: */
135: public void drop(DropTargetDropEvent 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 event) {
172: }
173:
174: /**
175: * a drag gesture has been initiated
176: *
177: */
178: public void dragGestureRecognized(DragGestureEvent 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(
190: items);
191: } else if (selection1.length != 0) {
192: IHeaderItem[] items = new IHeaderItem[selection1.length];
193: items = selection1;
194: HeaderItemDNDManager.getInstance().setHeaderItemList(
195: items);
196: }
197:
198: /*
199: * else {
200: *
201: * HeaderItem[] items = new HeaderItem[1]; items[0] = (HeaderItem)
202: * getSelectedValue();
203: * HeaderItemDNDManager.getInstance().setHeaderItemList(items); }
204: */
205: } else {
206: /*
207: * HeaderItem[] items = new HeaderItem[selection2.length]; items =
208: * selection2;
209: * HeaderItemDNDManager.getInstance().setHeaderItemList(items);
210: */
211: if (selection2.length != 0) {
212: IHeaderItem[] items = new IHeaderItem[selection2.length];
213: items = selection2;
214: HeaderItemDNDManager.getInstance().setHeaderItemList(
215: items);
216: } else {
217: IHeaderItem[] items = new IHeaderItem[1];
218: items[0] = (IHeaderItem) getSelectedValue();
219:
220: HeaderItemDNDManager.getInstance().setHeaderItemList(
221: items);
222: }
223: }
224:
225: /*
226: * dragSource.startDrag( event, new Cursor(Cursor.DEFAULT_CURSOR),
227: * ImageLoader.getImageIcon("contact_small","Add16").getImage(), new
228: * Point(5, 5), new StringSelection("contact"), this);
229: */
230: StringSelection text = new StringSelection("contact");
231:
232: dragSource.startDrag(event, DragSource.DefaultMoveDrop, text,
233: this );
234:
235: clearSelection();
236: }
237:
238: /**
239: * this message goes to DragSourceListener, informing it that the dragging
240: * has ended
241: *
242: */
243: public void dragDropEnd(DragSourceDropEvent event) {
244: if (event.getDropSuccess()) {
245: if (acceptDrop == true) {
246: IHeaderItem[] items = HeaderItemDNDManager
247: .getInstance().getHeaderItemList();
248:
249: for (int i = 0; i < items.length; i++) {
250: ((ContactListModel) getModel())
251: .removeElement(items[i]);
252: }
253:
254: // removeElement();
255: }
256: }
257: }
258:
259: /**
260: * this message goes to DragSourceListener, informing it that the dragging
261: * has entered the DropSite
262: *
263: */
264: public void dragEnter(DragSourceDragEvent event) {
265: }
266:
267: /**
268: * this message goes to DragSourceListener, informing it that the dragging
269: * has exited the DropSite
270: *
271: */
272: public void dragExit(DragSourceEvent event) {
273: }
274:
275: /**
276: * this message goes to DragSourceListener, informing it that the dragging
277: * is currently ocurring over the DropSite
278: *
279: */
280: public void dragOver(DragSourceDragEvent event) {
281: }
282:
283: /**
284: * is invoked when the user changes the dropAction
285: *
286: */
287: public void dropActionChanged(DragSourceDragEvent event) {
288: }
289:
290: /**
291: * adds elements to itself
292: *
293: */
294: /**
295: * removes an element from itself
296: */
297: public void removeElement() {
298: ((ContactListModel) getModel())
299: .removeElement((IHeaderItem) getSelectedValue());
300: }
301:
302: public void valueChanged(ListSelectionEvent e) {
303: if (dndAction == true) {
304: Object[] list = getSelectedValues();
305:
306: selection1 = new IHeaderItem[list.length];
307:
308: for (int i = 0; i < list.length; i++) {
309: selection1[i] = (IHeaderItem) list[i];
310: }
311:
312: dndAction = false;
313: } else {
314: Object[] list = getSelectedValues();
315:
316: selection2 = new IHeaderItem[list.length];
317:
318: for (int i = 0; i < list.length; i++) {
319: selection2[i] = (IHeaderItem) list[i];
320: }
321:
322: dndAction = true;
323: }
324: }
325: }
|