KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.dnd.DropTarget JavaDoc;
19 /**
20  * Starting from JDK 1.2 drag and drop was possible for Java applications. Unfortunately
21  * the framework to be used is rather complex. As of JDK 1.4 a new, more simple Dnd framework
22  * was added, but unfortunately if you want to be backwards compatible, you still have to
23  * stick with the old framework.
24  *
25  * This helper class should make it easier to implement DnD for JDK 1.2 and 1.3. To add drop
26  * support for a Component, you only have to write your Implementation of DropPasteWorkerInterface.
27  * This interface is responsible to read data from a Transferable and add it to the model of the
28  * target component.
29  * <CODE>
30  * DropTargetHelper helper = new DropTargetHelper();
31  * helper.registerDropPasteWorker (new ReverseDbNodesDropWorker());
32  * aComponent.setDropTarget(helper.getDropTarget ());
33  * </CODE>
34  * If you want to supply your own implementation of a DropTargetListener or an extension
35  * of the implementation in this class, you have to use the following code:
36  * <CODE>
37  * helper.setDefaultDropTargetListener(aDTListener);
38  * helper.removeDefaultDropTargetListener();
39  * helper.registerDefaultDropTargetListener();
40  * </CODE>
41  * Because the DropTarget is a unicast source, you first have to remove the
42  * old listener and the register the new. If you do not remove the listener
43  * before adding a new one, a TooManyListenersException is thrown.
44  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
45  * @version $Id: DropTargetHelper.java,v 1.1.2.1 2005/12/21 22:32:42 tomdz Exp $
46  */

