From: Kevin Lamonte Date: Wed, 30 Oct 2019 23:58:06 +0000 (-0500) Subject: #63 add to radiobutton API X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=b2efac6ed4525f77cbcb717a082d3a2884c91936;p=nikiroo-utils.git #63 add to radiobutton API --- diff --git a/build.xml b/build.xml index 613ea03..6f0c1ab 100644 --- a/build.xml +++ b/build.xml @@ -51,7 +51,9 @@ debuglevel="lines,vars,source" target="1.6" source="1.6" - /> + > + + diff --git a/src/jexer/TRadioButton.java b/src/jexer/TRadioButton.java index 60a6288..dcc5c13 100644 --- a/src/jexer/TRadioButton.java +++ b/src/jexer/TRadioButton.java @@ -50,9 +50,9 @@ public class TRadioButton extends TWidget { // ------------------------------------------------------------------------ /** - * RadioButton state, true means selected. + * RadioButton state, true means selected. Note package private access. */ - private boolean selected = false; + boolean selected = false; /** * The shortcut and radio button label. @@ -61,16 +61,16 @@ public class TRadioButton extends TWidget { /** * ID for this radio button. Buttons start counting at 1 in the - * RadioGroup. + * RadioGroup. Note package private access. */ - private int id; + int id; // ------------------------------------------------------------------------ // Constructors ----------------------------------------------------------- // ------------------------------------------------------------------------ /** - * Public constructor. + * Package private constructor. * * @param parent parent widget * @param x column relative to parent @@ -78,7 +78,7 @@ public class TRadioButton extends TWidget { * @param label label to display next to (right of) the radiobutton * @param id ID for this radio button */ - public TRadioButton(final TRadioGroup parent, final int x, final int y, + TRadioButton(final TRadioGroup parent, final int x, final int y, final String label, final int id) { // Set parent and window @@ -89,6 +89,8 @@ public class TRadioButton extends TWidget { setCursorVisible(true); setCursorX(1); + + parent.addRadioButton(this); } // ------------------------------------------------------------------------ @@ -120,8 +122,7 @@ public class TRadioButton extends TWidget { public void onMouseDown(final TMouseEvent mouse) { if ((mouseOnRadioButton(mouse)) && (mouse.isMouse1())) { // Switch state - selected = true; - ((TRadioGroup) getParent()).setSelected(this); + ((TRadioGroup) getParent()).setSelected(id); } } @@ -134,8 +135,7 @@ public class TRadioButton extends TWidget { public void onKeypress(final TKeypressEvent keypress) { if (keypress.equals(kbSpace)) { - selected = true; - ((TRadioGroup) getParent()).setSelected(this); + ((TRadioGroup) getParent()).setSelected(id); return; } @@ -222,14 +222,17 @@ public class TRadioButton extends TWidget { } /** - * Set RadioButton state, true means selected. Note package private - * access. + * Set RadioButton state, true means selected. * * @param selected if true then this is the one button in the group that * is selected */ - void setSelected(final boolean selected) { - this.selected = selected; + public void setSelected(final boolean selected) { + if (selected == true) { + ((TRadioGroup) getParent()).setSelected(id); + } else { + ((TRadioGroup) getParent()).setSelected(0); + } } /** diff --git a/src/jexer/TRadioGroup.java b/src/jexer/TRadioGroup.java index a82b074..7f8c99c 100644 --- a/src/jexer/TRadioGroup.java +++ b/src/jexer/TRadioGroup.java @@ -54,7 +54,7 @@ public class TRadioGroup extends TWidget { * If true, one of the children MUST be selected. Note package private * access. */ - boolean requiresSelection = true; + boolean requiresSelection = false; // ------------------------------------------------------------------------ // Constructors ----------------------------------------------------------- @@ -138,19 +138,6 @@ public class TRadioGroup extends TWidget { return selectedButton.getId(); } - /** - * Set the new selected radio button. Note package private access. - * - * @param button new button that became selected - */ - void setSelected(final TRadioButton button) { - assert (button.isSelected()); - if ((selectedButton != null) && (selectedButton != button)) { - selectedButton.setSelected(false); - } - selectedButton = button; - } - /** * Set the new selected radio button. 1-based. * @@ -161,19 +148,43 @@ public class TRadioGroup extends TWidget { return; } + for (TWidget widget: getChildren()) { + ((TRadioButton) widget).selected = false; + } if (id == 0) { - for (TWidget widget: getChildren()) { - ((TRadioButton) widget).setSelected(false); - } selectedButton = null; return; } assert ((id > 0) && (id <= getChildren().size())); TRadioButton button = (TRadioButton) (getChildren().get(id - 1)); - button.setSelected(true); + button.selected = true; selectedButton = button; } + /** + * Get the radio button that was selected. + * + * @return the selected button, or null if no button is selected + */ + public TRadioButton getSelectedButton() { + return selectedButton; + } + + /** + * Convenience function to add a radio button to this group. + * + * @param label label to display next to (right of) the radiobutton + * @param selected if true, this will be the selected radiobutton + * @return the new radio button + */ + public TRadioButton addRadioButton(final String label, + final boolean selected) { + + TRadioButton button = addRadioButton(label); + setSelected(button.id); + return button; + } + /** * Convenience function to add a radio button to this group. * @@ -181,14 +192,25 @@ public class TRadioGroup extends TWidget { * @return the new radio button */ public TRadioButton addRadioButton(final String label) { - int buttonX = 1; - int buttonY = getChildren().size() + 1; + return new TRadioButton(this, 0, 0, label, 0); + } + + /** + * Package private method for RadioButton to add itself to a RadioGroup + * container. + * + * @param button the button to add + */ + void addRadioButton(final TRadioButton button) { + super.setHeight(getChildren().size() + 2); + button.setX(1); + button.setY(getChildren().size()); + button.id = getChildren().size(); + String label = button.getMnemonic().getRawLabel(); + if (StringUtils.width(label) + 4 > getWidth()) { super.setWidth(StringUtils.width(label) + 7); } - super.setHeight(getChildren().size() + 3); - TRadioButton button = new TRadioButton(this, buttonX, buttonY, label, - getChildren().size() + 1); if (getParent().getLayoutManager() != null) { getParent().getLayoutManager().resetSize(this); @@ -196,8 +218,31 @@ public class TRadioGroup extends TWidget { // Default to the first item on the list. activate(getChildren().get(0)); + } - return button; + /** + * Get the requires selection flag. + * + * @return true if this radiogroup requires that one of the buttons be + * selected + */ + public boolean getRequiresSelection() { + return requiresSelection; + } + + /** + * Set the requires selection flag. + * + * @param requiresSelection if true, then this radiogroup requires that + * one of the buttons be selected + */ + public void setRequiresSelection(final boolean requiresSelection) { + this.requiresSelection = requiresSelection; + if (requiresSelection) { + if (getChildren().size() > 0) { + setSelected(1); + } + } } } diff --git a/src/jexer/demos/DemoCheckBoxWindow.java b/src/jexer/demos/DemoCheckBoxWindow.java index fda7bd7..961138d 100644 --- a/src/jexer/demos/DemoCheckBoxWindow.java +++ b/src/jexer/demos/DemoCheckBoxWindow.java @@ -103,7 +103,7 @@ public class DemoCheckBoxWindow extends TWindow { TRadioGroup group = addRadioGroup(1, row, i18n.getString("radioGroupTitle")); group.addRadioButton(i18n.getString("radioOption1")); - group.addRadioButton(i18n.getString("radioOption2")); + group.addRadioButton(i18n.getString("radioOption2"), true); group.addRadioButton(i18n.getString("radioOption3")); List comboValues = new ArrayList(); diff --git a/src/jexer/teditor/Document.java b/src/jexer/teditor/Document.java index 2abfef6..ffbe081 100644 --- a/src/jexer/teditor/Document.java +++ b/src/jexer/teditor/Document.java @@ -362,7 +362,7 @@ public class Document { // If at the beginning of a word already, push past it. if ((getChar() != -1) && (getRawLine().length() > 0) - && !Character.isSpace((char) getChar()) + && !Character.isWhitespace((char) getChar()) ) { left(); } @@ -370,7 +370,7 @@ public class Document { // int line = lineNumber; while ((getChar() == -1) || (getRawLine().length() == 0) - || Character.isSpace((char) getChar()) + || Character.isWhitespace((char) getChar()) ) { if (left() == false) { return; @@ -380,12 +380,12 @@ public class Document { assert (getChar() != -1); - if (!Character.isSpace((char) getChar()) + if (!Character.isWhitespace((char) getChar()) && (getRawLine().length() > 0) ) { // Advance until at the beginning of the document or a whitespace // is encountered. - while (!Character.isSpace((char) getChar())) { + while (!Character.isWhitespace((char) getChar())) { int line = lineNumber; if (left() == false) { // End of document, bail out. @@ -418,7 +418,7 @@ public class Document { } if (lineNumber != line) { // We wrapped a line. Here that counts as whitespace. - if (!Character.isSpace((char) getChar())) { + if (!Character.isWhitespace((char) getChar())) { // We found a character immediately after the line. // Done! return; @@ -429,12 +429,12 @@ public class Document { } assert (getChar() != -1); - if (!Character.isSpace((char) getChar()) + if (!Character.isWhitespace((char) getChar()) && (getRawLine().length() > 0) ) { // Advance until at the end of the document or a whitespace is // encountered. - while (!Character.isSpace((char) getChar())) { + while (!Character.isWhitespace((char) getChar())) { line = lineNumber; if (right() == false) { // End of document, bail out. @@ -442,7 +442,7 @@ public class Document { } if (lineNumber != line) { // We wrapped a line. Here that counts as whitespace. - if (!Character.isSpace((char) getChar()) + if (!Character.isWhitespace((char) getChar()) && (getRawLine().length() > 0) ) { // We found a character immediately after the line. @@ -462,7 +462,7 @@ public class Document { } if (lineNumber != line) { // We wrapped a line. Here that counts as whitespace. - if (!Character.isSpace((char) getChar())) { + if (!Character.isWhitespace((char) getChar())) { // We found a character immediately after the line. // Done! return; @@ -473,10 +473,10 @@ public class Document { } assert (getChar() != -1); - if (Character.isSpace((char) getChar())) { + if (Character.isWhitespace((char) getChar())) { // Advance until at the end of the document or a non-whitespace // is encountered. - while (Character.isSpace((char) getChar())) { + while (Character.isWhitespace((char) getChar())) { if (right() == false) { // End of document, bail out. return;