import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
//import javax.swing.table.*;
/**
* Text Component Popup Event Queue. This class when used will provide a context JPopupMenu for all
* JTextComponents within any application. It should be created as an EventQueue object which will register
* events from all components and show a JPopupMenu for JTextComponents on right click. The popup contains the
* standard options for Cut, Copy and Paste as well as a Select All option. More options can be added as and
* when necessary. Example usage: public static void main(String[] args) {
* Toolkit.getDefaultToolkit().getSystemEventQueue().push( new TCPopupEventQueue() ); MainAppFrame frame = new
* MainAppFrame(); frame.setVisible(true); }
*/
public class TCPopupEventQueue extends EventQueue {
private JPopupMenu popup;
private BasicAction cut, copy, paste, selectAll;
private ImageIcon cutGif = new ImageIcon(getClass().getResource("/images/cut.gif"));
private ImageIcon copyGif = new ImageIcon(getClass().getResource("/images/copy.gif"));
private ImageIcon pasteGif = new ImageIcon(getClass().getResource("/images/paste.gif"));
/**
*
*/
public TCPopupEventQueue() {
// createPopupMenu();
}
/**
* Creates the popup menu.
*
* @param tc The Textcomponent.
*/
public void createPopupMenu(JTextComponent tc) {
cut = new CutAction("Ausschneiden", cutGif);
copy = new CopyAction("Kopieren", copyGif);
paste = new PasteAction("Einfügen", pasteGif);
selectAll = new SelectAllAction("Alles markieren", null);
cut.setTextComponent(tc);
copy.setTextComponent(tc);
paste.setTextComponent(tc);
selectAll.setTextComponent(tc);
popup = new JPopupMenu();
popup.add(cut);
popup.add(copy);
popup.add(paste);
popup.addSeparator();
popup.add(selectAll);
}
/**
* Shows the popup menu.
*
* @param parent the parent component
* @param me the mouseevent.
*/
public void showPopup(Component parent, MouseEvent me) {
popup.validate();
popup.show(parent, me.getX(), me.getY());
}
/**
* @param event the AWTEvent
* @see java.awt.EventQueue#dispatchEvent(java.awt.AWTEvent)
*/
protected void dispatchEvent(final AWTEvent event) {
if ((event instanceof MouseEvent) && ((MouseEvent) event).isPopupTrigger()) {
MouseEvent me = (MouseEvent) event;
Component comp = SwingUtilities.getDeepestComponentAt((Component) me.getSource(), me.getX(), me.getY());
if ((comp instanceof JTextComponent)
&& (MenuSelectionManager.defaultManager().getSelectedPath().length <= 0)) {
createPopupMenu((JTextComponent) comp);
showPopup((Component) me.getSource(), me);
}
}
super.dispatchEvent(event);
}
/**
* =================== Inner Classes =====================.
*
* @author
*/
public abstract class BasicAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private JTextComponent comp;
/**
* Die 'basis' Aktion.
*
* @param text Der Text
* @param icon Das Icon
*/
public BasicAction(String text, Icon icon) {
super(text, icon);
putValue(Action.SHORT_DESCRIPTION, text);
}
/**
* Setzt die Textkomponente.
*
* @param comp Die Componente
*/
public void setTextComponent(JTextComponent comp) {
this.comp = comp;
}
/**
* @param e the ActionEvent
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public abstract void actionPerformed(ActionEvent e);
/**
* Liefert die Komponente.
*
* @return Returns the comp.
*/
public JTextComponent getComp() {
return comp;
}
}
/**
* Die 'Ausschneiden' Aktion.
*
* @author
*/
public class CutAction extends BasicAction {
private static final long serialVersionUID = 3321329294959169520L;
/**
* Die 'Ausschneiden' Aktion.
*
* @param text Der Text
* @param icon Das Icon
*/
public CutAction(String text, Icon icon) {
super(text, icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl X"));
}
/**
* @param e the ActionEvent
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
getComp().cut();
}
/**
* @return boolean true/false
* @see javax.swing.Action#isEnabled()
*/
public boolean isEnabled() {
return getComp() != null && getComp().isEditable() && getComp().getSelectedText() != null;
}
}
/**
* Die 'Kopieren' Aktion.
*
* @author
*/
public class CopyAction extends BasicAction {
/**
*
*/
private static final long serialVersionUID = 6718013900664404880L;
/**
* Die 'Kopieren' Aktion.
*
* @param text Der Text
* @param icon Das Icon
*/
public CopyAction(String text, Icon icon) {
super(text, icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl C"));
}
/**
* @param e the ActionEvent
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
getComp().copy();
}
/**
* @return boolean true/false
* @see javax.swing.Action#isEnabled()
*/
public boolean isEnabled() {
return getComp() != null && getComp().getSelectedText() != null;
}
}
/**
* Die 'Einfügen' Aktion.
*
* @author
*/
public class PasteAction extends BasicAction {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Die 'Einfügen' Aktion.
*
* @param text Der Text
* @param icon Das Icon
*/
public PasteAction(String text, Icon icon) {
super(text, icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl V"));
}
/**
* @param e the ActionEvent
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
getComp().paste();
}
/**
* @return boolean true/false
* @see javax.swing.Action#isEnabled()
*/
public boolean isEnabled() {
Transferable content = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
return getComp() != null && getComp().isEnabled() && getComp().isEditable()
&& content.isDataFlavorSupported(DataFlavor.stringFlavor);
}
}
/**
* Die 'alles markieren' Aktion.
*
* @author
*/
public class SelectAllAction extends BasicAction {
private static final long serialVersionUID = -4706100832515899182L;
/**
* Die 'Alles Markieren'Funktion.
*
* @param text the txt
* @param icon the icon
*/
public SelectAllAction(String text, Icon icon) {
super(text, icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl A"));
}
/**
* @param e the ActionEvent
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
getComp().selectAll();
}
/**
* @return boolean true/false
* @see javax.swing.Action#isEnabled()
*/
public boolean isEnabled() {
return getComp() != null
&& getComp().isEnabled()
&& getComp().getText().length() > 0
&& (getComp().getSelectedText() == null || getComp().getSelectedText().length() < getComp()
.getText().length());
}
}
}