You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SelectionAndCursorPosition.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.vaadin.tests.components.textfield;
  2. import com.vaadin.server.VaadinRequest;
  3. import com.vaadin.tests.components.AbstractReindeerTestUI;
  4. import com.vaadin.ui.AbstractTextField;
  5. import com.vaadin.ui.Button;
  6. import com.vaadin.ui.CheckBox;
  7. import com.vaadin.ui.FormLayout;
  8. import com.vaadin.ui.HorizontalLayout;
  9. import com.vaadin.ui.Panel;
  10. import com.vaadin.ui.TextField;
  11. import com.vaadin.v7.ui.TextArea;
  12. public class SelectionAndCursorPosition extends AbstractReindeerTestUI {
  13. static final String DEFAULT_TEXT = "So we have some text to select";
  14. static final String TEXTFIELD_ID = "tf";
  15. static final String TEXTAREA_ID = "ta";
  16. static final String SELECT_ALL_ID = "selectAll";
  17. static final String RANGE_START_ID = "rS";
  18. static final String RANGE_LENGTH_ID = "rL";
  19. static final String CURSOR_POS_ID = "cp";
  20. static final String RANGE_SET_BUTTON_ID = "setSelection";
  21. static final String CURSOR_POS_SET_ID = "cps";
  22. TextField textField = createTextField();
  23. TextArea textArea = createTextArea();
  24. AbstractTextField activeComponent = textField;
  25. @Override
  26. protected void setup(VaadinRequest request) {
  27. FormLayout fl = new FormLayout();
  28. Panel panel = new Panel(fl);
  29. panel.setCaption("Hackers panel");
  30. CheckBox ml = new CheckBox("Multiline");
  31. // FIXME re-add this when TextArea has been replaced with vaadin8
  32. // version
  33. // ml.addListener(new Property.ValueChangeListener() {
  34. // @Override
  35. // public void valueChange(ValueChangeEvent event) {
  36. // if (textField.getUI() == null
  37. // || textField.getUI().getSession() == null) {
  38. // replaceComponent(textArea, textField);
  39. // activeComponent = textField;
  40. // } else {
  41. // replaceComponent(textField, textArea);
  42. // activeComponent = textArea;
  43. // }
  44. // }
  45. // });
  46. fl.addComponent(ml);
  47. Button selectAll = new Button("Select all ( selectAll() )");
  48. selectAll.setId(SELECT_ALL_ID);
  49. selectAll.addClickListener(event -> activeComponent.selectAll());
  50. fl.addComponent(selectAll);
  51. HorizontalLayout selectRange = new HorizontalLayout();
  52. selectRange.setCaption(
  53. "Select range of text ( setSelectionRange(int start, int lengt) )");
  54. final TextField start = new TextField("From:");
  55. start.setId(RANGE_START_ID);
  56. final TextField length = new TextField("Selection length:");
  57. length.setId(RANGE_LENGTH_ID);
  58. Button select = new Button("select");
  59. select.setId(RANGE_SET_BUTTON_ID);
  60. select.addClickListener(event -> {
  61. int startPos = Integer.parseInt(start.getValue());
  62. int lenght = Integer.parseInt(length.getValue());
  63. activeComponent.setSelection(startPos, lenght);
  64. });
  65. selectRange.addComponent(start);
  66. selectRange.addComponent(length);
  67. selectRange.addComponent(select);
  68. fl.addComponent(selectRange);
  69. HorizontalLayout setCursorPosition = new HorizontalLayout();
  70. final TextField pos = new TextField("Position:");
  71. pos.setId(CURSOR_POS_ID);
  72. Button setCursorButton = new Button("set");
  73. setCursorButton.setId(CURSOR_POS_SET_ID);
  74. setCursorButton.addClickListener(event -> {
  75. int startPos = Integer.parseInt(pos.getValue());
  76. activeComponent.setCursorPosition(startPos);
  77. });
  78. setCursorPosition.addComponent(pos);
  79. setCursorPosition.addComponent(setCursorButton);
  80. setCursorPosition.setCaption(
  81. "Set cursor position ( setCursorPosition(int pos) )");
  82. fl.addComponent(setCursorPosition);
  83. getLayout().addComponent(textField);
  84. getLayout().addComponent(panel);
  85. }
  86. private static TextField createTextField() {
  87. TextField textField = new TextField();
  88. textField.setId(TEXTFIELD_ID);
  89. textField.setCaption("Text field");
  90. textField.setValue(DEFAULT_TEXT);
  91. textField.setWidth("400px");
  92. return textField;
  93. }
  94. private static TextArea createTextArea() {
  95. TextArea textArea = new TextArea();
  96. textArea.setId(TEXTAREA_ID);
  97. textArea.setCaption("Text area");
  98. textArea.setValue(DEFAULT_TEXT);
  99. textArea.setWidth("400px");
  100. textArea.setHeight("50px");
  101. return textArea;
  102. }
  103. @Override
  104. protected String getTestDescription() {
  105. return "Tests that setSelectionRange and setCursorPosition works for a TextField";
  106. }
  107. @Override
  108. protected Integer getTicketNumber() {
  109. return 2058;
  110. }
  111. }