Source for java.awt.dnd.DragSourceContext

   1: /* DragSourceContext.java --
   2:    Copyright (C) 2002 Free Software Foundation
   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.Image;
  46: import java.awt.Point;
  47: import java.awt.datatransfer.Transferable;
  48: import java.awt.dnd.peer.DragSourceContextPeer;
  49: import java.io.Serializable;
  50: import java.util.TooManyListenersException;
  51: 
  52: /**
  53:  * @since 1.2
  54:  */
  55: public class DragSourceContext
  56:   implements DragSourceListener, DragSourceMotionListener, Serializable
  57: {
  58:   /**
  59:    * Compatible with JDK 1.2+
  60:    */
  61:   static final long serialVersionUID = -115407898692194719L;
  62: 
  63:   protected static final int DEFAULT = 0;
  64:   protected static final int ENTER = 1;
  65:   protected static final int OVER = 2;
  66:   protected static final int CHANGED = 3;
  67: 
  68:   private DragSourceContextPeer peer;
  69:   private Cursor cursor;
  70:   private Transferable transferable;
  71:   private DragGestureEvent trigger;
  72:   private DragSourceListener dragSourceListener;
  73:   private boolean useCustomCursor;
  74:   private int sourceActions;
  75:   private Image image;
  76:   private Point offset;
  77:   
  78:   /**
  79:    * Initializes a drag source context.
  80:    *
  81:    * @exception IllegalArgumentException If Component or DragSource of trigger
  82:    * are null, the drag action for the trigger event is DnDConstants.ACTION_NONE
  83:    * or if the source actions for the DragGestureRecognizer associated with the
  84:    * trigger event are equal to DnDConstants.ACTION_NONE.
  85:    * @exception NullPointerException If peer, trans or trigger is null or if the
  86:    * image is not null but the offset is. 
  87:    */
  88:   public DragSourceContext (DragSourceContextPeer peer,
  89:                             DragGestureEvent trigger, Cursor cursor,
  90:                             Image image, Point offset, Transferable trans,
  91:                             DragSourceListener dsl)
  92:   {    
  93:     if (peer == null
  94:         || trigger == null || trans == null
  95:         || (image != null && offset == null))
  96:       throw new NullPointerException ();
  97: 
  98:     if (trigger.getComponent () == null
  99:         || trigger.getDragSource () == null
 100:         || trigger.getDragAction () == DnDConstants.ACTION_NONE
 101:         || trigger.getSourceAsDragGestureRecognizer ()
 102:               .getSourceActions () == DnDConstants.ACTION_NONE)
 103:       throw new IllegalArgumentException ();
 104: 
 105:     this.peer = peer;
 106:     this.trigger = trigger;
 107:     this.cursor = cursor;
 108:     this.image = image;
 109:     this.offset = offset;
 110:     this.transferable = trans;
 111:     this.dragSourceListener = dsl;
 112:     this.sourceActions = trigger.getSourceAsDragGestureRecognizer().getSourceActions();
 113:     
 114:     setCursor(cursor);
 115:     updateCurrentCursor(trigger.getDragAction(), sourceActions, DEFAULT);
 116:   }
 117: 
 118:   /**
 119:    * Returns the DragSource object associated with the
 120:    * DragGestureEvent.
 121:    * 
 122:    * @return the DragSource associated with the trigger.
 123:    */
 124:   public DragSource getDragSource()
 125:   {
 126:     return trigger.getDragSource ();
 127:   }
 128: 
 129:   /**
 130:    * Returns the component associated with this.
 131:    * 
 132:    * @return the component associated with the trigger.
 133:    */
 134:   public Component getComponent()
 135:   {
 136:     return trigger.getComponent ();
 137:   }
 138: 
 139:   /**
 140:    * Gets the trigger associated with this.
 141:    * 
 142:    * @return the trigger.
 143:    */
 144:   public DragGestureEvent getTrigger()
 145:   {
 146:     return trigger;
 147:   }
 148: 
 149:   /**
 150:    * Returns the source actions for the DragGestureRecognizer.
 151:    * 
 152:    * @return the source actions for DragGestureRecognizer.
 153:    */
 154:   public int getSourceActions()
 155:   {
 156:     if (sourceActions == 0)
 157:       sourceActions = trigger.getSourceAsDragGestureRecognizer().getSourceActions();
 158:     return sourceActions;
 159:   }
 160: 
 161:   /**
 162:    * Sets the cursor for this drag operation to the specified cursor.
 163:    * 
 164:    * @param cursor c - the Cursor to use, or null to use the default drag
 165:    *          cursor.
 166:    */
 167:   public void setCursor(Cursor cursor)
 168:   {
 169:     if (cursor == null)
 170:       useCustomCursor = false;
 171:     else
 172:       useCustomCursor = true;
 173:     this.cursor = cursor;
 174:     peer.setCursor(cursor);
 175:   }
 176: 
 177:   /**
 178:    * Returns the current cursor or null if the default
 179:    * drag cursor is used.
 180:    * 
 181:    * @return the current cursor or null.
 182:    */
 183:   public Cursor getCursor()
 184:   {
 185:     return cursor;
 186:   }
 187: 
 188:   /**
 189:    * Adds a <code>DragSourceListener</code>.
 190:    *
 191:    * @exception TooManyListenersException If a <code>DragSourceListener</code>
 192:    * has already been added.
 193:    */
 194:   public void addDragSourceListener (DragSourceListener dsl)
 195:     throws TooManyListenersException
 196:   {
 197:     if (dragSourceListener != null)
 198:       throw new TooManyListenersException ();
 199: 
 200:     dragSourceListener = dsl;
 201:   }
 202: 
 203:   public void removeDragSourceListener (DragSourceListener dsl)
 204:   {
 205:     if (dragSourceListener == dsl)
 206:       dragSourceListener = null;
 207:   }
 208: 
 209:   /**
 210:    * This function tells the peer that the DataFlavors have been modified.
 211:    */
 212:   public void transferablesFlavorsChanged()
 213:   {
 214:     peer.transferablesFlavorsChanged();
 215:   }
 216: 
 217:   /**
 218:    * Calls dragEnter on the listeners registered with this
 219:    * and with the DragSource.
 220:    * 
 221:    * @param e - the DragSourceDragEvent
 222:    */
 223:   public void dragEnter(DragSourceDragEvent e)
 224:   {
 225:     if (dragSourceListener != null)
 226:       dragSourceListener.dragEnter(e);
 227:     
 228:     DragSource ds = getDragSource();
 229:     DragSourceListener[] dsl = ds.getDragSourceListeners();
 230:     for (int i = 0; i < dsl.length; i++)
 231:       dsl[i].dragEnter(e);
 232:     
 233:     updateCurrentCursor(e.getDropAction(), e.getTargetActions(), ENTER);
 234:   }
 235: 
 236:   /**
 237:    * Calls dragOver on the listeners registered with this
 238:    * and with the DragSource.
 239:    * 
 240:    * @param e - the DragSourceDragEvent
 241:    */
 242:   public void dragOver(DragSourceDragEvent e)
 243:   {
 244:     if (dragSourceListener != null)
 245:       dragSourceListener.dragOver(e);
 246:     
 247:     DragSource ds = getDragSource();
 248:     DragSourceListener[] dsl = ds.getDragSourceListeners();
 249:     for (int i = 0; i < dsl.length; i++)
 250:       dsl[i].dragOver(e);
 251:     
 252:     updateCurrentCursor(e.getDropAction(), e.getTargetActions(), OVER);
 253:   }
 254:   
 255:   /**
 256:    * Calls dragExit on the listeners registered with this
 257:    * and with the DragSource.
 258:    * 
 259:    * @param e - the DragSourceEvent
 260:    */
 261:   public void dragExit(DragSourceEvent e)
 262:   {
 263:     if (dragSourceListener != null)
 264:       dragSourceListener.dragExit(e);
 265:     
 266:     DragSource ds = getDragSource();
 267:     DragSourceListener[] dsl = ds.getDragSourceListeners();
 268:     for (int i = 0; i < dsl.length; i++)
 269:       dsl[i].dragExit(e);
 270:     
 271:     updateCurrentCursor(0, 0, DEFAULT);
 272:   }
 273: 
 274:   /**
 275:    * Calls dropActionChanged on the listeners registered with this
 276:    * and with the DragSource.
 277:    * 
 278:    * @param e - the DragSourceDragEvent
 279:    */
 280:   public void dropActionChanged(DragSourceDragEvent e)
 281:   {
 282:     if (dragSourceListener != null)
 283:       dragSourceListener.dropActionChanged(e);
 284:     
 285:     DragSource ds = getDragSource();
 286:     DragSourceListener[] dsl = ds.getDragSourceListeners();
 287:     for (int i = 0; i < dsl.length; i++)
 288:       dsl[i].dropActionChanged(e);
 289:     
 290:     updateCurrentCursor(e.getDropAction(), e.getTargetActions(), CHANGED);
 291:   }
 292: 
 293:   /**
 294:    * Calls dragDropEnd on the listeners registered with this
 295:    * and with the DragSource.
 296:    * 
 297:    * @param e - the DragSourceDropEvent
 298:    */
 299:   public void dragDropEnd(DragSourceDropEvent e)
 300:   {
 301:     if (dragSourceListener != null)
 302:       dragSourceListener.dragDropEnd(e);
 303:     
 304:     DragSource ds = getDragSource();
 305:     DragSourceListener[] dsl = ds.getDragSourceListeners();
 306:     for (int i = 0; i < dsl.length; i++)
 307:       dsl[i].dragDropEnd(e);
 308:   }
 309: 
 310:   /**
 311:    * Calls dragMouseMoved on the listeners registered with the DragSource.
 312:    * 
 313:    * @param e - the DragSourceDragEvent
 314:    */
 315:   public void dragMouseMoved(DragSourceDragEvent e)
 316:   {
 317:     DragSource ds = getDragSource();
 318:     DragSourceMotionListener[] dsml = ds.getDragSourceMotionListeners();
 319:     for (int i = 0; i < dsml.length; i++)
 320:       dsml[i].dragMouseMoved(e);
 321:   }
 322: 
 323:   /**
 324:    * Returns the Transferable set with this object.
 325:    * 
 326:    * @return the transferable.
 327:    */
 328:   public Transferable getTransferable()
 329:   {
 330:     return transferable;
 331:   }
 332: 
 333:   /**
 334:    * This function sets the drag cursor for the specified operation, actions and
 335:    * status if the default drag cursor is active. Otherwise, the cursor is not
 336:    * updated in any way.
 337:    * 
 338:    * @param dropOp - the current operation.
 339:    * @param targetAct - the supported actions.
 340:    * @param status - the status of the cursor (constant).
 341:    */
 342:   protected void updateCurrentCursor(int dropOp, int targetAct, int status)
 343:     throws NotImplementedException
 344:   {
 345:     // FIXME: Not implemented fully
 346:     if (!useCustomCursor)
 347:       {
 348:         Cursor cursor = null;
 349:         switch (status)
 350:           {
 351:           case ENTER:
 352:             break;
 353:           case CHANGED:
 354:             break;
 355:           case OVER:
 356:             break;
 357:           default:
 358:             break;
 359:           }
 360:         
 361:         this.cursor = cursor;
 362:         peer.setCursor(cursor);
 363:       }
 364:   }
 365: } // class DragSourceContext