47 public class DropTargetHelper
48 {
49     private java.awt.dnd.DropTargetListener JavaDoc defaultDTListener
50         = new org.apache.ojb.tools.mapping.reversedb2.dnd2.DropTargetHelper.DTListener();
51     
52     private DropTarget JavaDoc defaultDropTarget = new DropTarget JavaDoc();
53     
54     private java.util.Set JavaDoc dropPasteWorkerSet = new java.util.HashSet JavaDoc();
55     
56     /** Creates a new instance of DropTarget */
57     public DropTargetHelper ()
58     {
59         super();
60         try
61         {
62             defaultDropTarget.addDropTargetListener(defaultDTListener);
63             defaultDropTarget.setActive(true);
64         }
65         catch (java.util.TooManyListenersException JavaDoc tmle)
66         {
67             throw new RuntimeException JavaDoc("Internal Error: Drop Target already has a listener registered but this mustn't be at this stage");
68         }
69     }
70     
71     /**
72      * Remove current DropTargetListener from the DropTarget.
73      */

74     public void removeDefaultDropTargetListener()
75     {
76         defaultDropTarget.removeDropTargetListener(defaultDTListener);
77     }
78     
79     /** Set the current DropTargetListener as listener of the current DropTarget.
80      * @throws TooManyListenersException
81      */

82     public void registerDefaultDropTargetListener()
83         throws java.util.TooManyListenersException JavaDoc
84     {
85         defaultDropTarget.addDropTargetListener(defaultDTListener);
86     }
87     
88     /** Set a new DropTargetListner this helper is going to use.
89      * @param dtl The new DropTargetListener
90      */

91     public void setDefaultDropTargetListener(java.awt.dnd.DropTargetListener JavaDoc dtl)
92     {
93         this.defaultDTListener = dtl;
94     }
95     
96     /** Register a new DropPasteWorkerInterface.
97      * @param worker The new worker
98      */

99     public void registerDropPasteWorker(DropPasteWorkerInterface worker)
100     {
101         this.dropPasteWorkerSet.add(worker);
102         defaultDropTarget.setDefaultActions(
103             defaultDropTarget.getDefaultActions()
104             | worker.getAcceptableActions(defaultDropTarget.getComponent())
105                                            );
106     }
107     
108     /** Remove a DropPasteWorker from the helper.
109      * @param worker the worker that should be removed
110      */

111     public void removeDropPasteWorker(DropPasteWorkerInterface worker)
112     {
113         this.dropPasteWorkerSet.remove(worker);
114         java.util.Iterator JavaDoc it = this.dropPasteWorkerSet.iterator();
115         int newDefaultActions = 0;
116         while (it.hasNext())
117             newDefaultActions |= ((DropPasteWorkerInterface)it.next()).getAcceptableActions(defaultDropTarget.getComponent());
118         defaultDropTarget.setDefaultActions(newDefaultActions);
119     }
120     
121     /** Get the DropTarget (to be used in Component.setDropTarget());
122      * @return a DropTarget
123      */

124     public DropTarget JavaDoc getDropTarget()
125     {
126         return this.defaultDropTarget;
127     }
128     
129     /**
130      * An implementation of a DropTargetListener.
131      */

132     public class DTListener implements java.awt.dnd.DropTargetListener JavaDoc
133     {
134         /** see java.awt.dnd.DropTargetListener
135          * @param dropTargetDragEvent
136          */

137         public void dragEnter (java.awt.dnd.DropTargetDragEvent JavaDoc dropTargetDragEvent)
138         {
139             // 1. Check which actions are supported with the given DataFlavors
140
// and Components
141
System.err.println("DTListener.dragEnter()" + dropTargetDragEvent);
142             java.util.Iterator JavaDoc it = dropPasteWorkerSet.iterator();
143             int dropTargetSupportedActions = DnDWorkerConstants.NONE;
144             while (it.hasNext())
145                 dropTargetSupportedActions |=
146                     ((DropPasteWorkerInterface)it.next()).getAcceptableActions(
147                       dropTargetDragEvent.getDropTargetContext().getComponent(),
148                       dropTargetDragEvent.getCurrentDataFlavors());
149 // System.err.println(dropTargetDragEvent.getDropTargetContext().getTransferable().getTransferDataFlavors());
150
int dragSourceSupportedActions = dropTargetDragEvent.getSourceActions();
151             // Check wheter current actions and acceptable actions match.
152
if ((dropTargetSupportedActions & dragSourceSupportedActions) != DnDWorkerConstants.NONE)
153             {
154                 System.err.println(" accepting " + (dropTargetSupportedActions & dragSourceSupportedActions));
155                 dropTargetDragEvent.acceptDrag(dropTargetSupportedActions & dragSourceSupportedActions);
156             }
157             // No match, accept the drag with the supported drop target actions
158
else if (dropTargetSupportedActions != DnDWorkerConstants.NONE)
159             {
160                 System.err.println(" accepting " + dropTargetSupportedActions);
161                 dropTargetDragEvent.acceptDrag(dropTargetSupportedActions);
162             }
163             // No import possible at all, reject the drag
164
else
165             {
166                 System.err.println(" rejecting");
167                 dropTargetDragEvent.rejectDrag();
168             }
169         }
170         
171         /** see java.awt.dnd.DropTargetListener
172          * @param dropTargetEvent
173          */

174         public void dragExit (java.awt.dnd.DropTargetEvent JavaDoc dropTargetEvent)
175         {
176             System.err.println("DTListener.dragExit()" + dropTargetEvent);
177         }
178         
179         /** see java.awt.dnd.DropTargetListener
180          * @param dropTargetDragEvent
181          */

182         public void dragOver (java.awt.dnd.DropTargetDragEvent JavaDoc dropTargetDragEvent)
183         {
184
185         }
186         
187         /** see java.awt.dnd.DropTargetListener
188          * @param dropTargetDropEvent
189          */

190         public void drop (java.awt.dnd.DropTargetDropEvent JavaDoc dropTargetDropEvent)
191         {
192             System.err.println("DTListener.drop()" + dropTargetDropEvent);
193             
194             java.util.Iterator JavaDoc it = dropPasteWorkerSet.iterator();
195             boolean result = false;
196             
197             while (!result & it.hasNext())
198             {
199                 DropPasteWorkerInterface worker = (DropPasteWorkerInterface)it.next();
200                 int acceptableActions = worker.getAcceptableActions(dropTargetDropEvent.getDropTargetContext ().getComponent(),
201                                                 dropTargetDropEvent.getTransferable().getTransferDataFlavors());
202                 if ((acceptableActions & dropTargetDropEvent.getDropAction())
203                     != DnDWorkerConstants.NONE)
204                 {
205                     dropTargetDropEvent.acceptDrop(acceptableActions & dropTargetDropEvent.getDropAction());
206                     result = worker.importData(dropTargetDropEvent.getDropTargetContext ().getComponent(),
207                                                dropTargetDropEvent.getTransferable(),
208                                                dropTargetDropEvent.getDropAction());
209                 }
210             }
211             dropTargetDropEvent.dropComplete(result);
212         }
213         
214         /** see java.awt.dnd.DropTargetListener
215          * @param dropTargetDragEvent
216          * @see java.awt.dnd.DropTargetListener
217          */

218         public void dropActionChanged (java.awt.dnd.DropTargetDragEvent JavaDoc dropTargetDragEvent)
219         {
220             System.err.println("DTListener.dragEnter()" + dropTargetDragEvent);
221             dragEnter(dropTargetDragEvent);
222         }
223         
224     }
225     
226 }
227
Popular Tags