Search in sources :

Example 46 with Transferable

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

the class PTableTest method assertHasClipboardValue.

private void assertHasClipboardValue(@Nullable String value) throws Exception {
    ArgumentCaptor<Transferable> captor = ArgumentCaptor.forClass(Transferable.class);
    verify(myCopyPasteManager).setContents(captor.capture());
    Transferable transferable = captor.getValue();
    assertThat(transferable).isNotNull();
    assertThat(transferable.isDataFlavorSupported(DataFlavor.stringFlavor)).isTrue();
    assertThat(transferable.getTransferData(DataFlavor.stringFlavor)).isEqualTo(value);
}
Also used : Transferable(java.awt.datatransfer.Transferable)

Example 47 with Transferable

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

the class RosterFrame method createTop.

JComponent createTop() {
    Object selectedRosterGroup = prefsMgr.getProperty(getWindowFrameRef(), SELECTED_ROSTER_GROUP);
    groups = new RosterGroupsPanel((selectedRosterGroup != null) ? selectedRosterGroup.toString() : null);
    groups.setNewWindowMenuAction(this.getNewWindowAction());
    setTitle(groups.getSelectedRosterGroup());
    final JPanel rosters = new JPanel();
    rosters.setLayout(new BorderLayout());
    // set up roster table
    rtable = new RosterTable(true, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    rtable.setRosterGroup(this.getSelectedRosterGroup());
    rtable.setRosterGroupSource(groups);
    rosters.add(rtable, BorderLayout.CENTER);
    // add selection listener
    rtable.getTable().getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> {
        JTable table = rtable.getTable();
        if (!e.getValueIsAdjusting()) {
            if (rtable.getSelectedRosterEntries().length == 1 && table.getSelectedRow() >= 0) {
                log.debug("Selected row ", table.getSelectedRow());
                locoSelected(rtable.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRow()), RosterTableModel.IDCOL).toString());
            } else if (rtable.getSelectedRosterEntries().length > 1 || table.getSelectedRow() < 0) {
                locoSelected(null);
            }
        // leave last selected item visible if no selection
        }
    });
    //Set all the sort and width details of the table first.
    String rostertableref = getWindowFrameRef() + ":roster";
    rtable.getTable().setName(rostertableref);
    // Allow only one column to be sorted at a time - 
    // Java allows multiple column sorting, but to effectly persist that, we
    // need to be intelligent about which columns can be meaningfully sorted
    // with other columns; this bypasses the problem by only allowing the
    // last column sorted to affect sorting
    RowSorterUtil.addSingleSortableColumnListener(rtable.getTable().getRowSorter());
    // Reset and then persist the table's ui state
    JTablePersistenceManager tpm = InstanceManager.getNullableDefault(JTablePersistenceManager.class);
    if (tpm != null) {
        tpm.resetState(rtable.getTable());
        tpm.persist(rtable.getTable());
    }
    rtable.getTable().setDragEnabled(true);
    rtable.getTable().setTransferHandler(new TransferHandler() {

        @Override
        public int getSourceActions(JComponent c) {
            return TransferHandler.COPY;
        }

        @Override
        public Transferable createTransferable(JComponent c) {
            JTable table = rtable.getTable();
            ArrayList<String> Ids = new ArrayList<>(table.getSelectedRowCount());
            for (int i = 0; i < table.getSelectedRowCount(); i++) {
                Ids.add(rtable.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRows()[i]), RosterTableModel.IDCOL).toString());
            }
            return new RosterEntrySelection(Ids);
        }

        @Override
        public void exportDone(JComponent c, Transferable t, int action) {
        // nothing to do
        }
    });
    MouseListener rosterMouseListener = new rosterPopupListener();
    rtable.getTable().addMouseListener(rosterMouseListener);
    try {
        clickDelay = ((Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"));
    } catch (RuntimeException e) {
        clickDelay = 500;
        log.debug("Unable to get the double click speed, Using JMRI default of half a second" + e.toString());
    }
    // assemble roster/groups splitpane
    rosterGroupSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, groups, rosters);
    rosterGroupSplitPane.setOneTouchExpandable(true);
    // emphasis rosters
    rosterGroupSplitPane.setResizeWeight(0);
    Object w = prefsMgr.getProperty(getWindowFrameRef(), "rosterGroupPaneDividerLocation");
    if (w != null) {
        groupSplitPaneLocation = (Integer) w;
        rosterGroupSplitPane.setDividerLocation(groupSplitPaneLocation);
    }
    if (!Roster.getDefault().getRosterGroupList().isEmpty()) {
        if (prefsMgr.getSimplePreferenceState(this.getClass().getName() + ".hideGroups")) {
            hideGroupsPane(true);
        }
    } else {
        enableRosterGroupMenuItems(false);
    }
    PropertyChangeListener propertyChangeListener = (PropertyChangeEvent changeEvent) -> {
        JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
        String propertyName = changeEvent.getPropertyName();
        if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
            int current = sourceSplitPane.getDividerLocation();
            hideGroups = current <= 1;
            Integer last = (Integer) changeEvent.getNewValue();
            if (current >= 2) {
                groupSplitPaneLocation = current;
            } else if (last >= 2) {
                groupSplitPaneLocation = last;
            }
        }
    };
    groups.addPropertyChangeListener(SELECTED_ROSTER_GROUP, new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent pce) {
            prefsMgr.setProperty(this.getClass().getName(), SELECTED_ROSTER_GROUP, pce.getNewValue());
            setTitle((String) pce.getNewValue());
        }
    });
    rosterGroupSplitPane.addPropertyChangeListener(propertyChangeListener);
    Roster.getDefault().addPropertyChangeListener((PropertyChangeEvent e) -> {
        if (e.getPropertyName().equals("RosterGroupAdded") && Roster.getDefault().getRosterGroupList().size() == 1) {
            // if the pane is hidden, show it when 1st group is created
            hideGroupsPane(false);
            enableRosterGroupMenuItems(true);
        } else if (!rtable.isVisible() && (e.getPropertyName().equals("saved"))) {
            if (firstHelpLabel != null) {
                firstHelpLabel.setVisible(false);
            }
            rtable.setVisible(true);
            rtable.resetColumnWidths();
        }
    });
    if (Roster.getDefault().numEntries() == 0) {
        try {
            BufferedImage myPicture = ImageIO.read(FileUtil.findURL(("resources/" + Bundle.getMessage("ThrottleFirstUseImage")), FileUtil.Location.INSTALLED));
            //rosters.add(new JLabel(new ImageIcon( myPicture )), BorderLayout.CENTER);
            firstHelpLabel = new JLabel(new ImageIcon(myPicture));
            rtable.setVisible(false);
            rosters.add(firstHelpLabel, BorderLayout.NORTH);
            //tableArea.add(firstHelpLabel);
            rtable.setVisible(false);
        } catch (IOException ex) {
        // handle exception...
        }
    }
    return rosterGroupSplitPane;
}
Also used : RosterEntrySelection(jmri.util.datatransfer.RosterEntrySelection) JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) PropertyChangeListener(java.beans.PropertyChangeListener) ListSelectionEvent(javax.swing.event.ListSelectionEvent) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) MouseListener(java.awt.event.MouseListener) BorderLayout(java.awt.BorderLayout) PropertyChangeEvent(java.beans.PropertyChangeEvent) JComponent(javax.swing.JComponent) Transferable(java.awt.datatransfer.Transferable) JLabel(javax.swing.JLabel) IOException(java.io.IOException) JTablePersistenceManager(jmri.swing.JTablePersistenceManager) JTable(javax.swing.JTable) TransferHandler(javax.swing.TransferHandler) JSplitPane(javax.swing.JSplitPane)

Example 48 with Transferable

use of java.awt.datatransfer.Transferable in project processdash by dtuma.

the class WBSClipSelection method getNodeListFromClipboard.

/** Interact with the system clipboard to retrieve a list of WBSNodes.
     * 
     * @param prototype if the system clipboard contains plain text, a list
     *   of nodes modeled after this prototype will be returned.
     */
public static List getNodeListFromClipboard(WBSNode prototype) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable t = clipboard.getContents(prototype);
    // on the clipboard).
    try {
        Object d = t.getTransferData(WBS_LIST_FLAVOR);
        List result = ((WBSClipData) d).getWBSNodes(prototype.getWbsModel());
        if (result != null)
            return result;
    } catch (Exception e) {
    }
    // clipboard by some other WBS Editor.
    try {
        Object d = t.getTransferData(WBS_FLAVOR);
        List result = ((WBSClipData) d).getWBSNodes(prototype.getWbsModel());
        if (result != null)
            return result;
    } catch (Exception e) {
    }
    // names, and create some ultra-simple nodes with those names.
    try {
        String plainText = (String) t.getTransferData(DataFlavor.stringFlavor);
        String[] lines = plainText.split("\n");
        int baseIndent = Math.max(1, prototype.getIndentLevel());
        List result = new ArrayList();
        for (int i = 0; i < lines.length; i++) {
            Matcher m = PLAINTEXT_PATTERN.matcher(lines[i]);
            if (!m.find())
                continue;
            String nodeName = scrubName(m.group(2)).trim();
            int indent = baseIndent;
            if (m.group(1) != null && m.group(1).length() > 0) {
                Matcher mi = INDENT_PATTERN.matcher(m.group(1));
                while (mi.find()) indent++;
            }
            WBSNode node = new WBSNode(prototype.getWbsModel(), nodeName, WBSNode.UNKNOWN_TYPE, indent, true);
            result.add(node);
        }
        return result;
    } catch (Exception e) {
    }
    // if all else fails, return null to indicate failure.
    return null;
}
Also used : Matcher(java.util.regex.Matcher) Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Clipboard(java.awt.datatransfer.Clipboard) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException)

