View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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          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       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
91       */
92      @Override
93      public void setText(final String text) {
94          setValue(text);
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public String getText() {
102         return getRawValue();
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public void select() {
110         selectionDelegate_.select();
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public String getSelectedText() {
118         return selectionDelegate_.getSelectedText();
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public int getSelectionStart() {
126         return selectionDelegate_.getSelectionStart();
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public void setSelectionStart(final int selectionStart) {
134         selectionDelegate_.setSelectionStart(selectionStart);
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public int getSelectionEnd() {
142         return selectionDelegate_.getSelectionEnd();
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public void setSelectionEnd(final int selectionEnd) {
150         selectionDelegate_.setSelectionEnd(selectionEnd);
151     }
152 
153     /**
154      * {@inheritDoc}
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 }