Example usage for java.awt.datatransfer StringSelection StringSelection

List of usage examples for java.awt.datatransfer StringSelection StringSelection

Introduction

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

Prototype

public StringSelection(String data) 

Source Link

Document

Creates a Transferable capable of transferring the specified String .

Usage

From source file:is.iclt.jcorpald.CorpaldView.java

private void copyToClipboard() {
    try {/* w ww. j  a  v a  2s. c  om*/
        File toOpen = model.getResultFile();
        String resultString = FileUtils.readFileToString(toOpen);
        StringSelection stringSelection = new StringSelection(resultString);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, this);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Samples.Advanced.GraphEditorDemo.java

/**
* Place a String on the clipboard, and make this class the
* owner of the Clipboard's contents.//  ww  w  . j a v a  2s . c o m
*/
public void setClipboardContents(String aString) {
    StringSelection stringSelection = new StringSelection(aString);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(stringSelection, this);
}

From source file:com.igormaznitsa.nbmindmap.nb.swing.PlainTextEditor.java

private void buttonCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCopyActionPerformed
    StringSelection stringSelection = new StringSelection(this.lastEditor.getSelectedText());
    final Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
    clpbrd.setContents(stringSelection, null);
}

From source file:org.ut.biolab.medsavant.client.view.component.KeyValuePairPanel.java

public static Component getCopyButton(final String key, final KeyValuePairPanel p) {
    JButton button = ViewUtil/*from   w  ww  .ja v  a 2 s.  co m*/
            .getTexturedButton(IconFactory.getInstance().getIcon(IconFactory.StandardIcon.COPY));
    button.setToolTipText("Copy " + key);
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            String selection = p.getValue(key);
            StringSelection data = new StringSelection(selection);
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(data, data);
            DialogUtils.displayMessage("Copied \"" + selection + "\" to clipboard.");
        }
    });
    return button;
}

From source file:de.tor.tribes.ui.views.DSWorkbenchReTimerFrame.java

private void copyAttacksToClipboardAsBBCode() {
    jideRetimeTabbedPane.setSelectedIndex(1);
    try {//from  www  .ja  v  a  2s .  c om
        List<Attack> attacks = getSelectedAttacks();
        if (attacks.isEmpty()) {
            showInfo("Keine Angriffe ausgewhlt");
            return;
        }
        boolean extended = (JOptionPaneHelper.showQuestionConfirmBox(this,
                "Erweiterte BB-Codes verwenden (nur fr Forum und Notizen geeignet)?", "Erweiterter BB-Code",
                "Nein", "Ja") == JOptionPane.YES_OPTION);

        StringBuilder buffer = new StringBuilder();
        if (extended) {
            buffer.append("[u][size=12]Angriffsplan[/size][/u]\n\n");
        } else {
            buffer.append("[u]Angriffsplan[/u]\n\n");
        }

        buffer.append(new AttackListFormatter().formatElements(attacks, extended));

        if (extended) {
            buffer.append("\n[size=8]Erstellt am ");
            buffer.append(
                    new SimpleDateFormat("dd.MM.yy 'um' HH:mm:ss").format(Calendar.getInstance().getTime()));
            buffer.append(" mit DS Workbench ");
            buffer.append(Constants.VERSION).append(Constants.VERSION_ADDITION + "[/size]\n");
        } else {
            buffer.append("\nErstellt am ");
            buffer.append(
                    new SimpleDateFormat("dd.MM.yy 'um' HH:mm:ss").format(Calendar.getInstance().getTime()));
            buffer.append(" mit DS Workbench ");
            buffer.append(Constants.VERSION).append(Constants.VERSION_ADDITION + "\n");
        }

        String b = buffer.toString();
        StringTokenizer t = new StringTokenizer(b, "[");
        int cnt = t.countTokens();
        if (cnt > 1000) {
            if (JOptionPaneHelper.showQuestionConfirmBox(this,
                    "Die ausgewhlten Angriffe bentigen mehr als 1000 BB-Codes\n"
                            + "und knnen daher im Spiel (Forum/IGM/Notizen) nicht auf einmal dargestellt werden.\nTrotzdem exportieren?",
                    "Zu viele BB-Codes", "Nein", "Ja") == JOptionPane.NO_OPTION) {
                return;
            }
        }

        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(b), null);
        String result = "Daten in Zwischenablage kopiert.";
        showSuccess(result);
    } catch (Exception e) {
        logger.error("Failed to copy data to clipboard", e);
        String result = "Fehler beim Kopieren in die Zwischenablage.";
        showError(result);
    }
}

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

