svn-gvsig-desktop / branches / v10 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / panels / JPanelFieldDefinition.java @ 11976
History | View | Annotate | Download (11.7 KB)
1 |
package com.iver.cit.gvsig.gui.cad.panels; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Component; |
5 |
import java.sql.Types; |
6 |
import java.util.ArrayList; |
7 |
|
8 |
import javax.swing.DefaultCellEditor; |
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JComboBox; |
11 |
import javax.swing.JLabel; |
12 |
import javax.swing.JOptionPane; |
13 |
import javax.swing.JPanel; |
14 |
import javax.swing.JScrollPane; |
15 |
import javax.swing.JTable; |
16 |
import javax.swing.ListSelectionModel; |
17 |
import javax.swing.event.ListSelectionEvent; |
18 |
import javax.swing.event.ListSelectionListener; |
19 |
import javax.swing.table.DefaultTableModel; |
20 |
import javax.swing.table.TableColumn; |
21 |
|
22 |
import jwizardcomponent.JWizardComponents; |
23 |
import jwizardcomponent.JWizardPanel; |
24 |
|
25 |
import com.iver.andami.PluginServices; |
26 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
27 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
28 |
|
29 |
/**
|
30 |
* @author fjp
|
31 |
*
|
32 |
* Panel para que el usuario seleccione el driver que va a utilizar para
|
33 |
* crear un tema desde cero.
|
34 |
*
|
35 |
*/
|
36 |
public class JPanelFieldDefinition extends JWizardPanel { |
37 |
|
38 |
|
39 |
private JLabel jLabel = null; |
40 |
private JScrollPane jScrollPane = null; |
41 |
private JTable jTable = null; |
42 |
private JPanel jPanelEast = null; |
43 |
private JButton jButtonAddField = null; |
44 |
private JButton jButtonDeleteField = null; |
45 |
private int MAX_FIELD_LENGTH = 254; |
46 |
|
47 |
private IWriter writer = null; |
48 |
|
49 |
|
50 |
public JPanelFieldDefinition(JWizardComponents wizardComponents) {
|
51 |
super(wizardComponents, null); |
52 |
initialize(); |
53 |
// TODO Auto-generated constructor stub
|
54 |
} |
55 |
|
56 |
|
57 |
/* (non-Javadoc)
|
58 |
* @see jwizardcomponent.JWizardPanel#next()
|
59 |
*/
|
60 |
public void next() { |
61 |
DefaultTableModel tm=(DefaultTableModel) jTable.getModel(); |
62 |
boolean valid=true; |
63 |
for (int i = 0;i<tm.getRowCount();i++) { |
64 |
String s=(String)tm.getValueAt(i,0); |
65 |
valid=validate(s); |
66 |
String size=(String) tm.getValueAt(i,2); |
67 |
valid=valid && validateInteger(size); |
68 |
if (!valid){
|
69 |
return;
|
70 |
} |
71 |
String type = (String) tm.getValueAt(i,1); |
72 |
int length = Integer.parseInt((String) tm.getValueAt(i,2)); |
73 |
if (type.equals("String") && length > MAX_FIELD_LENGTH) { |
74 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "max_length_is") + ": " + MAX_FIELD_LENGTH+ |
75 |
"\n"+PluginServices.getText(this, "length_of_field")+ " '"+ s + "' " + PluginServices.getText(this, "will_be_truncated")); |
76 |
tm.setValueAt(String.valueOf(MAX_FIELD_LENGTH),i,2); |
77 |
} |
78 |
|
79 |
} |
80 |
|
81 |
// ensure no field name is used more than once
|
82 |
ArrayList fieldNames = new ArrayList(); |
83 |
for (int i = 0; i < jTable.getRowCount(); i++) { |
84 |
if (fieldNames.contains(tm.getValueAt(i,0))) { |
85 |
valid = false;
|
86 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "two_or_more_fields_with_the_same_name")); |
87 |
break;
|
88 |
} |
89 |
fieldNames.add(tm.getValueAt(i, 0));
|
90 |
} |
91 |
|
92 |
if (valid)
|
93 |
super.next();
|
94 |
if (getWizardComponents().getWizardPanel(2) instanceof FileBasedPanel) { |
95 |
if (!((FileBasedPanel)getWizardComponents().getWizardPanel(2)).getPath().equals("")) |
96 |
setFinishButtonEnabled(true);
|
97 |
else
|
98 |
setFinishButtonEnabled(false);
|
99 |
} |
100 |
} |
101 |
|
102 |
public void setWriter(IWriter writer) { |
103 |
this.writer = writer;
|
104 |
} |
105 |
|
106 |
public IWriter getWriter() {
|
107 |
return this.writer; |
108 |
} |
109 |
|
110 |
private boolean validateInteger(String size) { |
111 |
boolean valid=true; |
112 |
try{
|
113 |
Integer.parseInt(size);
|
114 |
}catch (NumberFormatException e) { |
115 |
valid=false;
|
116 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
117 |
PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
118 |
PluginServices.getText(this,"size")+" : "+size+"\n"+ |
119 |
PluginServices.getText(this,"incorrect_value")); |
120 |
} |
121 |
return valid;
|
122 |
} |
123 |
|
124 |
|
125 |
private boolean validate(String s) { |
126 |
boolean valid=true; |
127 |
if (s.equals("")) { |
128 |
valid=false;
|
129 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
130 |
PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
131 |
PluginServices.getText(this,"the_field_name_is_required")); |
132 |
} |
133 |
if (s.indexOf(" ")!=-1) { |
134 |
valid=false;
|
135 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
136 |
PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
137 |
PluginServices.getText(this,"field")+" : "+s+"\n"+ |
138 |
PluginServices.getText(this,"contiene_espacios_en_blanco")); |
139 |
} |
140 |
if (this.writer != null && this.writer.getCapability("FieldNameMaxLength") != null) { |
141 |
String value = writer.getCapability("FieldNameMaxLength"); |
142 |
int intValue;
|
143 |
try {
|
144 |
intValue = Integer.parseInt(value);
|
145 |
} catch (NumberFormatException e) { |
146 |
intValue = 0;
|
147 |
} |
148 |
if (intValue > 0 && s.length() > intValue) { |
149 |
valid=false;
|
150 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
151 |
PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
152 |
PluginServices.getText(this,"field")+" : "+s+"\n"+ |
153 |
PluginServices.getText(this,"too_long_name")+"\n"+ |
154 |
PluginServices.getText(this,"maximun_name_size")+" : "+intValue+"\n" |
155 |
); |
156 |
} |
157 |
} |
158 |
return valid;
|
159 |
} |
160 |
|
161 |
|
162 |
/**
|
163 |
* This method initializes this
|
164 |
*
|
165 |
*/
|
166 |
private void initialize() { |
167 |
jLabel = new JLabel(); |
168 |
jLabel.setText(PluginServices.getText(this,"define_fields")); |
169 |
this.setLayout(new BorderLayout(5,5)); |
170 |
this.setSize(new java.awt.Dimension(499,232)); |
171 |
this.add(jLabel, java.awt.BorderLayout.NORTH);
|
172 |
this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
|
173 |
this.add(getJPanelEast(), java.awt.BorderLayout.EAST);
|
174 |
} |
175 |
|
176 |
|
177 |
/**
|
178 |
* This method initializes jScrollPane
|
179 |
*
|
180 |
* @return javax.swing.JScrollPane
|
181 |
*/
|
182 |
private JScrollPane getJScrollPane() { |
183 |
if (jScrollPane == null) { |
184 |
jScrollPane = new JScrollPane(); |
185 |
jScrollPane.setViewportView(getJTable()); |
186 |
} |
187 |
return jScrollPane;
|
188 |
} |
189 |
|
190 |
|
191 |
/**
|
192 |
* This method initializes jTable
|
193 |
*
|
194 |
* @return javax.swing.JTable
|
195 |
*/
|
196 |
private JTable getJTable() { |
197 |
if (jTable == null) { |
198 |
jTable = new JTable(); |
199 |
|
200 |
DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
201 |
tm.addColumn(PluginServices.getText(this,"field")); |
202 |
|
203 |
|
204 |
// TableColumn fieldTypeColumn = new TableColumn(1);
|
205 |
// fieldTypeColumn.setHeaderValue("Type");
|
206 |
// jTable.addColumn(fieldTypeColumn);
|
207 |
tm.addColumn(PluginServices.getText(this,"type")); |
208 |
// MIRAR EL C?DIGO DEL BOT?N DE A?ADIR CAMPO PARA VER EL CellEditor con comboBox
|
209 |
|
210 |
|
211 |
|
212 |
/* TableColumn fieldLengthColumn = new TableColumn(2);
|
213 |
fieldLengthColumn.setHeaderValue("Length");
|
214 |
// fieldLengthColumn.setCellRenderer(new DefaultTableCellRenderer());
|
215 |
jTable.addColumn(fieldLengthColumn); */
|
216 |
tm.addColumn(PluginServices.getText(this,"length")); |
217 |
|
218 |
// Ask to be notified of selection changes.
|
219 |
ListSelectionModel rowSM = jTable.getSelectionModel();
|
220 |
rowSM.addListSelectionListener(new ListSelectionListener() { |
221 |
public void valueChanged(ListSelectionEvent e) { |
222 |
//Ignore extra messages.
|
223 |
if (e.getValueIsAdjusting()) return; |
224 |
|
225 |
ListSelectionModel lsm =
|
226 |
(ListSelectionModel)e.getSource();
|
227 |
if (lsm.isSelectionEmpty()) {
|
228 |
//no rows are selected
|
229 |
jButtonDeleteField.setEnabled(false);
|
230 |
} else {
|
231 |
// int selectedRow = lsm.getMinSelectionIndex();
|
232 |
//selectedRow is selected
|
233 |
jButtonDeleteField.setEnabled(true);
|
234 |
} |
235 |
} |
236 |
}); |
237 |
jTable.getColumn(PluginServices.getText(this,"field")).setWidth(180); |
238 |
|
239 |
|
240 |
} |
241 |
return jTable;
|
242 |
} |
243 |
|
244 |
|
245 |
/**
|
246 |
* This method initializes jPanelWest
|
247 |
*
|
248 |
* @return javax.swing.JPanel
|
249 |
*/
|
250 |
private JPanel getJPanelEast() { |
251 |
if (jPanelEast == null) { |
252 |
jPanelEast = new JPanel(); |
253 |
jPanelEast.setLayout(null);
|
254 |
jPanelEast.setPreferredSize(new java.awt.Dimension(170,100)); |
255 |
jPanelEast.add(getJButtonAddField(), null);
|
256 |
jPanelEast.add(getJButtonDeleteField(), null);
|
257 |
} |
258 |
return jPanelEast;
|
259 |
} |
260 |
|
261 |
|
262 |
/**
|
263 |
* This method initializes jButtonAddField
|
264 |
*
|
265 |
* @return javax.swing.JButton
|
266 |
*/
|
267 |
private JButton getJButtonAddField() { |
268 |
if (jButtonAddField == null) { |
269 |
jButtonAddField = new JButton(); |
270 |
jButtonAddField.setText(PluginServices.getText(this,"add_field")); |
271 |
jButtonAddField.setLocation(new java.awt.Point(7,5)); |
272 |
jButtonAddField.setSize(new java.awt.Dimension(145,23)); |
273 |
jButtonAddField.setPreferredSize(new java.awt.Dimension(100,26)); |
274 |
jButtonAddField.addActionListener(new java.awt.event.ActionListener() {
|
275 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
276 |
DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
277 |
|
278 |
// Figure out a suitable field name
|
279 |
ArrayList fieldNames = new ArrayList(); |
280 |
for (int i = 0; i < jTable.getRowCount(); i++) { |
281 |
fieldNames.add(tm.getValueAt(i, 0));
|
282 |
} |
283 |
String[] currentFieldNames = (String[]) fieldNames.toArray(new String[0]); |
284 |
String newField = PluginServices.getText(this, "field").replaceAll(" +", "_"); |
285 |
int index=0; |
286 |
for (int i = 0; i < currentFieldNames.length; i++) { |
287 |
if (currentFieldNames[i].startsWith(newField)) {
|
288 |
try {
|
289 |
index = Integer.parseInt(currentFieldNames[i].replaceAll(newField,"")); |
290 |
} catch (Exception ex) { /* we don't care */} |
291 |
} |
292 |
} |
293 |
String newFieldName = newField+(++index);
|
294 |
|
295 |
|
296 |
// Add a new row
|
297 |
Object[] newRow = new Object[tm.getColumnCount()]; |
298 |
newRow[0] = newFieldName;
|
299 |
newRow[1] = "String"; |
300 |
newRow[2] = "20"; |
301 |
tm.addRow(newRow); |
302 |
|
303 |
// Esto lo a?ado aqu? porque si no tiene registros, no hace caso. (Por eso no
|
304 |
// lo pongo en getJTable()
|
305 |
TableColumn typeColumn = jTable.getColumnModel().getColumn(1); |
306 |
JComboBox comboBox = new JComboBox(); |
307 |
comboBox.addItem("Boolean");
|
308 |
comboBox.addItem("Date");
|
309 |
comboBox.addItem("Integer");
|
310 |
comboBox.addItem("Double");
|
311 |
comboBox.addItem("String");
|
312 |
typeColumn.setCellEditor(new DefaultCellEditor(comboBox)); |
313 |
|
314 |
TableColumn widthColumn = jTable.getColumnModel().getColumn(2); |
315 |
|
316 |
// tm.setValueAt("NewField", tm.getRowCount()-1, 0);
|
317 |
} |
318 |
}); |
319 |
|
320 |
} |
321 |
return jButtonAddField;
|
322 |
} |
323 |
|
324 |
|
325 |
/**
|
326 |
* This method initializes jButton
|
327 |
*
|
328 |
* @return javax.swing.JButton
|
329 |
*/
|
330 |
private JButton getJButtonDeleteField() { |
331 |
if (jButtonDeleteField == null) { |
332 |
jButtonDeleteField = new JButton(); |
333 |
jButtonDeleteField.setText(PluginServices.getText(this,"delete_field")); |
334 |
jButtonDeleteField.setLocation(new java.awt.Point(7,33)); |
335 |
jButtonDeleteField.setSize(new java.awt.Dimension(145,23)); |
336 |
jButtonDeleteField.setEnabled(false);
|
337 |
jButtonDeleteField.addActionListener(new java.awt.event.ActionListener() {
|
338 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
339 |
int[] selecteds = jTable.getSelectedRows(); |
340 |
DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
341 |
|
342 |
for (int i=selecteds.length-1; i >=0; i--) |
343 |
tm.removeRow(selecteds[i]); |
344 |
} |
345 |
}); |
346 |
} |
347 |
return jButtonDeleteField;
|
348 |
} |
349 |
|
350 |
|
351 |
/**
|
352 |
* Convierte lo que hay en la tabla en una definici?n de campos
|
353 |
* adecuada para crear un LayerDefinition
|
354 |
* @return
|
355 |
*/
|
356 |
public FieldDescription[] getFieldsDescription() { |
357 |
DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
358 |
FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()]; |
359 |
|
360 |
for (int i=0; i < tm.getRowCount(); i++) |
361 |
{ |
362 |
fieldsDesc[i] = new FieldDescription();
|
363 |
fieldsDesc[i].setFieldName((String) tm.getValueAt(i,0)); |
364 |
String strType = (String) tm.getValueAt(i,1); |
365 |
if (strType.equals("String")) |
366 |
fieldsDesc[i].setFieldType(Types.VARCHAR);
|
367 |
if (strType.equals("Double")) |
368 |
fieldsDesc[i].setFieldType(Types.DOUBLE);
|
369 |
if (strType.equals("Integer")) |
370 |
fieldsDesc[i].setFieldType(Types.INTEGER);
|
371 |
if (strType.equals("Boolean")) |
372 |
fieldsDesc[i].setFieldType(Types.BOOLEAN);
|
373 |
if (strType.equals("Date")) |
374 |
fieldsDesc[i].setFieldType(Types.DATE);
|
375 |
int fieldLength = Integer.parseInt((String) tm.getValueAt(i,2)); |
376 |
fieldsDesc[i].setFieldLength(fieldLength); |
377 |
|
378 |
// TODO: HACERLO BIEN
|
379 |
if (strType.equals("Double")) |
380 |
fieldsDesc[i].setFieldDecimalCount(5);
|
381 |
|
382 |
} |
383 |
|
384 |
return fieldsDesc;
|
385 |
} |
386 |
|
387 |
|
388 |
|
389 |
} // @jve:decl-index=0:visual-constraint="10,10"
|