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.addressbook.gui.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.model.IBasicModelPartial;
038:
039: //import sun.security.krb5.internal.i;
040: //import sun.security.krb5.internal.crypto.b;
041:
042: /**
043: * @version 1.0
044: * @author
045: */
046: @SuppressWarnings({"serial","serial"})
047: public class AddressbookDNDListView extends AddressbookListView
048: implements DropTargetListener, DragSourceListener,
049: DragGestureListener, ListSelectionListener {
050:
051: /**
052: * enables this component to be a dropTarget
053: */
054: DropTarget dropTarget = null;
055:
056: /**
057: * enables this component to be a Drag Source
058: */
059: DragSource dragSource = null;
060:
061: boolean acceptDrop = true;
062:
063: private IBasicModelPartial[] selection1;
064:
065: private IBasicModelPartial[] selection2;
066:
067: int index = -1;
068:
069: private boolean dndAction = false;
070:
071: // Where, in the drag image, the mouse was clicked
072: public AddressbookDNDListView() {
073: super ();
074:
075: addListSelectionListener(this );
076:
077: dropTarget = new DropTarget(this , this );
078: dragSource = DragSource.getDefaultDragSource();
079:
080: if (acceptDrop == true) {
081: dragSource.createDefaultDragGestureRecognizer(this ,
082: DnDConstants.ACTION_COPY_OR_MOVE, this );
083: } else {
084: dragSource.createDefaultDragGestureRecognizer(this ,
085: DnDConstants.ACTION_COPY, this );
086: }
087: }
088:
089: public AddressbookDNDListView(AddressbookListModel model) {
090: super (model);
091:
092: addListSelectionListener(this );
093:
094: dropTarget = new DropTarget(this , this );
095: dragSource = new DragSource();
096:
097: if (acceptDrop == true) {
098: dragSource.createDefaultDragGestureRecognizer(this ,
099: 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 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 event) {
128: }
129:
130: /**
131: * is invoked when a drag operation is going on
132: *
133: */
134: public void dragOver(DropTargetDragEvent event) {
135: }
136:
137: /**
138: * a drop has occurred
139: *
140: */
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])
155: .clone());
156: }
157:
158: event.getDropTargetContext().dropComplete(true);
159:
160: clearSelection();
161: }
162:
163: /**
164: * is invoked if the use modifies the current drop gesture
165: *
166: */
167: public void dropActionChanged(DropTargetDragEvent event) {
168: }
169:
170: /**
171: * a drag gesture has been initiated
172: *
173: */
174: public void dragGestureRecognized(DragGestureEvent event) {
175: if (dndAction == false) {
176: /*
177: * HeaderItem[] items = new HeaderItem[selection1.length]; items =
178: * selection1;
179: * HeaderItemDNDManager.getInstance().setHeaderItemList(items);
180: */
181: if (selection1 == null) {
182: IBasicModelPartial[] items = new IBasicModelPartial[1];
183: items[0] = (IBasicModelPartial) getSelectedValue();
184:
185: HeaderItemDNDManager.getInstance().setHeaderItemList(
186: items);
187: } else if (selection1.length != 0) {
188: IBasicModelPartial[] items = new IBasicModelPartial[selection1.length];
189: items = selection1;
190: HeaderItemDNDManager.getInstance().setHeaderItemList(
191: items);
192: }
193:
194: /*
195: * else {
196: *
197: * HeaderItem[] items = new HeaderItem[1]; items[0] = (HeaderItem)
198: * getSelectedValue();
199: * HeaderItemDNDManager.getInstance().setHeaderItemList(items); }
200: */
201: } else {
202: /*
203: * HeaderItem[] items = new HeaderItem[selection2.length]; items =
204: * selection2;
205: * HeaderItemDNDManager.getInstance().setHeaderItemList(items);
206: */
207: if (selection2.length != 0) {
208: IBasicModelPartial[] items = new IBasicModelPartial[selection2.length];
209: items = selection2;
210: HeaderItemDNDManager.getInstance().setHeaderItemList(
211: items);
212: } else {
213: IBasicModelPartial[] items = new IBasicModelPartial[1];
214: items[0] = (IBasicModelPartial) getSelectedValue();
215:
216: HeaderItemDNDManager.getInstance().setHeaderItemList(
217: 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 text = new StringSelection("contact");
227:
228: dragSource.startDrag(event, DragSource.DefaultMoveDrop, text,
229: this );
230:
231: clearSelection();
232: }
233:
234: /**
235: * this message goes to DragSourceListener, informing it that the dragging
236: * has ended
237: *
238: */
239: public void dragDropEnd(DragSourceDropEvent event) {
240: if (event.getDropSuccess()) {
241: if (acceptDrop == true) {
242: IBasicModelPartial[] items = HeaderItemDNDManager
243: .getInstance().getHeaderItemList();
244:
245: for (int i = 0; i < items.length; i++) {
246: ((AddressbookListModel) getModel())
247: .removeElement(items[i]);
248: }
249:
250: // removeElement();
251: }
252: }
253: }
254:
255: /**
256: * this message goes to DragSourceListener, informing it that the dragging
257: * has entered the DropSite
258: *
259: */
260: public void dragEnter(DragSourceDragEvent event) {
261: }
262:
263: /**
264: * this message goes to DragSourceListener, informing it that the dragging
265: * has exited the DropSite
266: *
267: */
268: public void dragExit(DragSourceEvent event) {
269: }
270:
271: /**
272: * this message goes to DragSourceListener, informing it that the dragging
273: * is currently ocurring over the DropSite
274: *
275: */
276: public void dragOver(DragSourceDragEvent event) {
277: }
278:
279: /**
280: * is invoked when the user changes the dropAction
281: *
282: */
283: public void dropActionChanged(DragSourceDragEvent event) {
284: }
285:
286: /**
287: * adds elements to itself
288: *
289: */
290: /**
291: * removes an element from itself
292: */
293: public void removeElement() {
294: ((AddressbookListModel) getModel())
295: .removeElement((IBasicModelPartial) getSelectedValue());
296: }
297:
298: public void valueChanged(ListSelectionEvent e) {
299: if (dndAction == true) {
300: Object[] list = getSelectedValues();
301:
302: selection1 = new IBasicModelPartial[list.length];
303:
304: for (int i = 0; i < list.length; i++) {
305: selection1[i] = (IBasicModelPartial) list[i];
306: }
307:
308: dndAction = false;
309: } else {
310: Object[] list = getSelectedValues();
311:
312: selection2 = new IBasicModelPartial[list.length];
313:
314: for (int i = 0; i < list.length; i++) {
315: selection2[i] = (IBasicModelPartial) list[i];
316: }
317:
318: dndAction = true;
319: }
320: }
321: }
|