private void initGUI() {
    try {/*  w w  w . j ava  2 s .com*/
        {
            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:com.qspin.qtaste.ui.xmleditor.TestRequirementEditor.java

private void copySelectionToClipboard() {
    StringBuffer stringBuffer = new StringBuffer();
    int[] selectedRows = m_TestRequirementTable.getSelectedRows();
    int[] selectedCols = m_TestRequirementTable.getSelectedColumns();
    for (int i = 0; i < selectedRows.length; i++) {
        for (int j = 0; j < selectedCols.length; j++) {
            stringBuffer.append(m_TestRequirementTable.getValueAt(selectedRows[i], selectedCols[j]));
            if (j < selectedCols.length - 1) {
                stringBuffer.append("\t");
            }//from   w  ww .j a  va 2s  .c  o m
        }
        stringBuffer.append("\n");
    }
    StringSelection stringSelection = new StringSelection(stringBuffer.toString());
    m_systemClipboard.setContents(stringSelection, stringSelection);
}

From source file:com.seleniumtests.driver.CustomEventFiringWebDriver.java

/**
 * Use copy to clipboard and copy-paste keyboard shortcut to write something on upload window
 *///  w  ww. ja va2  s.  co  m
public static void uploadFileUsingClipboard(File tempFile) {

    // Copy to clipboard
    Toolkit.getDefaultToolkit().getSystemClipboard()
            .setContents(new StringSelection(tempFile.getAbsolutePath()), null);
    Robot robot;
    try {
        robot = new Robot();

        WaitHelper.waitForSeconds(1);

        //         // Press Enter
        //         robot.keyPress(KeyEvent.VK_ENTER);
        //   
        //         // Release Enter
        //         robot.keyRelease(KeyEvent.VK_ENTER);

        // Press CTRL+V
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);

        // Release CTRL+V
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_V);
        WaitHelper.waitForSeconds(1);

        // Press Enter
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    } catch (AWTException e) {
        throw new ScenarioException("could not initialize robot to upload file: " + e.getMessage());
    }
}

From source file:de.tor.tribes.ui.views.DSWorkbenchChurchFrame.java

private void bbCopySelection() {
    try {/*from w  w  w .  ja  va2 s. c o m*/
        int[] rows = jChurchTable.getSelectedRows();
        if (rows.length == 0) {
            return;
        }

        boolean extended = (JOptionPaneHelper.showQuestionConfirmBox(this,
                "Erweiterte BB-Codes verwenden (nur fr Forum und Notizen geeignet)?", "Erweiterter BB-Code",
                "Nein", "Ja") == JOptionPane.YES_OPTION);

        StringBuilder buffer = new StringBuilder();
        if (extended) {
            buffer.append("[u][size=12]Kirchendrfer[/size][/u]\n\n");
        } else {
            buffer.append("[u]Kirchendrfer[/u]\n\n");
        }

        buffer.append("[table]\n");
        buffer.append("[**]Spieler[||]Dorf[||]Radius[/**]\n");

        for (int row1 : rows) {
            int row = jChurchTable.convertRowIndexToModel(row1);
            int tribeCol = jChurchTable.convertColumnIndexToModel(0);
            int villageCol = jChurchTable.convertColumnIndexToModel(1);
            int rangeCol = jChurchTable.convertColumnIndexToModel(2);
            buffer.append("[*]").append(((Tribe) jChurchTable.getModel().getValueAt(row, tribeCol)).toBBCode())
                    .append("[|]")
                    .append(((Village) jChurchTable.getModel().getValueAt(row, villageCol)).toBBCode())
                    .append("[|]").append(jChurchTable.getModel().getValueAt(row, rangeCol));
        }

        buffer.append("[/table]");

        if (extended) {
            buffer.append("\n[size=8]Erstellt am ");
            buffer.append(
                    new SimpleDateFormat("dd.MM.yy 'um' HH:mm:ss").format(Calendar.getInstance().getTime()));
            buffer.append(" mit DS Workbench ");
            buffer.append(Constants.VERSION).append(Constants.VERSION_ADDITION + "[/size]\n");
        } else {
            buffer.append("\nErstellt am ");
            buffer.append(
                    new SimpleDateFormat("dd.MM.yy 'um' HH:mm:ss").format(Calendar.getInstance().getTime()));
            buffer.append(" mit DS Workbench ");
            buffer.append(Constants.VERSION).append(Constants.VERSION_ADDITION + "\n");
        }

        String b = buffer.toString();
        StringTokenizer t = new StringTokenizer(b, "[");
        int cnt = t.countTokens();
        if (cnt > 1000) {
            if (JOptionPaneHelper.showQuestionConfirmBox(this,
                    "Die ausgewhlten Kirchen bentigen mehr als 1000 BB-Codes\n"
                            + "und knnen daher im Spiel (Forum/IGM/Notizen) nicht auf einmal dargestellt werden.\n"
                            + "Trotzdem exportieren?",
                    "Zu viele BB-Codes", "Nein", "Ja") == JOptionPane.NO_OPTION) {
                return;
            }
        }

        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(b), null);
        String result = "Daten in Zwischenablage kopiert.";
        showSuccess(result);
    } catch (Exception e) {
        logger.error("Failed to copy data to clipboard", e);
        String result = "Fehler beim Kopieren in die Zwischenablage.";
        showError(result);
    }
}

From source file:biomine.bmvis2.Vis.java

public void setClipboard(String str) {
    assert str != null : "Null str";
    AppletContext appletContext = null;
    try {//ww w.  j a v a 2s .  c o  m
        appletContext = getAppletContext();
    } catch (Exception e) {
    }

    Clipboard clipboard;
    StringSelection data = new StringSelection(str);

    try {
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        if (clipboard != null) {
            clipboard.setContents(data, null);
        }
    } catch (java.security.AccessControlException e) {
        if (appletContext != null) {
            try {
                str = str.replace('\'', '"');
                appletContext
                        .showDocument(new URL("javascript:copyToClipboard(encodeURIComponent('" + str + "'))"));
            } catch (Exception e2) {
                JOptionPane.showMessageDialog(null, e2.getMessage(), "Copying to clipboard failed",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Copying to clipboard failed",
                JOptionPane.ERROR_MESSAGE);
    }
}