View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Abstract parent class to share {@link SelectableTextInput} implementation
25   * and typing support.
26   *
27   * @author Ronald Brill
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       * Creates an instance.
36       *
37       * @param qualifiedName the qualified name of the element type to instantiate
38       * @param page the page that contains this element
39       * @param attributes the initial attributes
40       */
41      HtmlSelectableTextInput(final String qualifiedName, final SgmlPage page,
42              final Map<String, DomAttr> attributes) {
43          super(qualifiedName, page, attributes);
44      }
45  
46      /**
47       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
90       */
91      @Override
92      public void setText(final String text) {
93          setValue(text);
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public String getText() {
101         return getRawValue();
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public void select() {
109         selectionDelegate_.select();
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public String getSelectedText() {
117         return selectionDelegate_.getSelectedText();
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public int getSelectionStart() {
125         return selectionDelegate_.getSelectionStart();
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public void setSelectionStart(final int selectionStart) {
133         selectionDelegate_.setSelectionStart(selectionStart);
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public int getSelectionEnd() {
141         return selectionDelegate_.getSelectionEnd();
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public void setSelectionEnd(final int selectionEnd) {
149         selectionDelegate_.setSelectionEnd(selectionEnd);
150     }
151 
152     /**
153      * {@inheritDoc}
154      * @see HtmlInput#reset()
155      */
156     @Override
157     public void reset() {
158         super.reset();
159         setSelectionEnd(0);
160     }
161 
162     /**
163      * {@inheritDoc}
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 }