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: package java.awt.dnd;
019:
020: import java.awt.Component;
021: import java.awt.Cursor;
022: import java.awt.GraphicsEnvironment;
023: import java.awt.HeadlessException;
024: import java.awt.Image;
025: import java.awt.Point;
026: import java.awt.Toolkit;
027: import java.awt.datatransfer.FlavorMap;
028: import java.awt.datatransfer.SystemFlavorMap;
029: import java.awt.datatransfer.Transferable;
030: import java.awt.dnd.peer.DragSourceContextPeer;
031: import java.io.Serializable;
032: import java.util.EventListener;
033:
034: import org.apache.harmony.awt.ListenerList;
035: import org.apache.harmony.awt.internal.nls.Messages;
036:
037: // TODO: think of synchronization
038: public class DragSource implements Serializable {
039:
040: private static final long serialVersionUID = 6236096958971414066L;
041:
042: public static final Cursor DefaultMoveDrop;
043:
044: public static final Cursor DefaultMoveNoDrop;
045:
046: public static final Cursor DefaultCopyDrop;
047:
048: public static final Cursor DefaultCopyNoDrop;
049:
050: public static final Cursor DefaultLinkDrop;
051:
052: public static final Cursor DefaultLinkNoDrop;
053:
054: private static final int DEFAULT_DRAG_THRESHOLD = 5;
055:
056: private static DragSource defaultSource;
057: private static DragSourceContext curContext;
058:
059: private final ListenerList<DragSourceListener> dragSourceListeners;
060: private final ListenerList<DragSourceMotionListener> dragSourceMotionListeners;
061:
062: static {
063: if (GraphicsEnvironment.isHeadless()) {
064: DefaultMoveDrop = DefaultMoveNoDrop = DefaultCopyDrop = null;
065: DefaultCopyNoDrop = DefaultLinkDrop = DefaultLinkNoDrop = null;
066: } else {
067: Toolkit toolkit = Toolkit.getDefaultToolkit();
068:
069: DefaultMoveDrop = getDefaultCursor(toolkit,
070: "dnd.MoveCursor"); //$NON-NLS-1$
071: DefaultMoveNoDrop = getDefaultCursor(toolkit,
072: "dnd.NoMoveCursor"); //$NON-NLS-1$
073: DefaultCopyDrop = getDefaultCursor(toolkit,
074: "dnd.CopyCursor"); //$NON-NLS-1$
075: DefaultCopyNoDrop = getDefaultCursor(toolkit,
076: "dnd.NoCopyCursor"); //$NON-NLS-1$
077: DefaultLinkDrop = getDefaultCursor(toolkit,
078: "dnd.LinkCursor"); //$NON-NLS-1$
079: DefaultLinkNoDrop = getDefaultCursor(toolkit,
080: "dnd.NoLinkCursor"); //$NON-NLS-1$
081: }
082: }
083:
084: private static Cursor getDefaultCursor(Toolkit toolkit, String name) {
085: try {
086: return (Cursor) toolkit.getDesktopProperty(name);
087: } catch (Exception e) {
088: e.printStackTrace();
089: throw new RuntimeException(Messages.getString("awt.170", e)); //$NON-NLS-1$
090: }
091: }
092:
093: public static DragSource getDefaultDragSource() {
094: if (GraphicsEnvironment.isHeadless()) {
095: throw new HeadlessException();
096: }
097:
098: if (defaultSource == null) {
099: defaultSource = new DragSource();
100: }
101:
102: return defaultSource;
103: }
104:
105: public static boolean isDragImageSupported() {
106: return false;
107: }
108:
109: public static int getDragThreshold() {
110: int threshold = Integer
111: .getInteger("awt.dnd.drag.threshold", -1).intValue(); //$NON-NLS-1$
112:
113: if (threshold <= 0) {
114: Object val = Toolkit.getDefaultToolkit()
115: .getDesktopProperty("DnD.gestureMotionThreshold" //$NON-NLS-1$
116: );
117:
118: if (val != null && val instanceof Integer) {
119: threshold = ((Integer) val).intValue();
120: }
121:
122: if (threshold <= 0) {
123: threshold = DEFAULT_DRAG_THRESHOLD;
124: }
125: }
126:
127: return threshold;
128: }
129:
130: public DragSource() throws HeadlessException {
131: if (GraphicsEnvironment.isHeadless()) {
132: throw new HeadlessException();
133: }
134:
135: dragSourceListeners = new ListenerList<DragSourceListener>();
136: dragSourceMotionListeners = new ListenerList<DragSourceMotionListener>();
137: }
138:
139: public DragSourceListener[] getDragSourceListeners() {
140: return dragSourceListeners
141: .getUserListeners(new DragSourceListener[0]);
142: }
143:
144: public void addDragSourceListener(DragSourceListener dsl) {
145: dragSourceListeners.addUserListener(dsl);
146: }
147:
148: public void removeDragSourceListener(DragSourceListener dsl) {
149: dragSourceListeners.removeUserListener(dsl);
150: }
151:
152: public DragSourceMotionListener[] getDragSourceMotionListeners() {
153: return dragSourceMotionListeners
154: .getUserListeners(new DragSourceMotionListener[0]);
155: }
156:
157: public void addDragSourceMotionListener(
158: DragSourceMotionListener dsml) {
159: dragSourceMotionListeners.addUserListener(dsml);
160: }
161:
162: public void removeDragSourceMotionListener(
163: DragSourceMotionListener dsml) {
164: dragSourceMotionListeners.removeUserListener(dsml);
165: }
166:
167: @SuppressWarnings("unchecked")
168: public <T extends EventListener> T[] getListeners(
169: Class<T> listenerType) {
170: if (DragSourceListener.class.isAssignableFrom(listenerType)) {
171: return (T[]) getDragSourceListeners();
172: } else if (DragSourceMotionListener.class
173: .isAssignableFrom(listenerType)) {
174: return (T[]) getDragSourceMotionListeners();
175: }
176:
177: return (T[]) new EventListener[0];
178: }
179:
180: protected DragSourceContext createDragSourceContext(
181: DragSourceContextPeer dscp, DragGestureEvent dgl,
182: Cursor dragCursor, Image dragImage, Point imageOffset,
183: Transferable t, DragSourceListener dsl) {
184: return new DragSourceContext(dscp, dgl, dragCursor, dragImage,
185: imageOffset, t, dsl);
186: }
187:
188: public FlavorMap getFlavorMap() {
189: return SystemFlavorMap.getDefaultFlavorMap();
190: }
191:
192: public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
193: Image dragImage, Point imageOffset,
194: Transferable transferable, DragSourceListener dsl,
195: FlavorMap flavorMap) throws InvalidDnDOperationException {
196:
197: if (curContext != null) {
198: // awt.171=Attempt to start a drag while an existing drag operation is still executing.
199: throw new InvalidDnDOperationException(Messages
200: .getString("awt.171")); //$NON-NLS-1$
201: }
202:
203: DragSourceContextPeer peer = Toolkit.getDefaultToolkit()
204: .createDragSourceContextPeer(trigger);
205: curContext = createDragSourceContext(peer, trigger, dragCursor,
206: dragImage, imageOffset, transferable, dsl);
207:
208: peer.startDrag(curContext, dragCursor, dragImage, imageOffset);
209: curContext = null;
210: }
211:
212: public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
213: Image dragImage, Point dragOffset,
214: Transferable transferable, DragSourceListener dsl)
215: throws InvalidDnDOperationException {
216:
217: startDrag(trigger, dragCursor, dragImage, dragOffset,
218: transferable, dsl, null);
219: }
220:
221: public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
222: Transferable transferable, DragSourceListener dsl,
223: FlavorMap flavorMap) throws InvalidDnDOperationException {
224:
225: startDrag(trigger, dragCursor, null, null, transferable, dsl,
226: flavorMap);
227: }
228:
229: public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
230: Transferable transferable, DragSourceListener dsl)
231: throws InvalidDnDOperationException {
232:
233: startDrag(trigger, dragCursor, transferable, dsl, null);
234: }
235:
236: @SuppressWarnings("unchecked")
237: public <T extends DragGestureRecognizer> T createDragGestureRecognizer(
238: Class<T> recognizerAbstractClass, Component c, int actions,
239: DragGestureListener dgl) {
240:
241: Toolkit t = Toolkit.getDefaultToolkit();
242: return t.createDragGestureRecognizer(recognizerAbstractClass,
243: this , c, actions, dgl);
244: }
245:
246: public DragGestureRecognizer createDefaultDragGestureRecognizer(
247: Component c, int actions, DragGestureListener dgl) {
248:
249: Toolkit t = Toolkit.getDefaultToolkit();
250: return t
251: .createDragGestureRecognizer(
252: MouseDragGestureRecognizer.class, this, c,
253: actions, dgl);
254: }
255:
256: }
|