svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / panels / FPanelCreateField.java @ 7265
History | View | Annotate | Download (11.1 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program 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
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.gui.panels; |
42 |
|
43 |
import java.awt.BorderLayout; |
44 |
import java.awt.LayoutManager; |
45 |
import java.awt.event.ActionListener; |
46 |
import java.awt.event.KeyEvent; |
47 |
import java.awt.event.KeyListener; |
48 |
import java.sql.Types; |
49 |
import java.text.ParseException; |
50 |
import java.util.Vector; |
51 |
|
52 |
import javax.swing.JComboBox; |
53 |
import javax.swing.JLabel; |
54 |
import javax.swing.JPanel; |
55 |
import javax.swing.JTextField; |
56 |
|
57 |
import org.gvsig.gui.beans.AcceptCancelPanel; |
58 |
|
59 |
import com.hardcode.gdbms.engine.instruction.SemanticException; |
60 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
61 |
import com.iver.andami.PluginServices; |
62 |
import com.iver.andami.ui.mdiManager.IWindow; |
63 |
import com.iver.andami.ui.mdiManager.WindowInfo; |
64 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
65 |
import java.awt.GridLayout; |
66 |
|
67 |
public class FPanelCreateField extends JPanel implements IWindow { |
68 |
|
69 |
private static final String DEFAULT_FIELD_LENGTH = "50"; |
70 |
private JLabel jLblFieldName = null; |
71 |
private JTextField jTxtFieldName = null; |
72 |
private JLabel jLblFieldType = null; |
73 |
private JComboBox jCboFieldType = null; |
74 |
private JLabel jLblFieldLength = null; |
75 |
private JTextField jTxtFieldLength = null; |
76 |
private JLabel jLblFieldPrecision = null; |
77 |
private JTextField jTxtFieldPrecision = null; |
78 |
private JLabel jLblDefaultValue = null; |
79 |
private JTextField jTxtDefaultValue = null; |
80 |
private WindowInfo viewInfo;
|
81 |
private JPanel jPanel = null; // @jve:decl-index=0:visual-constraint="299,27" |
82 |
private AcceptCancelPanel jPanelOkCancel = null; |
83 |
private JPanel jPnlFields = null; |
84 |
private KeyListener checkInt = new KeyListener() { |
85 |
public void keyPressed(KeyEvent e) { } |
86 |
public void keyReleased(KeyEvent e) { |
87 |
JTextField component = (JTextField) e.getComponent(); |
88 |
|
89 |
try {
|
90 |
component.setText( |
91 |
String.valueOf(
|
92 |
Integer.parseInt(component.getText())
|
93 |
) |
94 |
); |
95 |
|
96 |
} catch (Exception ex) { |
97 |
String text = component.getText();
|
98 |
text = (text.length() <= 1)? "0" : text.substring(0, text.length()-1); |
99 |
component.setText(text); |
100 |
}} |
101 |
public void keyTyped(KeyEvent e) { } |
102 |
}; |
103 |
private String[] currentFieldNames; |
104 |
|
105 |
public FPanelCreateField() {
|
106 |
super();
|
107 |
// TODO Auto-generated constructor stub
|
108 |
initialize(); |
109 |
} |
110 |
|
111 |
public FPanelCreateField(boolean isDoubleBuffered) { |
112 |
super(isDoubleBuffered);
|
113 |
// TODO Auto-generated constructor stub
|
114 |
initialize(); |
115 |
} |
116 |
|
117 |
public FPanelCreateField(LayoutManager layout) { |
118 |
super(layout);
|
119 |
// TODO Auto-generated constructor stub
|
120 |
initialize(); |
121 |
} |
122 |
|
123 |
public FPanelCreateField(LayoutManager layout, boolean isDoubleBuffered) { |
124 |
super(layout, isDoubleBuffered);
|
125 |
// TODO Auto-generated constructor stub
|
126 |
initialize(); |
127 |
} |
128 |
|
129 |
public WindowInfo getWindowInfo() {
|
130 |
if (viewInfo == null) |
131 |
{ |
132 |
viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
|
133 |
viewInfo.setWidth(this.getWidth()+8); |
134 |
viewInfo.setHeight(this.getHeight());
|
135 |
viewInfo.setTitle(PluginServices.getText(this, "new_field_properties")); |
136 |
} |
137 |
return viewInfo;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* This method initializes this
|
142 |
*
|
143 |
* @return void
|
144 |
*/
|
145 |
private void initialize() { |
146 |
|
147 |
this.setLayout(new BorderLayout()); |
148 |
this.setSize(300, 210); |
149 |
this.setPreferredSize(new java.awt.Dimension(300,210)); |
150 |
this.add(getJPanel(), java.awt.BorderLayout.CENTER);
|
151 |
this.add(getJPanelOkCancel(), java.awt.BorderLayout.SOUTH);
|
152 |
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* This method initializes jTxtFieldName
|
157 |
*
|
158 |
* @return javax.swing.JTextField
|
159 |
*/
|
160 |
private JTextField getJTxtFieldName() { |
161 |
if (jTxtFieldName == null) { |
162 |
jTxtFieldName = new JTextField(); |
163 |
jTxtFieldName.setBounds(new java.awt.Rectangle(147,15,138,22)); |
164 |
|
165 |
} |
166 |
return jTxtFieldName;
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* This method initializes jCboFieldType
|
171 |
*
|
172 |
* @return javax.swing.JComboBox
|
173 |
*/
|
174 |
private JComboBox getJCboFieldType() { |
175 |
if (jCboFieldType == null) { |
176 |
jCboFieldType = new JComboBox(); |
177 |
jCboFieldType.setBounds(new java.awt.Rectangle(147,52,138,22)); |
178 |
jCboFieldType.addItem("Boolean");
|
179 |
jCboFieldType.addItem("Date");
|
180 |
jCboFieldType.addItem("Integer");
|
181 |
jCboFieldType.addItem("Double");
|
182 |
jCboFieldType.addItem("String");
|
183 |
|
184 |
jCboFieldType.setSelectedIndex(4);
|
185 |
jCboFieldType.addActionListener(new java.awt.event.ActionListener() {
|
186 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
187 |
// System.out.println("actionPerformed()" + e.getActionCommand()); // TODO Auto-generated Event stub actionPerformed()
|
188 |
String strType = (String) getJCboFieldType().getModel().getSelectedItem(); |
189 |
int fieldType = FieldDescription.stringToType(strType);
|
190 |
getJTxtFieldPrecision().setEnabled((fieldType == Types.DOUBLE));
|
191 |
if (fieldType == Types.BOOLEAN) |
192 |
{ |
193 |
getJTxtFieldLength().setText("0");
|
194 |
getJTxtFieldLength().setEnabled(false);
|
195 |
} |
196 |
else
|
197 |
getJTxtFieldLength().setEnabled(true);
|
198 |
|
199 |
} |
200 |
}); |
201 |
|
202 |
} |
203 |
return jCboFieldType;
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* This method initializes jTxtFieldLength
|
208 |
*
|
209 |
* @return javax.swing.JTextField
|
210 |
*/
|
211 |
private JTextField getJTxtFieldLength() { |
212 |
if (jTxtFieldLength == null) { |
213 |
jTxtFieldLength = new JTextField(); |
214 |
jTxtFieldLength.setBounds(new java.awt.Rectangle(147,89,138,22)); |
215 |
jTxtFieldLength.setText(DEFAULT_FIELD_LENGTH); |
216 |
jTxtFieldLength.addKeyListener(checkInt); |
217 |
} |
218 |
return jTxtFieldLength;
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* This method initializes jTxtFieldPrecision
|
223 |
*
|
224 |
* @return javax.swing.JTextField
|
225 |
*/
|
226 |
private JTextField getJTxtFieldPrecision() { |
227 |
if (jTxtFieldPrecision == null) { |
228 |
jTxtFieldPrecision = new JTextField(); |
229 |
jTxtFieldPrecision.setBounds(new java.awt.Rectangle(147,126,138,22)); |
230 |
jTxtFieldPrecision.setEnabled(false);
|
231 |
jTxtFieldPrecision.addKeyListener(checkInt ); |
232 |
} |
233 |
return jTxtFieldPrecision;
|
234 |
} |
235 |
|
236 |
/**
|
237 |
* This method initializes jTxtDefaultValue
|
238 |
*
|
239 |
* @return javax.swing.JTextField
|
240 |
*/
|
241 |
private JTextField getJTxtDefaultValue() { |
242 |
if (jTxtDefaultValue == null) { |
243 |
jTxtDefaultValue = new JTextField(); |
244 |
jTxtDefaultValue.setBounds(new java.awt.Rectangle(147,163,138,22)); |
245 |
} |
246 |
return jTxtDefaultValue;
|
247 |
} |
248 |
|
249 |
public FieldDescription getFieldDescription() throws ParseException |
250 |
{ |
251 |
FieldDescription newField = new FieldDescription();
|
252 |
newField.setFieldName(getJTxtFieldName().getText()); |
253 |
String strType = (String) getJCboFieldType().getModel().getSelectedItem(); |
254 |
int fieldType = FieldDescription.stringToType(strType);
|
255 |
newField.setFieldType(fieldType); |
256 |
try {
|
257 |
int fieldLength = Integer.parseInt(getJTxtFieldLength().getText()); |
258 |
newField.setFieldLength(fieldLength); |
259 |
} catch (Exception e) { |
260 |
throw new ParseException(e.getMessage(), 0); |
261 |
} |
262 |
|
263 |
if (fieldType == Types.DOUBLE) |
264 |
{ |
265 |
newField.setFieldDecimalCount( |
266 |
Integer.parseInt(
|
267 |
getJTxtFieldPrecision().getText())); |
268 |
} |
269 |
else
|
270 |
newField.setFieldDecimalCount(0);
|
271 |
String defaultValue = getJTxtDefaultValue().getText();
|
272 |
if (defaultValue != null) |
273 |
{ |
274 |
|
275 |
if (defaultValue.compareTo("")==0) |
276 |
newField.setDefaultValue(ValueFactory.createNullValue()); |
277 |
else
|
278 |
newField.setDefaultValue(ValueFactory.createValueByType(defaultValue, fieldType)); |
279 |
} |
280 |
|
281 |
return newField;
|
282 |
} |
283 |
|
284 |
public void setOkAction(ActionListener okAction) { |
285 |
getJPanelOkCancel().setOkButtonActionListener(okAction); |
286 |
|
287 |
} |
288 |
|
289 |
/**
|
290 |
* This method initializes jPanel
|
291 |
*
|
292 |
* @return javax.swing.JPanel
|
293 |
*/
|
294 |
private JPanel getJPanel() { |
295 |
if (jPanel == null) { |
296 |
jPanel = new JPanel(); |
297 |
jPanel.setLayout(null);
|
298 |
|
299 |
jPanel.add(getJPnlFields(), null);
|
300 |
} |
301 |
return jPanel;
|
302 |
} |
303 |
|
304 |
/**
|
305 |
* This method initializes jPanelOkCancel
|
306 |
*
|
307 |
* @return javax.swing.JPanel
|
308 |
*/
|
309 |
private AcceptCancelPanel getJPanelOkCancel() {
|
310 |
if (jPanelOkCancel == null) { |
311 |
jPanelOkCancel = new AcceptCancelPanel();
|
312 |
jPanelOkCancel.setCancelButtonActionListener(new ActionListener(){ |
313 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
314 |
PluginServices.getMDIManager().closeWindow(FPanelCreateField.this); |
315 |
}; |
316 |
}); |
317 |
jPanelOkCancel.setPreferredSize(new java.awt.Dimension(10,50)); |
318 |
} |
319 |
return jPanelOkCancel;
|
320 |
} |
321 |
|
322 |
/**
|
323 |
* This method initializes jPnlFields
|
324 |
*
|
325 |
* @return javax.swing.JPanel
|
326 |
*/
|
327 |
private JPanel getJPnlFields() { |
328 |
if (jPnlFields == null) { |
329 |
GridLayout gridLayout = new GridLayout(); |
330 |
gridLayout.setRows(6);
|
331 |
gridLayout.setVgap(3);
|
332 |
gridLayout.setHgap(5);
|
333 |
gridLayout.setColumns(2);
|
334 |
jPnlFields = new JPanel(); |
335 |
jPnlFields.setLayout(gridLayout); |
336 |
jPnlFields.setBounds(new java.awt.Rectangle(5,12,290,142)); |
337 |
jLblDefaultValue = new JLabel(); |
338 |
jLblDefaultValue.setBounds(new java.awt.Rectangle(14,163,125,22)); |
339 |
jLblDefaultValue.setText(PluginServices.getText(this, "default_value")); |
340 |
jLblFieldPrecision = new JLabel(); |
341 |
jLblFieldPrecision.setBounds(new java.awt.Rectangle(14,126,112,22)); |
342 |
jLblFieldPrecision.setText(PluginServices.getText(this, "precision")); |
343 |
jLblFieldLength = new JLabel(); |
344 |
jLblFieldLength.setBounds(new java.awt.Rectangle(14,89,99,22)); |
345 |
jLblFieldLength.setText(PluginServices.getText(this, "field_length")); |
346 |
jLblFieldType = new JLabel(); |
347 |
jLblFieldType.setBounds(new java.awt.Rectangle(14,52,94,22)); |
348 |
jLblFieldType.setText(PluginServices.getText(this, "field_type")); |
349 |
jLblFieldName = new JLabel(); |
350 |
jLblFieldName.setText(PluginServices.getText(this, "field_name")); |
351 |
jLblFieldName.setBounds(new java.awt.Rectangle(14,15,99,22)); |
352 |
jPnlFields.add(jLblFieldName, null);
|
353 |
jPnlFields.add(getJTxtFieldName(), null);
|
354 |
jPnlFields.add(jLblFieldType, null);
|
355 |
jPnlFields.add(getJCboFieldType(), null);
|
356 |
jPnlFields.add(jLblFieldLength, null);
|
357 |
jPnlFields.add(getJTxtFieldLength(), null);
|
358 |
jPnlFields.add(jLblFieldPrecision, null);
|
359 |
jPnlFields.add(getJTxtFieldPrecision(), null);
|
360 |
jPnlFields.add(jLblDefaultValue, null);
|
361 |
jPnlFields.add(getJTxtDefaultValue(), null);
|
362 |
} |
363 |
return jPnlFields;
|
364 |
} |
365 |
|
366 |
public void setCurrentFieldNames(String[] fieldNames) { |
367 |
currentFieldNames = fieldNames; |
368 |
String newField = PluginServices.getText(this, "new_field").replaceAll(" +", "_"); |
369 |
int index=0; |
370 |
for (int i = 0; i < currentFieldNames.length; i++) |
371 |
if (currentFieldNames[i].startsWith(newField)) {
|
372 |
index = Integer.parseInt(currentFieldNames[i].replaceAll(newField,"")); |
373 |
} |
374 |
jTxtFieldName.setText(newField+(++index)); |
375 |
} |
376 |
|
377 |
} // @jve:decl-index=0:visual-constraint="9,10"
|