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