Source for java.awt.dnd.DragSource

   1: /* DragSource.java --
   2:    Copyright (C) 2002 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.awt.dnd;
  40: 
  41: import java.awt.Component;
  42: import java.awt.Cursor;
  43: import java.awt.GraphicsEnvironment;
  44: import java.awt.HeadlessException;
  45: import java.awt.Image;
  46: import java.awt.Point;
  47: import java.awt.Toolkit;
  48: import java.awt.datatransfer.FlavorMap;
  49: import java.awt.datatransfer.SystemFlavorMap;
  50: import java.awt.datatransfer.Transferable;
  51: import java.awt.dnd.peer.DragSourceContextPeer;
  52: import java.io.Serializable;
  53: import java.util.EventListener;
  54: 
  55: /**
  56:  * @since 1.2
  57:  */
  58: public class DragSource implements Serializable
  59: {
  60:   /**
  61:    * Compatible with JDK 1.2+.
  62:    */
  63:   private static final long serialVersionUID = 6236096958971414066L;
  64: 
  65:   public static final Cursor DefaultCopyDrop = null;
  66:   public static final Cursor DefaultMoveDrop = null;
  67:   public static final Cursor DefaultLinkDrop = null;
  68:   public static final Cursor DefaultCopyNoDrop = null;
  69:   public static final Cursor DefaultMoveNoDrop = null;
  70:   public static final Cursor DefaultLinkNoDrop = null;
  71: 
  72:   private transient FlavorMap flavorMap = SystemFlavorMap.getDefaultFlavorMap ();
  73: 
  74:   private transient DragSourceListener dragSourceListener;
  75:   private transient DragSourceMotionListener dragSourceMotionListener;
  76: 
  77:   /**
  78:    * Initializes the drag source.
  79:    *
  80:    * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  81:    */
  82:   public DragSource()
  83:   {
  84:     if (GraphicsEnvironment.isHeadless())
  85:       throw new HeadlessException ();
  86:   }
  87: 
  88:   /**
  89:    * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  90:    */
  91:   public static DragSource getDefaultDragSource()
  92:   {
  93:     return new DragSource();
  94:   }
  95: 
  96:   public static boolean isDragImageSupported()
  97:   {
  98:     return false;
  99:   }
 100: 
 101:   /**
 102:    * Start a drag, given the DragGestureEvent that initiated the drag.
 103:    *
 104:    * @exception InvalidDnDOperationException If the Drag and Drop system is
 105:    * unable to initiate a drag operation, or if the user attempts to start
 106:    * a drag while an existing drag operation is still executing.
 107:    */
 108:   public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
 109:                         Image dragImage, Point imageOffset,
 110:                         Transferable trans, DragSourceListener dsl,
 111:                         FlavorMap map)
 112:   {
 113:   }
 114: 
 115:   /**
 116:    * Start a drag, given the DragGestureEvent that initiated the drag.
 117:    *
 118:    * @exception InvalidDnDOperationException If the Drag and Drop system is
 119:    * unable to initiate a drag operation, or if the user attempts to start
 120:    * a drag while an existing drag operation is still executing.
 121:    */
 122:   public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
 123:                         Transferable trans, DragSourceListener dsl,
 124:                         FlavorMap map)
 125:   {
 126:     startDrag(trigger, dragCursor, null, null, trans, dsl, map);
 127:   }
 128: 
 129:   /**
 130:    * Start a drag, given the DragGestureEvent that initiated the drag.
 131:    *
 132:    * @exception InvalidDnDOperationException If the Drag and Drop system is
 133:    * unable to initiate a drag operation, or if the user attempts to start
 134:    * a drag while an existing drag operation is still executing.
 135:    */
 136:   public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
 137:                         Image dragImage, Point imageOffset,
 138:                         Transferable trans, DragSourceListener dsl)
 139:   {
 140:     startDrag(trigger, dragCursor, dragImage, imageOffset, trans, dsl, null);
 141:   }
 142: 
 143:   /**
 144:    * Start a drag, given the DragGestureEvent that initiated the drag.
 145:    *
 146:    * @exception InvalidDnDOperationException If the Drag and Drop system is
 147:    * unable to initiate a drag operation, or if the user attempts to start
 148:    * a drag while an existing drag operation is still executing.
 149:    */
 150:   public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
 151:                         Transferable trans, DragSourceListener dsl)
 152:   {
 153:     startDrag(trigger, dragCursor, null, null, trans, dsl, null);
 154:   }
 155: 
 156:   /**
 157:    * Creates the DragSourceContext to handle this drag.
 158:    *
 159:    * @exception IllegalArgumentException FIXME
 160:    * @exception NullPointerException If dscp, dgl, dragImage or t is null.
 161:    */
 162:   protected DragSourceContext
 163:     createDragSourceContext(DragSourceContextPeer peer, DragGestureEvent dge,
 164:                             Cursor cursor, Image image, Point offset,
 165:                             Transferable t, DragSourceListener dsl)
 166:   {
 167:     return null;
 168:   }
 169: 
 170:   public FlavorMap getFlavorMap()
 171:   {
 172:     return flavorMap;
 173:   }
 174: 
 175:   /**
 176:    * Dummy DragGestureRecognizer when Toolkit doesn't support drag and drop.
 177:    */
 178:   static class NoDragGestureRecognizer extends DragGestureRecognizer
 179:   {
 180:     NoDragGestureRecognizer(DragSource ds, Component c, int actions,
 181:                             DragGestureListener dgl)
 182:     {
 183:       super(ds, c, actions, dgl);
 184:     }
 185: 
 186:     protected void registerListeners() { }
 187:     protected void unregisterListeners() { }
 188:   }
 189: 
 190:   public DragGestureRecognizer
 191:     createDragGestureRecognizer(Class recognizer, Component c, int actions,
 192:                                 DragGestureListener dgl)
 193:   {
 194:     DragGestureRecognizer dgr;
 195:     dgr = Toolkit.getDefaultToolkit ()
 196:                   .createDragGestureRecognizer (recognizer, this, c, actions,
 197:                                                 dgl);
 198: 
 199:     if (dgr == null)
 200:       dgr = new NoDragGestureRecognizer(this, c, actions, dgl);
 201: 
 202:     return dgr;
 203:   }
 204: 
 205:   public DragGestureRecognizer
 206:     createDefaultDragGestureRecognizer(Component c, int actions,
 207:                                        DragGestureListener dgl)
 208:   {
 209:     return createDragGestureRecognizer (MouseDragGestureRecognizer.class, c,
 210:                                         actions, dgl);
 211:   }
 212: 
 213:   /**
 214:    * @since 1.4
 215:    */
 216:   public void addDragSourceListener(DragSourceListener l)
 217:   {
 218:     DnDEventMulticaster.add (dragSourceListener, l);
 219:   }
 220: 
 221:   /**
 222:    * @since 1.4
 223:    */
 224:   public void removeDragSourceListener(DragSourceListener l)
 225:   {
 226:     DnDEventMulticaster.remove (dragSourceListener, l);
 227:   }
 228: 
 229:   /**
 230:    * @since 1.4
 231:    */
 232:   public DragSourceListener[] getDragSourceListeners()
 233:   {
 234:     return (DragSourceListener[]) getListeners (DragSourceListener.class);
 235:   }
 236: 
 237:   /**
 238:    * @since 1.4
 239:    */
 240:   public void addDragSourceMotionListener(DragSourceMotionListener l)
 241:   {
 242:     DnDEventMulticaster.add (dragSourceMotionListener, l);
 243:   }
 244: 
 245:   /**
 246:    * @since 1.4
 247:    */
 248:   public void removeDragSourceMotionListener(DragSourceMotionListener l)
 249:   {
 250:     DnDEventMulticaster.remove (dragSourceMotionListener, l);
 251:   }
 252: 
 253:   /**
 254:    * @since 1.4
 255:    */
 256:   public DragSourceMotionListener[] getDragSourceMotionListeners ()
 257:   {
 258:     return (DragSourceMotionListener[]) getListeners
 259:                                          (DragSourceMotionListener.class);
 260:   }
 261: 
 262:   /**
 263:    * @since 1.4
 264:    */
 265:   public EventListener[] getListeners (Class listenerType)
 266:   {
 267:     if (listenerType == DragSourceListener.class)
 268:       return DnDEventMulticaster.getListeners (dragSourceListener,
 269:                                                listenerType);
 270: 
 271:     if (listenerType == DragSourceMotionListener.class)
 272:       return DnDEventMulticaster.getListeners (dragSourceMotionListener,
 273:                                                listenerType);
 274: 
 275:     // Return an empty EventListener array.
 276:     return new EventListener [0];
 277:   }
 278: } // class DragSource