Example usage for java.awt.datatransfer Clipboard setContents

List of usage examples for java.awt.datatransfer Clipboard setContents

Introduction

In this page you can find the example usage for java.awt.datatransfer Clipboard setContents.

Prototype

public synchronized void setContents(Transferable contents, ClipboardOwner owner) 

Source Link

Document

Sets the current contents of the clipboard to the specified transferable object and registers the specified clipboard owner as the owner of the new contents.

Usage

From source file:org.monkeys.gui.matcher.MatcherPanel.java

private void copyToClipboard() {
    String delim = ClipboardAction.LINE.delim();
    final Object selected = this.clipboardDropdown.getSelectedItem();
    if (selected instanceof ClipboardAction) {
        final ClipboardAction action = (ClipboardAction) selected;
        delim = action.delim();//  w  ww.  ja v  a 2 s .c o  m
    } else if (selected != null) {
        delim = " " + selected.toString().trim() + " ";
    }

    final StringBuilder buffer = new StringBuilder();
    boolean first = true;
    for (final String match : this.matchingList.getSelectedText()) {
        if (!first) {
            buffer.append(delim);
        }
        buffer.append(match);
        first = false;
    }

    final String blob = buffer.toString().trim();
    final StringSelection selection = new StringSelection(blob);
    final Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
    clpbrd.setContents(selection, selection);
}

From source file:com.raddle.tools.ClipboardTransferMain.java

