Search in sources :

Example 31 with TextInput

use of org.apache.pivot.wtk.TextInput in project pivot by apache.

the class TextInputValidatorTest method startup.

@Override
public void startup(final Display display, final Map<String, String> properties) throws Exception {
    System.out.println("Starting TextInputValidatorTest ...");
    System.out.println("current Locale is " + locale);
    // sample different ways to format numbers in i18n compatible way
    NumberFormat nf = NumberFormat.getInstance();
    // 
    // String customDecimalPattern = ""###,###.###"";
    // DecimalFormat df = new DecimalFormat(customDecimalPattern);
    // 
    // StringBuffer sb = new StringBuffer();
    // Formatter formatter = new Formatter(sb, locale);
    // String customDecimalFormat = "%,.3f";
    // 
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    window = new Window((Component) bxmlSerializer.readObject(getClass().getResource("text_input_validator_test.bxml")));
    Map<String, Object> namespace = bxmlSerializer.getNamespace();
    textinputLocale = (TextInput) namespace.get("textinputLocale");
    textinputComparableBigDecimal = (TextInput) namespace.get("textinputComparableBigDecimal");
    textinputComparableRange = (TextInput) namespace.get("textinputComparableRange");
    textinputDouble = (TextInput) namespace.get("textinputDouble");
    textinputFloat = (TextInput) namespace.get("textinputFloat");
    textinputFloatRange = (TextInput) namespace.get("textinputFloatRange");
    textinputIntRange = (TextInput) namespace.get("textinputIntRange");
    textinputDateRegex = (TextInput) namespace.get("textinputDateRegex");
    textinputCustomBoolean = (TextInput) namespace.get("textinputCustomBoolean");
    textinputNotEmptyText = (TextInput) namespace.get("textinputNotEmptyText");
    textinputEmptyText = (TextInput) namespace.get("textinputEmptyText");
    textinputLocale.setText(locale.toString());
    String testValue = "123456789.0";
    // new, validate a value but using BigDecimalValidator (subclass of ComparableValidator)
    // huge value, and outside double range ...
    textinputComparableBigDecimal.setText("1e300");
    BigDecimalValidator bdComp = new BigDecimalValidator();
    System.out.println("BigDecimalValidator: created instance with value: " + bdComp);
    // enable auto-trim of input string, before validating
    bdComp.setAutoTrim(true);
    System.out.println("BigDecimalValidator: enable auto-trim of input string, before validating");
    textinputComparableBigDecimal.setValidator(bdComp);
    // new, validate in a range but using ComparableRangeValidator
    textinputComparableRange.setText(nf.format(new BigDecimal(testValue)));
    ComparableRangeValidator<BigDecimal> bdCompRange = new ComparableRangeValidator<>(new BigDecimal("2.0"), new BigDecimal("123456789"));
    System.out.println("ComparableRangeValidator: created instance with value: " + bdCompRange);
    // enable auto-trim of input string, before validating
    bdCompRange.setAutoTrim(true);
    System.out.println("ComparableRangeValidator: enable auto-trim of input string, before validating");
    textinputComparableRange.setValidator(bdCompRange);
    textinputComparableRange.getTextInputListeners().add(new TextInputListener() {

        @Override
        public void textValidChanged(final TextInput textInput) {
            invalidComparableRangeLabel.setText(validText(textInput));
        }
    });
    invalidComparableRangeLabel = (Label) namespace.get("invalidComparableRangeLabel");
    // positive infinity text
    textinputDouble.setText("+\u221E");
    textinputDouble.setValidator(new DoubleValidator());
    // textinputFloat.setText("123456.789");
    // new, show different ways to format decimal values in i18n format
    Double value = new Double(testValue);
    // textinputFloat.setText(value.toString());
    // textinputFloat.setText(String.format(customDecimalFormat, value)); // sample using String.format
    // formatter.format(customDecimalFormat, value); // sample using Formatter
    // textinputFloat.setText(sb.toString()); // sample using Formatter
    // textinputFloat.setText(nf.format(value)); // sample using NumberFormat
    // textinputFloat.setText(df.format(value)); // sample using DecimalFormat
    // using this as a sample
    textinputFloat.setText(nf.format(value));
    textinputFloat.setValidator(new FloatValidator());
    // standard float range model
    // note that float approximations could give errors,
    // try to increment/decrement the initial value near a range end, to see problems ...
    textinputFloatRange.setText(nf.format(new Float(testValue)));
    textinputFloatRange.setValidator(new FloatRangeValidator(2.0f, 123456789f));
    // test the listener by updating a label
    textinputFloatRange.getTextInputListeners().add(new TextInputListener() {

        @Override
        public void textValidChanged(final TextInput textInput) {
            invalidLabel.setText(validText(textInput));
        }
    });
    invalidLabel = (Label) namespace.get("invalidLabel");
    // standard int range model
    textinputIntRange.setText("0");
    textinputIntRange.setValidator(new IntRangeValidator(0, 100));
    // validate using a date regex.
    textinputDateRegex.setText("2009-09-01");
    textinputDateRegex.setValidator(new RegexTextValidator("(19|20)\\d\\d[- /.](0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])"));
    // creating a custom model that only accepts "true" or "false"
    textinputCustomBoolean.setText("true");
    textinputCustomBoolean.setValidator(new Validator() {

        @Override
        public boolean isValid(final String s) {
            return "true".equals(s) || "false".equals(s);
        }
    });
    // validate any not-empty text
    textinputNotEmptyText.setText("  Not Empty, and with spaces  ");
    textinputNotEmptyText.setValidator(new NotEmptyTextValidator());
    // validate any empty text, edge case
    textinputEmptyText.setText("    ");
    textinputEmptyText.setValidator(new EmptyTextValidator());
    window.setTitle("Text Input Validator Test");
    window.setMaximized(true);
    window.open(display);
}
Also used : Window(org.apache.pivot.wtk.Window) BigDecimalValidator(org.apache.pivot.wtk.validation.BigDecimalValidator) ComparableRangeValidator(org.apache.pivot.wtk.validation.ComparableRangeValidator) NotEmptyTextValidator(org.apache.pivot.wtk.validation.NotEmptyTextValidator) EmptyTextValidator(org.apache.pivot.wtk.validation.EmptyTextValidator) NotEmptyTextValidator(org.apache.pivot.wtk.validation.NotEmptyTextValidator) RegexTextValidator(org.apache.pivot.wtk.validation.RegexTextValidator) BigDecimal(java.math.BigDecimal) DoubleValidator(org.apache.pivot.wtk.validation.DoubleValidator) FloatRangeValidator(org.apache.pivot.wtk.validation.FloatRangeValidator) FloatValidator(org.apache.pivot.wtk.validation.FloatValidator) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput) IntRangeValidator(org.apache.pivot.wtk.validation.IntRangeValidator) TextInputListener(org.apache.pivot.wtk.TextInputListener) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) Validator(org.apache.pivot.wtk.validation.Validator) ComparableRangeValidator(org.apache.pivot.wtk.validation.ComparableRangeValidator) EmptyTextValidator(org.apache.pivot.wtk.validation.EmptyTextValidator) FloatValidator(org.apache.pivot.wtk.validation.FloatValidator) BigDecimalValidator(org.apache.pivot.wtk.validation.BigDecimalValidator) NotEmptyTextValidator(org.apache.pivot.wtk.validation.NotEmptyTextValidator) FloatRangeValidator(org.apache.pivot.wtk.validation.FloatRangeValidator) DoubleValidator(org.apache.pivot.wtk.validation.DoubleValidator) IntRangeValidator(org.apache.pivot.wtk.validation.IntRangeValidator) RegexTextValidator(org.apache.pivot.wtk.validation.RegexTextValidator) NumberFormat(java.text.NumberFormat)

