Java Code Examples for javax.swing.JComponent#getTransferHandler()

The following examples show how to use javax.swing.JComponent#getTransferHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseCaret.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Determines if the following are true:
 * <ul>
 * <li>the press event is located over a selection
 * <li>the dragEnabled property is true
 * <li>A TranferHandler is installed
 * </ul>
 * <p>
 * This is implemented to check for a TransferHandler.
 * Subclasses should perform the remaining conditions.
 */
protected boolean isDragPossible(MouseEvent e) {
    JComponent comp = getEventComponent(e);
    boolean possible =  (comp == null) ? false : (comp.getTransferHandler() != null);
    if (possible) {
        JTextComponent c = (JTextComponent) getEventComponent(e);
        if (c.getDragEnabled()) {
            Caret caret = c.getCaret();
            int dot = caret.getDot();
            int mark = caret.getMark();
            if (dot != mark) {
                Point p = new Point(e.getX(), e.getY());
                int pos = c.viewToModel(p);

                int p0 = Math.min(dot, mark);
                int p1 = Math.max(dot, mark);
                if ((pos >= p0) && (pos < p1)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 2
Source File: DefaultTransferHandler.java    From freecol with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void dragGestureRecognized(DragGestureEvent dge) {
    JComponent c = (JComponent)dge.getComponent();
    DefaultTransferHandler th
        = (DefaultTransferHandler)c.getTransferHandler();
    Transferable t = th.createTransferable(c);
    if (t == null) {
        logger.warning("Unable to create transferable for: " + dge);
        th.exportDone(c, null, NONE);
        return;
    }

    this.scrolls = c.getAutoscrolls();
    c.setAutoscrolls(false);
    try {
        Cursor cursor = getCursor(c);
        dge.startDrag(cursor, t, this);
    } catch (RuntimeException re) {
        c.setAutoscrolls(this.scrolls);
    }
}
 
Example 3
Source File: DataObjectTransferHandler.java    From nextreports-designer with Apache License 2.0 6 votes vote down vote up
public void dragEnter(DropTargetDragEvent event) {
    DataFlavor[] dataFlavors = event.getCurrentDataFlavors();
    JComponent c = (JComponent) event.getDropTargetContext().getComponent();
    TransferHandler transferHandler = c.getTransferHandler();

    if ((transferHandler != null) && transferHandler.canImport(c, dataFlavors)) {
        canImport = true;
    } else {
        canImport = false;
    }

    int dropAction = event.getDropAction();

    if (canImport && actionSupported(dropAction)) {
        event.acceptDrag(dropAction);
    } else {
        event.rejectDrag();
    }
}
 
Example 4
Source File: DataObjectTransferHandler.java    From nextreports-designer with Apache License 2.0 6 votes vote down vote up
public void drop(DropTargetDropEvent event) {
    int dropAction = event.getDropAction();
    JComponent c = (JComponent) event.getDropTargetContext().getComponent();
    DataObjectTransferHandler transferHandler = (DataObjectTransferHandler) c.getTransferHandler();

    if (canImport && (transferHandler != null) && actionSupported(dropAction)) {
        event.acceptDrop(dropAction);
        try {
            Transferable transferable = event.getTransferable();
            transferHandler.setDropPoint(event.getLocation());
            transferHandler.setDropComponent(c);
            event.dropComplete(transferHandler.importData(c, transferable));
        } catch (RuntimeException e) {
            event.dropComplete(false);
        }
    } else {
        event.rejectDrop();
    }
}
 
Example 5
Source File: TableDNDRecognizer.java    From nextreports-designer with Apache License 2.0 6 votes vote down vote up
public void mouseDragged(MouseEvent ev) {
    Point p = ev.getPoint();
    if (!recognized
            && ev.isShiftDown()
            && ((Math.abs(pressedPoint.x - p.x) > 5) || (Math
                    .abs(pressedPoint.y - p.y) > 5))) {
        dragged = true;
        recognized = true;
        JComponent c = (JComponent) ev.getSource();
        TransferHandler th = c.getTransferHandler();
        if (th != null) {
            th.exportAsDrag(c, ev, ev.isAltDown() ? 
                    DnDConstants.ACTION_COPY : DnDConstants.ACTION_MOVE);
        }
    }
}
 
Example 6
Source File: mxGraphHandler.java    From blog-codes with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to return the component for a drop target event.
 */
protected static final mxGraphTransferHandler getGraphTransferHandler(
		DropTargetEvent e)
{
	JComponent component = getDropTarget(e);
	TransferHandler transferHandler = component.getTransferHandler();

	if (transferHandler instanceof mxGraphTransferHandler)
	{
		return (mxGraphTransferHandler) transferHandler;
	}

	return null;
}
 
Example 7
Source File: EditorCaret.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
    * Determines if the following are true:
    * <ul>
    * <li>the press event is located over a selection
    * <li>the dragEnabled property is true
    * <li>A TranferHandler is installed
    * </ul>
    * <p>
    * This is implemented to check for a TransferHandler.
    * Subclasses should perform the remaining conditions.
    */
   private boolean isDragPossible(MouseEvent e) {
Object src = e.getSource();
if (src instanceof JComponent) {
    JComponent comp = (JComponent) src;
           boolean possible =  (comp == null) ? false : (comp.getTransferHandler() != null);
           if (possible && comp instanceof JTextComponent) {
               JTextComponent c = (JTextComponent) comp;
               if (c.getDragEnabled()) {
                   Caret caret = c.getCaret();
                   int dot = caret.getDot();
                   int mark = caret.getMark();
                   if (dot != mark) {
                       Point p = new Point(e.getX(), e.getY());
                       int pos = c.viewToModel(p);

                       int p0 = Math.min(dot, mark);
                       int p1 = Math.max(dot, mark);
                       if ((pos >= p0) && (pos < p1)) {
                           return true;
                       }
                   }
               }
           }
       }
       return false;
   }
 
Example 8
Source File: DropListener.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets called when the mouse was released on a Swing component
 * that has this object as a MouseListener.
 *
 * @param e The event that holds the information about the mouse click.
 */
@Override
public void mouseReleased(MouseEvent e) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable clipData = clipboard.getContents(clipboard);
    if (clipData != null) {
        if (clipData.isDataFlavorSupported(DefaultTransferHandler.flavor)) {
            JComponent comp = (JComponent)e.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.importData(comp, clipData);
        }
    }
}
 
Example 9
Source File: MTransferableLabel.java    From javamelody with Apache License 2.0 5 votes vote down vote up
@Override
public void mouseDragged(MouseEvent e) {
	if (firstMouseEvent != null) {
		e.consume();
		final int dx = Math.abs(e.getX() - firstMouseEvent.getX());
		final int dy = Math.abs(e.getY() - firstMouseEvent.getY());
		if (dx > 5 || dy > 5) {
			final JComponent c = (JComponent) e.getSource();
			final TransferHandler handler = c.getTransferHandler();
			handler.exportAsDrag(c, firstMouseEvent, TransferHandler.COPY);
			firstMouseEvent = null;
		}
	}
}
 
Example 10
Source File: Tree.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
    * Handles drag and drop.
    *
    * @param e - the mousedragged event to handle drag and drop from.
    */
   @Override
public void mouseDragged(MouseEvent e) {
       final JComponent c = (JComponent)e.getSource();
       JiveTreeNode node = (JiveTreeNode)getLastSelectedPathComponent();
       if (node == null) {
           return;
       }
       if (node.isLeaf()) {
           TransferHandler handler = c.getTransferHandler();
           handler.exportAsDrag(c, e, TransferHandler.COPY);
       }
   }
 
Example 11
Source File: mxGraphHandler.java    From blog-codes with Apache License 2.0 4 votes vote down vote up
/**
 * 
 */
public void dragEnter(DropTargetDragEvent e)
{
	JComponent component = getDropTarget(e);
	TransferHandler th = component.getTransferHandler();
	boolean isLocal = th instanceof mxGraphTransferHandler
			&& ((mxGraphTransferHandler) th).isLocalDrag();

	if (isLocal)
	{
		canImport = true;
	}
	else
	{
		canImport = graphComponent.isImportEnabled()
				&& th.canImport(component, e.getCurrentDataFlavors());
	}

	if (canImport)
	{
		transferBounds = null;
		setVisible(false);

		try
		{
			Transferable t = e.getTransferable();

			if (t.isDataFlavorSupported(mxGraphTransferable.dataFlavor))
			{
				mxGraphTransferable gt = (mxGraphTransferable) t
						.getTransferData(mxGraphTransferable.dataFlavor);
				dragCells = gt.getCells();

				if (gt.getBounds() != null)
				{
					mxGraph graph = graphComponent.getGraph();
					double scale = graph.getView().getScale();
					transferBounds = gt.getBounds();
					int w = (int) Math.ceil((transferBounds.getWidth() + 1)
							* scale);
					int h = (int) Math
							.ceil((transferBounds.getHeight() + 1) * scale);
					setPreviewBounds(new Rectangle(
							(int) transferBounds.getX(),
							(int) transferBounds.getY(), w, h));

					if (imagePreview)
					{
						// Does not render fixed cells for local preview
						// but ignores movable state for non-local previews
						if (isLocal)
						{
							if (!isLivePreview())
							{
								updateDragImage(graph
										.getMovableCells(dragCells));
							}
						}
						else
						{
							Object[] tmp = graphComponent
									.getImportableCells(dragCells);
							updateDragImage(tmp);

							// Shows no drag icon if import is allowed but none
							// of the cells can be imported
							if (tmp == null || tmp.length == 0)
							{
								canImport = false;
								e.rejectDrag();

								return;
							}
						}
					}

					setVisible(true);
				}
			}

			e.acceptDrag(TransferHandler.COPY_OR_MOVE);
		}
		catch (Exception ex)
		{
			// do nothing
			log.log(Level.SEVERE, "Failed to handle dragEnter", ex);
		}

	}
	else
	{
		e.rejectDrag();
	}
}