PageRenderTime 33ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/popups/CopyPasteMenu.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
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
  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. */
  17. package mpv5.ui.popups;
  18. import java.awt.Toolkit;
  19. import java.awt.datatransfer.Clipboard;
  20. import java.awt.datatransfer.ClipboardOwner;
  21. import java.awt.datatransfer.DataFlavor;
  22. import java.awt.datatransfer.StringSelection;
  23. import java.awt.datatransfer.Transferable;
  24. import java.awt.datatransfer.UnsupportedFlavorException;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.MouseAdapter;
  27. import java.awt.event.MouseEvent;
  28. import java.io.IOException;
  29. import javax.swing.AbstractAction;
  30. import javax.swing.JPopupMenu;
  31. import javax.swing.JTextArea;
  32. import javax.swing.JTextField;
  33. import mpv5.ui.frames.MPView;
  34. /**
  35. *
  36. */
  37. public class CopyPasteMenu {
  38. JPopupMenu menu = new JPopupMenu("Menu");
  39. public CopyPasteMenu(final JTextArea field) {
  40. menu.add(new AbstractAction("Paste") {
  41. @Override
  42. public void actionPerformed(ActionEvent e) {
  43. field.append(getClipboard());
  44. }
  45. });
  46. menu.add(new AbstractAction("Copy") {
  47. @Override
  48. public void actionPerformed(ActionEvent e) {
  49. StringSelection stringSelection = new StringSelection(field.getSelectedText());
  50. Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  51. clipboard.setContents(stringSelection, new ClipboardOwner() {
  52. @Override
  53. public void lostOwnership(Clipboard clipboard, Transferable contents) {
  54. }
  55. });
  56. }
  57. });
  58. field.addMouseListener(new MouseAdapter() {
  59. @Override
  60. public void mouseClicked(MouseEvent e) {
  61. if (e.getButton() == MouseEvent.BUTTON3) {
  62. menu.show(e.getComponent(), e.getX(), e.getY());
  63. }
  64. super.mouseClicked(e);
  65. }
  66. });
  67. }
  68. public static String getClipboard() {
  69. Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
  70. try {
  71. if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
  72. return (String) t.getTransferData(DataFlavor.stringFlavor);
  73. }
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. return null;
  78. }
  79. }