Example 49 with Transferable

use of java.awt.datatransfer.Transferable in project cayenne by apache.

the class CayenneModelerController method initBindings.

protected void initBindings() {
    frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            PROJECT_STATE_UTIL.saveLastState(projectController);
            getApplication().getActionManager().getAction(ExitAction.class).exit();
        }
    });
    // This is in Mac OSX only.
    if (System.getProperty("os.name").startsWith("Mac OS")) {
        Runnable runner = new Runnable() {

            @Override
            public void run() {
                PROJECT_STATE_UTIL.saveLastState(projectController);
            }
        };
        Runtime.getRuntime().addShutdownHook(new Thread(runner, "Window Prefs Hook"));
    }
    new DropTarget(frame, new DropTargetAdapter() {

        @Override
        public void drop(DropTargetDropEvent dtde) {
            dtde.acceptDrop(dtde.getDropAction());
            Transferable transferable = dtde.getTransferable();
            dtde.dropComplete(processDropAction(transferable));
        }
    });
    ComponentGeometry geometry = new ComponentGeometry(frame.getClass(), null);
    geometry.bind(frame, 1200, 720, 0);
}
Also used : DropTargetAdapter(java.awt.dnd.DropTargetAdapter) ComponentGeometry(org.apache.cayenne.modeler.pref.ComponentGeometry) WindowEvent(java.awt.event.WindowEvent) Transferable(java.awt.datatransfer.Transferable) WindowAdapter(java.awt.event.WindowAdapter) DropTarget(java.awt.dnd.DropTarget) DropTargetDropEvent(java.awt.dnd.DropTargetDropEvent)

Example 50 with Transferable

use of java.awt.datatransfer.Transferable in project knime-core by knime.

the class SelectionPanel method drop.

/**
 * {@inheritDoc}
 */
@Override
public void drop(final DropTargetDropEvent dtde) {
    if (dtde.getDropAction() == DnDConstants.ACTION_MOVE) {
        dtde.acceptDrop(dtde.getDropAction());
        Transferable t = dtde.getTransferable();
        String s = "default";
        try {
            s = (String) t.getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException e) {
        // TODO Auto-generated catch block
        } catch (IOException e) {
        // TODO Auto-generated catch block
        }
        int i = m_config.drop(s);
        Pane dp = getNewPane(m_includePanel, m_config, i);
        m_includePanel.add(dp.getComponentPanel(), m_gbc);
        m_gbc.gridy++;
        m_includePanel.setBackground(UIManager.getColor("Panel.background"));
        m_scrollPane.revalidate();
        dtde.dropComplete(true);
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) JSplitPane(javax.swing.JSplitPane) Pane(org.knime.base.node.preproc.draganddroppanel.droppanes.Pane) JScrollPane(javax.swing.JScrollPane)

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