trackeditor: add copy paste popup menu to text fields

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@8274 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: d1c3394e4b7ecc26de1f61889c4ec89e6556c4d6
Former-commit-id: 21bceda68f97201bab1cb32ef1512db8d1a5acc8
This commit is contained in:
iobyte 2022-06-28 14:23:02 +00:00
parent d23f559887
commit 666e7f0cfb
3 changed files with 143 additions and 1 deletions

View file

@ -63,6 +63,7 @@ IF(Java_Development_FOUND AND Java_FOUND)
gui/view/CircuitViewSelectionEvent.java
gui/view/CircuitViewSelectionListener.java
miscel/EPMath.java
miscel/TCPopupEventQueue.java
plugin/Plugin.java
plugin/torcs/NoOpEntityResolver.java
plugin/torcs/TorcsPlugin.java
@ -209,4 +210,4 @@ ELSE(Java_Development_FOUND AND Java_FOUND)
MESSAGE(WARNING "Java_FOUND = ${Java_FOUND}")
MESSAGE(WARNING "Java_Development_FOUND = ${Java_Development_FOUND}")
MESSAGE(WARNING "TrackEditor NOT included!")
ENDIF(Java_Development_FOUND AND Java_FOUND)
ENDIF(Java_Development_FOUND AND Java_FOUND)

View file

@ -0,0 +1,136 @@
package miscel;
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.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
/*
* This is a modified copy of: https://www.roseindia.net/java/example/java/swing/copy-data-from-ms.shtml
* It is used with their permission as long as a link to the original code is included.
*/
public class TCPopupEventQueue extends EventQueue {
public JPopupMenu popup;
public BasicAction cut, copy, paste, selectAll;
public void createPopupMenu(JTextComponent text){
cut = new CutAction("Cut",null);
copy = new CopyAction("Copy", null);
paste = new PasteAction("Paste",null);
selectAll = new SelectAllAction("Select All",null);
cut.setTextComponent(text);
copy.setTextComponent(text);
paste.setTextComponent(text);
selectAll.setTextComponent(text);
popup = new JPopupMenu();
popup.add( cut );
popup.add( copy );
popup.add( paste );
popup.addSeparator();
popup.add( selectAll );
}
public void showPopup(Component parent, MouseEvent me){
popup.validate();
popup.show(parent, me.getX(), me.getY());
}
protected void dispatchEvent(AWTEvent event){
super.dispatchEvent(event);
if(!(event instanceof MouseEvent)){
return;
}
MouseEvent me = (MouseEvent)event;
if(!me.isPopupTrigger()) {
return;
}
if( !(me.getSource() instanceof Component) ) {
return;
}
Component comp = SwingUtilities.getDeepestComponentAt((Component)me.getSource(),me.getX(), me.getY());
if( !(comp instanceof JTextComponent)){
return;
}
if(MenuSelectionManager.defaultManager().getSelectedPath().length > 0){
return;
}
createPopupMenu((JTextComponent)comp);
showPopup((Component)me.getSource(), me);
}
public abstract class BasicAction extends AbstractAction{
JTextComponent comp;
public BasicAction(String text, Icon icon) {
super(text, icon);
putValue(Action.SHORT_DESCRIPTION, text);
}
public void setTextComponent(JTextComponent comp){
this.comp = comp;
}
public abstract void actionPerformed(ActionEvent e);
}
public class CutAction extends BasicAction {
public CutAction(String text, Icon icon) {
super(text, icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl X"));
}
public void actionPerformed(ActionEvent e){
comp.cut();
}
public boolean isEnabled(){
return comp != null && comp.isEditable() && comp.getSelectedText() != null;
}
}
public class CopyAction extends BasicAction{
public CopyAction(String text, Icon icon){
super(text,icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl C"));
}
public void actionPerformed(ActionEvent e) {
comp.copy();
}
public boolean isEnabled() {
return comp != null && comp.getSelectedText() != null;
}
}
public class PasteAction extends BasicAction{
public PasteAction(String text, Icon icon){
super(text,icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl V"));
}
public void actionPerformed(ActionEvent e) {
comp.paste();
}
public boolean isEnabled() {
Transferable content = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
return comp != null && comp.isEnabled() && comp.isEditable()
&& content.isDataFlavorSupported(DataFlavor.stringFlavor);
}
}
public class SelectAllAction extends BasicAction{
public SelectAllAction(String text, Icon icon){
super(text,icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl A"));
}
public void actionPerformed(ActionEvent e){
comp.selectAll();
}
public boolean isEnabled() {
return comp != null && comp.isEnabled() && comp.getText().length() > 0
&& (comp.getSelectedText() == null ||
comp.getSelectedText().length() < comp.getText().length());
}
}
}

View file

@ -20,6 +20,8 @@
*/
package utils;
import java.awt.Toolkit;
import javax.swing.UIManager;
import com.jgoodies.looks.plastic.Plastic3DLookAndFeel;
@ -27,6 +29,7 @@ import com.jgoodies.looks.plastic.PlasticLookAndFeel;
import com.jgoodies.looks.plastic.theme.DesertBlue;
import gui.EditorFrame;
import miscel.TCPopupEventQueue;
/**
@ -41,6 +44,8 @@ public class TrackEditor
public static void main(String[] args)
{
Toolkit.getDefaultToolkit().getSystemEventQueue().push(new TCPopupEventQueue());
PlasticLookAndFeel.setCurrentTheme(new DesertBlue());
try
{