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:screenieup.ImgurUpload.java

/**
 * Copy image link to user's clipboard./*from w  w  w.j  a v  a2  s  .c  o  m*/
 */
private void copyToClipBoard() {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Clipboard clipboard = toolkit.getSystemClipboard();
    StringSelection selection = new StringSelection(imgurl);
    clipboard.setContents(selection, null);
    System.out.println("Image URL copied to clipboard.");
}

From source file:org.apache.openmeetings.screenshare.job.RemoteJob.java

private void paste(String charValue) {
    Clipboard clippy = getDefaultToolkit().getSystemClipboard();
    try {/* www. j  a v a  2  s  .c om*/
        Transferable transferableText = new StringSelection(charValue);
        clippy.setContents(transferableText, null);

        if (SystemUtils.IS_OS_MAC) {
            // Macintosh simulate Insert
            press(157, 86);
        } else {
            // pressing CTRL+V == insert-mode
            press(KeyEvent.VK_CONTROL, KeyEvent.VK_V);
        }
    } catch (Exception e) {
        log.error("Unexpected exception while pressSpecialSign", e);
    }
}

From source file:org.pentaho.ui.xul.swing.tags.SwingWindow.java

public void copy(String content) throws XulException {
    StringSelection data = new StringSelection(content);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(data, data);
}

From source file:update.updateTest.java

@Test
public void buildMainUpdate() throws Exception {
    String URL = "http://sg-o.de/upd/V8/DecredJWallet.jar";
    String Updater = "https://github.com/SG-O/DcrdJGUIUpdater/releases/download/0.1/DcrdUpdater.jar";
    String RelativeURL = "DecredJWallet.jar.update";
    String RelativeUpdater = "DcrdUpdater.jar";
    File temp = File.createTempFile("dcrd", Long.toString(System.nanoTime()));
    if (!(temp.delete())) {
        System.out.println("Could not create temp dir");
        assertTrue(false);//from www.j ava 2 s  . c o m
        return;
    }

    if (!(temp.mkdir())) {
        System.out.println("Could not create temp dir");
        assertTrue(false);
        return;
    }
    downloadUpdateItem tmpDow = new downloadUpdateItem("generatedown1.tmp", URL, temp, "");
    System.out.println("Downloading1...");
    tmpDow.execute();
    tmpDow = new downloadUpdateItem("generatedown2.tmp", Updater, temp, "");
    System.out.println("Downloading2...");
    tmpDow.execute();
    System.out.println("Hashing");
    String hashResult1 = downloadUpdateItem.hash(new File(temp, "generatedown1.tmp"));
    String hashResult2 = downloadUpdateItem.hash(new File(temp, "generatedown2.tmp"));
    FileUtils.deleteDirectory(temp);
    String message = "[{\"ID\":\"" + RelativeURL + "\",\"type\":1,\"url\":\"" + URL + "\",\"hash\":\""
            + hashResult1 + "\"},{\"ID\":\"" + RelativeUpdater + "\",\"type\":1,\"url\":\"" + Updater
            + "\",\"hash\":\"" + hashResult2 + "\"},{\"relativeSource\":\"\",\"ID\":\"" + RelativeURL
            + "\",\"relativeDest\":\"" + RelativeURL + "\",\"type\":2},{\"relativeSource\":\"\",\"ID\":\""
            + RelativeUpdater + "\",\"relativeDest\":\"" + RelativeUpdater + "\",\"type\":2},{\"ID\":\""
            + RelativeUpdater + "\",\"type\":4}]";
    String sig = update.signString(message, "D:\\key.der");
    JSONArray tmpMes = new JSONArray(message);
    JSONObject obj = new JSONObject();
    obj.put("Commands", tmpMes);
    obj.put("Signature", sig);
    System.out.println("Output:");
    System.out.println(obj);
    StringSelection selection = new StringSelection(obj.toString());
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(selection, selection);
}

From source file:com.photon.phresco.service.admin.actions.ServiceBaseAction.java

public void copyToClipboard() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(
            new StringSelection(copyToClipboard.replaceAll(" ", "").replaceAll("(?m)^[ \t]*\r?\n", "")), null);
}

From source file:screenieup.MixtapeUpload.java

/**
 * Copy upload link to clipboard.//from  ww  w  .  ja  v  a 2  s . c  o m
 */
public void copyToClipBoard(String string) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Clipboard clipboard = toolkit.getSystemClipboard();
    StringSelection selection = new StringSelection(string);
    clipboard.setContents(selection, null);
    System.out.println("Image URL copied to clipboard.");
}

From source file:com.igormaznitsa.ideamindmap.swing.PlainTextEditor.java