Example 32 with TextInput

use of org.apache.pivot.wtk.TextInput in project pivot by apache.

the class MenuBarTest method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    BoxPane boxPane = new BoxPane(Orientation.VERTICAL);
    boxPane.add(new TextInput());
    boxPane.add(new TextInput());
    boxPane.add(new TextInput());
    frame1 = new Frame(boxPane);
    frame1.setLocation(50, 50);
    frame1.setPreferredSize(320, 240);
    frame1.setTitle("Frame 1");
    // put this before loading the related bxml, or an
    // IllegalArgumentException will be thrown
    Action.getNamedActions().put("about", new Action() {

        @Override
        public void perform(Component source) {
            String msg = "Hello from Pivot-" + ApplicationContext.getPivotVersion().toString() + ", running from Java " + // + ApplicationContext.getJVMVersion().toString()
            System.getProperty("java.version");
            // frame2);
            Alert.alert(msg, frame2.getRootOwner());
            System.out.println("Help triggered");
        }
    });
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    frame2 = (Frame) bxmlSerializer.readObject(MenuBarTest.class, "menu_bar_test.bxml");
    frame2.setTitle("Frame 2, from bxml");
    bxmlSerializer.bind(this, MenuBarTest.class);
    MenuHandler menuHandler = new MenuHandler() {

        @Override
        public void configureMenuBar(Component component, MenuBar menuBar) {
            System.out.println("Configure menu bar: got focus on " + component.getName());
        }

        @Override
        public void cleanupMenuBar(Component component, MenuBar menuBar) {
            System.out.println("Clean up menu bar: lost focus on " + component.getName());
        }
    };
    textInput1.setMenuHandler(menuHandler);
    textInput2.setMenuHandler(menuHandler);
    textInput3.setMenuHandler(menuHandler);
    frame1.open(display);
    frame2.open(display);
}
Also used : Frame(org.apache.pivot.wtk.Frame) Action(org.apache.pivot.wtk.Action) MenuHandler(org.apache.pivot.wtk.MenuHandler) BoxPane(org.apache.pivot.wtk.BoxPane) MenuBar(org.apache.pivot.wtk.MenuBar) TextInput(org.apache.pivot.wtk.TextInput) Component(org.apache.pivot.wtk.Component) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 33 with TextInput

