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 final String oldValue = getValue();
52 super.setValue(newValue);
53
54 final SgmlPage page = getPage();
55 if (page != null && page.isHtmlPage() && !newValue.equals(oldValue)) {
56 final int pos = newValue.length();
57 setSelectionStart(pos);
58 setSelectionEnd(pos);
59 }
60 }
61
62
63
64
65 @Override
66 protected void doType(final char c, final boolean lastType) {
67 doTypeProcessor_.doType(getRawValue(), selectionDelegate_, c, this, lastType);
68 }
69
70
71
72
73 @Override
74 protected void doType(final int keyCode, final boolean lastType) {
75 doTypeProcessor_.doType(getRawValue(), selectionDelegate_, keyCode, this, lastType);
76 }
77
78
79
80
81 @Override
82 protected void typeDone(final String newValue, final boolean notifyAttributeChangeListeners) {
83 if (!isMinMaxLengthSupported() || newValue.length() <= getMaxLength()) {
84 setRawValue(newValue);
85 markValueDirty();
86 }
87 }
88
89
90
91
92 @Override
93 public void setText(final String text) {
94 setValue(text);
95 }
96
97
98
99
100 @Override
101 public String getText() {
102 return getRawValue();
103 }
104
105
106
107
108 @Override
109 public void select() {
110 selectionDelegate_.select();
111 }
112
113
114
115
116 @Override
117 public String getSelectedText() {
118 return selectionDelegate_.getSelectedText();
119 }
120
121
122
123
124 @Override
125 public int getSelectionStart() {
126 return selectionDelegate_.getSelectionStart();
127 }
128
129
130
131
132 @Override
133 public void setSelectionStart(final int selectionStart) {
134 selectionDelegate_.setSelectionStart(selectionStart);
135 }
136
137
138
139
140 @Override
141 public int getSelectionEnd() {
142 return selectionDelegate_.getSelectionEnd();
143 }
144
145
146
147
148 @Override
149 public void setSelectionEnd(final int selectionEnd) {
150 selectionDelegate_.setSelectionEnd(selectionEnd);
151 }
152
153
154
155
156 @Override
157 public DomNode cloneNode(final boolean deep) {
158 final HtmlSelectableTextInput newNode = (HtmlSelectableTextInput) super.cloneNode(deep);
159 newNode.selectionDelegate_ = new SelectableTextSelectionDelegate(newNode);
160 newNode.doTypeProcessor_ = new DoTypeProcessor(newNode);
161
162 return newNode;
163 }
164 }