Search in sources :

Example 41 with Transferable

use of java.awt.datatransfer.Transferable in project JMRI by JMRI.

the class ControlPanelEditor method drop.

@SuppressWarnings("unchecked")
@Override
public void drop(DropTargetDropEvent evt) {
    try {
        //Point pt = evt.getLocation(); coords relative to entire window
        Point pt = _targetPanel.getMousePosition(true);
        Transferable tr = evt.getTransferable();
        if (log.isDebugEnabled()) {
            // avoid string building if not debug
            DataFlavor[] flavors = tr.getTransferDataFlavors();
            StringBuilder flavor = new StringBuilder();
            for (DataFlavor flavor1 : flavors) {
                flavor.append(flavor1.getRepresentationClass().getName()).append(", ");
            }
            log.debug("Editor Drop: flavor classes={}", flavor);
        }
        if (tr.isDataFlavorSupported(_positionableDataFlavor)) {
            Positionable item = (Positionable) tr.getTransferData(_positionableDataFlavor);
            if (item == null) {
                return;
            }
            item.setLocation(pt.x, pt.y);
            // now set display level in the pane.
            item.setDisplayLevel(item.getDisplayLevel());
            item.setEditor(this);
            putItem(item);
            item.updateSize();
            //if (_debug) log.debug("Drop positionable "+item.getNameString()+
            //                                    " as "+item.getClass().getName()+
            //                                    ", w= "+item.maxWidth()+", h= "+item.maxHeight());
            evt.dropComplete(true);
            return;
        } else if (tr.isDataFlavorSupported(_namedIconDataFlavor)) {
            NamedIcon newIcon = new NamedIcon((NamedIcon) tr.getTransferData(_namedIconDataFlavor));
            String url = newIcon.getURL();
            NamedIcon icon = NamedIcon.getIconByName(url);
            PositionableLabel ni = new PositionableLabel(icon, this);
            // infer a background icon from the size
            if (icon.getIconHeight() > 500 || icon.getIconWidth() > 600) {
                ni.setDisplayLevel(BKG);
            } else {
                ni.setDisplayLevel(ICONS);
            }
            ni.setLocation(pt.x, pt.y);
            ni.setEditor(this);
            putItem(ni);
            ni.updateSize();
            evt.dropComplete(true);
            return;
        } else if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String) tr.getTransferData(DataFlavor.stringFlavor);
            PositionableLabel l = new PositionableLabel(text, this);
            l.setSize(l.getPreferredSize().width, l.getPreferredSize().height);
            l.setDisplayLevel(LABELS);
            l.setLocation(pt.x, pt.y);
            l.setEditor(this);
            putItem(l);
            evt.dropComplete(true);
        } else if (tr.isDataFlavorSupported(_positionableListDataFlavor)) {
            List<Positionable> dragGroup = (List<Positionable>) tr.getTransferData(_positionableListDataFlavor);
            for (Positionable pos : dragGroup) {
                pos.setEditor(this);
                putItem(pos);
                pos.updateSize();
                log.debug("DnD Add {}", pos.getNameString());
            }
        } else {
            log.warn("Editor DropTargetListener  supported DataFlavors not avaialable at drop from " + tr.getClass().getName());
        }
    } catch (IOException ioe) {
        log.warn("Editor DropTarget caught IOException", ioe);
    } catch (UnsupportedFlavorException ufe) {
        log.warn("Editor DropTarget caught UnsupportedFlavorException", ufe);
    }
    log.debug("Editor DropTargetListener drop REJECTED!");
    evt.rejectDrop();
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Transferable(java.awt.datatransfer.Transferable) PositionableLabel(jmri.jmrit.display.PositionableLabel) List(java.util.List) ArrayList(java.util.ArrayList) Point(java.awt.Point) Positionable(jmri.jmrit.display.Positionable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 42 with Transferable

use of java.awt.datatransfer.Transferable in project JMRI by JMRI.

the class DnDTableImportExportHandler method importData.

@Override
public boolean importData(TransferHandler.TransferSupport support) {
    if (!canImport(support)) {
        return false;
    }
    if (log.isDebugEnabled()) {
        JTable.DropLocation loc = (JTable.DropLocation) support.getDropLocation();
        log.debug("importData: isDrop= {} location= {} row= {} col= {}", support.isDrop(), loc.getDropPoint(), loc.getRow(), loc.getColumn());
    }
    if (!support.isDrop()) {
        return false;
    }
    TransferHandler.DropLocation loc = support.getDropLocation();
    if (!(loc instanceof JTable.DropLocation)) {
        return false;
    }
    Component comp = support.getComponent();
    if (!(comp instanceof JTable)) {
        return false;
    }
    JTable.DropLocation location = (JTable.DropLocation) loc;
    JTable table = (JTable) comp;
    int row = location.getRow();
    int col = location.getColumn();
    row = table.convertRowIndexToModel(row);
    col = table.convertColumnIndexToModel(col);
    for (int i = 0; i < _skipColumns.length; i++) {
        if (_skipColumns[i] == col) {
            return false;
        }
    }
    try {
        Transferable trans = support.getTransferable();
        Object obj = trans.getTransferData(DataFlavor.stringFlavor);
        table.getModel().setValueAt(obj, row, col);
        return true;
    } catch (UnsupportedFlavorException ufe) {
        log.warn("DnDStringImportHandler.importData: " + ufe.getMessage());
    } catch (IOException ioe) {
        log.warn("DnDStringImportHandler.importData: " + ioe.getMessage());
    }
    return false;
}
Also used : JTable(javax.swing.JTable) Transferable(java.awt.datatransfer.Transferable) TransferHandler(javax.swing.TransferHandler) IOException(java.io.IOException) Component(java.awt.Component) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 43 with Transferable

use of java.awt.datatransfer.Transferable in project jdk8u_jdk by JetBrains.

the class SunDragSourceContextPeer method startDrag.

/**
     * initiate a DnD operation ...
     */
public void startDrag(DragSourceContext dsc, Cursor c, Image di, Point p) throws InvalidDnDOperationException {
    /* Fix for 4354044: don't initiate a drag if event sequence provided by
         * DragGestureRecognizer is empty */
    if (getTrigger().getTriggerEvent() == null) {
        throw new InvalidDnDOperationException("DragGestureEvent has a null trigger");
    }
    dragSourceContext = dsc;
    cursor = c;
    sourceActions = getDragSourceContext().getSourceActions();
    dragImage = di;
    dragImageOffset = p;
    Transferable transferable = getDragSourceContext().getTransferable();
    SortedMap<Long, DataFlavor> formatMap = DataTransferer.getInstance().getFormatsForTransferable(transferable, DataTransferer.adaptFlavorMap(getTrigger().getDragSource().getFlavorMap()));
    long[] formats = DataTransferer.getInstance().keysToLongArray(formatMap);
    startDrag(transferable, formats, formatMap);
    /*
         * Fix for 4613903.
         * Filter out all mouse events that are currently on the event queue.
         */
    discardingMouseEvents = true;
    EventQueue.invokeLater(new Runnable() {

        public void run() {
            discardingMouseEvents = false;
        }
    });
}
Also used : InvalidDnDOperationException(java.awt.dnd.InvalidDnDOperationException) Transferable(java.awt.datatransfer.Transferable) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 44 with Transferable

use of java.awt.datatransfer.Transferable in project jdk8u_jdk by JetBrains.

the class SunClipboard method lostOwnershipNow.

protected void lostOwnershipNow(final AppContext disposedContext) {
    final SunClipboard sunClipboard = SunClipboard.this;
    ClipboardOwner owner = null;
    Transferable contents = null;
    synchronized (sunClipboard) {
        final AppContext context = sunClipboard.contentsContext;
        if (context == null) {
            return;
        }
        if (disposedContext == null || context == disposedContext) {
            owner = sunClipboard.owner;
            contents = sunClipboard.contents;
            sunClipboard.contentsContext = null;
            sunClipboard.owner = null;
            sunClipboard.contents = null;
            sunClipboard.clearNativeContext();
            context.removePropertyChangeListener(AppContext.DISPOSED_PROPERTY_NAME, sunClipboard);
        } else {
            return;
        }
    }
    if (owner != null) {
        owner.lostOwnership(sunClipboard, contents);
    }
}
Also used : AppContext(sun.awt.AppContext) Transferable(java.awt.datatransfer.Transferable) ClipboardOwner(java.awt.datatransfer.ClipboardOwner)

Example 45 with Transferable

use of java.awt.datatransfer.Transferable in project android by JetBrains.

the class PTable method performPaste.

@Override
public void performPaste(@NotNull DataContext dataContext) {
    PTableItem item = getSelectedNonGroupItem();
    if (item == null) {
        return;
    }
    Transferable transferable = myCopyPasteManager.getContents();
    if (transferable == null || !transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        return;
    }
    try {
        item.setValue(transferable.getTransferData(DataFlavor.stringFlavor));
    } catch (IOException | UnsupportedFlavorException exception) {
        Logger.getInstance(PTable.class).warn(exception);
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Aggregations

Transferable (java.awt.datatransfer.Transferable)96 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)30 IOException (java.io.IOException)30 List (java.util.List)18 DataFlavor (java.awt.datatransfer.DataFlavor)14 ArrayList (java.util.ArrayList)14 File (java.io.File)13 Clipboard (java.awt.datatransfer.Clipboard)12 Point (java.awt.Point)9 StringSelection (java.awt.datatransfer.StringSelection)5 Project (com.intellij.openapi.project.Project)4 ClipboardOwner (java.awt.datatransfer.ClipboardOwner)4 TransferHandler (javax.swing.TransferHandler)4 Editor (com.intellij.openapi.editor.Editor)3 ActionEvent (java.awt.event.ActionEvent)3 JComponent (javax.swing.JComponent)3 TreePath (javax.swing.tree.TreePath)3 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 PsiElement (com.intellij.psi.PsiElement)2