KickJava   Java API By Example, From Geeks To Geeks.

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


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
30 import javax.swing.*;
31 import javax.swing.text.JTextComponent JavaDoc;
32
33 import org.lobobrowser.html.domimpl.*;
34 import org.lobobrowser.util.gui.WrapperLayout;
35
36 class InputTextAreaControl extends BaseInputControl {
37     private final JTextComponent JavaDoc widget;
38     
39     public InputTextAreaControl(HTMLBaseInputElement modelNode) {
40         super(modelNode);
41         this.setLayout(WrapperLayout.getInstance());
42         JTextComponent JavaDoc widget = this.createTextField();
43         this.widget = widget;
44         this.add(new JScrollPane(widget));
45     }
46
47     public void reset(int availWidth, int availHeight) {
48         super.reset(availWidth, availHeight);
49         ElementImpl element = this.controlElement;
50         String JavaDoc colsStr = element.getAttribute("cols");
51         if(colsStr != null) {
52             try {
53                 this.setCols(Integer.parseInt(colsStr));
54             } catch(NumberFormatException JavaDoc nfe) {
55                 // ignore
56
}
57         }
58         String JavaDoc rowsStr = element.getAttribute("rows");
59         if(rowsStr != null) {
60             try {
61                 this.setRows(Integer.parseInt(rowsStr));
62             } catch(NumberFormatException JavaDoc nfe) {
63                 // ignore
64
}
65         }
66         String JavaDoc value = element.getTextContent();
67         this.widget.setText(value);
68     }
69     
70     protected JTextComponent JavaDoc createTextField() {
71         return new JTextArea();
72     }
73
74     /* (non-Javadoc)
75      * @see org.xamjwg.html.renderer.BaseInputControl#getCols()
76      */

77     public int getCols() {
78         return this.cols;
79     }
80
81     /* (non-Javadoc)
82      * @see org.xamjwg.html.renderer.BaseInputControl#getRows()
83      */

84     public int getRows() {
85         return this.rows;
86     }
87
88     private int cols = -1;
89     private int rows = -1;
90     
91     /* (non-Javadoc)
92      * @see org.xamjwg.html.renderer.BaseInputControl#setCols(int)
93      */

94     public void setCols(int cols) {
95         this.cols = cols;
96         this.invalidate();
97     }
98
99     /* (non-Javadoc)
100      * @see org.xamjwg.html.renderer.BaseInputControl#setRows(int)
101      */

102     public void setRows(int rows) {
103         this.rows = rows;
104         this.invalidate();
105     }
106
107     public java.awt.Dimension JavaDoc getPreferredSize() {
108         int pw;
109         int cols = this.cols;
110         if(cols == -1) {
111             pw = 100;
112         }
113         else {
114             Font JavaDoc f = this.widget.getFont();
115             FontMetrics JavaDoc fm = this.widget.getFontMetrics(f);
116             Insets JavaDoc insets = this.widget.getInsets();
117             pw = insets.left + insets.right + fm.charWidth('*') * cols;
118         }
119         int ph;
120         int rows = this.rows;
121         if(rows == -1) {
122             ph = 100;
123         }
124         else {
125             Font JavaDoc f = this.widget.getFont();
126             FontMetrics JavaDoc fm = this.widget.getFontMetrics(f);
127             Insets JavaDoc insets = this.widget.getInsets();
128             ph = insets.top + insets.bottom + fm.getHeight() * rows;
129         }
130         return new java.awt.Dimension JavaDoc(pw, ph);
131         
132     }
133     
134     /* (non-Javadoc)
135      * @see org.xamjwg.html.renderer.BaseInputControl#getReadOnly()
136      */

137     public boolean getReadOnly() {
138         return !this.widget.isEditable();
139     }
140
141     /* (non-Javadoc)
142      * @see org.xamjwg.html.renderer.BaseInputControl#getValue()
143      */

144     public String JavaDoc getValue() {
145         return this.widget.getText();
146     }
147
148     /* (non-Javadoc)
149      * @see org.xamjwg.html.renderer.BaseInputControl#setReadOnly(boolean)
150      */

151     public void setReadOnly(boolean readOnly) {
152         this.widget.setEditable(readOnly);
153     }
154
155     /* (non-Javadoc)
156      * @see org.xamjwg.html.renderer.BaseInputControl#setValue(java.lang.String)
157      */

158     public void setValue(String JavaDoc value) {
159         this.widget.setText(value);
160     }
161     
162     public void resetInput() {
163         this.widget.setText("");
164     }
165 }
166
Popular Tags