View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.html;
16  
17  import java.util.Map;
18  
19  import com.gargoylesoftware.htmlunit.SgmlPage;
20  import com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput;
21  import com.gargoylesoftware.htmlunit.html.impl.SelectableTextSelectionDelegate;
22  
23  /**
24   * Wrapper for the HTML element "input" where type is "tel".
25   *
26   * @author Ahmed Ashour
27   */
28  public class HtmlTelInput extends HtmlInput implements SelectableTextInput {
29  
30      private final SelectableTextSelectionDelegate selectionDelegate_ = new SelectableTextSelectionDelegate(this);
31  
32      private final 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      HtmlTelInput(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 int getSelectionStart() {
51          return selectionDelegate_.getSelectionStart();
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public void setSelectionStart(final int selectionStart) {
59          selectionDelegate_.setSelectionStart(selectionStart);
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public int getSelectionEnd() {
67          return selectionDelegate_.getSelectionEnd();
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public void setSelectionEnd(final int selectionEnd) {
75          selectionDelegate_.setSelectionEnd(selectionEnd);
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public String getSelectedText() {
83          return selectionDelegate_.getSelectedText();
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public void select() {
91          selectionDelegate_.select();
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public void setText(final String text) {
99          setValueAttribute(text);
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public String getText() {
107         return getValueAttribute();
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     protected void doType(final char c, final boolean startAtEnd, final boolean lastType) {
115         if (startAtEnd) {
116             selectionDelegate_.setSelectionStart(getValueAttribute().length());
117         }
118         doTypeProcessor_.doType(getValueAttribute(), selectionDelegate_, c, this, lastType);
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     protected void doType(final int keyCode, final boolean startAtEnd, final boolean lastType) {
126         if (startAtEnd) {
127             selectionDelegate_.setSelectionStart(getValueAttribute().length());
128         }
129         doTypeProcessor_.doType(getValueAttribute(), selectionDelegate_, keyCode, this, lastType);
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     protected void typeDone(final String newValue, final boolean notifyAttributeChangeListeners) {
137         if (newValue.length() <= getMaxLength()) {
138             setAttributeNS(null, "value", newValue, notifyAttributeChangeListeners, false);
139         }
140     }
141 
142 }