001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: * @author Michael Danilov, Pavel Dolgov
019: * @version $Revision$
020: */package java.awt.dnd;
021:
022: import java.awt.Component;
023: import java.awt.GraphicsEnvironment;
024: import java.awt.HeadlessException;
025: import java.awt.Point;
026: import java.awt.datatransfer.FlavorMap;
027: import java.awt.datatransfer.SystemFlavorMap;
028: import java.awt.dnd.peer.DropTargetContextPeer;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.peer.ComponentPeer;
032: import java.io.Serializable;
033: import java.util.TooManyListenersException;
034:
035: import org.apache.harmony.awt.datatransfer.DTK;
036: import org.apache.harmony.awt.internal.nls.Messages;
037: import org.apache.harmony.luni.util.NotImplementedException;
038:
039: public class DropTarget implements DropTargetListener, Serializable {
040:
041: private static final long serialVersionUID = -6283860791671019047L;
042:
043: protected static class DropTargetAutoScroller implements
044: ActionListener {
045: Component component;
046: Point point;
047:
048: protected DropTargetAutoScroller(Component c, Point p) {
049: component = c;
050: point = p;
051: }
052:
053: protected void stop() throws NotImplementedException {
054: }
055:
056: protected void updateLocation(Point newLocn)
057: throws NotImplementedException {
058: point = (Point) newLocn.clone();
059: }
060:
061: public void actionPerformed(ActionEvent e)
062: throws NotImplementedException {
063: }
064:
065: }
066:
067: Component component;
068: boolean active;
069: int actions;
070: FlavorMap flavorMap;
071: DropTargetListener dropTargetListener;
072: final DropTargetContext context;
073: DropTargetAutoScroller autoScroller;
074:
075: public DropTarget(Component c, int ops, DropTargetListener dtl,
076: boolean act, FlavorMap fm) throws HeadlessException {
077: if (GraphicsEnvironment.isHeadless()) {
078: throw new HeadlessException();
079: }
080: context = createDropTargetContext();
081: setDefaultActionsImpl(ops);
082: active = (c != null) ? act : true;
083: dropTargetListener = dtl;
084: flavorMap = (fm != null) ? fm : SystemFlavorMap
085: .getDefaultFlavorMap();
086: component = c;
087: if (c != null) {
088: c.setDropTarget(this );
089: }
090: }
091:
092: public DropTarget(Component c, DropTargetListener dtl)
093: throws HeadlessException {
094: this (c, DnDConstants.ACTION_COPY_OR_MOVE, dtl, true, null);
095: }
096:
097: public DropTarget(Component c, int ops, DropTargetListener dtl,
098: boolean act) throws HeadlessException {
099: this (c, ops, dtl, act, null);
100: }
101:
102: public DropTarget(Component c, int ops, DropTargetListener dtl)
103: throws HeadlessException {
104: this (c, ops, dtl, true, null);
105: }
106:
107: public DropTarget() throws HeadlessException {
108: this (null, DnDConstants.ACTION_COPY_OR_MOVE, null, true, null);
109: }
110:
111: protected DropTargetAutoScroller createDropTargetAutoScroller(
112: Component c, Point p) {
113: return new DropTargetAutoScroller(c, p);
114: }
115:
116: public void removeNotify(ComponentPeer peer)
117: throws NotImplementedException {
118: context.removeNotify();
119: }
120:
121: public void addNotify(ComponentPeer peer)
122: throws NotImplementedException {
123: if (component == null) {
124: return;
125: }
126:
127: DTK dtk = DTK.getDTK();
128: DropTargetContextPeer dtp = dtk
129: .createDropTargetContextPeer(context);
130: context.addNotify(dtp);
131: }
132:
133: public synchronized void removeDropTargetListener(
134: DropTargetListener dtl) {
135: if (dtl == null || dropTargetListener == null) {
136: return;
137: }
138: if (dtl != dropTargetListener) {
139: // awt.175=Listener mismatch
140: throw new IllegalArgumentException(Messages
141: .getString("awt.175")); //$NON-NLS-1$
142: }
143: dropTargetListener = null;
144: }
145:
146: public synchronized void addDropTargetListener(
147: DropTargetListener dtl) throws TooManyListenersException {
148: if (dtl == null) {
149: return;
150: }
151: if (dtl == this ) {
152: // awt.176=DropTarget cannot be added as listener to itself
153: throw new IllegalArgumentException(Messages
154: .getString("awt.176")); //$NON-NLS-1$
155: }
156: if (dropTargetListener != null) {
157: throw new TooManyListenersException();
158: }
159: dropTargetListener = dtl;
160: }
161:
162: public synchronized void dragExit(DropTargetEvent dte) {
163: if (!isActive()) {
164: return;
165: }
166: if (dropTargetListener != null) {
167: dropTargetListener.dragExit(dte);
168: }
169: }
170:
171: public synchronized void drop(DropTargetDropEvent dtde) {
172: if (!isActive()) {
173: if (dtde == null) {
174: throw new NullPointerException();
175: }
176: return;
177: }
178: if (dtde == null && dropTargetListener == null) {
179: throw new NullPointerException();
180: }
181: if (dropTargetListener != null) {
182: dropTargetListener.drop(dtde);
183: }
184: }
185:
186: public synchronized void dropActionChanged(DropTargetDragEvent dtde) {
187: if (!isActive()) {
188: return;
189: }
190: if (dtde == null) {
191: throw new NullPointerException();
192: }
193: if (dropTargetListener != null) {
194: dropTargetListener.dropActionChanged(dtde);
195: }
196: }
197:
198: public synchronized void dragOver(DropTargetDragEvent dtde) {
199: if (!isActive()) {
200: return;
201: }
202: if (dtde == null) {
203: throw new NullPointerException();
204: }
205: if (dropTargetListener != null) {
206: dropTargetListener.dragOver(dtde);
207: }
208: }
209:
210: public synchronized void dragEnter(DropTargetDragEvent dtde) {
211: if (!isActive()) {
212: return;
213: }
214: if (dtde == null) {
215: throw new NullPointerException();
216: }
217: if (dropTargetListener != null) {
218: dropTargetListener.dragEnter(dtde);
219: }
220: }
221:
222: public DropTargetContext getDropTargetContext() {
223: return context;
224: }
225:
226: protected DropTargetContext createDropTargetContext() {
227: return new DropTargetContext(this );
228: }
229:
230: public void setFlavorMap(FlavorMap fm) {
231: synchronized (this ) {
232: flavorMap = (fm != null) ? fm : SystemFlavorMap
233: .getDefaultFlavorMap();
234: }
235: }
236:
237: public FlavorMap getFlavorMap() {
238: synchronized (this ) {
239: return flavorMap;
240: }
241: }
242:
243: protected void updateAutoscroll(Point dragCursorLocn) {
244: synchronized (this ) {
245: autoScroller.updateLocation(dragCursorLocn);
246: }
247: }
248:
249: protected void initializeAutoscrolling(Point p) {
250: synchronized (this ) {
251: autoScroller = createDropTargetAutoScroller(component, p);
252: }
253: }
254:
255: public synchronized void setComponent(Component c) {
256: if (component == c) {
257: return;
258: }
259: Component oldComponent = component;
260: component = c;
261: if (oldComponent != null) {
262: oldComponent.setDropTarget(null);
263: }
264: if (c != null) {
265: c.setDropTarget(this );
266: }
267: }
268:
269: public synchronized Component getComponent() {
270: return component;
271: }
272:
273: public synchronized void setActive(boolean isActive) {
274: synchronized (this ) {
275: active = isActive;
276: }
277: }
278:
279: public void setDefaultActions(int ops) {
280: synchronized (this ) {
281: setDefaultActionsImpl(ops);
282: }
283: }
284:
285: private void setDefaultActionsImpl(int ops) {
286: actions = ops
287: & (DnDConstants.ACTION_LINK | DnDConstants.ACTION_COPY_OR_MOVE);
288: }
289:
290: public boolean isActive() {
291: synchronized (this ) {
292: return active;
293: }
294: }
295:
296: protected void clearAutoscroll() {
297: synchronized (this ) {
298: autoScroller = null;
299: }
300: }
301:
302: public int getDefaultActions() {
303: synchronized (this) {
304: return actions;
305: }
306: }
307:
308: }
|