use of org.apache.pivot.wtk.TextInput in project pivot by apache.

the class TableViewRowEditor method beginEdit.

@Override
public void beginEdit(TableView tableViewArgument, int rowIndexArgument, int columnIndexArgument) {
    this.tableView = tableViewArgument;
    this.rowIndex = rowIndexArgument;
    this.columnIndex = columnIndexArgument;
    Container tableViewParent = tableViewArgument.getParent();
    tableViewScrollPane = (tableViewParent instanceof ScrollPane) ? (ScrollPane) tableViewParent : null;
    // Add/create the editor components
    TableView.ColumnSequence tableViewColumns = tableViewArgument.getColumns();
    TablePane.ColumnSequence tablePaneColumns = tablePane.getColumns();
    for (int i = 0, n = tableViewColumns.getLength(); i < n; i++) {
        // Add a new column to the table pane to match the table view column
        TablePane.Column tablePaneColumn = new TablePane.Column();
        tablePaneColumn.setWidth(tableViewArgument.getColumnBounds(i).width);
        tablePaneColumns.add(tablePaneColumn);
        // Determine which component to use as the editor for this column
        String columnName = tableViewColumns.get(i).getName();
        Component editorComponent = null;
        if (columnName != null) {
            editorComponent = cellEditors.get(columnName);
        }
        // Default to a read-only text input editor
        if (editorComponent == null) {
            TextInput editorTextInput = new TextInput();
            editorTextInput.setTextKey(columnName);
            editorTextInput.setEnabled(false);
            editorTextInput.setTextBindType(BindType.LOAD);
            editorComponent = editorTextInput;
        }
        // Add the editor component to the table pane
        editorRow.add(editorComponent);
    }
    // Get the data being edited
    List<?> tableData = tableViewArgument.getTableData();
    Object tableRow = tableData.get(rowIndexArgument);
    // Load the row data into the editor components
    tablePane.load(tableRow);
    // Get the row bounds
    Bounds rowBounds = tableViewArgument.getRowBounds(rowIndexArgument);
    rowImage.bounds = rowBounds;
    // Scroll to make the row as visible as possible
    tableViewArgument.scrollAreaToVisible(rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height);
    // Constrain the bounds by what is visible through viewport ancestors
    rowBounds = tableViewArgument.getVisibleArea(rowBounds);
    Point location = tableViewArgument.mapPointToAncestor(tableViewArgument.getDisplay(), rowBounds.x, rowBounds.y);
    // Set size and location and match scroll left
    setPreferredWidth(rowBounds.width);
    setLocation(location.x, location.y + (rowBounds.height - getPreferredHeight(-1)) / 2);
    if (tableViewScrollPane != null) {
        scrollPane.setScrollLeft(tableViewScrollPane.getScrollLeft());
    }
    // Open the editor
    open(tableViewArgument.getWindow());
    // Start the transition
    cardPane.setSelectedIndex(EDITOR_CARD_INDEX);
}
Also used : Bounds(org.apache.pivot.wtk.Bounds) Point(org.apache.pivot.wtk.Point) Point(org.apache.pivot.wtk.Point) Container(org.apache.pivot.wtk.Container) ScrollPane(org.apache.pivot.wtk.ScrollPane) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput) TableView(org.apache.pivot.wtk.TableView) TablePane(org.apache.pivot.wtk.TablePane)

