Esempio n. 1
0
    /** This is where the ghost image is drawn */
    public void dragOver(final DropTargetDragEvent e) {
      if ((e == null)
          || (_raGhost == null)
          || (_ptLast == null)
          || (_ptOffset == null)
          || (_imgGhost == null)
          || (_raCueLine == null)) return;
      // Even if the mouse is not moving, this method is still invoked 10 times per second
      final Point pt = e.getLocation();
      if (pt == null) return;
      if (pt.equals(_ptLast)) return;

      // Try to determine whether the user is flicking the cursor right or left
      final int nDeltaLeftRight = pt.x - _ptLast.x;
      if (((_nLeftRight > 0) && (nDeltaLeftRight < 0))
          || ((_nLeftRight < 0) && (nDeltaLeftRight > 0))) _nLeftRight = 0;
      _nLeftRight += nDeltaLeftRight;
      _ptLast = pt;
      final Graphics2D g2 = (Graphics2D) getGraphics();
      if (g2 == null) return;

      // If a drag image is not supported by the platform, then draw my own drag image
      if (!DragSource.isDragImageSupported()) {
        JDragTree.this.paintImmediately(
            _raGhost.getBounds()); // Rub out the last ghost image and cue line
        // And remember where we are about to draw the new ghost image
        _raGhost.setRect(
            pt.x - _ptOffset.x, pt.y - _ptOffset.y, _imgGhost.getWidth(), _imgGhost.getHeight());
        g2.drawImage(
            _imgGhost,
            AffineTransform.getTranslateInstance(_raGhost.getX(), _raGhost.getY()),
            null);
      } else // Just rub out the last cue line
      JDragTree.this.paintImmediately(_raCueLine.getBounds());

      final TreePath path = getClosestPathForLocation(pt.x, pt.y);
      if (!(path == _pathLast)) {
        _nLeftRight = 0; // We've moved up or down, so reset left/right movement trend
        _pathLast = path;
        _timerHover.restart();
      }

      // In any case draw (over the ghost image if necessary) a cue line indicating where a drop
      // will occur
      final Rectangle raPath = getPathBounds(path);
      _raCueLine.setRect(0, raPath.y + (int) raPath.getHeight(), getWidth(), 2);

      g2.setColor(_colorCueLine);
      g2.fill(_raCueLine);

      // And include the cue line in the area to be rubbed out next time
      _raGhost = _raGhost.createUnion(_raCueLine);

      // Do this if you want to prohibit dropping onto the drag source
      if (path.equals(_pathSource)) e.rejectDrag();
      else e.acceptDrag(e.getDropAction());
    }
  public void dragGestureRecognized(DragGestureEvent evt) {
    if (files == null) return;
    if (files.length <= 0) {
      return;
    }
    Icon icn = files[0].getIcon(false);
    Toolkit tk = Toolkit.getDefaultToolkit();
    if (icn == null) icn = new EmptyIcon();
    Dimension dim = tk.getBestCursorSize(icn.getIconWidth(), icn.getIconHeight());

    // set up drag image
    if (DragSource.isDragImageSupported()) {
      BufferedImage buff =
          new BufferedImage(dim.width + 100, dim.height, BufferedImage.TYPE_INT_ARGB);
      Graphics graphics = buff.getGraphics();
      Color color = graphics.getColor();
      icn.paintIcon(null, graphics, 0, 0);
      graphics.setColor(color);
      graphics.setColor(Color.RED);
      graphics.drawString(files.length + " Elements", icn.getIconWidth(), dim.height / 2);
      try {
        evt.startDrag(
            DragSource.DefaultCopyDrop,
            buff,
            new Point(0, 0),
            new FileTransferable(files, table.getTableModel()),
            this);
      } catch (IOException ex) {
        Logger.getLogger(FileDragGestureListener.class.getName()).log(Level.SEVERE, null, ex);
      }
    } else {
      BufferedImage buff = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
      Graphics graphics = buff.getGraphics();
      icn.paintIcon(null, graphics, 0, 0);
      try {
        cursor = tk.createCustomCursor(buff, new Point(0, 0), "tempcursor");
        evt.startDrag(
            cursor,
            null,
            new Point(0, 0),
            new FileTransferable(files, table.getTableModel()),
            this);
      } catch (IOException ex) {
        Logger.getLogger(FileDragGestureListener.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }
Esempio n. 3
0
 public void dragExit(final DropTargetEvent e) {
   if (!DragSource.isDragImageSupported()) {
     JDragTree.this.repaint(_raGhost.getBounds());
   }
 }