Search in sources :

Example 1 with Limits

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

the class ComponentInspectorSkin method addLimitsControl.

private static Component addLimitsControl(final Dictionary<String, Object> dictionary, final String key, Form.Section section) {
    Limits limits = (Limits) dictionary.get(key);
    BoxPane boxPane = new BoxPane(Orientation.VERTICAL);
    section.add(boxPane);
    Form.setLabel(boxPane, key);
    FlowPane flowPane = new FlowPane();
    flowPane.getStyles().put(Style.alignToBaseline, true);
    flowPane.getStyles().put(Style.horizontalSpacing, 5);
    boxPane.add(flowPane);
    TextInput textInput = new TextInput();
    textInput.setTextSize(10);
    textInput.setMaximumLength(10);
    textInput.setValidator(new IntValidator());
    textInput.setText(String.valueOf(limits.minimum));
    flowPane.add(textInput);
    textInput.getComponentStateListeners().add(new ComponentStateListener() {

        @Override
        public void focusedChanged(Component component, Component obverseComponent) {
            if (!component.isFocused()) {
                TextInput textInputLocal = (TextInput) component;
                Limits limitsLocal = (Limits) dictionary.get(key);
                try {
                    int min = Integer.parseInt(textInputLocal.getText());
                    dictionary.put(key, new Limits(min, limitsLocal.maximum));
                } catch (Exception exception) {
                    displayErrorMessage(exception, component.getWindow());
                    textInputLocal.setText(String.valueOf(limitsLocal.minimum));
                }
            }
        }
    });
    Label label = new Label("min");
    label.getStyles().put(Style.font, "{italic:true}");
    flowPane.add(label);
    flowPane = new FlowPane();
    flowPane.getStyles().put(Style.alignToBaseline, true);
    flowPane.getStyles().put(Style.horizontalSpacing, 5);
    boxPane.add(flowPane);
    textInput = new TextInput();
    textInput.setTextSize(10);
    textInput.setMaximumLength(10);
    textInput.setValidator(new IntValidator());
    textInput.setText(String.valueOf(limits.maximum));
    flowPane.add(textInput);
    textInput.getComponentStateListeners().add(new ComponentStateListener() {

        @Override
        public void focusedChanged(Component component, Component obverseComponent) {
            if (!component.isFocused()) {
                TextInput textInputLocal = (TextInput) component;
                Limits limitsLocal = (Limits) dictionary.get(key);
                try {
                    int max = Integer.parseInt(textInputLocal.getText());
                    dictionary.put(key, new Limits(limitsLocal.minimum, max));
                } catch (Exception exception) {
                    displayErrorMessage(exception, component.getWindow());
                    textInputLocal.setText(String.valueOf(limitsLocal.maximum));
                }
            }
        }
    });
    label = new Label("max");
    label.getStyles().put(Style.font, "{italic:true}");
    flowPane.add(label);
    return boxPane;
}
Also used : Limits(org.apache.pivot.wtk.Limits) IntValidator(org.apache.pivot.wtk.validation.IntValidator) BoxPane(org.apache.pivot.wtk.BoxPane) Label(org.apache.pivot.wtk.Label) FlowPane(org.apache.pivot.wtk.FlowPane) TextInput(org.apache.pivot.wtk.TextInput) Component(org.apache.pivot.wtk.Component) ComponentStateListener(org.apache.pivot.wtk.ComponentStateListener)

Example 2 with Limits

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

the class ComponentInspectorSkin method updateLimitsControl.

private void updateLimitsControl(Dictionary<String, Object> dictionary, String key) {
    BoxPane boxPane = (BoxPane) controls.get(key);
    if (boxPane != null) {
        Limits limits = (Limits) dictionary.get(key);
        TextInput minTextInput = (TextInput) ((FlowPane) boxPane.get(0)).get(0);
        TextInput maxTextInput = (TextInput) ((FlowPane) boxPane.get(1)).get(0);
        minTextInput.setText(String.valueOf(limits.minimum));
        maxTextInput.setText(String.valueOf(limits.maximum));
    }
}
Also used : Limits(org.apache.pivot.wtk.Limits) BoxPane(org.apache.pivot.wtk.BoxPane) TextInput(org.apache.pivot.wtk.TextInput)

Example 3 with Limits

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

the class LimitsTest method test.

@Test
public void test() {
    Limits lmMinus1 = new Limits(-1, 0);
    Limits lm0 = new Limits(0);
    Limits lm1 = new Limits(0, 1);
    Limits lm2 = Limits.decode("[0, 1]");
    Limits lm3 = Limits.decode("{minimum:2, maximum:3}");
    Limits lm3a = new Limits(2, 3);
    Limits lm4 = new Limits(4);
    Limits lm5 = new Limits(3, 4);
    Limits lm5a = new Limits(3, 4);
    Limits lm5b = Limits.decode("[3, 4]");
    Limits lmN = new Limits(0, 4);
    Limits lm6 = Limits.decode("5; 6");
    Limits lm6a = new Limits(5, 6);
    Limits lm7 = Limits.decode("9 - 10");
    Limits lm7a = new Limits(9, 10);
    assertEquals(lmMinus1.range(), 2);
    assertEquals(lm0.range(), 1);
    assertTrue(lm1.contains(0));
    assertTrue(lm1.contains(1));
    assertFalse(lm1.contains(2));
    assertFalse(lm0.equals(lm1));
    assertTrue(lm1.equals(lm2));
    assertEquals(lm3, lm3a);
    assertTrue(lm5.equals(lm5a));
    assertEquals(lm2.minimum, 0);
    assertEquals(lm2.maximum, 1);
    assertTrue(lm5a.equals(lm5b));
    assertEquals(lmN.constrain(5), 4);
    assertEquals(lmN.constrain(-2), 0);
    assertEquals(lm6, lm6a);
    assertEquals(lm6.toString(), "Limits [5-6]");
    assertEquals(lm7, lm7a);
    assertEquals(lm7.toString(), "Limits [9-10]");
}
Also used : Limits(org.apache.pivot.wtk.Limits) Test(org.junit.Test)

Aggregations

Limits (org.apache.pivot.wtk.Limits)3 BoxPane (org.apache.pivot.wtk.BoxPane)2 TextInput (org.apache.pivot.wtk.TextInput)2 Component (org.apache.pivot.wtk.Component)1 ComponentStateListener (org.apache.pivot.wtk.ComponentStateListener)1 FlowPane (org.apache.pivot.wtk.FlowPane)1 Label (org.apache.pivot.wtk.Label)1 IntValidator (org.apache.pivot.wtk.validation.IntValidator)1 Test (org.junit.Test)1