summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/textfield/SelectionAndCursorPosition.java
blob: d63d7ddc6654cd7e89e02b3a1e13eb55d8fa61da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.vaadin.tests.components.textfield;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.AbstractTextField;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

public class SelectionAndCursorPosition extends TestBase {

    TextField textField = createTextField();
    TextArea textArea = createTextArea();
    AbstractTextField activeComponent = textField;

    @Override
    protected void setup() {
        FormLayout fl = new FormLayout();
        Panel panel = new Panel(fl);
        panel.setCaption("Hackers panel");
        CheckBox ml = new CheckBox("Multiline");
        ml.setImmediate(true);
        ml.addListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
                if (textField.getUI() == null
                        || textField.getUI().getSession() == null) {
                    replaceComponent(textArea, textField);
                    activeComponent = textField;
                } else {
                    replaceComponent(textField, textArea);
                    activeComponent = textArea;
                }
            }
        });
        fl.addComponent(ml);

        Button b = new Button("Select all ( selectAll() )");
        b.addListener(new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                activeComponent.selectAll();
            }
        });
        fl.addComponent(b);

        HorizontalLayout selectRange = new HorizontalLayout();
        selectRange
                .setCaption("Select range of text ( setSelectionRange(int start, int lengt) )");
        final TextField start = new TextField("From:");
        final TextField length = new TextField("Selection length:");
        b = new Button("select");
        b.addListener(new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                int startPos = Integer.parseInt(start.getValue());
                int lenght = Integer.parseInt(length.getValue());

                activeComponent.setSelectionRange(startPos, lenght);
            }
        });

        selectRange.addComponent(start);
        selectRange.addComponent(length);
        selectRange.addComponent(b);
        fl.addComponent(selectRange);

        HorizontalLayout setCursorPosition = new HorizontalLayout();
        final TextField pos = new TextField("Position:");
        b = new Button("set");
        b.addListener(new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                int startPos = Integer.parseInt(pos.getValue());
                activeComponent.setCursorPosition(startPos);
            }
        });

        setCursorPosition.addComponent(pos);
        setCursorPosition.addComponent(b);
        setCursorPosition
                .setCaption("Set cursor position ( setCursorPosition(int pos) )");
        fl.addComponent(setCursorPosition);

        getLayout().addComponent(textField);
        getLayout().addComponent(panel);

    }

    private static TextField createTextField() {
        TextField tf = new TextField();
        tf.setCaption("Text field");
        tf.setValue("So we have some text to select");
        tf.setWidth("400px");

        return tf;
    }

    private static TextArea createTextArea() {
        TextArea ta = new TextArea();
        ta.setCaption("Text area");
        ta.setValue("So we have some text to select");
        ta.setWidth("400px");
        ta.setHeight("50px");

        return ta;
    }

    @Override
    protected String getDescription() {
        return "For usability reasons it is often essential that developer "
                + "can hint how to select the text in the "
                + "field or where to set the cursor position.";
    }

    @Override
    protected Integer getTicketNumber() {
        return 2058;
    }

}