List of usage examples for java.awt.datatransfer Clipboard getContents
public synchronized Transferable getContents(Object requestor)
From source file:gdt.jgui.entity.folder.JFolderPanel.java
private boolean hasToInsert() { try {// ww w. j a v a 2s .com Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable clipboardContents = systemClipboard.getContents(null); if (clipboardContents == null) return false; Object transferData = clipboardContents.getTransferData(DataFlavor.javaFileListFlavor); @SuppressWarnings("unchecked") List<File> files = (List<File>) transferData; for (int i = 0; i < files.size(); i++) { File file = (File) files.get(i); if (file.exists() && file.isFile()) return true; } } catch (Exception ee) { LOGGER.severe(ee.toString()); } return false; }
From source file:kolacer.Kolacer.java
private String stringZeSchranky() { Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable t = c.getContents(this); if (t == null) return null; try {/*from w w w . j a v a 2s . co m*/ return ((String) t.getTransferData(DataFlavor.stringFlavor)); } catch (UnsupportedFlavorException | IOException e) { System.err.println(e.getMessage()); return "Chyba pi ?ten ze schrnky"; } //try }
From source file:com._17od.upm.gui.AccountDialog.java
/** * This method takes in a JTextField object and then sets the text of that * text field to the contents of the system clipboard. * /*from w w w. j a v a2 s .c om*/ * @param textField */ public void pasteToTextField(JTextField textField) { String text = ""; Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable clipText = clipboard.getContents(null); if ((clipText != null) && clipText.isDataFlavorSupported(DataFlavor.stringFlavor)) { try { text = (String) clipText.getTransferData(DataFlavor.stringFlavor); } catch (UnsupportedFlavorException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } textField.setText(text); }
From source file:com._17od.upm.gui.AccountDialog.java
/** * This method takes in a JTextArea object and then inserts the contents of * the system clipboard into that text area at the cursor position. * /* w w w. j av a 2 s . com*/ * @param textArea */ public void pasteToTextArea(JTextArea textArea) { String text = ""; Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable clipText = clipboard.getContents(null); if ((clipText != null) && clipText.isDataFlavorSupported(DataFlavor.stringFlavor)) { try { text = (String) clipText.getTransferData(DataFlavor.stringFlavor); } catch (UnsupportedFlavorException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } textArea.insert(text, textArea.getCaretPosition()); textArea.requestFocus(); }
From source file:gdt.jgui.entity.folder.JFolderPanel.java
/** * Get the context menu.//w w w . j a v a 2 s . com * @return the context menu. */ @Override public JMenu getContextMenu() { menu = super.getContextMenu(); mia = null; int cnt = menu.getItemCount(); if (cnt > 0) { mia = new JMenuItem[cnt]; for (int i = 0; i < cnt; i++) mia[i] = menu.getItem(i); } menu.addMenuListener(new MenuListener() { @Override public void menuSelected(MenuEvent e) { //System.out.println("EntitiesPanel:getConextMenu:menu selected"); menu.removeAll(); if (mia != null) { for (JMenuItem mi : mia) menu.add(mi); menu.addSeparator(); } JMenuItem refreshItem = new JMenuItem("Refresh"); refreshItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JConsoleHandler.execute(console, locator$); } }); menu.add(refreshItem); JMenuItem openItem = new JMenuItem("Open folder"); openItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { File folder = new File(entihome$ + "/" + entityKey$); if (!folder.exists()) folder.mkdir(); Desktop.getDesktop().open(folder); } catch (Exception ee) { LOGGER.severe(ee.toString()); } } }); menu.add(openItem); JMenuItem importItem = new JMenuItem("Import"); importItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { File home = new File(System.getProperty("user.home")); Desktop.getDesktop().open(home); } catch (Exception ee) { LOGGER.severe(ee.toString()); } } }); menu.add(importItem); JMenuItem newItem = new JMenuItem("New text"); newItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { JTextEditor textEditor = new JTextEditor(); String teLocator$ = textEditor.getLocator(); teLocator$ = Locator.append(teLocator$, Entigrator.ENTIHOME, entihome$); String text$ = "New" + Identity.key().substring(0, 4) + ".txt"; teLocator$ = Locator.append(teLocator$, JTextEditor.TEXT, text$); JFolderPanel fp = new JFolderPanel(); String fpLocator$ = fp.getLocator(); fpLocator$ = Locator.append(fpLocator$, Entigrator.ENTIHOME, entihome$); fpLocator$ = Locator.append(fpLocator$, EntityHandler.ENTITY_KEY, entityKey$); fpLocator$ = Locator.append(fpLocator$, BaseHandler.HANDLER_METHOD, "response"); fpLocator$ = Locator.append(fpLocator$, JRequester.REQUESTER_ACTION, ACTION_CREATE_FILE); String requesterResponseLocator$ = Locator.compressText(fpLocator$); teLocator$ = Locator.append(teLocator$, JRequester.REQUESTER_RESPONSE_LOCATOR, requesterResponseLocator$); JConsoleHandler.execute(console, teLocator$); } catch (Exception ee) { LOGGER.severe(ee.toString()); } } }); menu.add(newItem); //menu.addSeparator(); if (hasToInsert()) { menu.addSeparator(); JMenuItem insertItem = new JMenuItem("Insert"); insertItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable clipboardContents = systemClipboard.getContents(null); if (clipboardContents == null) return; Object transferData = clipboardContents .getTransferData(DataFlavor.javaFileListFlavor); List<File> files = (List<File>) transferData; for (int i = 0; i < files.size(); i++) { File file = (File) files.get(i); if (file.exists() && file.isFile()) { System.out.println("FolderPanel:insert:in=" + file.getPath()); File dir = new File(entihome$ + "/" + entityKey$); if (!dir.exists()) dir.mkdir(); File out = new File(entihome$ + "/" + entityKey$ + "/" + file.getName()); if (!out.exists()) out.createNewFile(); System.out.println("FolderPanel:insert:out=" + out.getPath()); FileExpert.copyFile(file, out); JConsoleHandler.execute(console, getLocator()); } // System.out.println("FolderPanel:import:file="+file.getPath()); } } catch (Exception ee) { LOGGER.severe(ee.toString()); } } }); menu.add(insertItem); } if (hasToPaste()) { menu.addSeparator(); JMenuItem pasteItem = new JMenuItem("Paste"); pasteItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String[] sa = console.clipboard.getContent(); Properties locator; String file$; File file; File target; String dir$ = entihome$ + "/" + entityKey$; File dir = new File(dir$); if (!dir.exists()) dir.mkdir(); for (String aSa : sa) { try { locator = Locator.toProperties(aSa); if (LOCATOR_TYPE_FILE.equals(locator.getProperty(Locator.LOCATOR_TYPE))) { file$ = locator.getProperty(FILE_PATH); file = new File(file$); target = new File(dir$ + "/" + file.getName()); if (!target.exists()) target.createNewFile(); FileExpert.copyFile(file, target); } } catch (Exception ee) { LOGGER.info(ee.toString()); } } JConsoleHandler.execute(console, locator$); } }); menu.add(pasteItem); } if (hasSelectedItems()) { menu.addSeparator(); JMenuItem deleteItem = new JMenuItem("Delete"); deleteItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int response = JOptionPane.showConfirmDialog(console.getContentPanel(), "Delete ?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (response == JOptionPane.YES_OPTION) { String[] sa = JFolderPanel.this.listSelectedItems(); if (sa == null) return; Properties locator; String file$; File file; for (String aSa : sa) { locator = Locator.toProperties(aSa); file$ = locator.getProperty(FILE_PATH); file = new File(file$); try { if (file.isDirectory()) FileExpert.clear(file$); file.delete(); } catch (Exception ee) { LOGGER.info(ee.toString()); } } } JConsoleHandler.execute(console, locator$); } }); menu.add(deleteItem); JMenuItem copyItem = new JMenuItem("Copy"); copyItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String[] sa = JFolderPanel.this.listSelectedItems(); console.clipboard.clear(); if (sa != null) for (String aSa : sa) console.clipboard.putString(aSa); } }); menu.add(copyItem); JMenuItem exportItem = new JMenuItem("Export"); exportItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String[] sa = JFolderPanel.this.listSelectedItems(); Properties locator; String file$; File file; ArrayList<File> fileList = new ArrayList<File>(); for (String aSa : sa) { try { locator = Locator.toProperties(aSa); file$ = locator.getProperty(FILE_PATH); file = new File(file$); fileList.add(file); } catch (Exception ee) { LOGGER.severe(ee.toString()); } } File[] fa = fileList.toArray(new File[0]); if (fa.length < 1) return; // System.out.println("Folderpanel:finish:list="+fa.length); JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File(System.getProperty("user.home"))); chooser.setDialogTitle("Export files"); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setAcceptAllFileFilterUsed(false); if (chooser.showSaveDialog(JFolderPanel.this) == JFileChooser.APPROVE_OPTION) { String dir$ = chooser.getSelectedFile().getPath(); File target; for (File f : fa) { target = new File(dir$ + "/" + f.getName()); if (!target.exists()) target.createNewFile(); FileExpert.copyFile(f, target); } } else { Logger.getLogger(JMainConsole.class.getName()).info(" no selection"); } // System.out.println("Folderpanel:finish:list="+fileList.size()); } catch (Exception eee) { LOGGER.severe(eee.toString()); } } }); menu.add(exportItem); } } @Override public void menuDeselected(MenuEvent e) { } @Override public void menuCanceled(MenuEvent e) { } }); return menu; }
From source file:com.netease.dagger.BrowserEmulator.java
/** * Paste text from clipboard content/*from w w w. jav a2 s . c o m*/ */ public String getClipboardContent() { pause(stepInterval); try { String text = ""; Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable data = clip.getContents(null); if (data != null) { if (data.isDataFlavorSupported(DataFlavor.stringFlavor)) { text = (String) data.getTransferData(DataFlavor.stringFlavor); } } return text; } catch (Exception e) { e.printStackTrace(); handleFailure("Failed to get clipboard content"); } logger.info("Success to get clipboard content"); return null; }
From source file:org.languagetool.gui.Main.java
private String getClipboardText() { // get text from clipboard or selection: Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemSelection(); if (clipboard == null) { // on Windows clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); }//from w w w .j a v a2 s .com String s; Transferable data = clipboard.getContents(this); try { if (data != null && data.isDataFlavorSupported(DataFlavor.getTextPlainUnicodeFlavor())) { DataFlavor df = DataFlavor.getTextPlainUnicodeFlavor(); try (Reader sr = df.getReaderForText(data)) { s = StringTools.readerToString(sr); } } else { s = ""; } } catch (Exception ex) { s = data.toString(); } return s; }
From source file:ded.ui.DiagramController.java
/** Try to read the clipboard contents as a Diagram. Return null and * display an error if we cannot. */ private Diagram getClipboardAsDiagram() { // Let's start with the "selection" because if it is valid JSON // then it's probably what we want. String selErrorMessage = null; String selContents = null;/*from w ww . ja va 2s. c o m*/ Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemSelection(); if (clipboard == null) { // Probably we're running on something other than X Windows, // so we thankfully don't have to deal with the screwy // "selection" concept. } else { Transferable clipData = clipboard.getContents(clipboard); if (clipData == null) { selErrorMessage = "Nothing is in the \"selection\"."; } else { try { if (clipData.isDataFlavorSupported(DataFlavor.stringFlavor)) { selContents = (String) (clipData.getTransferData(DataFlavor.stringFlavor)); // Try to parse it. try { return Diagram.parseJSONString(selContents); } catch (JSONException e) { selErrorMessage = "Could not parse selection data as Diagram JSON: " + e; } } else { selErrorMessage = "The data in the selection is not a string."; } } catch (Exception e) { selErrorMessage = "Error while retrieving selection data: " + e; } } } // Now try again with the clipboard. String clipErrorMessage = null; String clipContents = null; clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); if (clipboard == null) { clipErrorMessage = "getSystemClipboard returned null?!"; } else { Transferable clipData = clipboard.getContents(clipboard); if (clipData == null) { clipErrorMessage = "Nothing is in the clipboard."; } else { try { if (clipData.isDataFlavorSupported(DataFlavor.stringFlavor)) { clipContents = (String) (clipData.getTransferData(DataFlavor.stringFlavor)); try { return Diagram.parseJSONString(clipContents); } catch (JSONException e) { clipErrorMessage = "Could not parse clipboard data as Diagram JSON: " + e; } } else { clipErrorMessage = "The data in the clipboard is not a string."; } } catch (Exception e) { clipErrorMessage = "Error while retrieving clipboard data: " + e; } } } // Both methods failed, and we have one or two error messages. // Decide what to show. if (selErrorMessage == null) { this.errorMessageBox(clipErrorMessage); } else if (selContents == null && clipContents != null) { this.errorMessageBox(clipErrorMessage); } else if (selContents != null && clipContents == null) { this.errorMessageBox(selErrorMessage); } else if (selContents.equals(clipContents)) { this.errorMessageBox(clipErrorMessage + " (The selection and clipboard contents are the same.)"); } else { this.errorMessageBox("Failed to read either the selection or the clipboard. " + selErrorMessage + " " + clipErrorMessage); } return null; }
From source file:com.cohort.util.String2.java
/** * Get gets the String from the system clipboard * (or null if none)./*from ww w. j ava 2 s .co m*/ * This works in a standalone Java program, not an applet. * From Java Developers Almanac. * This won't throw an exception. */ public static String getClipboardString() { try { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable t = clipboard.getContents(null); if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) return (String) t.getTransferData(DataFlavor.stringFlavor); } catch (Throwable th) { log(ERROR + " while getting the string from the clipboard:\n" + MustBe.throwableToString(th)); } return null; }