001 /* DragGestureRecognizer.java --
002 Copyright (C) 2002, 2005, 2006 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.awt.dnd;
040
041 import java.awt.Component;
042 import java.awt.Point;
043 import java.awt.event.InputEvent;
044 import java.io.IOException;
045 import java.io.ObjectInputStream;
046 import java.io.ObjectOutputStream;
047 import java.io.Serializable;
048 import java.util.ArrayList;
049 import java.util.TooManyListenersException;
050
051 /**
052 * STUBBED
053 * @author Michael Koch (konqueror@gmx.de)
054 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
055 * @since 1.2
056 */
057 public abstract class DragGestureRecognizer implements Serializable
058 {
059 /**
060 * Compatible with JDK 1.2+.
061 */
062 private static final long serialVersionUID = 8996673345831063337L;
063
064 protected DragSource dragSource;
065 protected Component component;
066 protected transient DragGestureListener dragGestureListener;
067 protected int sourceActions;
068 protected ArrayList<InputEvent> events = new ArrayList<InputEvent>();
069
070 protected DragGestureRecognizer(DragSource ds, Component c, int sa,
071 DragGestureListener dgl)
072 {
073 if (ds == null)
074 throw new IllegalArgumentException();
075 dragSource = ds;
076 component = c;
077 sourceActions = sa;
078 dragGestureListener = dgl;
079 }
080
081 protected DragGestureRecognizer(DragSource ds, Component c, int sa)
082 {
083 this(ds, c, sa, null);
084 }
085
086 protected DragGestureRecognizer(DragSource ds, Component c)
087 {
088 this(ds, c, 0, null);
089 }
090
091 protected DragGestureRecognizer(DragSource ds)
092 {
093 this(ds, null, 0, null);
094 }
095
096 protected abstract void registerListeners();
097
098 protected abstract void unregisterListeners();
099
100 public DragSource getDragSource()
101 {
102 return dragSource;
103 }
104
105 public Component getComponent()
106 {
107 return component;
108 }
109
110 public void setComponent(Component c)
111 {
112 component = c;
113 }
114
115 public int getSourceActions()
116 {
117 return sourceActions;
118 }
119
120 public void setSourceActions(int sa)
121 {
122 sourceActions = sa;
123 }
124
125 public InputEvent getTriggerEvent()
126 {
127 return events.size() > 0 ? events.get(0) : null;
128 }
129
130 /**
131 * Resets the recognizer. If a gesture is currently recognize, discard it.
132 */
133 public void resetRecognizer()
134 {
135 events.clear();
136 }
137
138 /**
139 * Register a new DragGestureListener.
140 *
141 * @exception TooManyListenersException If a DragGestureListener has already
142 * been added.
143 */
144 public void addDragGestureListener(DragGestureListener dgl)
145 throws TooManyListenersException
146 {
147 if (dragGestureListener != null)
148 throw new TooManyListenersException();
149 dragGestureListener = dgl;
150 }
151
152 public void removeDragGestureListener(DragGestureListener dgl)
153 {
154 if (dragGestureListener != dgl)
155 throw new IllegalArgumentException();
156 dragGestureListener = null;
157 }
158
159 /**
160 * Fires a <code>DragGestureEvent</code> to the DragGestureListener
161 * associated with this object, if there is one.
162 */
163 protected void fireDragGestureRecognized(int dragAction, Point p)
164 {
165 if(dragGestureListener != null)
166 dragGestureListener.dragGestureRecognized
167 (new DragGestureEvent(this, dragAction, p, events));
168 resetRecognizer();
169 }
170
171 protected void appendEvent(InputEvent e)
172 {
173 if (e == null)
174 return;
175 events.add(e);
176 }
177
178 private void readObject(ObjectInputStream s)
179 throws ClassNotFoundException, IOException
180 {
181 s.defaultReadObject();
182 dragGestureListener = (DragGestureListener) s.readObject();
183 }
184
185 private void writeObject(ObjectOutputStream s) throws IOException
186 {
187 s.defaultWriteObject();
188 s.writeObject(dragGestureListener instanceof Serializable
189 ? dragGestureListener : null);
190 }
191 } // class DragGestureRecognizer