X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTField.java;h=992f274fb388792c1ed76c5e33bcc6a4472cb6a8;hb=2b4274048c2f409b5eba8373ab3018aa75911c73;hp=c93cecbcbf97670babe1d07d26508498467a3030;hpb=1b1206070bb94d706c14400b8332a81646b77a25;p=fanfix.git diff --git a/src/jexer/TField.java b/src/jexer/TField.java index c93cecb..992f274 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,30 +39,19 @@ import static jexer.TKeypress.*; */ public class TField extends TWidget { - /** - * Field text. - */ - protected String text = ""; + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ /** - * Get field text. - * - * @return field text + * Background character for unfilled-in text. */ - public final String getText() { - return text; - } + protected char backgroundChar = GraphicsChars.HATCH; /** - * Set field text. - * - * @param text the new field text + * Field text. */ - public final void setText(String text) { - this.text = text; - position = 0; - windowStart = 0; - } + protected String text = ""; /** * If true, only allow enough characters that will fit in the width. If @@ -100,6 +89,20 @@ public class TField extends TWidget { */ protected TAction updateAction; + /** + * The color to use when this field is active. + */ + private String activeColorKey = "tfield.active"; + + /** + * The color to use when this field is not active. + */ + private String inactiveColorKey = "tfield.inactive"; + + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Public constructor. * @@ -157,6 +160,10 @@ public class TField extends TWidget { this.updateAction = updateAction; } + // ------------------------------------------------------------------------ + // Event handlers --------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Returns true if the mouse is currently on the field. * @@ -174,80 +181,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 +352,126 @@ public class TField extends TWidget { super.onKeypress(keypress); } + // ------------------------------------------------------------------------ + // TWidget ---------------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Draw the text field. + */ + @Override + public void draw() { + CellAttributes fieldColor; + + if (isAbsoluteActive()) { + fieldColor = getTheme().getColor(activeColorKey); + } else { + fieldColor = getTheme().getColor(inactiveColorKey); + } + + int end = windowStart + getWidth(); + if (end > text.length()) { + end = text.length(); + } + hLineXY(0, 0, getWidth(), backgroundChar, fieldColor); + putStringXY(0, 0, text.substring(windowStart, end), fieldColor); + + // Fix the cursor, it will be rendered by TApplication.drawAll(). + updateCursor(); + } + + // ------------------------------------------------------------------------ + // TField ----------------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Get field background character. + * + * @return background character + */ + public final char getBackgroundChar() { + return backgroundChar; + } + + /** + * Set field background character. + * + * @param backgroundChar the background character + */ + public void setBackgroundChar(final char backgroundChar) { + this.backgroundChar = backgroundChar; + } + + /** + * 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. * @@ -500,4 +553,42 @@ public class TField extends TWidget { normalizeWindowStart(); } + /** + * Set the active color key. + * + * @param activeColorKey ColorTheme key color to use when this field is + * active + */ + public void setActiveColorKey(final String activeColorKey) { + this.activeColorKey = activeColorKey; + } + + /** + * Set the inactive color key. + * + * @param inactiveColorKey ColorTheme key color to use when this field is + * inactive + */ + public void setInactiveColorKey(final String inactiveColorKey) { + this.inactiveColorKey = inactiveColorKey; + } + + /** + * Set the action to perform when the user presses enter. + * + * @param action the action to perform when the user presses enter + */ + public void setEnterAction(final TAction action) { + enterAction = action; + } + + /** + * Set the action to perform when the field is updated. + * + * @param action the action to perform when the field is updated + */ + public void setUpdateAction(final TAction action) { + updateAction = action; + } + }