public PlainTextEditor(final Project project, final String text) {
    super(new BorderLayout());
    this.editor = new EmptyTextEditor(project);

    final JToolBar menu = new JToolBar();

    final JButton buttonImport = new JButton("Import", AllIcons.Buttons.IMPORT);
    final PlainTextEditor theInstance = this;

    buttonImport.addActionListener(new ActionListener() {
        @Override// ww  w .ja  va 2 s  .co m
        public void actionPerformed(ActionEvent e) {
            final File home = new File(System.getProperty("user.home")); //NOI18N

            final File toOpen = IdeaUtils.chooseFile(theInstance, true,
                    BUNDLE.getString("PlainTextEditor.buttonLoadActionPerformed.title"), home,
                    TEXT_FILE_FILTER);

            if (toOpen != null) {
                try {
                    final String text = FileUtils.readFileToString(toOpen, "UTF-8"); //NOI18N
                    editor.setText(text);
                } catch (Exception ex) {
                    LOGGER.error("Error during text file loading", ex); //NOI18N
                    Messages.showErrorDialog(
                            BUNDLE.getString("PlainTextEditor.buttonLoadActionPerformed.msgError"), "Error");
                }
            }

        }
    });

    final JButton buttonExport = new JButton("Export", AllIcons.Buttons.EXPORT);
    buttonExport.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final File home = new File(System.getProperty("user.home")); //NOI18N
            final File toSave = IdeaUtils.chooseFile(theInstance, true,
                    BUNDLE.getString("PlainTextEditor.buttonSaveActionPerformed.saveTitle"), home,
                    TEXT_FILE_FILTER);
            if (toSave != null) {
                try {
                    final String text = getText();
                    FileUtils.writeStringToFile(toSave, text, "UTF-8"); //NOI18N
                } catch (Exception ex) {
                    LOGGER.error("Error during text file saving", ex); //NOI18N
                    Messages.showErrorDialog(
                            BUNDLE.getString("PlainTextEditor.buttonSaveActionPerformed.msgError"), "Error");
                }
            }
        }
    });

    final JButton buttonCopy = new JButton("Copy", AllIcons.Buttons.COPY);
    buttonCopy.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final StringSelection stringSelection = new StringSelection(editor.getSelectedText());
            final Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
            clpbrd.setContents(stringSelection, null);
        }
    });

    final JButton buttonPaste = new JButton("Paste", AllIcons.Buttons.PASTE);
    buttonPaste.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                final String clipboardText = (String) Toolkit.getDefaultToolkit().getSystemClipboard()
                        .getData(DataFlavor.stringFlavor);
                editor.replaceSelection(clipboardText);
            } catch (UnsupportedFlavorException ex) {
                // no text data in clipboard
            } catch (IOException ex) {
                LOGGER.error("Error during paste from clipboard", ex); //NOI18N
            }
        }
    });

    final JButton buttonClearAll = new JButton("Clear All", AllIcons.Buttons.CLEARALL);
    buttonClearAll.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            editor.clear();
        }
    });

    menu.add(buttonImport);
    menu.add(buttonExport);
    menu.add(buttonCopy);
    menu.add(buttonPaste);
    menu.add(buttonClearAll);

    this.add(menu, BorderLayout.NORTH);
    this.add(editor, BorderLayout.CENTER);

    // I made so strange trick to move the caret into the start of document, all other ways didn't work :(
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            editor.replaceSelection(text);
        }
    });
}

From source file:org.gitools.ui.platform.dialog.ExceptionGlassPane.java

private void copyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyButtonActionPerformed
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    pw.println(cause.getMessage());/*ww  w.j a v  a2  s.  c  om*/
    cause.printStackTrace(pw);
    pw.close();

    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection ss = new StringSelection(sw.toString());
    cb.setContents(ss, ss);
}

From source file:cn.lambdalib.cgui.gui.component.TextBox.java

private void saveClipboardContent() {
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection ss = new StringSelection(content);
    cb.setContents(ss, ss);
}

From source file:uk.nhs.cfh.dsp.srth.desktop.modules.resultexplanationpanel.QueryStatisticsCollectionPanel.java

/**
 * Creates the statements panel./*from w  w w . j  a  v  a 2  s .  co m*/
 * 
 * @param textArea the text area
 * @param title the title
 * 
 * @return the j panel
 */
private synchronized JPanel createStatementsPanel(final JTextArea textArea, String title) {

    JPanel panel = new JPanel(new BorderLayout());
    panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(),
            BorderFactory.createTitledBorder(title)));

    // create buttons for copy and export
    JButton copyButton = new JButton(new AbstractAction("Copy") {

        public void actionPerformed(ActionEvent event) {
            StringSelection selection = new StringSelection(textArea.getSelectedText());
            // get contents of text area and copy to system clipboard
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(selection, QueryStatisticsCollectionPanel.this);
        }
    });

    JButton exportButton = new JButton(new AbstractAction("Export") {

        public void actionPerformed(ActionEvent event) {
            // open a file dialog and export the contents
            FileDialog fd = new FileDialog(applicationService.getFrameView().getActiveFrame(),
                    "Select file to export to", FileDialog.SAVE);
            fd.setVisible(true);

            String fileName = fd.getFile();
            String fileDirectory = fd.getDirectory();

            if (fileDirectory != null && fileName != null) {
                File file = new File(fileDirectory, fileName);
                try {
                    String contents = textArea.getText();
                    FileWriter fw = new FileWriter(file);
                    fw.flush();
                    fw.write(contents);
                    fw.close();
                    // inform user
                    //                  manager.getStatusMessageLabel().setText("Successfully saved contents to file "+fileName);
                    logger.info("Successfully saved contents to file " + fileName);

                } catch (IOException e) {
                    logger.warn(e.fillInStackTrace());
                    //                  manager.getStatusMessageLabel().setText("Errors saving contents to file "+fileName);
                }
            }
        }
    });
    // create buttons panel
    JPanel buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.LINE_AXIS));
    buttonsPanel.add(Box.createHorizontalStrut(200));
    buttonsPanel.add(copyButton);
    buttonsPanel.add(exportButton);

    panel.add(buttonsPanel, BorderLayout.SOUTH);
    panel.add(new JScrollPane(textArea), BorderLayout.CENTER);

    return panel;
}