Example 34 with TextInput

use of org.apache.pivot.wtk.TextInput in project pivot by apache.

the class TerraTextInputSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    TextInput textInput = (TextInput) component;
    textInput.getTextInputListeners().add(this);
    textInput.getTextInputContentListeners().add(this);
    textInput.getTextInputSelectionListeners().add(this);
    textInput.setCursor(Cursor.TEXT);
    updateSelection();
}
Also used : TextInput(org.apache.pivot.wtk.TextInput)

Example 35 with TextInput

use of org.apache.pivot.wtk.TextInput in project pivot by apache.

the class TerraTextInputSkin method mouseDown.

@Override
public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
    boolean consumed = super.mouseDown(component, button, x, y);
    if (button == Mouse.Button.LEFT) {
        TextInput textInput = (TextInput) getComponent();
        anchor = getInsertionPoint(x);
        // TODO: this logic won't work in the presence of composed (but not yet committed) text...
        if (anchor != -1) {
            if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                // Select the range
                int selectionStart = textInput.getSelectionStart();
                if (anchor > selectionStart) {
                    textInput.setSelection(selectionStart, anchor - selectionStart);
                } else {
                    textInput.setSelection(anchor, selectionStart - anchor);
                }
            } else {
                // Move the caret to the insertion point
                textInput.setSelection(anchor, 0);
                selectDirection = null;
                consumed = true;
            }
        }
        // Set focus to the text input
        textInput.requestFocus();
    }
    return consumed;
}
Also used : TextInput(org.apache.pivot.wtk.TextInput)

Aggregations

TextInput (org.apache.pivot.wtk.TextInput)51 Component (org.apache.pivot.wtk.Component)21 BoxPane (org.apache.pivot.wtk.BoxPane)15 ComponentStateListener (org.apache.pivot.wtk.ComponentStateListener)11 TextInputContentListener (org.apache.pivot.wtk.TextInputContentListener)9 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)8 Label (org.apache.pivot.wtk.Label)8 Point (org.apache.pivot.wtk.Point)8 IntValidator (org.apache.pivot.wtk.validation.IntValidator)8 FlowPane (org.apache.pivot.wtk.FlowPane)7 ArrayList (org.apache.pivot.collections.ArrayList)6 IOException (java.io.IOException)5 Button (org.apache.pivot.wtk.Button)5 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)5 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)5 Keyboard (org.apache.pivot.wtk.Keyboard)5 Mouse (org.apache.pivot.wtk.Mouse)5 Sequence (org.apache.pivot.collections.Sequence)4 SerializationException (org.apache.pivot.serialization.SerializationException)4 ComponentKeyListener (org.apache.pivot.wtk.ComponentKeyListener)4