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.Point;
022: import java.awt.event.InputEvent;
023: import java.io.Serializable;
024: import java.util.ArrayList;
025: import java.util.TooManyListenersException;
026:
027: import org.apache.harmony.awt.internal.nls.Messages;
028:
029: public abstract class DragGestureRecognizer implements Serializable {
030:
031: private static final long serialVersionUID = 8996673345831063337L;
032:
033: protected DragSource dragSource;
034:
035: protected int sourceActions;
036:
037: protected Component component;
038:
039: protected ArrayList<InputEvent> events;
040:
041: protected transient DragGestureListener dragGestureListener;
042:
043: protected DragGestureRecognizer(DragSource ds, Component c, int sa,
044: DragGestureListener dgl) {
045: if (ds == null) {
046: // awt.172=Drag source is null.
047: throw new IllegalArgumentException(Messages
048: .getString("awt.172")); //$NON-NLS-1$
049: }
050:
051: dragSource = ds;
052: component = c;
053: sourceActions = sa;
054:
055: try {
056: addDragGestureListener(dgl);
057: } catch (TooManyListenersException e) {
058: }
059:
060: events = null;
061: }
062:
063: protected DragGestureRecognizer(DragSource ds, Component c, int sa) {
064: this (ds, c, sa, null);
065: }
066:
067: protected DragGestureRecognizer(DragSource ds, Component c) {
068: this (ds, c, DnDConstants.ACTION_NONE, null);
069: }
070:
071: protected DragGestureRecognizer(DragSource ds) {
072: this (ds, null, DnDConstants.ACTION_NONE, null);
073: }
074:
075: public DragSource getDragSource() {
076: return dragSource;
077: }
078:
079: public synchronized Component getComponent() {
080: return component;
081: }
082:
083: public synchronized void setComponent(Component c) {
084: unregisterListeners();
085: component = c;
086: if (dragGestureListener != null) {
087: registerListeners();
088: }
089: }
090:
091: public synchronized int getSourceActions() {
092: return sourceActions;
093: }
094:
095: public synchronized void setSourceActions(int actions) {
096: sourceActions = actions;
097: }
098:
099: protected synchronized void appendEvent(InputEvent awtie) {
100: if (awtie == null) {
101: return;
102: }
103:
104: if (events == null) {
105: events = new ArrayList<InputEvent>();
106: }
107:
108: events.add(awtie);
109: }
110:
111: public InputEvent getTriggerEvent() {
112: if ((events == null) || events.isEmpty()) {
113: return null;
114: }
115:
116: return events.get(0);
117: }
118:
119: public void resetRecognizer() {
120: events.clear();
121: }
122:
123: public synchronized void addDragGestureListener(
124: DragGestureListener dgl) throws TooManyListenersException {
125: if (dgl == null) {
126: return;
127: }
128: if (dragGestureListener != null) {
129: // awt.173=One listener is already exist.
130: throw new TooManyListenersException(Messages
131: .getString("awt.173")); //$NON-NLS-1$
132: }
133:
134: dragGestureListener = dgl;
135: registerListeners();
136: }
137:
138: public synchronized void removeDragGestureListener(
139: DragGestureListener dgl) {
140: if (dragGestureListener != dgl) {
141: // awt.174=dgl is not current listener.
142: throw new IllegalArgumentException(Messages
143: .getString("awt.174")); //$NON-NLS-1$
144: }
145: if (dragGestureListener == null) {
146: return;
147: }
148:
149: unregisterListeners();
150: dragGestureListener = null;
151: }
152:
153: protected synchronized void fireDragGestureRecognized(
154: int dragAction, Point p) {
155: if (dragGestureListener != null) {
156: // Supposed that we are on event dispatch thread
157: try {
158: DragGestureEvent event = new DragGestureEvent(this ,
159: dragAction, p, events);
160:
161: dragGestureListener.dragGestureRecognized(event);
162: } catch (Exception e) {
163: e.printStackTrace();
164: }
165: }
166: resetRecognizer();
167: }
168:
169: protected abstract void registerListeners();
170:
171: protected abstract void unregisterListeners();
172:
173: }
|