Java2sAutoTextField : non-strict default setting


/ Published in: Java
Save to your folder(s)

This is a modification of the Sun tutorial code that initializes the text editor to accept words that are not part of the auto-complete list. In the terms of the implementation, it is referred to as "not strict".


Copy this code and paste it in your HTML
  1. /*
  2.  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are met:
  6.  *
  7.  * -Redistribution of source code must retain the above copyright notice, this
  8.  * list of conditions and the following disclaimer.
  9.  *
  10.  * -Redistribution in binary form must reproduce the above copyright notice,
  11.  * this list of conditions and the following disclaimer in the documentation
  12.  * and/or other materials provided with the distribution.
  13.  *
  14.  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
  15.  * be used to endorse or promote products derived from this software without
  16.  * specific prior written permission.
  17.  *
  18.  * This software is provided "AS IS," without a warranty of any kind. ALL
  19.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  20.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  21.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
  22.  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
  23.  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
  24.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  25.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  26.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  27.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  28.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  29.  *
  30.  * You acknowledge that this software is not designed, licensed or intended
  31.  * for use in the design, construction, operation or maintenance of any
  32.  * nuclear facility.
  33.  */
  34. import java.util.List;
  35. import javax.swing.JTextField;
  36. import javax.swing.text.*;
  37.  
  38. public class Java2sAutoTextField extends JTextField {
  39. class AutoDocument extends PlainDocument {
  40.  
  41. public void replace(int i, int j, String s, AttributeSet attributeset)
  42. super.remove(i, j);
  43. insertString(i, s, attributeset);
  44. }
  45.  
  46. public void insertString(int i, String s, AttributeSet attributeset)
  47. if (s == null || "".equals(s))
  48. return;
  49. String s1 = getText(0, i);
  50. String s2 = getMatch(s1 + s);
  51. int j = (i + s.length()) - 1;
  52. if (isStrict && s2 == null) {
  53. s2 = getMatch(s1);
  54. j--;
  55. } else if (!isStrict && s2 == null) {
  56. super.insertString(i, s, attributeset);
  57. return;
  58. }
  59. if (autoComboBox != null && s2 != null)
  60. autoComboBox.setSelectedValue(s2);
  61. super.remove(0, getLength());
  62. super.insertString(0, s2, attributeset);
  63. setSelectionStart(j + 1);
  64. setSelectionEnd(getLength());
  65. }
  66.  
  67. public void remove(int i, int j) throws BadLocationException {
  68. int k = getSelectionStart();
  69. if (k > 0)
  70. k--;
  71. String s = getMatch(getText(0, k));
  72. if (!isStrict && s == null) {
  73. super.remove(i, j);
  74. } else {
  75. super.remove(0, getLength());
  76. super.insertString(0, s, null);
  77. }
  78. if (autoComboBox != null && s != null)
  79. autoComboBox.setSelectedValue(s);
  80. try {
  81. setSelectionStart(k);
  82. setSelectionEnd(getLength());
  83. } catch (Exception exception) {
  84. }
  85. }
  86.  
  87. }
  88.  
  89. public Java2sAutoTextField(List list) {
  90. isCaseSensitive = false;
  91. isStrict = false;
  92. autoComboBox = null;
  93. if (list == null) {
  94. throw new IllegalArgumentException("values can not be null");
  95. } else {
  96. dataList = list;
  97. init();
  98. return;
  99. }
  100. }
  101.  
  102. Java2sAutoTextField(List list, Java2sAutoComboBox b) {
  103. isCaseSensitive = false;
  104. isStrict = false;
  105. autoComboBox = null;
  106. if (list == null) {
  107. throw new IllegalArgumentException("values can not be null");
  108. } else {
  109. dataList = list;
  110. autoComboBox = b;
  111. init();
  112. return;
  113. }
  114. }
  115.  
  116. private void init() {
  117. setDocument(new AutoDocument());
  118. if (isStrict && dataList.size() > 0)
  119. setText(dataList.get(0).toString());
  120. }
  121.  
  122. private String getMatch(String s) {
  123. for (int i = 0; i < dataList.size(); i++) {
  124. String s1 = dataList.get(i).toString();
  125. if (s1 != null) {
  126. if (!isCaseSensitive
  127. && s1.toLowerCase().startsWith(s.toLowerCase()))
  128. return s1;
  129. if (isCaseSensitive && s1.startsWith(s))
  130. return s1;
  131. }
  132. }
  133.  
  134. return null;
  135. }
  136.  
  137. public void replaceSelection(String s) {
  138. AutoDocument _lb = (AutoDocument) getDocument();
  139. if (_lb != null)
  140. try {
  141. int i = Math.min(getCaret().getDot(), getCaret().getMark());
  142. int j = Math.max(getCaret().getDot(), getCaret().getMark());
  143. _lb.replace(i, j - i, s, null);
  144. } catch (Exception exception) {
  145. }
  146. }
  147.  
  148. public boolean isCaseSensitive() {
  149. return isCaseSensitive;
  150. }
  151.  
  152. public void setCaseSensitive(boolean flag) {
  153. isCaseSensitive = flag;
  154. }
  155.  
  156. public boolean isStrict() {
  157. return isStrict;
  158. }
  159.  
  160. public void setStrict(boolean flag) {
  161. isStrict = flag;
  162. }
  163.  
  164. public List getDataList() {
  165. return dataList;
  166. }
  167.  
  168. public void setDataList(List list) {
  169. if (list == null) {
  170. throw new IllegalArgumentException("values can not be null");
  171. } else {
  172. dataList = list;
  173. return;
  174. }
  175. }
  176.  
  177. private List dataList;
  178.  
  179. private boolean isCaseSensitive;
  180.  
  181. private boolean isStrict;
  182.  
  183. private Java2sAutoComboBox autoComboBox;
  184. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.