X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTField.java;h=90dd4e427abcf5660bfcc04b32b76c7c5da8edf7;hb=HEAD;hp=6aaf5912497659bfa3b3b07fc0ff1e64743ba0ad;hpb=ebb9a1e491ed62be0a1f0d9e5b45fe856d849e07;p=fanfix.git diff --git a/src/jexer/TField.java b/src/jexer/TField.java index 6aaf591..90dd4e4 100644 --- a/src/jexer/TField.java +++ b/src/jexer/TField.java @@ -31,14 +31,16 @@ package jexer; import jexer.bits.CellAttributes; import jexer.bits.GraphicsChars; import jexer.bits.StringUtils; +import jexer.event.TCommandEvent; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; +import static jexer.TCommand.*; import static jexer.TKeypress.*; /** * TField implements an editable text field. */ -public class TField extends TWidget { +public class TField extends TWidget implements EditMenuUser { // ------------------------------------------------------------------------ // Variables -------------------------------------------------------------- @@ -234,12 +236,13 @@ public class TField extends TWidget { if (keypress.equals(kbRight)) { if (position < text.length()) { + int lastPosition = position; screenPosition += StringUtils.width(text.codePointAt(position)); position += Character.charCount(text.codePointAt(position)); if (fixed == true) { if (screenPosition == getWidth()) { screenPosition--; - position -= Character.charCount(text.codePointAt(position)); + position -= Character.charCount(text.codePointAt(lastPosition)); } } else { while ((screenPosition - windowStart + @@ -374,10 +377,58 @@ public class TField extends TWidget { super.onKeypress(keypress); } + /** + * Handle posted command events. + * + * @param command command event + */ + @Override + public void onCommand(final TCommandEvent command) { + if (command.equals(cmCut)) { + // Copy text to clipboard, and then remove it. + getClipboard().copyText(text); + setText(""); + return; + } + + if (command.equals(cmCopy)) { + // Copy text to clipboard. + getClipboard().copyText(text); + return; + } + + if (command.equals(cmPaste)) { + // Paste text from clipboard. + String newText = getClipboard().pasteText(); + if (newText != null) { + setText(newText); + } + return; + } + + if (command.equals(cmClear)) { + // Remove text. + setText(""); + return; + } + + } + // ------------------------------------------------------------------------ // TWidget ---------------------------------------------------------------- // ------------------------------------------------------------------------ + /** + * Override TWidget's height: we can only set height at construction + * time. + * + * @param height new widget height (ignored) + */ + @Override + public void setHeight(final int height) { + // Do nothing + } + /** * Draw the text field. */ @@ -456,7 +507,11 @@ public class TField extends TWidget { assert (text != null); this.text = text; position = 0; + screenPosition = 0; windowStart = 0; + if ((fixed == true) && (this.text.length() > getWidth())) { + this.text = this.text.substring(0, getWidth()); + } } /** @@ -468,11 +523,11 @@ public class TField extends TWidget { protected void dispatch(final boolean enter) { if (enter) { if (enterAction != null) { - enterAction.DO(); + enterAction.DO(this); } } else { if (updateAction != null) { - updateAction.DO(); + updateAction.DO(this); } } } @@ -657,4 +712,44 @@ public class TField extends TWidget { updateAction = action; } + // ------------------------------------------------------------------------ + // EditMenuUser ----------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Check if the cut menu item should be enabled. + * + * @return true if the cut menu item should be enabled + */ + public boolean isEditMenuCut() { + return true; + } + + /** + * Check if the copy menu item should be enabled. + * + * @return true if the copy menu item should be enabled + */ + public boolean isEditMenuCopy() { + return true; + } + + /** + * Check if the paste menu item should be enabled. + * + * @return true if the paste menu item should be enabled + */ + public boolean isEditMenuPaste() { + return true; + } + + /** + * Check if the clear menu item should be enabled. + * + * @return true if the clear menu item should be enabled + */ + public boolean isEditMenuClear() { + return true; + } + }