/testApp/core/source/org/openfaces/testapp/suggestionfield/SuggestionFieldBean.java

https://github.com/grb123/OpenFaces · Java · 193 lines · 147 code · 32 blank · 14 comment · 18 complexity · b28657b253e620cf3e731f42c3d1be6e MD5 · raw file

  1. /*
  2. * OpenFaces - JSF Component Library 2.0
  3. * Copyright (C) 2007-2011, TeamDev Ltd.
  4. * licensing@openfaces.org
  5. * Unless agreed in writing the contents of this file are subject to
  6. * the GNU Lesser General Public License Version 2.1 (the "LGPL" License).
  7. * This library is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * Please visit http://openfaces.org/licensing/ for more details.
  11. */
  12. package org.openfaces.testapp.suggestionfield;
  13. import org.openfaces.util.Faces;
  14. import org.openfaces.testapp.datatable.Color;
  15. import org.openfaces.testapp.datatable.ColorDB;
  16. import org.openfaces.testapp.dropdown.DropDownBean;
  17. import javax.faces.component.UIComponent;
  18. import javax.faces.context.FacesContext;
  19. import javax.faces.convert.Converter;
  20. import javax.faces.convert.ConverterException;
  21. import javax.faces.event.ValueChangeEvent;
  22. import java.io.BufferedReader;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.InputStreamReader;
  26. import java.io.Serializable;
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. /**
  31. * @author Darya Shumilina
  32. */
  33. public class SuggestionFieldBean {
  34. private String testSelectedValue;
  35. private String testSelectedValue2;
  36. private List<String> plants = new ArrayList<String>();
  37. private Color selectedBGColor = new Color("AliceBlue", 240, 248, 255, "#F0F8FF");
  38. private Converter colorConverter = new ColorConverter();
  39. private ColorDB colors = new ColorDB();
  40. private boolean disabled = false;
  41. private String selectedValue = "";
  42. private int valueChangeListenerCounter = 0;
  43. public static boolean testValueChangeListener;
  44. public SuggestionFieldBean() {
  45. try {
  46. InputStream resource = DropDownBean.class.getResourceAsStream("houseplants.txt");
  47. BufferedReader reader = new BufferedReader(new InputStreamReader(resource));
  48. String currentString;
  49. while (true) {
  50. currentString = reader.readLine();
  51. if (currentString == null) break;
  52. plants.add(new String(currentString.getBytes(), "UTF-8"));
  53. }
  54. reader.close();
  55. } catch (IOException e) {
  56. throw new RuntimeException(e);
  57. }
  58. }
  59. public String getTestSelectedValue() {
  60. return testSelectedValue;
  61. }
  62. public void setTestSelectedValue(String testSelectedValue) {
  63. this.testSelectedValue = testSelectedValue;
  64. }
  65. public List<String> getSimpleTestCollection() {
  66. return Arrays.asList("Red", "Yellow", "Blue");
  67. }
  68. public String getTestSelectedValue2() {
  69. return testSelectedValue2;
  70. }
  71. public void setTestSelectedValue2(String testSelectedValue2) {
  72. this.testSelectedValue2 = testSelectedValue2;
  73. }
  74. public List<String> getSuggestedPlants() {
  75. List<String> suggestedPlants = new ArrayList<String>();
  76. String typedValue = (String) Faces.var("searchString");
  77. if (typedValue != null) {
  78. for (Object myPlant : plants) {
  79. String plant = (String) myPlant;
  80. String plantForComparison = plant.toLowerCase();
  81. String typedValueForComparison = typedValue.toLowerCase();
  82. if (plantForComparison.startsWith(typedValueForComparison))
  83. suggestedPlants.add(plant);
  84. }
  85. } else {
  86. for (int i = 0; i < plants.size(); i++) {
  87. if (i % 20 == 0) {
  88. String plant = plants.get(i);
  89. suggestedPlants.add(plant);
  90. }
  91. }
  92. }
  93. return suggestedPlants;
  94. }
  95. public List<String> getPlants() {
  96. return plants;
  97. }
  98. public Color getSelectedBGColor() {
  99. return selectedBGColor;
  100. }
  101. public void setSelectedBGColor(Color selectedBGColor) {
  102. this.selectedBGColor = selectedBGColor;
  103. }
  104. public String getSelectedValue() {
  105. return selectedValue;
  106. }
  107. public void setSelectedValue(String selectedValue) {
  108. this.selectedValue = selectedValue;
  109. }
  110. private class ColorConverter implements Converter, Serializable {
  111. public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
  112. return colors.getColorByText(value);
  113. }
  114. public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
  115. if (value == null)
  116. return "";
  117. return ((Color) value).getName();
  118. }
  119. }
  120. public List<Color> getColors() {
  121. List<Color> allColors = colors.getColors();
  122. String searchString = (String) Faces.var("searchString");
  123. if (searchString == null)
  124. return allColors;
  125. List<Color> result = new ArrayList<Color>();
  126. for (Object allColor : allColors) {
  127. Color color = (Color) allColor;
  128. String colorName = color.getName();
  129. if (colorName.toUpperCase().indexOf(searchString.toUpperCase()) != -1)
  130. result.add(color);
  131. }
  132. return result;
  133. }
  134. public Converter getColorConverter() {
  135. return colorConverter;
  136. }
  137. public boolean isDisabled() {
  138. return disabled;
  139. }
  140. public void setDisabled(boolean disabled) {
  141. this.disabled = disabled;
  142. }
  143. public void makeDisabled() {
  144. setDisabled(true);
  145. }
  146. public void valueChangedAttribute(ValueChangeEvent event) {
  147. valueChangeListenerCounter++;
  148. }
  149. public int getValueChangeListenerCounter() {
  150. return valueChangeListenerCounter;
  151. }
  152. public void setValueChangeListenerCounter(int valueChangeListenerCounter) {
  153. this.valueChangeListenerCounter = valueChangeListenerCounter;
  154. }
  155. public String getValueChangeListenerFlag() {
  156. return String.valueOf(valueChangeListenerCounter);
  157. }
  158. public boolean isTestValueChangeListener() {
  159. return testValueChangeListener;
  160. }
  161. }