private void initGUI() {
    try {/*from www  .jav a 2s  .c  o m*/
        {
            this.setTitle("\u8fdc\u7a0b\u526a\u5207\u677f");
            getContentPane().setLayout(null);
            {
                remoteClipGetBtn = new JButton();
                getContentPane().add(remoteClipGetBtn);
                remoteClipGetBtn.setText("\u83b7\u5f97\u8fdc\u7a0b\u526a\u5207\u677f");
                remoteClipGetBtn.setBounds(12, 22, 151, 29);
                remoteClipGetBtn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        doInSocket(new SocketCallback() {

                            @Override
                            public Object connected(Socket socket) throws Exception {
                                if (!isProcessing) {
                                    isProcessing = true;
                                    try {
                                        ClipCommand cmd = new ClipCommand();
                                        cmd.setCmdCode(ClipCommand.CMD_GET_CLIP);
                                        updateMessage("??");
                                        // ??
                                        ObjectOutputStream out = new ObjectOutputStream(
                                                socket.getOutputStream());
                                        out.writeObject(cmd);
                                        // 
                                        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                                        ClipResult result = (ClipResult) in.readObject();
                                        if (result.isSuccess()) {
                                            setLocalClipboard(result);
                                            StringBuilder sb = new StringBuilder();
                                            for (DataFlavor dataFlavor : result.getClipdata().keySet()) {
                                                sb.append("\n");
                                                sb.append(dataFlavor.getPrimaryType()).append("/")
                                                        .append(dataFlavor.getSubType());
                                            }
                                            updateMessage("??? " + sb);
                                        } else {
                                            updateMessage("?:" + result.getMessage());
                                        }
                                        in.close();
                                        out.close();
                                    } catch (Exception e) {
                                        updateMessage("?:" + e.getMessage());
                                    } finally {
                                        isProcessing = false;
                                    }
                                }
                                return null;
                            }
                        });
                    }
                });
            }
            {
                remoteClipSetBtn = new JButton();
                getContentPane().add(remoteClipSetBtn);
                remoteClipSetBtn.setText("\u8bbe\u7f6e\u8fdc\u7a0b\u526a\u5207\u677f");
                remoteClipSetBtn.setBounds(181, 22, 159, 29);
                remoteClipSetBtn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        setRemoteClipboard(true);
                    }
                });
            }
            {
                serverLeb = new JLabel();
                getContentPane().add(serverLeb);
                serverLeb.setText("\u8fdc\u7a0b\u670d\u52a1\u5668\u5730\u5740(IP:PORT)");
                serverLeb.setBounds(12, 63, 162, 17);
            }
            {
                serverAddrTxt = new JTextField();
                getContentPane().add(serverAddrTxt);
                serverAddrTxt.setBounds(169, 58, 186, 27);
            }
            {
                jLabel1 = new JLabel();
                getContentPane().add(jLabel1);
                jLabel1.setText("\u6d88\u606f\uff1a");
                jLabel1.setBounds(12, 97, 48, 24);
            }
            {
                jLabel2 = new JLabel();
                getContentPane().add(jLabel2);
                jLabel2.setText("\u672c\u5730\u526a\u5207\u677f\u670d\u52a1");
                jLabel2.setBounds(12, 297, 91, 20);
            }
            {
                clipServerStartBtn = new JButton();
                getContentPane().add(clipServerStartBtn);
                clipServerStartBtn.setText("\u542f\u52a8");
                clipServerStartBtn.setBounds(12, 329, 79, 29);
                clipServerStartBtn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        startServer();
                    }
                });
            }
            {
                clipServerStopBtn = new JButton();
                getContentPane().add(clipServerStopBtn);
                clipServerStopBtn.setText("\u505c\u6b62");
                clipServerStopBtn.setBounds(103, 329, 81, 29);
                clipServerStopBtn.setEnabled(false);
                clipServerStopBtn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        shutdown();
                    }
                });
            }
            {
                jLabel3 = new JLabel();
                getContentPane().add(jLabel3);
                jLabel3.setText("\u7aef\u53e3\uff1a");
                jLabel3.setBounds(196, 335, 44, 17);
            }
            {
                portTxt = new JTextField();
                getContentPane().add(portTxt);
                portTxt.setText("11221");
                portTxt.setBounds(252, 330, 88, 27);
            }
            {
                modifyClipChk = new JCheckBox();
                getContentPane().add(modifyClipChk);
                modifyClipChk.setText("\u5141\u8bb8\u8fdc\u7a0b\u4fee\u6539\u526a\u5207\u677f");
                modifyClipChk.setBounds(12, 377, 172, 22);
            }
            {
                clearBtn = new JButton();
                getContentPane().add(clearBtn);
                clearBtn.setText("\u6e05\u7a7a\u672c\u5730\u7cfb\u7edf\u526a\u5207\u677f");
                clearBtn.setBounds(196, 374, 159, 29);
                clearBtn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
                        Transferable tText = new StringSelection(null);
                        sysc.setContents(tText, null);
                    }
                });
            }
            {
                autoChk = new JCheckBox();
                autoChk.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        m.setEnabled(autoChk.isSelected());
                    }
                });
                getContentPane().add(autoChk);
                autoChk.setText("\u81ea\u52a8\u8bbe\u7f6e\u8fdc\u7a0b\u526a\u5207\u677f");
                autoChk.setBounds(12, 405, 172, 22);
            }
        }

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(55, 97, 542, 199);
        getContentPane().add(scrollPane);
        {
            messageArea = new JTextArea();
            scrollPane.setViewportView(messageArea);
        }
        this.setSize(611, 465);
        {
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:TransferableScribblePane.java

/** Copy the selected line to the clipboard */
public void copy() {
    if (selectedLine == null)
        return; // Only works if a line is selected
    // Get the system Clipboard object.
    Clipboard c = this.getToolkit().getSystemClipboard();

    // Wrap the selected line in a TransferablePolyLine object
    // and pass it to the clipboard, with an object to receive notification
    // when some other application takes ownership of the clipboard
    c.setContents(new TransferablePolyLine((PolyLine) selectedLine.clone()), new ClipboardOwner() {
        public void lostOwnership(Clipboard c, Transferable t) {
            // This method is called when something else
            // is copied to the clipboard. We could use it
            // to deselect the selected line, if we wanted.
        }//from   w  ww  .j av  a 2 s. c o  m
    });
}

From source file:SciTK.Plot.java

/** 
* Copy displayed data to the system clipboard 
*//*from   w ww  .j  a  v  a 2  s . c om*/
public void copyData() {
    Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection s2 = new StringSelection(toString());
    c.setContents(s2, this);
}

From source file:com.photon.phresco.framework.rest.api.UtilService.java

/**
 * Copy to clipboard.//w w w.  j  a  v a 2  s  .  c om
 *
 * @param content the content
 * @throws UnsupportedEncodingException 
 */
private void copyToClipboard(String content) throws PhrescoException {
    try {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        String decodedContent = java.net.URLDecoder.decode(content, "UTF-8");
        clipboard.setContents(new StringSelection(decodedContent.replaceAll("(?m)^[ \t]*\r?\n", "")), null);
    } catch (Exception e) {
        throw new PhrescoException(e);
    }
}

From source file:com.opendoorlogistics.studio.scripts.editor.ScriptEditor.java

protected SimpleAction createCopyAction() {
    return new SimpleAction("Copy", "Copy script to clipboard", "edit-copy-7.png") {

        @Override/*from  ww w.  jav  a  2 s.co  m*/
        public void actionPerformed(ActionEvent e) {
            StringSelection stringSelection = new StringSelection(getXML());
            Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
            clpbrd.setContents(stringSelection, null);
        }
    };
}

From source file:gdt.jgui.entity.webset.JWeblinkEditor.java

private void showLoginMenu(MouseEvent e) {
    try {/*  w  w  w . jav a2  s .  co  m*/
        JPopupMenu logonMenu = new JPopupMenu();
        JMenuItem copyItem = new JMenuItem("Copy");
        logonMenu.add(copyItem);
        copyItem.setHorizontalTextPosition(JMenuItem.RIGHT);
        copyItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    StringSelection stringSelection = new StringSelection(loginField.getText());
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(stringSelection, JWeblinkEditor.this);
                } catch (Exception ee) {
                    Logger.getLogger(getClass().getName()).info(ee.toString());
                }
            }
        });
        logonMenu.show(e.getComponent(), e.getX(), e.getY());
    } catch (Exception ee) {
        Logger.getLogger(getClass().getName()).severe(ee.toString());
    }
}

