PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/controlsfx-samples/src/main/java/org/controlsfx/samples/textfields/HelloAutoComplete.java

https://bitbucket.org/raghsr/controlsfx
Java | 154 lines | 92 code | 27 blank | 35 comment | 3 complexity | c93a638a12dfb22a9ade6d60be4f33aa MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /**
  2. * Copyright (c) 2014, ControlsFX
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of ControlsFX, any associated website, nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL CONTROLSFX BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package org.controlsfx.samples.textfields;
  28. import javafx.event.EventHandler;
  29. import javafx.geometry.Insets;
  30. import javafx.scene.Node;
  31. import javafx.scene.control.Label;
  32. import javafx.scene.control.TextField;
  33. import javafx.scene.input.KeyEvent;
  34. import javafx.scene.layout.BorderPane;
  35. import javafx.scene.layout.GridPane;
  36. import javafx.scene.layout.Priority;
  37. import javafx.stage.Stage;
  38. import org.controlsfx.ControlsFXSample;
  39. import org.controlsfx.control.textfield.AutoCompletionBinding;
  40. import org.controlsfx.control.textfield.TextFields;
  41. import org.controlsfx.samples.Utils;
  42. import java.util.Arrays;
  43. import java.util.HashSet;
  44. import java.util.Set;
  45. public class HelloAutoComplete extends ControlsFXSample {
  46. private AutoCompletionBinding<String> autoCompletionBinding;
  47. private String[] _possibleSuggestions = {"Hey", "Hello", "Hello World", "Apple", "Cool", "Costa", "Cola", "Coca Cola"};
  48. private Set<String> possibleSuggestions = new HashSet<>(Arrays.asList(_possibleSuggestions));
  49. private TextField learningTextField;
  50. @Override public String getSampleName() {
  51. return "AutoComplete";
  52. }
  53. @Override public String getJavaDocURL() {
  54. return Utils.JAVADOC_BASE + "org/controlsfx/control/textfield/TextFields.html";
  55. }
  56. @Override public String getSampleDescription() {
  57. return "AutoComplete helps a user with suggestions to type faster, "
  58. + "but does not limit the user from entering alternative text."
  59. + "\n\n"
  60. + "The textfields have been primed with the following words:\n"
  61. + "\"Hey\", \"Hello\", \"Hello World\", \"Apple\", \"Cool\", "
  62. + "\"Costa\", \"Cola\", \"Coca Cola\""
  63. + "\n\n"
  64. + "The 'Learning TextField' will add whatever words are typed "
  65. + "to the auto-complete popup, as long as you press Enter once "
  66. + "you've finished typing the word.";
  67. }
  68. @Override public Node getPanel(final Stage stage) {
  69. BorderPane root = new BorderPane();
  70. GridPane grid = new GridPane();
  71. grid.setVgap(10);
  72. grid.setHgap(10);
  73. grid.setPadding(new Insets(30, 30, 0, 30));
  74. //
  75. // TextField with static auto-complete functionality
  76. //
  77. TextField textField = new TextField();
  78. TextFields.bindAutoCompletion(
  79. textField,
  80. "Hey", "Hello", "Hello World", "Apple", "Cool", "Costa", "Cola", "Coca Cola");
  81. grid.add(new Label("Auto-complete Text"), 0, 0);
  82. grid.add(textField, 1, 0);
  83. GridPane.setHgrow(textField, Priority.ALWAYS);
  84. //
  85. // TextField with learning auto-complete functionality
  86. // Learn the word when user presses ENTER
  87. //
  88. learningTextField = new TextField();
  89. autoCompletionBinding = TextFields.bindAutoCompletion(learningTextField, possibleSuggestions);
  90. learningTextField.setOnKeyPressed(new EventHandler<KeyEvent>() {
  91. @Override
  92. public void handle(KeyEvent ke) {
  93. switch (ke.getCode()) {
  94. case ENTER:
  95. autoCompletionLearnWord(learningTextField.getText().trim());
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. });
  102. grid.add(new Label("Learning TextField"), 0, 1);
  103. grid.add(learningTextField, 1, 1);
  104. GridPane.setHgrow(learningTextField, Priority.ALWAYS);
  105. root.setTop(grid);
  106. return root;
  107. }
  108. private void autoCompletionLearnWord(String newWord){
  109. possibleSuggestions.add(newWord);
  110. // we dispose the old binding and recreate a new binding
  111. if (autoCompletionBinding != null) {
  112. autoCompletionBinding.dispose();
  113. }
  114. autoCompletionBinding = TextFields.bindAutoCompletion(learningTextField, possibleSuggestions);
  115. }
  116. @Override public Node getControlPanel() {
  117. GridPane grid = new GridPane();
  118. grid.setVgap(10);
  119. grid.setHgap(10);
  120. grid.setPadding(new Insets(30, 30, 0, 30));
  121. // TODO Add customization example controls
  122. return grid;
  123. }
  124. public static void main(String[] args) {
  125. launch(args);
  126. }
  127. }