// ---------------------- CutNPastePopup.java -------------------------- // package com.woven_media.gui.menu; /*********************************************************************** Copyright (C) 2008, Brent Allen Parrish This program 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. This program 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. ************************************************************************/ // awt.datatransfer import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; //io import java.io.IOException; // swingx import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JMenuItem; /** * CutNPastePopup class provides a popup menu which implements cut, copy and paste * commands by extending com.woven_media.gui.menu.AbstractPopup * class. * * @author Brent Allen Parrish * @version %I%, %G% * @since 1.0 */ public class CutNPastePopup extends AbstractPopup { private JFrame host; private JTextField textComponent; /** * Class constructor sets the parent member * with reference to this instance's parent * java.awt.Component and a component reference * to a javax.swing.JTextField component. * * @param host java.awt.Component reference * to the parent component. * @since 1.0 */ public CutNPastePopup(JFrame host, JTextField component) { super(host, component); this.host = host; textComponent = component; } /** * Clears text and transfers data to the system clipboard. * * @since 1.0 */ protected void cut() { if(textComponent.getSelectedText() != null) { copy(); textComponent.setText(""); } } /** * Copies selected text to the system clipboard. * * @see java.awt.datatransfer.Clipboard * @see java.awt.datatransfer.StringSelection * @since 1.0 */ protected void copy() { String data = textComponent.getSelectedText(); if(data != null) { StringSelection ss = new StringSelection(data); clipboard.setContents(ss, ss); } else { return; } } /** * Transfers data from the system clipboard to this instance's * member component. * * @see java.awt.datatransfer.Transferable * @see java.awt.datatransfer.UnsupportedFlavorException * @see java.io.IOException * @since 1.0 */ protected void paste() { Transferable t = clipboard.getContents(textComponent); if(t == null) { return; // do nothing } try { String s = (String)t.getTransferData(DataFlavor.stringFlavor); textComponent.setText(s); } catch(UnsupportedFlavorException ufe) { return; } catch(IOException ioe) { return; } } } // End class