Source for gnu.java.awt.dnd.GtkMouseDragGestureRecognizer

   1: /* GtkMouseDragGestureRecognizer.java --
   2:    Copyright (C) 2006 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 gnu.java.awt.dnd;
  40: 
  41: import java.awt.Component;
  42: import java.awt.Point;
  43: import java.awt.dnd.DnDConstants;
  44: import java.awt.dnd.DragGestureListener;
  45: import java.awt.dnd.DragSource;
  46: import java.awt.dnd.MouseDragGestureRecognizer;
  47: import java.awt.event.MouseEvent;
  48: 
  49: public class GtkMouseDragGestureRecognizer
  50:     extends MouseDragGestureRecognizer
  51: {
  52: 
  53:   public GtkMouseDragGestureRecognizer (DragSource ds)
  54:   {
  55:     this(ds, null, 0, null);
  56:   }
  57: 
  58:   public GtkMouseDragGestureRecognizer (DragSource ds, Component c)
  59:   {
  60:     this (ds, c, 0, null);
  61:   }
  62: 
  63:   public GtkMouseDragGestureRecognizer (DragSource ds, Component c, int act)
  64:   {
  65:     this(ds, c, act, null);
  66:   }
  67: 
  68:   public GtkMouseDragGestureRecognizer (DragSource ds, Component c, int act,
  69:                                         DragGestureListener dgl)
  70:   {
  71:     super(ds, c, act, dgl);
  72:   }
  73: 
  74:   public void registerListeners ()
  75:   {
  76:     super.registerListeners();
  77:   }
  78: 
  79:   public void unregisterListeners ()
  80:   {
  81:     super.unregisterListeners();
  82:   }
  83: 
  84:   public void mouseClicked (MouseEvent e)
  85:   {
  86:     // Nothing to do here.
  87:   }
  88: 
  89:   public void mousePressed (MouseEvent e)
  90:   {
  91:     events.clear();
  92:     if (getDropActionFromEvent(e) != DnDConstants.ACTION_NONE)
  93:       appendEvent(e);
  94:   }
  95: 
  96:   public void mouseReleased (MouseEvent e)
  97:   {
  98:     events.clear();
  99:   }
 100: 
 101:   public void mouseEntered (MouseEvent e)
 102:   {
 103:     events.clear();
 104:   }
 105: 
 106:   public void mouseExited(MouseEvent e)
 107:   {
 108:     if (!events.isEmpty())
 109:       if (getDropActionFromEvent(e) == DnDConstants.ACTION_NONE)
 110:         events.clear();
 111:   }
 112: 
 113:   public void mouseDragged(MouseEvent e)
 114:   {
 115:     if (!events.isEmpty())
 116:       {
 117:         int act = getDropActionFromEvent(e);
 118: 
 119:         if (act == DnDConstants.ACTION_NONE)
 120:           return;
 121: 
 122:         Point origin = ((MouseEvent) events.get(0)).getPoint();
 123:         Point current = e.getPoint();
 124:         int dx = Math.abs(origin.x - current.x);
 125:         int dy = Math.abs(origin.y - current.y);
 126:         int threshold = DragSource.getDragThreshold();
 127: 
 128:         if (dx > threshold || dy > threshold)
 129:           fireDragGestureRecognized(act, origin);
 130:         else
 131:           appendEvent(e);
 132:       }
 133:   }
 134: 
 135:   public void mouseMoved (MouseEvent e)
 136:   {
 137:     // Nothing to do here.
 138:   }
 139: 
 140:   private int getDropActionFromEvent(MouseEvent e)
 141:   {
 142:     int modEx = e.getModifiersEx();
 143:     int buttons =  modEx & (MouseEvent.BUTTON1_DOWN_MASK
 144:                | MouseEvent.BUTTON2_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK);
 145:     if (!(buttons == MouseEvent.BUTTON1_DOWN_MASK ||
 146:         buttons == MouseEvent.BUTTON2_DOWN_MASK))
 147:       return DnDConstants.ACTION_NONE;
 148: 
 149:     // Convert modifier to a drop action
 150:     int sourceActions = getSourceActions();
 151:     int mod = modEx
 152:               & (MouseEvent.SHIFT_DOWN_MASK | MouseEvent.CTRL_DOWN_MASK);
 153:     switch (mod)
 154:       {
 155:       case MouseEvent.SHIFT_DOWN_MASK | MouseEvent.CTRL_DOWN_MASK:
 156:         return DnDConstants.ACTION_LINK & sourceActions;
 157:       case MouseEvent.CTRL_DOWN_MASK:
 158:         return DnDConstants.ACTION_COPY & sourceActions;
 159:       case MouseEvent.SHIFT_DOWN_MASK:
 160:         return DnDConstants.ACTION_MOVE & sourceActions;
 161:       default:
 162:         if ((sourceActions & DnDConstants.ACTION_MOVE) != 0)
 163:           return DnDConstants.ACTION_MOVE & sourceActions;
 164:         else if ((sourceActions & DnDConstants.ACTION_COPY) != 0)
 165:           return DnDConstants.ACTION_COPY & sourceActions;
 166:         else if ((sourceActions & DnDConstants.ACTION_LINK) != 0)
 167:           return DnDConstants.ACTION_LINK & sourceActions;
 168:       }
 169: 
 170:     return DnDConstants.ACTION_NONE & sourceActions;
 171:   }
 172: }