X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTField.java;h=b4330b4cb3c809210c4abf07b6592b0ac69bbd20;hb=2a92cf977ee2ae37d8302a294c6338fa51a5ca45;hp=c93cecbcbf97670babe1d07d26508498467a3030;hpb=1b1206070bb94d706c14400b8332a81646b77a25;p=fanfix.git diff --git a/src/jexer/TField.java b/src/jexer/TField.java index c93cecb..b4330b4 100644 --- a/src/jexer/TField.java +++ b/src/jexer/TField.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2017 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -39,31 +39,15 @@ import static jexer.TKeypress.*; */ public class TField extends TWidget { + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Field text. */ protected String text = ""; - /** - * Get field text. - * - * @return field text - */ - public final String getText() { - return text; - } - - /** - * Set field text. - * - * @param text the new field text - */ - public final void setText(String text) { - this.text = text; - position = 0; - windowStart = 0; - } - /** * If true, only allow enough characters that will fit in the width. If * false, allow the field to scroll to the right. @@ -100,6 +84,10 @@ public class TField extends TWidget { */ protected TAction updateAction; + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Public constructor. * @@ -157,6 +145,10 @@ public class TField extends TWidget { this.updateAction = updateAction; } + // ------------------------------------------------------------------------ + // Event handlers --------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Returns true if the mouse is currently on the field. * @@ -174,80 +166,6 @@ public class TField extends TWidget { return false; } - /** - * Dispatch to the action function. - * - * @param enter if true, the user pressed Enter, else this was an update - * to the text. - */ - protected void dispatch(final boolean enter) { - if (enter) { - if (enterAction != null) { - enterAction.DO(); - } - } else { - if (updateAction != null) { - updateAction.DO(); - } - } - } - - /** - * Draw the text field. - */ - @Override - public void draw() { - CellAttributes fieldColor; - - if (isAbsoluteActive()) { - fieldColor = getTheme().getColor("tfield.active"); - } else { - fieldColor = getTheme().getColor("tfield.inactive"); - } - - int end = windowStart + getWidth(); - if (end > text.length()) { - end = text.length(); - } - getScreen().hLineXY(0, 0, getWidth(), GraphicsChars.HATCH, fieldColor); - getScreen().putStringXY(0, 0, text.substring(windowStart, end), - fieldColor); - - // Fix the cursor, it will be rendered by TApplication.drawAll(). - updateCursor(); - } - - /** - * Update the visible cursor position to match the location of position - * and windowStart. - */ - protected void updateCursor() { - if ((position > getWidth()) && fixed) { - setCursorX(getWidth()); - } else if ((position - windowStart == getWidth()) && !fixed) { - setCursorX(getWidth() - 1); - } else { - setCursorX(position - windowStart); - } - } - - /** - * Normalize windowStart such that most of the field data if visible. - */ - protected void normalizeWindowStart() { - if (fixed) { - // windowStart had better be zero, there is nothing to do here. - assert (windowStart == 0); - return; - } - windowStart = position - (getWidth() - 1); - if (windowStart < 0) { - windowStart = 0; - } - - updateCursor(); - } - /** * Handle mouse button presses. * @@ -419,6 +337,108 @@ public class TField extends TWidget { super.onKeypress(keypress); } + // ------------------------------------------------------------------------ + // TWidget ---------------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Draw the text field. + */ + @Override + public void draw() { + CellAttributes fieldColor; + + if (isAbsoluteActive()) { + fieldColor = getTheme().getColor("tfield.active"); + } else { + fieldColor = getTheme().getColor("tfield.inactive"); + } + + int end = windowStart + getWidth(); + if (end > text.length()) { + end = text.length(); + } + hLineXY(0, 0, getWidth(), GraphicsChars.HATCH, fieldColor); + putStringXY(0, 0, text.substring(windowStart, end), fieldColor); + + // Fix the cursor, it will be rendered by TApplication.drawAll(). + updateCursor(); + } + + // ------------------------------------------------------------------------ + // TField ----------------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Get field text. + * + * @return field text + */ + public final String getText() { + return text; + } + + /** + * Set field text. + * + * @param text the new field text + */ + public void setText(final String text) { + assert (text != null); + this.text = text; + position = 0; + windowStart = 0; + } + + /** + * Dispatch to the action function. + * + * @param enter if true, the user pressed Enter, else this was an update + * to the text. + */ + protected void dispatch(final boolean enter) { + if (enter) { + if (enterAction != null) { + enterAction.DO(); + } + } else { + if (updateAction != null) { + updateAction.DO(); + } + } + } + + /** + * Update the visible cursor position to match the location of position + * and windowStart. + */ + protected void updateCursor() { + if ((position > getWidth()) && fixed) { + setCursorX(getWidth()); + } else if ((position - windowStart == getWidth()) && !fixed) { + setCursorX(getWidth() - 1); + } else { + setCursorX(position - windowStart); + } + } + + /** + * Normalize windowStart such that most of the field data if visible. + */ + protected void normalizeWindowStart() { + if (fixed) { + // windowStart had better be zero, there is nothing to do here. + assert (windowStart == 0); + return; + } + windowStart = position - (getWidth() - 1); + if (windowStart < 0) { + windowStart = 0; + } + + updateCursor(); + } + /** * Append char to the end of the field. *