/src/mpv5/ui/popups/CopyPasteMenu.java
Java | 91 lines | 60 code | 12 blank | 19 comment | 5 complexity | e638b583fc0305d916e2a6d53153f974 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
- /*
- * This file is part of YaBS.
- *
- * YaBS is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * YaBS is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
- */
- package mpv5.ui.popups;
- import java.awt.Toolkit;
- import java.awt.datatransfer.Clipboard;
- import java.awt.datatransfer.ClipboardOwner;
- import java.awt.datatransfer.DataFlavor;
- import java.awt.datatransfer.StringSelection;
- import java.awt.datatransfer.Transferable;
- import java.awt.datatransfer.UnsupportedFlavorException;
- import java.awt.event.ActionEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.io.IOException;
- import javax.swing.AbstractAction;
- import javax.swing.JPopupMenu;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import mpv5.ui.frames.MPView;
- /**
- *
- */
- public class CopyPasteMenu {
- JPopupMenu menu = new JPopupMenu("Menu");
- public CopyPasteMenu(final JTextArea field) {
- menu.add(new AbstractAction("Paste") {
- @Override
- public void actionPerformed(ActionEvent e) {
- field.append(getClipboard());
- }
- });
- menu.add(new AbstractAction("Copy") {
- @Override
- public void actionPerformed(ActionEvent e) {
- StringSelection stringSelection = new StringSelection(field.getSelectedText());
- Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
- clipboard.setContents(stringSelection, new ClipboardOwner() {
- @Override
- public void lostOwnership(Clipboard clipboard, Transferable contents) {
- }
- });
- }
- });
- field.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getButton() == MouseEvent.BUTTON3) {
- menu.show(e.getComponent(), e.getX(), e.getY());
- }
- super.mouseClicked(e);
- }
- });
- }
- public static String getClipboard() {
- Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
- try {
- if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
- return (String) t.getTransferData(DataFlavor.stringFlavor);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }