/src/mpv5/ui/popups/CopyPasteMenu.java
Java | 91 lines | 60 code | 12 blank | 19 comment | 5 complexity | e638b583fc0305d916e2a6d53153f974 MD5 | raw file
1/* 2 * This file is part of YaBS. 3 * 4 * YaBS is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * YaBS is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with YaBS. If not, see <http://www.gnu.org/licenses/>. 16 */ 17package mpv5.ui.popups; 18 19import java.awt.Toolkit; 20import java.awt.datatransfer.Clipboard; 21import java.awt.datatransfer.ClipboardOwner; 22import java.awt.datatransfer.DataFlavor; 23import java.awt.datatransfer.StringSelection; 24import java.awt.datatransfer.Transferable; 25import java.awt.datatransfer.UnsupportedFlavorException; 26import java.awt.event.ActionEvent; 27import java.awt.event.MouseAdapter; 28import java.awt.event.MouseEvent; 29import java.io.IOException; 30import javax.swing.AbstractAction; 31import javax.swing.JPopupMenu; 32import javax.swing.JTextArea; 33import javax.swing.JTextField; 34import mpv5.ui.frames.MPView; 35 36/** 37 * 38 */ 39public class CopyPasteMenu { 40 41 JPopupMenu menu = new JPopupMenu("Menu"); 42 43 public CopyPasteMenu(final JTextArea field) { 44 menu.add(new AbstractAction("Paste") { 45 46 @Override 47 public void actionPerformed(ActionEvent e) { 48 field.append(getClipboard()); 49 } 50 }); 51 menu.add(new AbstractAction("Copy") { 52 53 @Override 54 public void actionPerformed(ActionEvent e) { 55 StringSelection stringSelection = new StringSelection(field.getSelectedText()); 56 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 57 clipboard.setContents(stringSelection, new ClipboardOwner() { 58 @Override 59 public void lostOwnership(Clipboard clipboard, Transferable contents) { 60 61 } 62 }); 63 64 } 65 }); 66 67 field.addMouseListener(new MouseAdapter() { 68 69 @Override 70 public void mouseClicked(MouseEvent e) { 71 if (e.getButton() == MouseEvent.BUTTON3) { 72 menu.show(e.getComponent(), e.getX(), e.getY()); 73 } 74 super.mouseClicked(e); 75 } 76 }); 77 78 } 79 80 public static String getClipboard() { 81 Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 82 try { 83 if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) { 84 return (String) t.getTransferData(DataFlavor.stringFlavor); 85 } 86 } catch (Exception e) { 87 e.printStackTrace(); 88 } 89 return null; 90 } 91}