1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.util.Map;
18
19 import org.htmlunit.SgmlPage;
20 import org.htmlunit.html.impl.SelectableTextInput;
21 import org.htmlunit.html.impl.SelectableTextSelectionDelegate;
22
23
24
25
26
27
28
29 public abstract class HtmlSelectableTextInput extends HtmlInput implements SelectableTextInput {
30
31 private SelectableTextSelectionDelegate selectionDelegate_ = new SelectableTextSelectionDelegate(this);
32 private DoTypeProcessor doTypeProcessor_ = new DoTypeProcessor(this);
33
34
35
36
37
38
39
40
41 HtmlSelectableTextInput(final String qualifiedName, final SgmlPage page,
42 final Map<String, DomAttr> attributes) {
43 super(qualifiedName, page, attributes);
44 }
45
46
47
48
49 @Override
50 public void setValue(final String newValue) {
51 super.setValue(newValue);
52
53 final SgmlPage page = getPage();
54 if (page != null && page.isHtmlPage()) {
55 final int pos = newValue.length();
56 setSelectionStart(pos);
57 setSelectionEnd(pos);
58 }
59 }
60
61
62
63
64 @Override
65 protected void doType(final char c, final boolean lastType) {
66 doTypeProcessor_.doType(getRawValue(), selectionDelegate_, c, this, lastType);
67 }
68
69
70
71
72 @Override
73 protected void doType(final int keyCode, final boolean lastType) {
74 doTypeProcessor_.doType(getRawValue(), selectionDelegate_, keyCode, this, lastType);
75 }
76
77
78
79
80 @Override
81 protected void typeDone(final String newValue, final boolean notifyAttributeChangeListeners) {
82 if (!isMinMaxLengthSupported() || newValue.length() <= getMaxLength()) {
83 setRawValue(newValue);
84 markValueDirty();
85 }
86 }
87
88
89
90
91 @Override
92 public void setText(final String text) {
93 setValue(text);
94 }
95
96
97
98
99 @Override
100 public String getText() {
101 return getRawValue();
102 }
103
104
105
106
107 @Override
108 public void select() {
109 selectionDelegate_.select();
110 }
111
112
113
114
115 @Override
116 public String getSelectedText() {
117 return selectionDelegate_.getSelectedText();
118 }
119
120
121
122
123 @Override
124 public int getSelectionStart() {
125 return selectionDelegate_.getSelectionStart();
126 }
127
128
129
130
131 @Override
132 public void setSelectionStart(final int selectionStart) {
133 selectionDelegate_.setSelectionStart(selectionStart);
134 }
135
136
137
138
139 @Override
140 public int getSelectionEnd() {
141 return selectionDelegate_.getSelectionEnd();
142 }
143
144
145
146
147 @Override
148 public void setSelectionEnd(final int selectionEnd) {
149 selectionDelegate_.setSelectionEnd(selectionEnd);
150 }
151
152
153
154
155
156 @Override
157 public void reset() {
158 super.reset();
159 setSelectionEnd(0);
160 }
161
162
163
164
165 @Override
166 public DomNode cloneNode(final boolean deep) {
167 final HtmlSelectableTextInput newNode = (HtmlSelectableTextInput) super.cloneNode(deep);
168 newNode.selectionDelegate_ = new SelectableTextSelectionDelegate(newNode);
169 newNode.doTypeProcessor_ = new DoTypeProcessor(newNode);
170
171 return newNode;
172 }
173 }