001: package org.apache.ojb.tools.mapping.reversedb2.dnd2;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * This class allows you to implement the drag of DnD in your GUI by simply creating
020: * an instance of this class, supplying your implementation of a DragCopyCutWorkerInterface
021: * and register the Component with the helper using registerCopmponent().
022: * If the default implementation of DnD by this class doesn't satisfy your needs
023: * you can override all of the functionality by supplying your own DragGestureListener
024: * and DragSourceListener. Those interfaces are part of the Java 1.2/1.3 Dnd framework,
025: * so more information about these interfaces can be found in the JDK docs.
026: *
027: * This class is closely related to DropTargetHelper, the class responsible for
028: * the drop in DnD.
029: *
030: * To implement DnD for any Component, you have to write the following code:
031: * <CODE>
032: * new DragHelper(new YourDragCopyCutWorkerInterfaceImplementation()).registerComponent(aComponent);
033: * </CODE>
034: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
035: * @version $Id: DragHelper.java,v 1.1.2.1 2005/12/21 22:32:42 tomdz Exp $
036: */
037: public class DragHelper {
038: private java.awt.dnd.DragGestureListener dgListener = new DGListener();
039: private java.awt.dnd.DragSourceListener dsListener = new DSListener();
040: private java.awt.dnd.DragSource dragSource;
041: private java.util.Map hmDragGestureRecognizers = new java.util.HashMap();
042: private Class recognizerAbstractClass = null;
043: private DragCopyCutWorkerInterface dragWorker;
044:
045: /** Using this constructor you can completely customize the drag behaviour. You
046: * have to supply your own DragGestureListener and DragSourcecListener in addition
047: * to the DragSource, the drag gesture recognizer and the worker.
048: *
049: * The default implementation of DragGestureListener and DragSourceListener are
050: * exposed publicly in this class, so you are able to provide your own
051: * implementation for DragGestureListener or DragSourceListener and use the default
052: * one for the other.
053: * @param pDgListener Your implementation of DragGestureListener. In case you want to
054: * use the default supplied within this class, instantiate a DGListener and supply
055: * it here.
056: * @param pDsListener Your implementation of DragSourceListener. In case you want to
057: * use the default supplied within this class, instantiate a DSListener and supply
058: * it here.
059: * @param pDragSource Your DragSource implementation. The default AWT DragSource is exposed by java.awt.dnd.DragSource.getDefaultDragSource()
060: * @param pRecognizerAbstractClass The drag gesture recognizer. To use the AWT-built-in default supply a null here.
061: * @param pDragWorker Your DragWorker implementation
062: */
063: public DragHelper(java.awt.dnd.DragGestureListener pDgListener,
064: java.awt.dnd.DragSourceListener pDsListener,
065: java.awt.dnd.DragSource pDragSource,
066: Class pRecognizerAbstractClass,
067: DragCopyCutWorkerInterface pDragWorker) {
068: this (pDragSource, pRecognizerAbstractClass, pDragWorker);
069: this .dgListener = pDgListener;
070: this .dsListener = pDsListener;
071: }
072:
073: /** A more complex way of setting up dragging. In addition to your worker you need
074: * to supply the recognizer and the DragSource (usually
075: * java.awt.dnd.DragSource.getDefaultDragSource(), but you can supply your own
076: * here)
077: * @param pDragSource The drag source
078: * @param pRecognizerAbstractClass The recognizer, may be null if you want to use the Swing default implementation
079: * @param pDragWorker Your DragCopyCutWorkerInterface
080: */
081: public DragHelper(java.awt.dnd.DragSource pDragSource,
082: Class pRecognizerAbstractClass,
083: DragCopyCutWorkerInterface pDragWorker) {
084: this .dragSource = pDragSource;
085: this .recognizerAbstractClass = pRecognizerAbstractClass;
086: this .dragWorker = pDragWorker;
087: }
088:
089: /** Easiest way to setup dragging for your GUI. The default implementations for
090: * DragGestureListener, DragSourceListener and the drag gesture recognizer
091: * are used. You just need to supply a DragCopyCutWorkerInterface.
092: * @param pDragWorker Your implementation of the DragCopyCutWorkerInterface
093: */
094: public DragHelper(DragCopyCutWorkerInterface pDragWorker) {
095: this (java.awt.dnd.DragSource.getDefaultDragSource(), null,
096: pDragWorker);
097: }
098:
099: /** add a Component to this Worker. After the call dragging is enabled for this
100: * Component.
101: * @param c the Component to register
102: */
103: public void registerComponent(java.awt.Component c) {
104: unregisterComponent(c);
105: if (recognizerAbstractClass == null) {
106: hmDragGestureRecognizers.put(c, dragSource
107: .createDefaultDragGestureRecognizer(c, dragWorker
108: .getAcceptableActions(c), dgListener));
109: } else {
110: hmDragGestureRecognizers.put(c, dragSource
111: .createDragGestureRecognizer(
112: recognizerAbstractClass, c, dragWorker
113: .getAcceptableActions(c),
114: dgListener));
115: }
116: }
117:
118: /** remove drag support from the given Component.
119: * @param c the Component to remove
120: */
121: public void unregisterComponent(java.awt.Component c) {
122: java.awt.dnd.DragGestureRecognizer recognizer = (java.awt.dnd.DragGestureRecognizer) this .hmDragGestureRecognizers
123: .remove(c);
124: if (recognizer != null)
125: recognizer.setComponent(null);
126: }
127:
128: /** For more information see the javadocs of java.awt.DragGestureListener
129: * @see java.awt.dnd.DragGestureListener
130: */
131: public class DGListener implements java.awt.dnd.DragGestureListener {
132:
133: /** For more information see the javadocs of java.awt.DragGestureListener. Basically
134: * this method is called by AWT if a drag gesture has been recognized and therefore
135: * a drag action should be initiated. This method checks whether it can perform a
136: * drag, gets the transferable from the worker and starts the drag on the drag
137: * source.
138: * @param dragGestureEvent For more information see the javadocs of java.awt.DragGestureListener
139: */
140: public void dragGestureRecognized(
141: java.awt.dnd.DragGestureEvent dragGestureEvent) {
142: System.err
143: .println("DGListener.dragGestureRecognized() dragAction:"
144: + dragGestureEvent.getDragAction());
145: if (dragWorker.getAcceptableActions(dragGestureEvent
146: .getComponent()) == DnDWorkerConstants.NONE)
147: return;
148: java.awt.datatransfer.Transferable transferable = dragWorker
149: .getTransferable(dragGestureEvent
150: .getSourceAsDragGestureRecognizer()
151: .getComponent());
152: try {
153: if (transferable != null) {
154: dragSource.startDrag(dragGestureEvent, null,
155: dragWorker.getDragImage(dragGestureEvent
156: .getComponent(), transferable,
157: dragGestureEvent.getDragAction()),
158: new java.awt.Point(0, 0), transferable,
159: dsListener);
160: }
161: } catch (Throwable t) {
162: t.printStackTrace();
163: }
164: }
165: }
166:
167: /** an implementation of java.awt.dnd.DragSourceListener. The methods of this
168: * listener get called when a drag is in process.
169: */
170: public class DSListener implements java.awt.dnd.DragSourceListener {
171:
172: /** Informs the listener that the drag process has ended. If the drag was
173: * successful, the exportDone method of the worker is called.
174: * @param dragSourceDropEvent the event.
175: */
176: public void dragDropEnd(
177: java.awt.dnd.DragSourceDropEvent dragSourceDropEvent) {
178: System.err.println("DSListener.dragDropEnd()");
179: if (dragSourceDropEvent.getDropSuccess()) {
180: dragWorker.exportDone(dragSourceDropEvent
181: .getDragSourceContext().getComponent(),
182: dragSourceDropEvent.getDropAction());
183: } else {
184: // ????
185: }
186: }
187:
188: /** For more information see the javadocs of java.awt.dnd.DragSourceListener
189: * @param dragSourceDragEvent
190: */
191: public void dragEnter(
192: java.awt.dnd.DragSourceDragEvent dragSourceDragEvent) {
193: System.err.println("DSListener.dragEnter() dropAction:"
194: + dragSourceDragEvent.getDropAction());
195: /* if ( (dragSourceDragEvent.getDropAction()
196: & dragWorker.getAcceptableActions(dragSourceDragEvent.getDragSourceContext ().getComponent()))
197: != DnDWorkerConstants.NONE)
198: {
199: dragSourceDragEvent.getDragSourceContext().setCursor(java.awt.dnd.DragSource.DefaultCopyDrop);
200: }
201: else
202: {
203: dragSourceDragEvent.getDragSourceContext().setCursor(java.awt.dnd.DragSource.DefaultCopyNoDrop);
204: }*/
205: }
206:
207: /** DragSourceListener */
208: public void dragExit(
209: java.awt.dnd.DragSourceEvent dragSourceEvent) {
210: System.err.println("DSListener.dragExit()");
211: }
212:
213: /** For more information see the javadocs of java.awt.dnd.DragSourceListener
214: * @param dragSourceDragEvent
215: */
216: public void dragOver(
217: java.awt.dnd.DragSourceDragEvent dragSourceDragEvent) {
218: // System.err.println("DSListener.dragOver()");
219: }
220:
221: /** For more information see the javadocs of java.awt.dnd.DragSourceListener
222: * @param dragSourceDragEvent
223: */
224: public void dropActionChanged(
225: java.awt.dnd.DragSourceDragEvent dragSourceDragEvent) {
226: System.err
227: .println("DSListener.dropActionChanged() dropAction:"
228: + dragSourceDragEvent.getDropAction());
229: /* if ( (dragSourceDragEvent.getDropAction()
230: & dragWorker.getAcceptableActions(dragSourceDragEvent.getDragSourceContext ().getComponent()))
231: != DnDWorkerConstants.NONE)
232: {
233: dragSourceDragEvent.getDragSourceContext().setCursor(java.awt.dnd.DragSource.DefaultCopyDrop);
234: }
235: else
236: {
237: dragSourceDragEvent.getDragSourceContext().setCursor(java.awt.dnd.DragSource.DefaultCopyNoDrop);
238: }*/
239: }
240:
241: }
242:
243: }
|