From source file:com.egangotri.transliteratorAsSwing.TransliteratorJFrame.java

public void copyToClipBoard(String source) {
    Clipboard clipboard;
    clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable content = clipboard.getContents(this);
    try {/*  ww  w . jav a 2  s  .com*/
        String data = (String) content.getTransferData(DataFlavor.stringFlavor);
        StringSelection contents;
        if ("cb1".equalsIgnoreCase(source))
            contents = new StringSelection(tb1.getText());
        else if ("cb2".equalsIgnoreCase(source))
            contents = new StringSelection(tb2.getText());
        else
            contents = new StringSelection(tb3.getText());
        clipboard.setContents(contents, this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:gdt.jgui.entity.webset.JWeblinkEditor.java

private void showPasswordMenu(MouseEvent e) {
    try {//from  w w  w.j  av  a 2  s .c  o m
        JPopupMenu passwordMenu = new JPopupMenu();
        JMenuItem copyItem = new JMenuItem("Copy");
        passwordMenu.add(copyItem);
        copyItem.setHorizontalTextPosition(JMenuItem.RIGHT);
        copyItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    StringSelection stringSelection = new StringSelection(passwordField.getText());
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(stringSelection, JWeblinkEditor.this);
                } catch (Exception ee) {
                    Logger.getLogger(getClass().getName()).info(ee.toString());
                }
            }
        });
        JMenuItem encodeItem = new JMenuItem("Encrypt/decrypt");
        passwordMenu.add(encodeItem);
        encodeItem.setHorizontalTextPosition(JMenuItem.RIGHT);
        encodeItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    save();
                    JTextEncrypter te = new JTextEncrypter();
                    String teLocator$ = te.getLocator();
                    teLocator$ = Locator.append(teLocator$, Entigrator.ENTIHOME, entihome$);
                    teLocator$ = Locator.append(teLocator$, JTextEditor.TEXT, passwordField.getText());
                    teLocator$ = Locator.append(teLocator$, JTextEditor.TEXT_TITLE, nameField.getText());
                    String weLocator$ = JWeblinkEditor.this.getLocator();
                    weLocator$ = Locator.append(weLocator$, BaseHandler.HANDLER_METHOD, "response");
                    weLocator$ = Locator.append(weLocator$, JRequester.REQUESTER_ACTION,
                            ACTION_ENCODE_PASSWORD);
                    teLocator$ = Locator.append(teLocator$, JRequester.REQUESTER_RESPONSE_LOCATOR,
                            Locator.compressText(weLocator$));
                    JConsoleHandler.execute(console, teLocator$);
                } catch (Exception ee) {
                    Logger.getLogger(getClass().getName()).info(ee.toString());
                }
            }
        });
        passwordMenu.show(e.getComponent(), e.getX(), e.getY());
    } catch (Exception ee) {
        Logger.getLogger(getClass().getName()).severe(ee.toString());
    }
}

From source file:net.sf.nmedit.nomad.core.Nomad.java

public void export() {
    Document doc = getDocumentManager().getSelection();
    if (!(doc instanceof Transferable))
        return;// ww  w .j  a  v a  2 s.  c o m

    Transferable transferable = (Transferable) doc;

    String title = doc.getTitle();
    if (title == null)
        title = "Export";
    else
        title = "Export '" + title + "'";

    JComboBox src = new JComboBox(transferable.getTransferDataFlavors());
    src.setRenderer(new DefaultListCellRenderer() {
        /**
         * 
         */
        private static final long serialVersionUID = -4553255745845039428L;

        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            String text;
            if (value instanceof DataFlavor) {
                DataFlavor flavor = (DataFlavor) value;
                String mimeType = flavor.getMimeType();
                String humanRep = flavor.getHumanPresentableName();
                String charset = flavor.getParameter("charset");

                if (mimeType == null)
                    text = "?";
                else {
                    text = mimeType;
                    int ix = text.indexOf(';');
                    if (ix >= 0)
                        text = text.substring(0, ix).trim();
                }
                if (charset != null)
                    text += "; charset=" + charset;
                if (humanRep != null)
                    text += " (" + humanRep + ")";
            } else {
                text = String.valueOf(value);
            }
            return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
        }
    });

    JComboBox dst = new JComboBox(new Object[] { "File", "Clipboard" });

    Object[] msg = { "Source:", doc.getTitle(), "Export as:", src, "Export to:", dst };
    Object[] options = { "Ok", "Cancel" };

    JOptionPane op = new JOptionPane(msg, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null,
            options);

    JDialog dialog = op.createDialog(getWindow(), title);
    dialog.setModal(true);
    dialog.setVisible(true);

    boolean ok = "Ok".equals(op.getValue());

    DataFlavor flavor = (DataFlavor) src.getSelectedItem();
    dialog.dispose();
    if (!ok)
        return;

    if (flavor == null)
        return;

    if ("Clipboard".equals(dst.getSelectedItem())) {
        Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
        cb.setContents(new SelectedTransfer(flavor, transferable), null);
    } else {
        export(transferable, flavor);
    }
}