Source for java.awt.dnd.DropTarget

   1: /* DropTarget.java -- 
   2:    Copyright (C) 2002, 2003, 2004  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.GraphicsEnvironment;
  45: import java.awt.HeadlessException;
  46: import java.awt.Point;
  47: import java.awt.datatransfer.FlavorMap;
  48: import java.awt.dnd.peer.DropTargetPeer;
  49: import java.awt.event.ActionEvent;
  50: import java.awt.event.ActionListener;
  51: import java.awt.peer.ComponentPeer;
  52: import java.awt.peer.LightweightPeer;
  53: import java.io.Serializable;
  54: import java.util.EventListener;
  55: import java.util.TooManyListenersException;
  56: 
  57: /**
  58:  * @author Michael Koch
  59:  * @since 1.2
  60:  */
  61: public class DropTarget
  62:   implements DropTargetListener, EventListener, Serializable
  63: {
  64:   /**
  65:    * Compatible with JDK 1.2+
  66:    */
  67:   private static final long serialVersionUID = -6283860791671019047L;
  68: 
  69:   protected static class DropTargetAutoScroller
  70:     implements ActionListener
  71:   {
  72:     private Component component;
  73:     private Point point;
  74:     
  75:     protected DropTargetAutoScroller (Component c, Point p)
  76:     {
  77:       component = c;
  78:       point = p;
  79:     }
  80: 
  81:     protected void updateLocation (Point newLocn)
  82:     {
  83:       point = newLocn;
  84:     }
  85: 
  86:     protected void stop ()
  87:       throws NotImplementedException
  88:     {
  89:       // FIXME: implement this
  90:     }
  91: 
  92:     public void actionPerformed (ActionEvent e)
  93:       throws NotImplementedException
  94:     {
  95:       // FIXME: implement this
  96:     }
  97:   }
  98: 
  99:   private Component component;
 100:   private FlavorMap flavorMap;
 101:   private int actions;
 102:   private DropTargetPeer peer;
 103:   private DropTargetContext dropTargetContext;
 104:   private DropTargetListener dropTargetListener;
 105:   private DropTarget.DropTargetAutoScroller autoscroller;
 106:   private boolean active = true;
 107:     
 108:   /**
 109:    * Creates a <code>DropTarget</code> object.
 110:    *
 111:    * @exception HeadlessException If GraphicsEnvironment.isHeadless()
 112:    * returns true.
 113:    */
 114:   public DropTarget ()
 115:   {
 116:     this (null, 0, null, true, null);
 117:   }
 118:   
 119:   /**
 120:    * Creates a <code>DropTarget</code> object.
 121:    *
 122:    * @exception HeadlessException If GraphicsEnvironment.isHeadless()
 123:    * returns true.
 124:    */
 125:   public DropTarget (Component c, DropTargetListener dtl)
 126:   {
 127:     this (c, 0, dtl, true, null);
 128:   }
 129:   
 130:   /**
 131:    * Creates a <code>DropTarget</code> object.
 132:    *
 133:    * @exception HeadlessException If GraphicsEnvironment.isHeadless()
 134:    * returns true.
 135:    */
 136:   public DropTarget (Component c, int i, DropTargetListener dtl)
 137:   {
 138:     this (c, i, dtl, true, null);
 139:   }
 140:   
 141:   /**
 142:    * Creates a <code>DropTarget</code> object.
 143:    *
 144:    * @exception HeadlessException If GraphicsEnvironment.isHeadless()
 145:    * returns true.
 146:    */
 147:   public DropTarget (Component c, int i, DropTargetListener dtl, boolean b)
 148:   {
 149:     this (c, i, dtl, b, null);
 150:   }
 151:   
 152:   /**
 153:    * Creates a <code>DropTarget</code> object.
 154:    *
 155:    * @exception HeadlessException If GraphicsEnvironment.isHeadless()
 156:    * returns true.
 157:    */
 158:   public DropTarget (Component c, int i, DropTargetListener dtl, boolean b,
 159:                      FlavorMap fm)
 160:   {
 161:     if (GraphicsEnvironment.isHeadless ())
 162:       throw new HeadlessException ();
 163: 
 164:     setComponent(c);
 165:     setDefaultActions(i);
 166:     dropTargetListener = dtl;
 167:     flavorMap = fm;
 168:     
 169:     setActive (b);
 170:     
 171:     if (c != null)
 172:       c.setDropTarget(this);
 173:   }
 174: 
 175:   /**
 176:    * Sets the component associated with this drop target object.
 177:    */
 178:   public void setComponent (Component c)
 179:   {
 180:     component = c;
 181:   }
 182: 
 183:   /**
 184:    * Returns the component associated with this drop target object.
 185:    */
 186:   public Component getComponent ()
 187:   {
 188:     return component;
 189:   }
 190: 
 191:   /**
 192:    * Sets the default actions.
 193:    */
 194:   public void setDefaultActions (int ops)
 195:   {
 196:     actions = ops;
 197:   }
 198: 
 199:   /**
 200:    * Returns the default actions.
 201:    */
 202:   public int getDefaultActions ()
 203:   {
 204:     return actions;
 205:   }
 206: 
 207:   public void setActive (boolean active)
 208:   {
 209:     this.active = active;
 210:   }
 211: 
 212:   public boolean isActive()
 213:   {
 214:     return active;
 215:   }
 216: 
 217:   /**
 218:    * Adds a new <code>DropTargetListener</code>.
 219:    * 
 220:    * @exception TooManyListenersException Sun's JDK does not, despite
 221:    * documentation, throw this exception here when you install an additional
 222:    * <code>DropTargetListener</code>.  So to be compatible, we do the same
 223:    * thing.
 224:    */
 225:   public void addDropTargetListener (DropTargetListener dtl)
 226:     throws TooManyListenersException
 227:   {
 228:     if (dropTargetListener != null)
 229:       throw new TooManyListenersException ();
 230:     
 231:     dropTargetListener = dtl;
 232:   }
 233: 
 234:   public void removeDropTargetListener(DropTargetListener dtl)
 235:   {
 236:     if (dropTargetListener != null)
 237:       dropTargetListener = null;
 238:   }
 239: 
 240:   public void dragEnter(DropTargetDragEvent dtde)
 241:   {
 242:     if (dropTargetListener != null)
 243:       dropTargetListener.dragEnter(dtde);
 244:   }
 245: 
 246:   public void dragOver(DropTargetDragEvent dtde)
 247:   {
 248:     if (dropTargetListener != null)
 249:       dropTargetListener.dragOver(dtde);
 250:   }
 251: 
 252:   public void dropActionChanged(DropTargetDragEvent dtde)
 253:   {
 254:     if (dropTargetListener != null)
 255:       dropTargetListener.dropActionChanged(dtde);
 256:   }
 257: 
 258:   public void dragExit(DropTargetEvent dte)
 259:   {
 260:     if (dropTargetListener != null)
 261:       dropTargetListener.dragExit(dte);
 262:   }
 263: 
 264:   public void drop(DropTargetDropEvent dtde)
 265:   {
 266:     if (dropTargetListener != null)
 267:       dropTargetListener.drop(dtde);
 268:   }
 269: 
 270:   public FlavorMap getFlavorMap()
 271:   {
 272:     return flavorMap;
 273:   }
 274: 
 275:   public void setFlavorMap(FlavorMap fm)
 276:   {
 277:     flavorMap = fm;
 278:   }
 279: 
 280:   public void addNotify(ComponentPeer p)
 281:   {
 282:     Component c = component;
 283:     while (c != null && p instanceof LightweightPeer)
 284:       {
 285:         p = c.getPeer();
 286:         c = c.getParent();
 287:       }
 288: 
 289:     if (p instanceof DropTargetPeer)
 290:       {
 291:         peer = ((DropTargetPeer) p);
 292:         peer.addDropTarget(this);
 293:       }
 294:     else
 295:       peer = null;
 296:   }
 297: 
 298:   public void removeNotify(ComponentPeer p)
 299:   {
 300:     ((DropTargetPeer) peer).removeDropTarget(this);
 301:     peer = null;
 302:     p = null;
 303:   }
 304: 
 305:   public DropTargetContext getDropTargetContext()
 306:   {
 307:     if (dropTargetContext == null)
 308:       dropTargetContext = createDropTargetContext ();
 309:     
 310:     return dropTargetContext;
 311:   }
 312: 
 313:   protected DropTargetContext createDropTargetContext()
 314:   {
 315:     if (dropTargetContext == null)
 316:       dropTargetContext = new DropTargetContext (this);
 317:     
 318:     return dropTargetContext;
 319:   }
 320: 
 321:   protected DropTarget.DropTargetAutoScroller createDropTargetAutoScroller
 322:                                                        (Component c, Point p)
 323:   {
 324:     if (autoscroller == null)
 325:       autoscroller = new DropTarget.DropTargetAutoScroller (c, p);
 326:     
 327:     return autoscroller;
 328:   }
 329: 
 330:   protected void initializeAutoscrolling(Point p)
 331:   {
 332:     createDropTargetAutoScroller (component, p);
 333:   }
 334: 
 335:   protected void updateAutoscroll(Point dragCursorLocn)
 336:   {
 337:     if (autoscroller != null)
 338:       autoscroller.updateLocation(dragCursorLocn);
 339:   }
 340: 
 341:   protected void clearAutoscroll()
 342:   {
 343:     autoscroller = null;
 344:   }
 345: } // class DropTarget