Java Code Examples for javax.swing.TransferHandler#canImport()

The following examples show how to use javax.swing.TransferHandler#canImport() . 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: 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 2
Source File: ScriptTreeTransferHandler.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
    logger.debug("canImport " + support.getComponent().getClass().getCanonicalName());
    TransferHandler th = getTransferHandlerForSelection(support.getComponent());
    if (th != null) {
        return th.canImport(support);
    }

    return false;
}
 
Example 3
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();
	}
}