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: import java.awt.dnd.DropTarget;
019:
020: /**
021: * Starting from JDK 1.2 drag and drop was possible for Java applications. Unfortunately
022: * the framework to be used is rather complex. As of JDK 1.4 a new, more simple Dnd framework
023: * was added, but unfortunately if you want to be backwards compatible, you still have to
024: * stick with the old framework.
025: *
026: * This helper class should make it easier to implement DnD for JDK 1.2 and 1.3. To add drop
027: * support for a Component, you only have to write your Implementation of DropPasteWorkerInterface.
028: * This interface is responsible to read data from a Transferable and add it to the model of the
029: * target component.
030: * <CODE>
031: * DropTargetHelper helper = new DropTargetHelper();
032: * helper.registerDropPasteWorker (new ReverseDbNodesDropWorker());
033: * aComponent.setDropTarget(helper.getDropTarget ());
034: * </CODE>
035: * If you want to supply your own implementation of a DropTargetListener or an extension
036: * of the implementation in this class, you have to use the following code:
037: * <CODE>
038: * helper.setDefaultDropTargetListener(aDTListener);
039: * helper.removeDefaultDropTargetListener();
040: * helper.registerDefaultDropTargetListener();
041: * </CODE>
042: * Because the DropTarget is a unicast source, you first have to remove the
043: * old listener and the register the new. If you do not remove the listener
044: * before adding a new one, a TooManyListenersException is thrown.
045: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
046: * @version $Id: DropTargetHelper.java,v 1.1.2.1 2005/12/21 22:32:42 tomdz Exp $
047: */
048: public class DropTargetHelper {
049: private java.awt.dnd.DropTargetListener defaultDTListener = new org.apache.ojb.tools.mapping.reversedb2.dnd2.DropTargetHelper.DTListener();
050:
051: private DropTarget defaultDropTarget = new DropTarget();
052:
053: private java.util.Set dropPasteWorkerSet = new java.util.HashSet();
054:
055: /** Creates a new instance of DropTarget */
056: public DropTargetHelper() {
057: super ();
058: try {
059: defaultDropTarget.addDropTargetListener(defaultDTListener);
060: defaultDropTarget.setActive(true);
061: } catch (java.util.TooManyListenersException tmle) {
062: throw new RuntimeException(
063: "Internal Error: Drop Target already has a listener registered but this mustn't be at this stage");
064: }
065: }
066:
067: /**
068: * Remove current DropTargetListener from the DropTarget.
069: */
070: public void removeDefaultDropTargetListener() {
071: defaultDropTarget.removeDropTargetListener(defaultDTListener);
072: }
073:
074: /** Set the current DropTargetListener as listener of the current DropTarget.
075: * @throws TooManyListenersException
076: */
077: public void registerDefaultDropTargetListener()
078: throws java.util.TooManyListenersException {
079: defaultDropTarget.addDropTargetListener(defaultDTListener);
080: }
081:
082: /** Set a new DropTargetListner this helper is going to use.
083: * @param dtl The new DropTargetListener
084: */
085: public void setDefaultDropTargetListener(
086: java.awt.dnd.DropTargetListener dtl) {
087: this .defaultDTListener = dtl;
088: }
089:
090: /** Register a new DropPasteWorkerInterface.
091: * @param worker The new worker
092: */
093: public void registerDropPasteWorker(DropPasteWorkerInterface worker) {
094: this .dropPasteWorkerSet.add(worker);
095: defaultDropTarget.setDefaultActions(defaultDropTarget
096: .getDefaultActions()
097: | worker.getAcceptableActions(defaultDropTarget
098: .getComponent()));
099: }
100:
101: /** Remove a DropPasteWorker from the helper.
102: * @param worker the worker that should be removed
103: */
104: public void removeDropPasteWorker(DropPasteWorkerInterface worker) {
105: this .dropPasteWorkerSet.remove(worker);
106: java.util.Iterator it = this .dropPasteWorkerSet.iterator();
107: int newDefaultActions = 0;
108: while (it.hasNext())
109: newDefaultActions |= ((DropPasteWorkerInterface) it.next())
110: .getAcceptableActions(defaultDropTarget
111: .getComponent());
112: defaultDropTarget.setDefaultActions(newDefaultActions);
113: }
114:
115: /** Get the DropTarget (to be used in Component.setDropTarget());
116: * @return a DropTarget
117: */
118: public DropTarget getDropTarget() {
119: return this .defaultDropTarget;
120: }
121:
122: /**
123: * An implementation of a DropTargetListener.
124: */
125: public class DTListener implements java.awt.dnd.DropTargetListener {
126: /** see java.awt.dnd.DropTargetListener
127: * @param dropTargetDragEvent
128: */
129: public void dragEnter(
130: java.awt.dnd.DropTargetDragEvent dropTargetDragEvent) {
131: // 1. Check which actions are supported with the given DataFlavors
132: // and Components
133: System.err.println("DTListener.dragEnter()"
134: + dropTargetDragEvent);
135: java.util.Iterator it = dropPasteWorkerSet.iterator();
136: int dropTargetSupportedActions = DnDWorkerConstants.NONE;
137: while (it.hasNext())
138: dropTargetSupportedActions |= ((DropPasteWorkerInterface) it
139: .next()).getAcceptableActions(
140: dropTargetDragEvent.getDropTargetContext()
141: .getComponent(), dropTargetDragEvent
142: .getCurrentDataFlavors());
143: // System.err.println(dropTargetDragEvent.getDropTargetContext().getTransferable().getTransferDataFlavors());
144: int dragSourceSupportedActions = dropTargetDragEvent
145: .getSourceActions();
146: // Check wheter current actions and acceptable actions match.
147: if ((dropTargetSupportedActions & dragSourceSupportedActions) != DnDWorkerConstants.NONE) {
148: System.err
149: .println(" accepting "
150: + (dropTargetSupportedActions & dragSourceSupportedActions));
151: dropTargetDragEvent
152: .acceptDrag(dropTargetSupportedActions
153: & dragSourceSupportedActions);
154: }
155: // No match, accept the drag with the supported drop target actions
156: else if (dropTargetSupportedActions != DnDWorkerConstants.NONE) {
157: System.err.println(" accepting "
158: + dropTargetSupportedActions);
159: dropTargetDragEvent
160: .acceptDrag(dropTargetSupportedActions);
161: }
162: // No import possible at all, reject the drag
163: else {
164: System.err.println(" rejecting");
165: dropTargetDragEvent.rejectDrag();
166: }
167: }
168:
169: /** see java.awt.dnd.DropTargetListener
170: * @param dropTargetEvent
171: */
172: public void dragExit(
173: java.awt.dnd.DropTargetEvent dropTargetEvent) {
174: System.err.println("DTListener.dragExit()"
175: + dropTargetEvent);
176: }
177:
178: /** see java.awt.dnd.DropTargetListener
179: * @param dropTargetDragEvent
180: */
181: public void dragOver(
182: java.awt.dnd.DropTargetDragEvent dropTargetDragEvent) {
183:
184: }
185:
186: /** see java.awt.dnd.DropTargetListener
187: * @param dropTargetDropEvent
188: */
189: public void drop(
190: java.awt.dnd.DropTargetDropEvent dropTargetDropEvent) {
191: System.err.println("DTListener.drop()"
192: + dropTargetDropEvent);
193:
194: java.util.Iterator it = dropPasteWorkerSet.iterator();
195: boolean result = false;
196:
197: while (!result & it.hasNext()) {
198: DropPasteWorkerInterface worker = (DropPasteWorkerInterface) it
199: .next();
200: int acceptableActions = worker.getAcceptableActions(
201: dropTargetDropEvent.getDropTargetContext()
202: .getComponent(), dropTargetDropEvent
203: .getTransferable()
204: .getTransferDataFlavors());
205: if ((acceptableActions & dropTargetDropEvent
206: .getDropAction()) != DnDWorkerConstants.NONE) {
207: dropTargetDropEvent.acceptDrop(acceptableActions
208: & dropTargetDropEvent.getDropAction());
209: result = worker.importData(dropTargetDropEvent
210: .getDropTargetContext().getComponent(),
211: dropTargetDropEvent.getTransferable(),
212: dropTargetDropEvent.getDropAction());
213: }
214: }
215: dropTargetDropEvent.dropComplete(result);
216: }
217:
218: /** see java.awt.dnd.DropTargetListener
219: * @param dropTargetDragEvent
220: * @see java.awt.dnd.DropTargetListener
221: */
222: public void dropActionChanged(
223: java.awt.dnd.DropTargetDragEvent dropTargetDragEvent) {
224: System.err.println("DTListener.dragEnter()"
225: + dropTargetDragEvent);
226: dragEnter(dropTargetDragEvent);
227: }
228:
229: }
230:
231: }
|