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 gnu.classpath.NotImplementedException;
  42: 
  43: import java.awt.Component;
  44: import java.awt.Cursor;
  45: import java.awt.GraphicsEnvironment;
  46: import java.awt.HeadlessException;
  47: import java.awt.Image;
  48: import java.awt.Point;
  49: import java.awt.Toolkit;
  50: import java.awt.datatransfer.FlavorMap;
  51: import java.awt.datatransfer.SystemFlavorMap;
  52: import java.awt.datatransfer.Transferable;
  53: import java.awt.dnd.peer.DragSourceContextPeer;
  54: import java.io.Serializable;
  55: import java.util.EventListener;
  56: 
  57: /**
  58:  * @since 1.2
  59:  */
  60: public class DragSource implements Serializable
  61: {
  62:   /**
  63:    * Compatible with JDK 1.2+.
  64:    */
  65:   private static final long serialVersionUID = 6236096958971414066L;
  66: 
  67:   public static final Cursor DefaultCopyDrop = null;
  68:   public static final Cursor DefaultMoveDrop = null;
  69:   public static final Cursor DefaultLinkDrop = null;
  70:   public static final Cursor DefaultCopyNoDrop = null;
  71:   public static final Cursor DefaultMoveNoDrop = null;
  72:   public static final Cursor DefaultLinkNoDrop = null;
  73: 
  74:   private transient FlavorMap flavorMap = SystemFlavorMap.getDefaultFlavorMap ();
  75:   private transient DragSourceListener dragSourceListener;
  76:   private transient DragSourceMotionListener dragSourceMotionListener;
  77:   
  78:   private static DragSource ds;
  79:   private DragSourceContextPeer peer;
  80:   private DragSourceContext context;
  81: 
  82:   /**
  83:    * Initializes the drag source.
  84:    *
  85:    * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  86:    */
  87:   public DragSource()
  88:   {
  89:     if (GraphicsEnvironment.isHeadless())
  90:       {
  91:         ds = null;
  92:         throw new HeadlessException();
  93:       }
  94:   }
  95: 
  96:   /**
  97:    * Gets the default drag source.
  98:    * 
  99:    * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
 100:    */
 101:   public static DragSource getDefaultDragSource()
 102:   {
 103:     if (GraphicsEnvironment.isHeadless())
 104:       {
 105:         ds = null;
 106:         throw new HeadlessException();
 107:       }
 108:     
 109:     if (ds == null)
 110:       ds = new DragSource();
 111:     return ds;
 112:   }
 113: 
 114:   public static boolean isDragImageSupported()
 115:     throws NotImplementedException
 116:   {
 117:     // FIXME: Implement this
 118:     return false;
 119:   }
 120: 
 121:   /**
 122:    * Start a drag, given the DragGestureEvent that initiated the drag.
 123:    *
 124:    * @exception InvalidDnDOperationException If the Drag and Drop system is
 125:    * unable to initiate a drag operation, or if the user attempts to start
 126:    * a drag while an existing drag operation is still executing.
 127:    */
 128:   public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
 129:                         Image dragImage, Point imageOffset,
 130:                         Transferable trans, DragSourceListener dsl,
 131:                         FlavorMap map)
 132:   {
 133:     // http://www.javaworld.com/javaworld/jw-03-1999/jw-03-dragndrop.html
 134: 
 135:     // This function creates a DragSourceContext object. This object tracks the
 136:     // state of the operation by listening to a native peer. In this situation,
 137:     // the DragSource may be obtained from the event or by an instance variable.
 138:     // This function also creates a new DragSourceContextPeer.
 139: 
 140:     // This function sends the same message to the context, which then forwards
 141:     // it to the peer, passing itself as a parameter. Now, the native system has
 142:     // access to the Transferable through the context.
 143: 
 144:     // FIXME: Add check to determine if dragging.
 145:     
 146:     try
 147:       {
 148:         flavorMap = map;
 149:         
 150:         if (peer == null)
 151:           peer = Toolkit.getDefaultToolkit().createDragSourceContextPeer(trigger);
 152:         
 153:         if (context == null)
 154:           context = createDragSourceContext(peer, trigger,
 155:                                                             dragCursor,
 156:                                                             dragImage,
 157:                                                             imageOffset, trans,
 158:                                                             dsl);
 159: 
 160:         if (peer == null)
 161:           throw new InvalidDnDOperationException();
 162: 
 163:         peer.startDrag(context, dragCursor, dragImage, imageOffset);
 164:       }
 165:     catch (Exception e)
 166:       {
 167:         throw new InvalidDnDOperationException("Drag and Drop system is "
 168:                                 + "unable to initiate a drag operation.");
 169:       }
 170:   }
 171: 
 172:   /**
 173:    * Start a drag, given the DragGestureEvent that initiated the drag.
 174:    *
 175:    * @exception InvalidDnDOperationException If the Drag and Drop system is
 176:    * unable to initiate a drag operation, or if the user attempts to start
 177:    * a drag while an existing drag operation is still executing.
 178:    */
 179:   public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
 180:                         Transferable trans, DragSourceListener dsl,
 181:                         FlavorMap map)
 182:   {
 183:     startDrag(trigger, dragCursor, null, null, trans, dsl, map);
 184:   }
 185: 
 186:   /**
 187:    * Start a drag, given the DragGestureEvent that initiated the drag.
 188:    *
 189:    * @exception InvalidDnDOperationException If the Drag and Drop system is
 190:    * unable to initiate a drag operation, or if the user attempts to start
 191:    * a drag while an existing drag operation is still executing.
 192:    */
 193:   public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
 194:                         Image dragImage, Point imageOffset,
 195:                         Transferable trans, DragSourceListener dsl)
 196:   {
 197:     startDrag(trigger, dragCursor, dragImage, imageOffset, trans, dsl, null);
 198:   }
 199: 
 200:   /**
 201:    * Start a drag, given the DragGestureEvent that initiated the drag.
 202:    *
 203:    * @exception InvalidDnDOperationException If the Drag and Drop system is
 204:    * unable to initiate a drag operation, or if the user attempts to start
 205:    * a drag while an existing drag operation is still executing.
 206:    */
 207:   public void startDrag(DragGestureEvent trigger, Cursor dragCursor,
 208:                         Transferable trans, DragSourceListener dsl)
 209:   {
 210:     startDrag(trigger, dragCursor, null, null, trans, dsl, null);
 211:   }
 212: 
 213:   /**
 214:    * Creates the DragSourceContext to handle this drag.
 215:    *
 216:    * @exception IllegalArgumentException 
 217:    * @exception NullPointerException If dscp, dgl, dragImage or t is null.
 218:    */
 219:   protected DragSourceContext
 220:     createDragSourceContext(DragSourceContextPeer peer, DragGestureEvent dge,
 221:                             Cursor cursor, Image image, Point offset,
 222:                             Transferable t, DragSourceListener dsl)
 223:   {
 224:     return new DragSourceContext(peer, dge, cursor, image, offset, t, dsl);
 225:   }
 226: 
 227:   public FlavorMap getFlavorMap()
 228:   {
 229:     return flavorMap;
 230:   }
 231: 
 232:   public DragGestureRecognizer createDragGestureRecognizer(Class recognizer,
 233:                                                            Component c,
 234:                                                            int actions,
 235:                                                            DragGestureListener dgl)
 236:   {
 237:     return Toolkit.getDefaultToolkit().createDragGestureRecognizer(recognizer,
 238:                                                                    this, c,
 239:                                                                    actions, dgl);
 240:   }
 241: 
 242:   public DragGestureRecognizer createDefaultDragGestureRecognizer(Component c,
 243:                                                                   int actions,
 244:                                                                   DragGestureListener dgl)
 245:   {
 246:     return createDragGestureRecognizer(MouseDragGestureRecognizer.class, c,
 247:                                        actions, dgl);
 248:   }
 249: 
 250:   /**
 251:    * @since 1.4
 252:    */
 253:   public void addDragSourceListener(DragSourceListener l)
 254:   {
 255:     DnDEventMulticaster.add (dragSourceListener, l);
 256:   }
 257: 
 258:   /**
 259:    * @since 1.4
 260:    */
 261:   public void removeDragSourceListener(DragSourceListener l)
 262:   {
 263:     DnDEventMulticaster.remove (dragSourceListener, l);
 264:   }
 265: 
 266:   /**
 267:    * @since 1.4
 268:    */
 269:   public DragSourceListener[] getDragSourceListeners()
 270:   {
 271:     return (DragSourceListener[]) getListeners (DragSourceListener.class);
 272:   }
 273: 
 274:   /**
 275:    * @since 1.4
 276:    */
 277:   public void addDragSourceMotionListener(DragSourceMotionListener l)
 278:   {
 279:     DnDEventMulticaster.add (dragSourceMotionListener, l);
 280:   }
 281: 
 282:   /**
 283:    * @since 1.4
 284:    */
 285:   public void removeDragSourceMotionListener(DragSourceMotionListener l)
 286:   {
 287:     DnDEventMulticaster.remove (dragSourceMotionListener, l);
 288:   }
 289: 
 290:   /**
 291:    * @since 1.4
 292:    */
 293:   public DragSourceMotionListener[] getDragSourceMotionListeners ()
 294:   {
 295:     return (DragSourceMotionListener[]) getListeners
 296:                                          (DragSourceMotionListener.class);
 297:   }
 298: 
 299:   /**
 300:    * @since 1.4
 301:    */
 302:   public EventListener[] getListeners (Class listenerType)
 303:   {
 304:     if (listenerType == DragSourceListener.class)
 305:       return DnDEventMulticaster.getListeners (dragSourceListener,
 306:                                                listenerType);
 307: 
 308:     if (listenerType == DragSourceMotionListener.class)
 309:       return DnDEventMulticaster.getListeners (dragSourceMotionListener,
 310:                                                listenerType);
 311: 
 312:     // Return an empty EventListener array.
 313:     return new EventListener [0];
 314:   }
 315:   
 316:   /**
 317:    * TODO
 318:    * @return
 319:    * 
 320:    * @since 1.5
 321:    */
 322:   public static int getDragThreshold()
 323:     throws NotImplementedException
 324:   {
 325:     // FIXME: Not implemented.
 326:     return 4;
 327:   }
 328: } // class DragSource