diff options
Diffstat (limited to 'NET/worlds/console/FocusPreservingTextField.java')
| -rw-r--r-- | NET/worlds/console/FocusPreservingTextField.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/NET/worlds/console/FocusPreservingTextField.java b/NET/worlds/console/FocusPreservingTextField.java new file mode 100644 index 0000000..57b656b --- /dev/null +++ b/NET/worlds/console/FocusPreservingTextField.java @@ -0,0 +1,89 @@ +package NET.worlds.console; + +import NET.worlds.scape.EventQueue; +import NET.worlds.scape.Pilot; +import java.awt.Event; +import java.awt.Font; +import java.awt.TextField; + +class FocusPreservingTextField extends TextField { + private static final long serialVersionUID = 7475622191515920214L; + private static FocusPreservingTextField lostFocus; + private static FocusPreservingTextField hasFocus; + private static FocusPreservingTextField chatLine; + private static Object hasFocusMutex = new Object(); + private static Font font = new Font(Console.message("GammaTextFont"), 0, 12); + boolean preserveFocus; + boolean takeNextFocus; + + public FocusPreservingTextField() { + super(30); + this.setFocusable(true); + this.setFont(font); + } + + public void isChatLine() { + chatLine = this; + } + + @Override + public void requestFocus() { + if (!this.takeNextFocus) { + synchronized (hasFocusMutex) { + this.preserveFocus = hasFocus != null && hasFocus.getText().length() != 0 || chatLine != null && chatLine.getText().length() != 0; + } + } else { + this.takeNextFocus = false; + } + + if (!this.preserveFocus) { + super.requestFocus(); + } + } + + public void takeNextFocus() { + this.preserveFocus = false; + this.takeNextFocus = true; + } + + @Override + public boolean handleEvent(Event e) { + synchronized (hasFocusMutex) { + if (e.id == 1005) { + if (hasFocus == this) { + lostFocus = this; + hasFocus = null; + this.preserveFocus = false; + } + } else if (e.id == 1004) { + hasFocus = this; + if (this.preserveFocus && lostFocus != null) { + lostFocus.takeNextFocus(); + lostFocus.requestFocus(); + } + + lostFocus = null; + this.preserveFocus = false; + } + } + + if (e.id == 401) { + Console.wake(); + if (e.key == 27) { + this.setText(""); + return true; + } + + if ((e.modifiers & 2) != 0 && e.key >= 1 && e.key <= 26 && e.key != 22) { + Pilot pilot = Pilot.getActive(); + if (pilot != null) { + pilot.animate("abcdefghijklmnopqrstuvwxyz".substring(e.key - 1, e.key)); + } + + return true; + } + } + + return EventQueue.redirectDrivingKeys(e) ? true : super.handleEvent(e); + } +} |