KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tools > mapping > reversedb2 > dnd2 > DragHelper


1 package org.apache.ojb.tools.mapping.reversedb2.dnd2;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /**
19  * This class allows you to implement the drag of DnD in your GUI by simply creating
20  * an instance of this class, supplying your implementation of a DragCopyCutWorkerInterface
21  * and register the Component with the helper using registerCopmponent().
22  * If the default implementation of DnD by this class doesn't satisfy your needs
23  * you can override all of the functionality by supplying your own DragGestureListener
24  * and DragSourceListener. Those interfaces are part of the Java 1.2/1.3 Dnd framework,
25  * so more information about these interfaces can be found in the JDK docs.
26  *
27  * This class is closely related to DropTargetHelper, the class responsible for
28  * the drop in DnD.
29  *
30  * To implement DnD for any Component, you have to write the following code:
31  * <CODE>
32  * new DragHelper(new YourDragCopyCutWorkerInterfaceImplementation()).registerComponent(aComponent);
33  * </CODE>
34  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
35  * @version $Id: DragHelper.java,v 1.1.2.1 2005/12/21 22:32:42 tomdz Exp $
36  */

37 public class DragHelper
38 {
39     private java.awt.dnd.DragGestureListener JavaDoc dgListener = new DGListener();
40     private java.awt.dnd.DragSourceListener JavaDoc dsListener = new DSListener();
41     private java.awt.dnd.DragSource JavaDoc dragSource;
42     private java.util.Map JavaDoc hmDragGestureRecognizers = new java.util.HashMap JavaDoc();
43     private Class JavaDoc recognizerAbstractClass = null;
44     private DragCopyCutWorkerInterface dragWorker;
45     
46     /** Using this constructor you can completely customize the drag behaviour. You
47      * have to supply your own DragGestureListener and DragSourcecListener in addition
48      * to the DragSource, the drag gesture recognizer and the worker.
49      *
50      * The default implementation of DragGestureListener and DragSourceListener are
51      * exposed publicly in this class, so you are able to provide your own
52      * implementation for DragGestureListener or DragSourceListener and use the default
53      * one for the other.
54      * @param pDgListener Your implementation of DragGestureListener. In case you want to
55      * use the default supplied within this class, instantiate a DGListener and supply
56      * it here.
57      * @param pDsListener Your implementation of DragSourceListener. In case you want to
58      * use the default supplied within this class, instantiate a DSListener and supply
59      * it here.
60      * @param pDragSource Your DragSource implementation. The default AWT DragSource is exposed by java.awt.dnd.DragSource.getDefaultDragSource()
61      * @param pRecognizerAbstractClass The drag gesture recognizer. To use the AWT-built-in default supply a null here.
62      * @param pDragWorker Your DragWorker implementation
63      */

64     public DragHelper(java.awt.dnd.DragGestureListener JavaDoc pDgListener,
65                       java.awt.dnd.DragSourceListener JavaDoc pDsListener,
66                       java.awt.dnd.DragSource JavaDoc pDragSource,
67                       Class JavaDoc pRecognizerAbstractClass,
68                       DragCopyCutWorkerInterface pDragWorker)
69     {
70         this(pDragSource, pRecognizerAbstractClass, pDragWorker);
71         this.dgListener = pDgListener;
72         this.dsListener = pDsListener;
73     }
74     
75     /** A more complex way of setting up dragging. In addition to your worker you need
76      * to supply the recognizer and the DragSource (usually
77      * java.awt.dnd.DragSource.getDefaultDragSource(), but you can supply your own
78      * here)
79      * @param pDragSource The drag source
80      * @param pRecognizerAbstractClass The recognizer, may be null if you want to use the Swing default implementation
81      * @param pDragWorker Your DragCopyCutWorkerInterface
82      */

83     public DragHelper(java.awt.dnd.DragSource JavaDoc pDragSource,
84                       Class JavaDoc pRecognizerAbstractClass,
85                       DragCopyCutWorkerInterface pDragWorker)
86     {
87         this.dragSource = pDragSource;
88         this.recognizerAbstractClass = pRecognizerAbstractClass;
89         this.dragWorker = pDragWorker;
90     }
91     
92     
93     /** Easiest way to setup dragging for your GUI. The default implementations for
94      * DragGestureListener, DragSourceListener and the drag gesture recognizer
95      * are used. You just need to supply a DragCopyCutWorkerInterface.
96      * @param pDragWorker Your implementation of the DragCopyCutWorkerInterface
97      */

98     public DragHelper(DragCopyCutWorkerInterface pDragWorker)
99     {
100         this(java.awt.dnd.DragSource.getDefaultDragSource(),
101              null, pDragWorker);
102     }
103     
104     /** add a Component to this Worker. After the call dragging is enabled for this
105      * Component.
106      * @param c the Component to register
107      */

108     public void registerComponent(java.awt.Component JavaDoc c)
109     {
110         unregisterComponent(c);
111         if (recognizerAbstractClass == null)
112         {
113             hmDragGestureRecognizers.put(c,
114                 dragSource.createDefaultDragGestureRecognizer(c,
115                     dragWorker.getAcceptableActions(c), dgListener)
116                                         );
117         }
118         else
119         {
120             hmDragGestureRecognizers.put(c,
121                 dragSource.createDragGestureRecognizer (recognizerAbstractClass,
122                     c, dragWorker.getAcceptableActions(c), dgListener)
123                                         );
124         }
125     }
126     
127     /** remove drag support from the given Component.
128      * @param c the Component to remove
129      */

130     public void unregisterComponent(java.awt.Component JavaDoc c)
131     {
132         java.awt.dnd.DragGestureRecognizer JavaDoc recognizer =
133             (java.awt.dnd.DragGestureRecognizer JavaDoc)this.hmDragGestureRecognizers.remove(c);
134         if (recognizer != null)
135             recognizer.setComponent(null);
136     }
137     
138     /** For more information see the javadocs of java.awt.DragGestureListener
139      * @see java.awt.dnd.DragGestureListener
140      */

141     public class DGListener implements java.awt.dnd.DragGestureListener JavaDoc
142     {
143         
144         /** For more information see the javadocs of java.awt.DragGestureListener. Basically
145          * this method is called by AWT if a drag gesture has been recognized and therefore
146          * a drag action should be initiated. This method checks whether it can perform a
147          * drag, gets the transferable from the worker and starts the drag on the drag
148          * source.
149          * @param dragGestureEvent For more information see the javadocs of java.awt.DragGestureListener
150          */

151         public void dragGestureRecognized (java.awt.dnd.DragGestureEvent JavaDoc dragGestureEvent)
152         {
153             System.err.println("DGListener.dragGestureRecognized() dragAction:" + dragGestureEvent.getDragAction());
154             if (dragWorker.getAcceptableActions (dragGestureEvent.getComponent()) == DnDWorkerConstants.NONE) return;
155             java.awt.datatransfer.Transferable JavaDoc transferable =
156                 dragWorker.getTransferable(dragGestureEvent.getSourceAsDragGestureRecognizer ().getComponent ());
157             try
158             {
159                 if (transferable != null)
160                 {
161                     dragSource.startDrag(dragGestureEvent,
162                                          null,
163                                          dragWorker.getDragImage(dragGestureEvent.getComponent(), transferable, dragGestureEvent.getDragAction ()),
164                                          new java.awt.Point JavaDoc(0,0),
165                                          transferable,
166                                          dsListener);
167                 }
168             }
169             catch (Throwable JavaDoc t)
170             {
171                 t.printStackTrace();
172             }
173         }
174     }
175     
176     
177     /** an implementation of java.awt.dnd.DragSourceListener. The methods of this
178      * listener get called when a drag is in process.
179      */

180     public class DSListener implements java.awt.dnd.DragSourceListener JavaDoc
181     {
182         
183         /** Informs the listener that the drag process has ended. If the drag was
184          * successful, the exportDone method of the worker is called.
185          * @param dragSourceDropEvent the event.
186          */

187         public void dragDropEnd (java.awt.dnd.DragSourceDropEvent JavaDoc dragSourceDropEvent)
188         {
189             System.err.println("DSListener.dragDropEnd()");
190             if (dragSourceDropEvent.getDropSuccess())
191             {
192                 dragWorker.exportDone(dragSourceDropEvent.getDragSourceContext ().getComponent (),
193                                       dragSourceDropEvent.getDropAction());
194             }
195             else
196             {
197                 // ????
198
}
199         }
200         
201         /** For more information see the javadocs of java.awt.dnd.DragSourceListener
202          * @param dragSourceDragEvent
203          */

204         public void dragEnter (java.awt.dnd.DragSourceDragEvent JavaDoc dragSourceDragEvent)
205         {
206             System.err.println("DSListener.dragEnter() dropAction:" + dragSourceDragEvent.getDropAction());
207 /* if ( (dragSourceDragEvent.getDropAction()
208                  & dragWorker.getAcceptableActions(dragSourceDragEvent.getDragSourceContext ().getComponent()))
209                  != DnDWorkerConstants.NONE)
210             {
211                 dragSourceDragEvent.getDragSourceContext().setCursor(java.awt.dnd.DragSource.DefaultCopyDrop);
212             }
213             else
214             {
215                 dragSourceDragEvent.getDragSourceContext().setCursor(java.awt.dnd.DragSource.DefaultCopyNoDrop);
216             }*/

217         }
218         
219         /** DragSourceListener */
220         public void dragExit (java.awt.dnd.DragSourceEvent JavaDoc dragSourceEvent)
221         {
222             System.err.println("DSListener.dragExit()");
223         }
224         
225         /** For more information see the javadocs of java.awt.dnd.DragSourceListener
226          * @param dragSourceDragEvent
227          */

228         public void dragOver (java.awt.dnd.DragSourceDragEvent JavaDoc dragSourceDragEvent)
229         {
230             // System.err.println("DSListener.dragOver()");
231
}
232         
233         /** For more information see the javadocs of java.awt.dnd.DragSourceListener
234          * @param dragSourceDragEvent
235          */

236         public void dropActionChanged (java.awt.dnd.DragSourceDragEvent JavaDoc dragSourceDragEvent)
237         {
238             System.err.println("DSListener.dropActionChanged() dropAction:" + dragSourceDragEvent.getDropAction());
239 /* if ( (dragSourceDragEvent.getDropAction()
240                 & dragWorker.getAcceptableActions(dragSourceDragEvent.getDragSourceContext ().getComponent()))
241                 != DnDWorkerConstants.NONE)
242             {
243                 dragSourceDragEvent.getDragSourceContext().setCursor(java.awt.dnd.DragSource.DefaultCopyDrop);
244             }
245             else
246             {
247                 dragSourceDragEvent.getDragSourceContext().setCursor(java.awt.dnd.DragSource.DefaultCopyNoDrop);
248             }*/

249         }
250         
251     }
252     
253 }
254
Popular Tags