KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > renderer > BaseInputTextControl


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Jan 15, 2006
23  */

24 package org.lobobrowser.html.renderer;
25
26 import java.awt.Font JavaDoc;
27 import java.awt.FontMetrics JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import javax.swing.text.AttributeSet JavaDoc;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import javax.swing.text.JTextComponent JavaDoc;
32
33 import org.lobobrowser.html.domimpl.*;
34 import org.lobobrowser.util.gui.WrapperLayout;
35
36 abstract class BaseInputTextControl extends BaseInputControl {
37     private static final float DEFAULT_FONT_SIZE = 14.0f;
38     protected final JTextComponent JavaDoc widget;
39     
40     public BaseInputTextControl(final HTMLBaseInputElement modelNode) {
41         super(modelNode);
42         this.setLayout(WrapperLayout.getInstance());
43         JTextComponent JavaDoc widget = this.createTextField();
44         Font JavaDoc font = widget.getFont();
45         widget.setFont(font.deriveFont(DEFAULT_FONT_SIZE));
46         widget.setDocument(new LimitedDocument());
47         this.widget = widget;
48         this.add(widget);
49     }
50     
51     public void reset(int availWidth, int availHeight) {
52         super.reset(availWidth, availHeight);
53         ElementImpl element = this.controlElement;
54         String JavaDoc value = element.getAttribute("value");
55         widget.setText(value);
56         String JavaDoc maxLengthText = this.controlElement.getAttribute("maxlength");
57         if(maxLengthText != null) {
58             try {
59                 this.maxLength = Integer.parseInt(maxLengthText);
60             } catch(NumberFormatException JavaDoc nfe) {
61                 // ignore
62
}
63         }
64         
65     }
66     
67     protected abstract JTextComponent JavaDoc createTextField();
68     
69     private int maxLength = -1;
70     
71     /* (non-Javadoc)
72      * @see org.xamjwg.html.domimpl.InputContext#getMaxLength()
73      */

74     public int getMaxLength() {
75         return this.maxLength;
76     }
77
78     /* (non-Javadoc)
79      * @see org.xamjwg.html.domimpl.InputContext#getReadOnly()
80      */

81     public boolean getReadOnly() {
82         return !this.widget.isEditable();
83     }
84
85     /* (non-Javadoc)
86      * @see org.xamjwg.html.domimpl.InputContext#getValue()
87      */

88     public String JavaDoc getValue() {
89         return this.widget.getText();
90     }
91
92     /* (non-Javadoc)
93      * @see org.xamjwg.html.domimpl.InputContext#select()
94      */

95     public void select() {
96         this.widget.selectAll();
97     }
98
99     /* (non-Javadoc)
100      * @see org.xamjwg.html.domimpl.InputContext#setDisabled(boolean)
101      */

102     public void setDisabled(boolean disabled) {
103         super.setDisabled(disabled);
104         this.widget.setEnabled(!disabled);
105     }
106
107     /* (non-Javadoc)
108      * @see org.xamjwg.html.domimpl.InputContext#setMaxLength(int)
109      */

110     public void setMaxLength(int maxLength) {
111         this.maxLength = maxLength;
112     }
113
114     /* (non-Javadoc)
115      * @see org.xamjwg.html.domimpl.InputContext#setReadOnly(boolean)
116      */

117     public void setReadOnly(boolean readOnly) {
118         this.widget.setEditable(!readOnly);
119     }
120
121     /* (non-Javadoc)
122      * @see org.xamjwg.html.domimpl.InputContext#setValue(java.lang.String)
123      */

124     public void setValue(String JavaDoc value) {
125         this.widget.setText(value);
126     }
127
128     public java.awt.Dimension JavaDoc getPreferredSize() {
129         int size = this.size;
130         JTextComponent JavaDoc widget = this.widget;
131         FontMetrics JavaDoc fm = widget.getFontMetrics(widget.getFont());
132         Insets JavaDoc insets = widget.getInsets();
133         int pw, ph;
134         if(size == -1) {
135             pw = 100;
136         }
137         else {
138             pw = insets.left + insets.right + fm.charWidth('0') * size;
139         }
140         ph = fm.getHeight() + insets.top + insets.bottom;
141         return new java.awt.Dimension JavaDoc(pw, ph);
142     }
143     
144     public void resetInput() {
145         this.widget.setText("");
146     }
147     
148     /**
149      * Implements maxlength functionality.
150      */

151     private class LimitedDocument extends javax.swing.text.PlainDocument JavaDoc {
152         /* (non-Javadoc)
153          * @see javax.swing.text.PlainDocument#insertString(int, java.lang.String, javax.swing.text.AttributeSet)
154          */

155         public void insertString(int offs, String JavaDoc str, AttributeSet JavaDoc a) throws BadLocationException JavaDoc {
156             int max = BaseInputTextControl.this.maxLength;
157             if(max != -1) {
158                 int docLength = this.getLength();
159                 if(docLength >= max) {
160                     return;
161                 }
162                 int strLen = str.length();
163                 if(docLength + strLen > max) {
164                     String JavaDoc shorterStr = str.substring(0, max - docLength);
165                     super.insertString(offs, shorterStr, a);
166                 }
167                 else {
168                     super.insertString(offs, str, a);
169                 }
170             }
171             else {
172                 super.insertString(offs, str, a);
173             }
174         }
175     }
176 }
177
Popular Tags