public class KontextMenu
{
private JPopupMenu menu = new JPopupMenu();
private JMenuItem entry = null;
public KontextMenu(final JTextComponent comp)
{
ActionListener al = new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
if(comp.getToolkit().getSystemClipboard() != null)
{
if( e.getActionCommand().equals("Ausschneiden") )
{
comp.cut();
}
else if( e.getActionCommand().equals("Kopieren") )
{
comp.copy();
}
else if( e.getActionCommand().equals("Einfügen") )
{
comp.paste();
}
else if( e.getActionCommand().equals("Alles markieren") )
{
comp.selectAll();
}
}
}
};
comp.addMouseListener( new MouseAdapter()
{
public void mouseReleased( MouseEvent me )
{
if ( me.isPopupTrigger() )
{
// Prüft, ob das TextComponent editierbar ist
if( comp.isEditable() )
{
menu.getComponent(2).setEnabled(true);
menu.getComponent(4).setEnabled(true);
}
else
{
menu.getComponent(2).setEnabled(false);
menu.getComponent(4).setEnabled(false);
}
menu.show( me.getComponent(), me.getX(), me.getY() );
}
}
}
);
menu.add(new JMenuItem("Menü"));
menu.getComponent(0).setEnabled(false);
menu.addSeparator();
entry = new JMenuItem( "Ausschneiden" );
menu.add( entry );
entry.addActionListener(al);
entry = new JMenuItem( "Kopieren" );
menu.add( entry );
entry.addActionListener(al);
entry = new JMenuItem( "Einfügen" );
menu.add( entry );
entry.addActionListener(al);
menu.addSeparator();
entry = new JMenuItem( "Alles markieren" );
menu.add( entry );
entry.addActionListener(al);
}
}