svn-gvsig-desktop / tags / v1_1_Build_1010 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / panels / JPanelFieldDefinition.java @ 12804
History | View | Annotate | Download (11.7 KB)
1 | 4364 | fjp | package com.iver.cit.gvsig.gui.cad.panels; |
---|---|---|---|
2 | |||
3 | 4380 | fjp | import java.awt.BorderLayout; |
4 | 6010 | caballero | import java.awt.Component; |
5 | 4385 | fjp | import java.sql.Types; |
6 | 7493 | jaume | import java.util.ArrayList; |
7 | 4380 | fjp | |
8 | import javax.swing.DefaultCellEditor; |
||
9 | import javax.swing.JButton; |
||
10 | import javax.swing.JComboBox; |
||
11 | 4364 | fjp | import javax.swing.JLabel; |
12 | 6010 | caballero | import javax.swing.JOptionPane; |
13 | 4380 | fjp | import javax.swing.JPanel; |
14 | 4367 | fjp | import javax.swing.JScrollPane; |
15 | import javax.swing.JTable; |
||
16 | 4380 | fjp | 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 | 4364 | fjp | |
22 | 7493 | jaume | import jwizardcomponent.JWizardComponents; |
23 | import jwizardcomponent.JWizardPanel; |
||
24 | |||
25 | 4584 | caballero | import com.iver.andami.PluginServices; |
26 | 4385 | fjp | import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
27 | 10043 | jmvivo | import com.iver.cit.gvsig.fmap.edition.IWriter; |
28 | 4385 | fjp | |
29 | 4364 | fjp | /**
|
30 | * @author fjp
|
||
31 | 4584 | caballero | *
|
32 | 4364 | fjp | * 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 | 4367 | fjp | private JLabel jLabel = null; |
40 | private JScrollPane jScrollPane = null; |
||
41 | private JTable jTable = null; |
||
42 | 4380 | fjp | private JPanel jPanelEast = null; |
43 | private JButton jButtonAddField = null; |
||
44 | private JButton jButtonDeleteField = null; |
||
45 | 8765 | jjdelcerro | private int MAX_FIELD_LENGTH = 254; |
46 | 10043 | jmvivo | |
47 | private IWriter writer = null; |
||
48 | 4367 | fjp | |
49 | |||
50 | 4380 | fjp | public JPanelFieldDefinition(JWizardComponents wizardComponents) {
|
51 | super(wizardComponents, null); |
||
52 | 4364 | fjp | initialize(); |
53 | // TODO Auto-generated constructor stub
|
||
54 | } |
||
55 | |||
56 | 4584 | caballero | |
57 | 4380 | fjp | /* (non-Javadoc)
|
58 | * @see jwizardcomponent.JWizardPanel#next()
|
||
59 | */
|
||
60 | public void next() { |
||
61 | 6010 | caballero | DefaultTableModel tm=(DefaultTableModel) jTable.getModel(); |
62 | boolean valid=true; |
||
63 | for (int i = 0;i<tm.getRowCount();i++) { |
||
64 | 6727 | caballero | String s=(String)tm.getValueAt(i,0); |
65 | 8765 | jjdelcerro | valid=validate(s); |
66 | 9015 | caballero | String size=(String) tm.getValueAt(i,2); |
67 | 10043 | jmvivo | valid=valid && validateInteger(size); |
68 | 9015 | caballero | if (!valid){
|
69 | return;
|
||
70 | } |
||
71 | 7493 | jaume | 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 | 7494 | jaume | "\n"+PluginServices.getText(this, "length_of_field")+ " '"+ s + "' " + PluginServices.getText(this, "will_be_truncated")); |
76 | 7493 | jaume | tm.setValueAt(String.valueOf(MAX_FIELD_LENGTH),i,2); |
77 | } |
||
78 | |||
79 | 6010 | caballero | } |
80 | 7487 | jaume | |
81 | 8765 | jjdelcerro | // ensure no field name is used more than once
|
82 | 7493 | jaume | 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 | 10043 | jmvivo | |
92 | 6010 | caballero | if (valid)
|
93 | super.next();
|
||
94 | 11976 | jmvivo | if (getWizardComponents().getWizardPanel(2) instanceof FileBasedPanel) { |
95 | if (!((FileBasedPanel)getWizardComponents().getWizardPanel(2)).getPath().equals("")) |
||
96 | setFinishButtonEnabled(true);
|
||
97 | else
|
||
98 | setFinishButtonEnabled(false);
|
||
99 | } |
||
100 | 4380 | fjp | } |
101 | |||
102 | 10043 | jmvivo | public void setWriter(IWriter writer) { |
103 | this.writer = writer;
|
||
104 | } |
||
105 | |||
106 | public IWriter getWriter() {
|
||
107 | return this.writer; |
||
108 | } |
||
109 | 4380 | fjp | |
110 | 9015 | caballero | 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 | 8765 | jjdelcerro | private boolean validate(String s) { |
126 | 6010 | caballero | boolean valid=true; |
127 | 10043 | jmvivo | 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 | 6010 | caballero | 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 | 10043 | jmvivo | 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 | 6010 | caballero | return valid;
|
159 | } |
||
160 | |||
161 | |||
162 | 4364 | fjp | /**
|
163 | * This method initializes this
|
||
164 | 4584 | caballero | *
|
165 | 4364 | fjp | */
|
166 | private void initialize() { |
||
167 | 4367 | fjp | jLabel = new JLabel(); |
168 | 4584 | caballero | jLabel.setText(PluginServices.getText(this,"define_fields")); |
169 | 4380 | fjp | this.setLayout(new BorderLayout(5,5)); |
170 | 8968 | caballero | this.setSize(new java.awt.Dimension(499,232)); |
171 | 4380 | fjp | this.add(jLabel, java.awt.BorderLayout.NORTH);
|
172 | this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
|
||
173 | this.add(getJPanelEast(), java.awt.BorderLayout.EAST);
|
||
174 | 4364 | fjp | } |
175 | |||
176 | |||
177 | 4367 | fjp | /**
|
178 | 4584 | caballero | * This method initializes jScrollPane
|
179 | *
|
||
180 | * @return javax.swing.JScrollPane
|
||
181 | 4367 | fjp | */
|
182 | private JScrollPane getJScrollPane() { |
||
183 | if (jScrollPane == null) { |
||
184 | jScrollPane = new JScrollPane(); |
||
185 | jScrollPane.setViewportView(getJTable()); |
||
186 | } |
||
187 | return jScrollPane;
|
||
188 | } |
||
189 | 4364 | fjp | |
190 | 4367 | fjp | |
191 | /**
|
||
192 | 4584 | caballero | * This method initializes jTable
|
193 | *
|
||
194 | * @return javax.swing.JTable
|
||
195 | 4367 | fjp | */
|
196 | private JTable getJTable() { |
||
197 | if (jTable == null) { |
||
198 | jTable = new JTable(); |
||
199 | 8765 | jjdelcerro | |
200 | 4380 | fjp | DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
201 | 4584 | caballero | tm.addColumn(PluginServices.getText(this,"field")); |
202 | |||
203 | 9015 | caballero | |
204 | 4380 | fjp | // TableColumn fieldTypeColumn = new TableColumn(1);
|
205 | // fieldTypeColumn.setHeaderValue("Type");
|
||
206 | // jTable.addColumn(fieldTypeColumn);
|
||
207 | 4584 | caballero | tm.addColumn(PluginServices.getText(this,"type")); |
208 | 4380 | fjp | // MIRAR EL C?DIGO DEL BOT?N DE A?ADIR CAMPO PARA VER EL CellEditor con comboBox
|
209 | |||
210 | 4584 | caballero | |
211 | 9015 | caballero | |
212 | 4380 | fjp | /* TableColumn fieldLengthColumn = new TableColumn(2);
|
213 | fieldLengthColumn.setHeaderValue("Length");
|
||
214 | // fieldLengthColumn.setCellRenderer(new DefaultTableCellRenderer());
|
||
215 | jTable.addColumn(fieldLengthColumn); */
|
||
216 | 4584 | caballero | tm.addColumn(PluginServices.getText(this,"length")); |
217 | |||
218 | 4380 | fjp | // 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 | 8870 | jmvivo | jTable.getColumn(PluginServices.getText(this,"field")).setWidth(180); |
238 | 4380 | fjp | |
239 | 4584 | caballero | |
240 | 4367 | fjp | } |
241 | return jTable;
|
||
242 | } |
||
243 | |||
244 | |||
245 | 4380 | fjp | /**
|
246 | 4584 | caballero | * This method initializes jPanelWest
|
247 | *
|
||
248 | * @return javax.swing.JPanel
|
||
249 | 4380 | fjp | */
|
250 | private JPanel getJPanelEast() { |
||
251 | if (jPanelEast == null) { |
||
252 | jPanelEast = new JPanel(); |
||
253 | jPanelEast.setLayout(null);
|
||
254 | 8968 | caballero | jPanelEast.setPreferredSize(new java.awt.Dimension(170,100)); |
255 | 4380 | fjp | jPanelEast.add(getJButtonAddField(), null);
|
256 | jPanelEast.add(getJButtonDeleteField(), null);
|
||
257 | } |
||
258 | return jPanelEast;
|
||
259 | } |
||
260 | 4367 | fjp | |
261 | 4380 | fjp | |
262 | /**
|
||
263 | 4584 | caballero | * This method initializes jButtonAddField
|
264 | *
|
||
265 | * @return javax.swing.JButton
|
||
266 | 4380 | fjp | */
|
267 | private JButton getJButtonAddField() { |
||
268 | if (jButtonAddField == null) { |
||
269 | jButtonAddField = new JButton(); |
||
270 | 4584 | caballero | jButtonAddField.setText(PluginServices.getText(this,"add_field")); |
271 | 8968 | caballero | 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 | 4380 | fjp | jButtonAddField.addActionListener(new java.awt.event.ActionListener() {
|
275 | public void actionPerformed(java.awt.event.ActionEvent e) { |
||
276 | 7493 | jaume | 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 | 10043 | jmvivo | String newField = PluginServices.getText(this, "field").replaceAll(" +", "_"); |
285 | 7493 | jaume | 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 | 10043 | jmvivo | |
295 | 7493 | jaume | |
296 | 4380 | fjp | // Add a new row
|
297 | Object[] newRow = new Object[tm.getColumnCount()]; |
||
298 | 7493 | jaume | newRow[0] = newFieldName;
|
299 | 4380 | fjp | newRow[1] = "String"; |
300 | 4534 | fjp | newRow[2] = "20"; |
301 | 4380 | fjp | tm.addRow(newRow); |
302 | 4584 | caballero | |
303 | 4380 | fjp | // 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 | 4534 | fjp | |
314 | TableColumn widthColumn = jTable.getColumnModel().getColumn(2); |
||
315 | 4584 | caballero | |
316 | 4380 | fjp | // tm.setValueAt("NewField", tm.getRowCount()-1, 0);
|
317 | } |
||
318 | }); |
||
319 | 4584 | caballero | |
320 | 4380 | fjp | } |
321 | return jButtonAddField;
|
||
322 | } |
||
323 | |||
324 | |||
325 | /**
|
||
326 | 4584 | caballero | * This method initializes jButton
|
327 | *
|
||
328 | * @return javax.swing.JButton
|
||
329 | 4380 | fjp | */
|
330 | private JButton getJButtonDeleteField() { |
||
331 | if (jButtonDeleteField == null) { |
||
332 | jButtonDeleteField = new JButton(); |
||
333 | 4584 | caballero | jButtonDeleteField.setText(PluginServices.getText(this,"delete_field")); |
334 | 8968 | caballero | jButtonDeleteField.setLocation(new java.awt.Point(7,33)); |
335 | jButtonDeleteField.setSize(new java.awt.Dimension(145,23)); |
||
336 | 4380 | fjp | 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 | 4584 | caballero | |
342 | 4380 | fjp | for (int i=selecteds.length-1; i >=0; i--) |
343 | tm.removeRow(selecteds[i]); |
||
344 | } |
||
345 | }); |
||
346 | } |
||
347 | return jButtonDeleteField;
|
||
348 | } |
||
349 | |||
350 | |||
351 | 4385 | fjp | /**
|
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 | 4584 | caballero | |
360 | 4385 | fjp | 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 | 4534 | fjp | int fieldLength = Integer.parseInt((String) tm.getValueAt(i,2)); |
376 | fieldsDesc[i].setFieldLength(fieldLength); |
||
377 | 4584 | caballero | |
378 | 4385 | fjp | // TODO: HACERLO BIEN
|
379 | if (strType.equals("Double")) |
||
380 | fieldsDesc[i].setFieldDecimalCount(5);
|
||
381 | 4584 | caballero | |
382 | 4385 | fjp | } |
383 | 4584 | caballero | |
384 | 4385 | fjp | return fieldsDesc;
|
385 | } |
||
386 | 4380 | fjp | |
387 | 4385 | fjp | |
388 | |||
389 | 4364 | fjp | } // @jve:decl-index=0:visual-constraint="10,10" |