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
019: * @version $Revision$
020: */package java.awt.dnd;
021:
022: import java.awt.*;
023: import java.awt.datatransfer.Transferable;
024: import java.awt.dnd.peer.DragSourceContextPeer;
025: import java.io.Serializable;
026: import java.util.TooManyListenersException;
027:
028: import org.apache.harmony.awt.internal.nls.Messages;
029:
030: public class DragSourceContext implements DragSourceListener,
031: DragSourceMotionListener, Serializable {
032:
033: private static final long serialVersionUID = -115407898692194719L;
034:
035: protected static final int DEFAULT = 0;
036:
037: protected static final int ENTER = 1;
038:
039: protected static final int OVER = 2;
040:
041: protected static final int CHANGED = 3;
042:
043: private static final int EXIT = DEFAULT;
044:
045: private final DragSource dragSource;
046: private final DragGestureEvent trigger;
047: private final Transferable transferable;
048: private final Component component;
049: private final DragSourceContextPeer peer;
050:
051: private int sourceAction;
052: private DragSourceListener listener;
053: private Cursor cursor;
054: private boolean defaultCursor;
055: private int lastTargetAction;
056: private int lastStatus;
057:
058: public DragSourceContext(DragSourceContextPeer dscp,
059: DragGestureEvent trigger, Cursor dragCursor,
060: Image dragImage, Point offset, Transferable t,
061: DragSourceListener dsl) {
062: if (dscp == null) {
063: // awt.179=Context peer is null.
064: throw new NullPointerException(Messages
065: .getString("awt.179")); //$NON-NLS-1$
066: }
067: if (trigger == null) {
068: // awt.17A=Trigger event is null.
069: throw new NullPointerException(Messages
070: .getString("awt.17A")); //$NON-NLS-1$
071: }
072: if (trigger.getDragAction() == DnDConstants.ACTION_NONE) {
073: // awt.17B=Can't init ACTION_NONE drag.
074: throw new RuntimeException(Messages.getString("awt.17B")); //$NON-NLS-1$
075: }
076: if ((dragImage != null) && (offset == null)) {
077: // awt.17C=Image offset is null.
078: throw new NullPointerException(Messages
079: .getString("awt.17C")); //$NON-NLS-1$
080: }
081: if (t == null) {
082: // awt.17D=Transferable is null.
083: throw new NullPointerException(Messages
084: .getString("awt.17D")); //$NON-NLS-1$
085: }
086: if (trigger.getComponent() == null) {
087: // awt.17E=Component associated with the trigger event is null.
088: throw new IllegalArgumentException(Messages
089: .getString("awt.17E")); //$NON-NLS-1$
090: }
091: if (trigger.getDragSource() == null) {
092: // awt.17F=DragSource for the trigger event is null.
093: throw new IllegalArgumentException(Messages
094: .getString("awt.17F")); //$NON-NLS-1$
095: }
096: if (trigger.getSourceAsDragGestureRecognizer()
097: .getSourceActions() == DnDConstants.ACTION_NONE) {
098: // awt.180=Source actions for the DragGestureRecognizer associated with the trigger event are equal to DnDConstants.ACTION_NONE.
099: throw new IllegalArgumentException(Messages
100: .getString("awt.180")); //$NON-NLS-1$
101: }
102:
103: this .trigger = trigger;
104: transferable = t;
105: dragSource = trigger.getDragSource();
106: sourceAction = trigger.getDragAction();
107: component = trigger.getComponent();
108: peer = dscp;
109:
110: try {
111: addDragSourceListener(dsl);
112: } catch (TooManyListenersException e) {
113: }
114: lastTargetAction = DnDConstants.ACTION_NONE;
115: lastStatus = DEFAULT;
116: setCursor(dragCursor);
117: }
118:
119: public DragGestureEvent getTrigger() {
120: return trigger;
121: }
122:
123: public Transferable getTransferable() {
124: return transferable;
125: }
126:
127: public DragSource getDragSource() {
128: return dragSource;
129: }
130:
131: public int getSourceActions() {
132: return sourceAction;
133: }
134:
135: public Component getComponent() {
136: return component;
137: }
138:
139: public Cursor getCursor() {
140: return cursor;
141: }
142:
143: public synchronized void setCursor(Cursor c) {
144: cursor = c;
145:
146: defaultCursor = (cursor == null);
147: if (defaultCursor) {
148: updateCurrentCursor(sourceAction, lastTargetAction,
149: lastStatus);
150: } else {
151: peer.setCursor(cursor);
152: }
153: }
154:
155: public synchronized void addDragSourceListener(
156: DragSourceListener dsl) throws TooManyListenersException {
157: if (dsl == null) {
158: return;
159: }
160: if (dsl == this ) {
161: // awt.181=Attempt to register context as its listener.
162: throw new IllegalArgumentException(Messages
163: .getString("awt.181")); //$NON-NLS-1$
164: }
165: if (listener != null) {
166: // awt.173=One listener is already exist.
167: throw new TooManyListenersException(Messages
168: .getString("awt.173")); //$NON-NLS-1$
169: }
170:
171: listener = dsl;
172: }
173:
174: public synchronized void removeDragSourceListener(
175: DragSourceListener dsl) {
176: if (listener != dsl) {
177: // awt.182=dsl is not current listener.
178: throw new IllegalArgumentException(Messages
179: .getString("awt.182")); //$NON-NLS-1$
180: }
181:
182: listener = null;
183: }
184:
185: protected synchronized void updateCurrentCursor(int dropOp,
186: int targetAct, int status) {
187: if (!defaultCursor) {
188: return;
189: }
190: if ((status < DEFAULT) || (status > CHANGED)) {
191: // awt.183=Invalid status.
192: throw new RuntimeException(Messages.getString("awt.183")); //$NON-NLS-1$
193: }
194:
195: int possibleOps = dropOp
196: & ((status == DEFAULT) ? DnDConstants.ACTION_NONE
197: : targetAct);
198: int theOperation;
199: boolean opEnabled;
200:
201: if (possibleOps == DnDConstants.ACTION_NONE) {
202: theOperation = findBestAction(dropOp);
203: opEnabled = false;
204: } else {
205: theOperation = findBestAction(possibleOps);
206: opEnabled = true;
207: }
208:
209: peer.setCursor(findCursor(theOperation, opEnabled));
210: }
211:
212: private void updateCursor(int dropOp, int targetAct, int status) {
213: lastTargetAction = targetAct;
214: lastStatus = status;
215:
216: updateCurrentCursor(dropOp, targetAct, status);
217: }
218:
219: private int findBestAction(int actions) {
220: if ((actions & DnDConstants.ACTION_MOVE) != 0) {
221: return DnDConstants.ACTION_MOVE;
222: } else if ((actions & DnDConstants.ACTION_COPY) != 0) {
223: return DnDConstants.ACTION_COPY;
224: } else if ((actions & DnDConstants.ACTION_LINK) != 0) {
225: return DnDConstants.ACTION_LINK;
226: } else {
227: return DnDConstants.ACTION_MOVE;
228: }
229: }
230:
231: private Cursor findCursor(int action, boolean enabled) {
232: switch (action) {
233: case DnDConstants.ACTION_MOVE:
234: return (enabled ? DragSource.DefaultMoveDrop
235: : DragSource.DefaultMoveNoDrop);
236: case DnDConstants.ACTION_COPY:
237: return (enabled ? DragSource.DefaultCopyDrop
238: : DragSource.DefaultCopyNoDrop);
239: case DnDConstants.ACTION_LINK:
240: return (enabled ? DragSource.DefaultLinkDrop
241: : DragSource.DefaultLinkNoDrop);
242: default:
243: // awt.184=Invalid action.
244: throw new RuntimeException(Messages.getString("awt.184")); //$NON-NLS-1$
245: }
246: }
247:
248: public void transferablesFlavorsChanged() {
249: peer.transferablesFlavorsChanged();
250: }
251:
252: public void dragEnter(DragSourceDragEvent dsde) {
253: if (listener != null) {
254: listener.dragEnter(dsde);
255: }
256: updateCursor(sourceAction, dsde.getTargetActions(), ENTER);
257: }
258:
259: public void dragOver(DragSourceDragEvent dsde) {
260: if (listener != null) {
261: listener.dragOver(dsde);
262: }
263: updateCursor(sourceAction, dsde.getTargetActions(), OVER);
264: }
265:
266: public void dropActionChanged(DragSourceDragEvent dsde) {
267: sourceAction = dsde.getDropAction();
268: if (listener != null) {
269: listener.dropActionChanged(dsde);
270: }
271: updateCursor(sourceAction, dsde.getTargetActions(), CHANGED);
272: }
273:
274: public void dragExit(DragSourceEvent dse) {
275: if (listener != null) {
276: listener.dragExit(dse);
277: }
278: updateCursor(sourceAction, DnDConstants.ACTION_NONE, EXIT);
279: }
280:
281: public void dragDropEnd(DragSourceDropEvent dsde) {
282: if (listener != null) {
283: listener.dragDropEnd(dsde);
284: }
285: }
286:
287: public void dragMouseMoved(DragSourceDragEvent dsde) {
288: DragSourceMotionListener[] listeners = dragSource
289: .getDragSourceMotionListeners();
290:
291: for (DragSourceMotionListener element : listeners) {
292: element.dragMouseMoved(dsde);
293: }
294: }
295:
296: }
|