gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.serv / org.gvsig.tools.swing.serv.field / src / main / java / org / gvsig / tools / swing / serv / field / component / JTextFieldComponent.java @ 232
History | View | Annotate | Download (7.56 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
/*
|
23 |
* AUTHORS (In addition to CIT):
|
24 |
* 2010 Institute of New Imaging Technologies (INIT):
|
25 |
* http://www.init.uji.es
|
26 |
* Geographic Information research group:
|
27 |
* http://www.geoinfo.uji.es
|
28 |
* Universitat Jaume I, Spain
|
29 |
*/
|
30 |
|
31 |
/**
|
32 |
*
|
33 |
*/
|
34 |
package org.gvsig.tools.swing.serv.field.component; |
35 |
|
36 |
import java.awt.Component; |
37 |
import java.awt.event.KeyEvent; |
38 |
import java.awt.event.KeyListener; |
39 |
|
40 |
import javax.swing.JComponent; |
41 |
import javax.swing.JLabel; |
42 |
import javax.swing.JTextArea; |
43 |
import javax.swing.JTextField; |
44 |
import javax.swing.text.JTextComponent; |
45 |
|
46 |
import org.gvsig.tools.dynobject.DynField; |
47 |
import org.gvsig.tools.service.ServiceException; |
48 |
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent; |
49 |
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField; |
50 |
import org.gvsig.tools.swing.spi.AbstractJDynFieldComponent; |
51 |
|
52 |
/**
|
53 |
* @author <a href="mailto:reinhold@uji.es">cmartin</a>
|
54 |
*
|
55 |
*/
|
56 |
public class JTextFieldComponent extends AbstractJDynFieldComponent implements |
57 |
JDynFieldComponent, KeyListener {
|
58 |
|
59 |
protected JLabel label; |
60 |
|
61 |
private JTextComponent jtext; |
62 |
|
63 |
private JComponent pane; |
64 |
|
65 |
/**
|
66 |
* @param dynField
|
67 |
* @param dynObject
|
68 |
* @throws ServiceException
|
69 |
*/
|
70 |
public JTextFieldComponent(DynField dynField, ValueField dynObject)
|
71 |
throws ServiceException {
|
72 |
super(dynField, dynObject);
|
73 |
} |
74 |
|
75 |
/*
|
76 |
* (non-Javadoc)
|
77 |
*
|
78 |
* @see org.gvsig.tools.swing.spi.AbstractJDynFieldComponent#afterUI()
|
79 |
*/
|
80 |
@Override
|
81 |
protected void afterUI() { |
82 |
// TODO Auto-generated method stub
|
83 |
|
84 |
} |
85 |
|
86 |
/*
|
87 |
* (non-Javadoc)
|
88 |
*
|
89 |
* @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
|
90 |
*/
|
91 |
public JComponent asJComponent() { |
92 |
if (pane!=null) return pane; |
93 |
if (label != null) |
94 |
this.pane = createPane(label);
|
95 |
else if (jtext != null) |
96 |
this.pane = createPane(jtext);
|
97 |
// if (label != null)
|
98 |
// return this.label;
|
99 |
// else if (jtext != null)
|
100 |
// return this.jtext;
|
101 |
return pane;
|
102 |
} |
103 |
|
104 |
private JComponent createPane(Component comp){ |
105 |
// JPanel pane = new JPanel(new GridBagLayout());
|
106 |
// GridBagConstraints constr = new GridBagConstraints();
|
107 |
// constr.insets = new Insets(3, 3, 3, 3);
|
108 |
// constr.gridwidth = 1;
|
109 |
// constr.fill = GridBagConstraints.REMAINDER;
|
110 |
// pane.add(comp,constr);
|
111 |
JTextArea tf = new JTextArea(); |
112 |
tf.setText(getLabel()); |
113 |
if (this.getDynField().isReadOnly()){ |
114 |
tf.setEditable(false);
|
115 |
}else{
|
116 |
tf.setFont(tf.getFont().deriveFont(12f));
|
117 |
tf.setRows(3);
|
118 |
} |
119 |
JTextField tff = new JTextField(); |
120 |
tff.setEditable(false);
|
121 |
tff.add(tf); |
122 |
// tf.setVisible(false);
|
123 |
return tf;
|
124 |
} |
125 |
|
126 |
private String getLabel() { |
127 |
Object value = this.getFieldValue(); |
128 |
if (value == null) |
129 |
value = this.getDefaultFieldValue();
|
130 |
return (value == null) ? "" : value.toString(); |
131 |
} |
132 |
|
133 |
/*
|
134 |
* (non-Javadoc)
|
135 |
*
|
136 |
* @see
|
137 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#getValue
|
138 |
* ()
|
139 |
*/
|
140 |
public Object getValue() { |
141 |
if (label != null) |
142 |
return label.getText();
|
143 |
else if (jtext != null) |
144 |
return jtext.getText();
|
145 |
return null; |
146 |
} |
147 |
|
148 |
/*
|
149 |
* (non-Javadoc)
|
150 |
*
|
151 |
* @see org.gvsig.tools.swing.spi.AbstractJDynFieldComponent#initData()
|
152 |
*/
|
153 |
@Override
|
154 |
protected void initData() { |
155 |
// TODO Auto-generated method stub
|
156 |
|
157 |
} |
158 |
|
159 |
/**
|
160 |
* Creates the component ui.
|
161 |
*/
|
162 |
@Override
|
163 |
protected void initUI() { |
164 |
DynField field = this.getDynField();
|
165 |
if (field.isReadOnly()) {
|
166 |
label = new JLabel(this.getLabel()); |
167 |
// label.setBorder(BorderFactory.createEmptyBorder(2, 10, 4, 4));
|
168 |
} else {
|
169 |
// this.panel = new JPanel(new BorderLayout());
|
170 |
JTextArea textArea = new JTextArea(this.getLabel()); |
171 |
textArea.setFont(textArea.getFont().deriveFont(12f));
|
172 |
textArea.setWrapStyleWord(true);
|
173 |
textArea.setLineWrap(true);
|
174 |
// textArea.setBorder(BorderFactory.createEmptyBorder(4, 10, 4, 4));
|
175 |
jtext = textArea; |
176 |
// textArea.setBorder(BorderFactory.createTitledBorder(""));
|
177 |
// jtext.setBorder(BorderFactory.createLineBorder(Color.GRAY));
|
178 |
} |
179 |
} |
180 |
|
181 |
/*
|
182 |
* (non-Javadoc)
|
183 |
*
|
184 |
* @see
|
185 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#isMandatory
|
186 |
* ()
|
187 |
*/
|
188 |
@Override
|
189 |
public boolean isMandatory() { |
190 |
return this.getDynField().isMandatory(); |
191 |
} |
192 |
|
193 |
/*
|
194 |
* (non-Javadoc)
|
195 |
*
|
196 |
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
|
197 |
*/
|
198 |
public void keyPressed(KeyEvent e) { |
199 |
// TODO Auto-generated method stub
|
200 |
|
201 |
} |
202 |
|
203 |
/*
|
204 |
* (non-Javadoc)
|
205 |
*
|
206 |
* @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
|
207 |
*/
|
208 |
public void keyReleased(KeyEvent e) { |
209 |
// TODO Auto-generated method stub
|
210 |
|
211 |
} |
212 |
|
213 |
/*
|
214 |
* (non-Javadoc)
|
215 |
*
|
216 |
* @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
|
217 |
*/
|
218 |
public void keyTyped(KeyEvent e) { |
219 |
this.fireValueChangedEvent();
|
220 |
} |
221 |
|
222 |
/*
|
223 |
* (non-Javadoc)
|
224 |
*
|
225 |
* @see
|
226 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#requestFocus
|
227 |
* ()
|
228 |
*/
|
229 |
public void requestFocus() { |
230 |
if (label != null) |
231 |
label.requestFocus(); |
232 |
else
|
233 |
jtext.requestFocus(); |
234 |
} |
235 |
|
236 |
/*
|
237 |
* (non-Javadoc)
|
238 |
*
|
239 |
* @see
|
240 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#setEnabled
|
241 |
* (boolean)
|
242 |
*/
|
243 |
public void setEnabled(boolean isEnabled) { |
244 |
label.setEnabled(isEnabled); |
245 |
if (label != null) |
246 |
label.setEnabled(isEnabled); |
247 |
else if (jtext != null) |
248 |
jtext.setEnabled(isEnabled); |
249 |
} |
250 |
|
251 |
/*
|
252 |
* (non-Javadoc)
|
253 |
*
|
254 |
* @seeorg.gvsig.tools.swing.spi.AbstractJDynFieldComponent#
|
255 |
* setJDynFieldComponentListeners()
|
256 |
*/
|
257 |
@Override
|
258 |
protected void setJDynFieldComponentListeners() { |
259 |
if (label != null) |
260 |
label.addKeyListener(this);
|
261 |
else if (jtext != null) |
262 |
jtext.addKeyListener(this);
|
263 |
} |
264 |
|
265 |
/*
|
266 |
* (non-Javadoc)
|
267 |
*
|
268 |
* @see org.gvsig.tools.swing.spi.AbstractJDynFieldComponent#setReadOnly()
|
269 |
*/
|
270 |
@Override
|
271 |
protected void setReadOnly() { |
272 |
if (jtext != null) { |
273 |
jtext.setEditable(false);
|
274 |
} |
275 |
} |
276 |
|
277 |
/*
|
278 |
* (non-Javadoc)
|
279 |
*
|
280 |
* @see
|
281 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#setValue
|
282 |
* (java.lang.Object)
|
283 |
*/
|
284 |
protected void setNullValue() { |
285 |
setNonNullValue("");
|
286 |
} |
287 |
|
288 |
protected void setNonNullValue(Object value) { |
289 |
if (label != null) |
290 |
label.setText(value.toString()); |
291 |
else if (jtext != null) { |
292 |
jtext.setText(value.toString()); |
293 |
} |
294 |
} |
295 |
} |