public void dragGestureRecognized(DragGestureEvent event) {
    if (folderPanel.getMainFrame().getNoEventsMode()) return;

    FileTable fileTable = folderPanel.getFileTable();
    FileTableModel tableModel = fileTable.getFileTableModel();

    // Return (do not initiate drag) if mouse button2 or button3 was used
    if ((event.getTriggerEvent().getModifiers()
            & (InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK))
        != 0) return;

    // Do not use that to retrieve the current selected file as it is inaccurate: the selection
    // could have changed since the
    // the mouse was clicked.
    //        AbstractFile selectedFile = fileTable.getSelectedFile(false);
    //        // Return if selected file is null (could happen if '..' is selected)
    //        if(selectedFile==null)
    //            return;

    // Find out which row was clicked
    int clickedRow = fileTable.rowAtPoint(event.getDragOrigin());
    // Return (do not initiate drag) if the selected file is the parent folder '..'
    if (clickedRow == -1 || fileTable.isParentFolder(clickedRow)) return;

    // Retrieve the file corresponding to the clicked row
    AbstractFile selectedFile = tableModel.getFileAtRow(clickedRow);

    // Find out which files are to be dragged, based on the selected file and currenlty marked
    // files.
    // If there are some files marked, drag marked files only if the selected file is one of the
    // marked files.
    // In any other case, only drag the selected file.
    FileSet markedFiles;
    FileSet draggedFiles;
    if (tableModel.getNbMarkedFiles() > 0
        && (markedFiles = fileTable.getSelectedFiles()).contains(selectedFile)) {
      draggedFiles = markedFiles;
    } else {
      draggedFiles = new FileSet(fileTable.getCurrentFolder(), selectedFile);
    }

    // Set initial DnDContext information
    DnDContext.setDragInitiatedByMucommander(true);
    DnDContext.setDragInitiator(folderPanel);
    DnDContext.setDragGestureModifiersEx(event.getTriggerEvent().getModifiersEx());

    // Start dragging
    DragSource.getDefaultDragSource()
        .startDrag(event, null, new TransferableFileSet(draggedFiles), this);
    //        DragSource.getDefaultDragSource().startDrag(createCustomDragGestureEvent(event,
    // DnDConstants.ACTION_MOVE), null, new TransferableFileSet(draggedFiles), this);
  }
Example #2
0
  // DragGestureListener interface method
  public void dragGestureRecognized(final DragGestureEvent e) {
    // we should make sure we aren't in edit mode
    final InputEvent ievent = e.getTriggerEvent();
    if (ievent instanceof MouseEvent) {
      // even though I tell dgRecognizer to ignore the the right mouse button,
      // it thinks the RMB starts a drag event...argh
      if ((((MouseEvent) ievent).getModifiers() & InputEvent.BUTTON3_MASK) != 0) return;
    }

    // begin dnd
    final Point ptDragOrigin = e.getDragOrigin();
    final TreePath path = getPathForLocation(ptDragOrigin.x, ptDragOrigin.y);
    if (path == null) return;
    if (isRootPath(path)) return; // Ignore user trying to drag the root node

    // Work out the offset of the drag point from the TreePath bounding rectangle origin
    final Rectangle raPath = getPathBounds(path);
    _ptOffset.setLocation(ptDragOrigin.x - raPath.x, ptDragOrigin.y - raPath.y);

    // Get the cell renderer (which is a JLabel) for the path being dragged
    final JLabel lbl =
        (JLabel)
            getCellRenderer()
                .getTreeCellRendererComponent(
                    this, // tree
                    path.getLastPathComponent(), // value
                    false, // isSelected   (dont want a colored background)
                    this.isExpanded(path), // isExpanded
                    getModel().isLeaf(path.getLastPathComponent()), // isLeaf
                    0, // row          (not important for rendering)
                    false // hasFocus     (dont want a focus rectangle)
                    );
    lbl.setSize(
        (int) raPath.getWidth(),
        (int) raPath.getHeight()); // <-- The layout manager would normally do this

    // Get a buffered image of the selection for dragging a ghost image
    _imgGhost =
        new BufferedImage(
            (int) raPath.getWidth(), (int) raPath.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
    final Graphics2D g2 = _imgGhost.createGraphics();

    // Ask the cell renderer to paint itself into the BufferedImage
    g2.setComposite(
        AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f)); // Make the image ghostlike
    lbl.paint(g2);

    // Now paint a gradient UNDER the ghosted JLabel text (but not under the icon if any)
    // Note: this will need tweaking if your icon is not positioned to the left of the text
    final Icon icon = lbl.getIcon();
    final int nStartOfText = (icon == null) ? 0 : icon.getIconWidth() + lbl.getIconTextGap();
    g2.setComposite(
        AlphaComposite.getInstance(AlphaComposite.DST_OVER, 0.5f)); // Make the gradient ghostlike
    g2.setPaint(
        new GradientPaint(
            nStartOfText,
            0,
            SystemColor.controlShadow,
            getWidth(),
            0,
            new Color(255, 255, 255, 0)));
    g2.fillRect(nStartOfText, 0, getWidth(), _imgGhost.getHeight());
    g2.dispose();

    setSelectionPath(path); // Select this path in the tree

    // Wrap the path being transferred into a Transferable object
    final Transferable transferable = new CTransferableTreePath(path);

    // Remember the path being dragged (because if it is being moved, we will have to delete it
    // later)
    _pathSource = path;

    // We pass our drag image just in case it IS supported by the platform
    e.startDrag(null, _imgGhost, new Point(5, 5), transferable, this);
  }