KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > gui > data > UniqueIDDropTargetListener


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ic2d.gui.data;
32
33 import org.objectweb.proactive.core.UniqueID;
34
35 /**
36  * MyDropTargetListener
37  * a listener that tracks the state of the operation
38  * @see java.awt.dnd.DropTargetListener
39  * @see java.awt.dnd.DropTarget
40  */

41 public abstract class UniqueIDDropTargetListener implements java.awt.dnd.DropTargetListener JavaDoc {
42   
43
44   //
45
// -- CONSTRUCTORS -----------------------------------------------
46
//
47

48   public UniqueIDDropTargetListener() {
49   }
50   
51   
52   //
53
// -- PUBLIC METHODS -----------------------------------------------
54
//
55

56   //
57
// -- implements DropTargetListener -----------------------------------------------
58
//
59

60   /**
61    * start "drag under" feedback on component
62    * invoke acceptDrag or rejectDrag based on isDragOk
63    */

64   public void dragEnter(java.awt.dnd.DropTargetDragEvent JavaDoc e) {
65     //System.out.println("dragEnter");
66
if (isDragOk(e)) {
67       showDragFeedBack();
68       e.acceptDrag(e.getDropAction());
69     } else {
70       hideDnDFeedBack();
71       e.rejectDrag();
72     }
73   }
74
75   /**
76    * continue "drag under" feedback on component
77    * invoke acceptDrag or rejectDrag based on isDragOk
78    */

79   public void dragOver(java.awt.dnd.DropTargetDragEvent JavaDoc e) {
80     //System.out.println("dragOver");
81
if (isDragOk(e)) {
82         e.acceptDrag(e.getDropAction());
83     } else {
84       e.rejectDrag();
85     }
86   }
87   
88   public void dropActionChanged(java.awt.dnd.DropTargetDragEvent JavaDoc e) {
89     //System.out.println("dropActionChanged");
90
if (isDragOk(e)) {
91       e.acceptDrag(e.getDropAction());
92     } else {
93       e.rejectDrag();
94     }
95   }
96   
97   public void dragExit(java.awt.dnd.DropTargetEvent JavaDoc e) {
98     //System.out.println("dragExit");
99
hideDnDFeedBack();
100   }
101
102   /**
103    * perform action from getSourceActions on the transferrable
104    * invoke acceptDrop or rejectDrop, invoke dropComplete
105    * get the transferable according to the chosen flavor, do the transfer
106    */

107   public void drop(java.awt.dnd.DropTargetDropEvent JavaDoc event) {
108     showDropFeedBack();
109     java.awt.datatransfer.Transferable JavaDoc transferable = event.getTransferable();
110     // we accept only UniqueID
111
if (! transferable.isDataFlavorSupported(TransferableUniqueID.UNIQUEID_FLAVOR)) {
112       rejectDrop(event);
113       return;
114     }
115     // try to get the ActiveObject
116
Object JavaDoc data = null;
117     try {
118       data = event.getTransferable().getTransferData(TransferableUniqueID.UNIQUEID_FLAVOR);
119     } catch (java.io.IOException JavaDoc e) {
120       rejectDrop(event);
121       return;
122     } catch (java.awt.datatransfer.UnsupportedFlavorException JavaDoc e) {
123       rejectDrop(event);
124       return;
125     }
126     if (data == null || !(data instanceof UniqueID)) {
127       rejectDrop(event);
128       return;
129     }
130     UniqueID uniqueID = (UniqueID) data;
131     boolean result = processDrop(event, uniqueID);
132     // check if not the same node
133
if (! result) {
134       rejectDrop(event);
135       return;
136     }
137   }
138   
139   
140   //
141
// -- PROTECTED METHODS -----------------------------------------------
142
//
143

144  /**
145   * Processes the drop and return false if the drop is rejected or true if the drop is accepted.
146   * The method must not call the rejectDrop as returning false signel that the drop is rejected.
147   * On the other hand it is the responsability of this method to call the acceptDrop and dropComplete
148   * when accepting the drop and returning true
149   */

150   protected abstract boolean processDrop(java.awt.dnd.DropTargetDropEvent JavaDoc event, UniqueID id);
151   
152
153  /**
154   * Displays a user feed back to show that the drag is going on
155   */

156   protected abstract void showDragFeedBack();
157   
158
159  /**
160   * Displays a user feed back to show that the drop is going on
161   */

162   protected abstract void showDropFeedBack();
163      
164    
165  /**
166   * Removes the user feed back that shows the drag
167   */

168   protected abstract void hideDnDFeedBack();
169      
170    
171   //
172
// -- PRIVATE METHODS -----------------------------------------------
173
//
174

175   private void rejectDrop(java.awt.dnd.DropTargetDropEvent JavaDoc event) {
176     event.rejectDrop();
177     hideDnDFeedBack();
178   }
179
180
181   /**
182    * Called by dragEnter and dragOver
183    * Checks the flavors and operations
184    * @param e the event object
185    * @return whether the flavor and operation is ok
186    */

187   private boolean isDragOk(java.awt.dnd.DropTargetDragEvent JavaDoc event) {
188     java.awt.datatransfer.DataFlavor JavaDoc[] df = event.getCurrentDataFlavors();
189     if (! event.isDataFlavorSupported(TransferableUniqueID.UNIQUEID_FLAVOR)) return false;
190     // we're saying that these actions are necessary
191
if ((event.getDropAction() & java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE) == 0) return false;
192     return true;
193   }
194
195
196 }
197
Popular Tags