svn-gvsig-desktop / tags / v1_0_2_Build_896 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / panels / JPanelFieldDefinition.java @ 10391
History | View | Annotate | Download (11.6 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 | 5178 | caballero | if (!((FileBasedPanel)getWizardComponents().getWizardPanel(2)).getPath().equals("")) |
95 | 6158 | caballero | setFinishButtonEnabled(true);
|
96 | 5178 | caballero | else
|
97 | 6158 | caballero | setFinishButtonEnabled(false);
|
98 | 4380 | fjp | } |
99 | |||
100 | 10043 | jmvivo | public void setWriter(IWriter writer) { |
101 | this.writer = writer;
|
||
102 | } |
||
103 | |||
104 | public IWriter getWriter() {
|
||
105 | return this.writer; |
||
106 | } |
||
107 | 4380 | fjp | |
108 | 9015 | caballero | private boolean validateInteger(String size) { |
109 | boolean valid=true; |
||
110 | try{
|
||
111 | Integer.parseInt(size);
|
||
112 | }catch (NumberFormatException e) { |
||
113 | valid=false;
|
||
114 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
||
115 | PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
||
116 | PluginServices.getText(this,"size")+" : "+size+"\n"+ |
||
117 | PluginServices.getText(this,"incorrect_value")); |
||
118 | } |
||
119 | return valid;
|
||
120 | } |
||
121 | |||
122 | |||
123 | 8765 | jjdelcerro | private boolean validate(String s) { |
124 | 6010 | caballero | boolean valid=true; |
125 | 10043 | jmvivo | if (s.equals("")) { |
126 | valid=false;
|
||
127 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
||
128 | PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
||
129 | PluginServices.getText(this,"the_field_name_is_required")); |
||
130 | } |
||
131 | 6010 | caballero | if (s.indexOf(" ")!=-1) { |
132 | valid=false;
|
||
133 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
||
134 | PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
||
135 | PluginServices.getText(this,"field")+" : "+s+"\n"+ |
||
136 | PluginServices.getText(this,"contiene_espacios_en_blanco")); |
||
137 | } |
||
138 | 10043 | jmvivo | if (this.writer != null && this.writer.getCapability("FieldNameMaxLength") != null) { |
139 | String value = writer.getCapability("FieldNameMaxLength"); |
||
140 | int intValue;
|
||
141 | try {
|
||
142 | intValue = Integer.parseInt(value);
|
||
143 | } catch (NumberFormatException e) { |
||
144 | intValue = 0;
|
||
145 | } |
||
146 | if (intValue > 0 && s.length() > intValue) { |
||
147 | valid=false;
|
||
148 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
||
149 | PluginServices.getText(this,"no_puede_continuar")+"\n"+ |
||
150 | PluginServices.getText(this,"field")+" : "+s+"\n"+ |
||
151 | PluginServices.getText(this,"too_long_name")+"\n"+ |
||
152 | PluginServices.getText(this,"maximun_name_size")+" : "+intValue+"\n" |
||
153 | ); |
||
154 | } |
||
155 | } |
||
156 | 6010 | caballero | return valid;
|
157 | } |
||
158 | |||
159 | |||
160 | 4364 | fjp | /**
|
161 | * This method initializes this
|
||
162 | 4584 | caballero | *
|
163 | 4364 | fjp | */
|
164 | private void initialize() { |
||
165 | 4367 | fjp | jLabel = new JLabel(); |
166 | 4584 | caballero | jLabel.setText(PluginServices.getText(this,"define_fields")); |
167 | 4380 | fjp | this.setLayout(new BorderLayout(5,5)); |
168 | 8968 | caballero | this.setSize(new java.awt.Dimension(499,232)); |
169 | 4380 | fjp | this.add(jLabel, java.awt.BorderLayout.NORTH);
|
170 | this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
|
||
171 | this.add(getJPanelEast(), java.awt.BorderLayout.EAST);
|
||
172 | 4364 | fjp | } |
173 | |||
174 | |||
175 | 4367 | fjp | /**
|
176 | 4584 | caballero | * This method initializes jScrollPane
|
177 | *
|
||
178 | * @return javax.swing.JScrollPane
|
||
179 | 4367 | fjp | */
|
180 | private JScrollPane getJScrollPane() { |
||
181 | if (jScrollPane == null) { |
||
182 | jScrollPane = new JScrollPane(); |
||
183 | jScrollPane.setViewportView(getJTable()); |
||
184 | } |
||
185 | return jScrollPane;
|
||
186 | } |
||
187 | 4364 | fjp | |
188 | 4367 | fjp | |
189 | /**
|
||
190 | 4584 | caballero | * This method initializes jTable
|
191 | *
|
||
192 | * @return javax.swing.JTable
|
||
193 | 4367 | fjp | */
|
194 | private JTable getJTable() { |
||
195 | if (jTable == null) { |
||
196 | jTable = new JTable(); |
||
197 | 8765 | jjdelcerro | |
198 | 4380 | fjp | DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
199 | 4584 | caballero | tm.addColumn(PluginServices.getText(this,"field")); |
200 | |||
201 | 9015 | caballero | |
202 | 4380 | fjp | // TableColumn fieldTypeColumn = new TableColumn(1);
|
203 | // fieldTypeColumn.setHeaderValue("Type");
|
||
204 | // jTable.addColumn(fieldTypeColumn);
|
||
205 | 4584 | caballero | tm.addColumn(PluginServices.getText(this,"type")); |
206 | 4380 | fjp | // MIRAR EL C?DIGO DEL BOT?N DE A?ADIR CAMPO PARA VER EL CellEditor con comboBox
|
207 | |||
208 | 4584 | caballero | |
209 | 9015 | caballero | |
210 | 4380 | fjp | /* TableColumn fieldLengthColumn = new TableColumn(2);
|
211 | fieldLengthColumn.setHeaderValue("Length");
|
||
212 | // fieldLengthColumn.setCellRenderer(new DefaultTableCellRenderer());
|
||
213 | jTable.addColumn(fieldLengthColumn); */
|
||
214 | 4584 | caballero | tm.addColumn(PluginServices.getText(this,"length")); |
215 | |||
216 | 4380 | fjp | // Ask to be notified of selection changes.
|
217 | ListSelectionModel rowSM = jTable.getSelectionModel();
|
||
218 | rowSM.addListSelectionListener(new ListSelectionListener() { |
||
219 | public void valueChanged(ListSelectionEvent e) { |
||
220 | //Ignore extra messages.
|
||
221 | if (e.getValueIsAdjusting()) return; |
||
222 | |||
223 | ListSelectionModel lsm =
|
||
224 | (ListSelectionModel)e.getSource();
|
||
225 | if (lsm.isSelectionEmpty()) {
|
||
226 | //no rows are selected
|
||
227 | jButtonDeleteField.setEnabled(false);
|
||
228 | } else {
|
||
229 | // int selectedRow = lsm.getMinSelectionIndex();
|
||
230 | //selectedRow is selected
|
||
231 | jButtonDeleteField.setEnabled(true);
|
||
232 | } |
||
233 | } |
||
234 | }); |
||
235 | 8870 | jmvivo | jTable.getColumn(PluginServices.getText(this,"field")).setWidth(180); |
236 | 4380 | fjp | |
237 | 4584 | caballero | |
238 | 4367 | fjp | } |
239 | return jTable;
|
||
240 | } |
||
241 | |||
242 | |||
243 | 4380 | fjp | /**
|
244 | 4584 | caballero | * This method initializes jPanelWest
|
245 | *
|
||
246 | * @return javax.swing.JPanel
|
||
247 | 4380 | fjp | */
|
248 | private JPanel getJPanelEast() { |
||
249 | if (jPanelEast == null) { |
||
250 | jPanelEast = new JPanel(); |
||
251 | jPanelEast.setLayout(null);
|
||
252 | 8968 | caballero | jPanelEast.setPreferredSize(new java.awt.Dimension(170,100)); |
253 | 4380 | fjp | jPanelEast.add(getJButtonAddField(), null);
|
254 | jPanelEast.add(getJButtonDeleteField(), null);
|
||
255 | } |
||
256 | return jPanelEast;
|
||
257 | } |
||
258 | 4367 | fjp | |
259 | 4380 | fjp | |
260 | /**
|
||
261 | 4584 | caballero | * This method initializes jButtonAddField
|
262 | *
|
||
263 | * @return javax.swing.JButton
|
||
264 | 4380 | fjp | */
|
265 | private JButton getJButtonAddField() { |
||
266 | if (jButtonAddField == null) { |
||
267 | jButtonAddField = new JButton(); |
||
268 | 4584 | caballero | jButtonAddField.setText(PluginServices.getText(this,"add_field")); |
269 | 8968 | caballero | jButtonAddField.setLocation(new java.awt.Point(7,5)); |
270 | jButtonAddField.setSize(new java.awt.Dimension(145,23)); |
||
271 | jButtonAddField.setPreferredSize(new java.awt.Dimension(100,26)); |
||
272 | 4380 | fjp | jButtonAddField.addActionListener(new java.awt.event.ActionListener() {
|
273 | public void actionPerformed(java.awt.event.ActionEvent e) { |
||
274 | 7493 | jaume | DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
275 | |||
276 | // Figure out a suitable field name
|
||
277 | ArrayList fieldNames = new ArrayList(); |
||
278 | for (int i = 0; i < jTable.getRowCount(); i++) { |
||
279 | fieldNames.add(tm.getValueAt(i, 0));
|
||
280 | } |
||
281 | String[] currentFieldNames = (String[]) fieldNames.toArray(new String[0]); |
||
282 | 10043 | jmvivo | String newField = PluginServices.getText(this, "field").replaceAll(" +", "_"); |
283 | 7493 | jaume | int index=0; |
284 | for (int i = 0; i < currentFieldNames.length; i++) { |
||
285 | if (currentFieldNames[i].startsWith(newField)) {
|
||
286 | try {
|
||
287 | index = Integer.parseInt(currentFieldNames[i].replaceAll(newField,"")); |
||
288 | } catch (Exception ex) { /* we don't care */} |
||
289 | } |
||
290 | } |
||
291 | String newFieldName = newField+(++index);
|
||
292 | 10043 | jmvivo | |
293 | 7493 | jaume | |
294 | 4380 | fjp | // Add a new row
|
295 | Object[] newRow = new Object[tm.getColumnCount()]; |
||
296 | 7493 | jaume | newRow[0] = newFieldName;
|
297 | 4380 | fjp | newRow[1] = "String"; |
298 | 4534 | fjp | newRow[2] = "20"; |
299 | 4380 | fjp | tm.addRow(newRow); |
300 | 4584 | caballero | |
301 | 4380 | fjp | // Esto lo a?ado aqu? porque si no tiene registros, no hace caso. (Por eso no
|
302 | // lo pongo en getJTable()
|
||
303 | TableColumn typeColumn = jTable.getColumnModel().getColumn(1); |
||
304 | JComboBox comboBox = new JComboBox(); |
||
305 | comboBox.addItem("Boolean");
|
||
306 | comboBox.addItem("Date");
|
||
307 | comboBox.addItem("Integer");
|
||
308 | comboBox.addItem("Double");
|
||
309 | comboBox.addItem("String");
|
||
310 | typeColumn.setCellEditor(new DefaultCellEditor(comboBox)); |
||
311 | 4534 | fjp | |
312 | TableColumn widthColumn = jTable.getColumnModel().getColumn(2); |
||
313 | 4584 | caballero | |
314 | 4380 | fjp | // tm.setValueAt("NewField", tm.getRowCount()-1, 0);
|
315 | } |
||
316 | }); |
||
317 | 4584 | caballero | |
318 | 4380 | fjp | } |
319 | return jButtonAddField;
|
||
320 | } |
||
321 | |||
322 | |||
323 | /**
|
||
324 | 4584 | caballero | * This method initializes jButton
|
325 | *
|
||
326 | * @return javax.swing.JButton
|
||
327 | 4380 | fjp | */
|
328 | private JButton getJButtonDeleteField() { |
||
329 | if (jButtonDeleteField == null) { |
||
330 | jButtonDeleteField = new JButton(); |
||
331 | 4584 | caballero | jButtonDeleteField.setText(PluginServices.getText(this,"delete_field")); |
332 | 8968 | caballero | jButtonDeleteField.setLocation(new java.awt.Point(7,33)); |
333 | jButtonDeleteField.setSize(new java.awt.Dimension(145,23)); |
||
334 | 4380 | fjp | jButtonDeleteField.setEnabled(false);
|
335 | jButtonDeleteField.addActionListener(new java.awt.event.ActionListener() {
|
||
336 | public void actionPerformed(java.awt.event.ActionEvent e) { |
||
337 | int[] selecteds = jTable.getSelectedRows(); |
||
338 | DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
||
339 | 4584 | caballero | |
340 | 4380 | fjp | for (int i=selecteds.length-1; i >=0; i--) |
341 | tm.removeRow(selecteds[i]); |
||
342 | } |
||
343 | }); |
||
344 | } |
||
345 | return jButtonDeleteField;
|
||
346 | } |
||
347 | |||
348 | |||
349 | 4385 | fjp | /**
|
350 | * Convierte lo que hay en la tabla en una definici?n de campos
|
||
351 | * adecuada para crear un LayerDefinition
|
||
352 | * @return
|
||
353 | */
|
||
354 | public FieldDescription[] getFieldsDescription() { |
||
355 | DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
||
356 | FieldDescription[] fieldsDesc = new FieldDescription[tm.getRowCount()]; |
||
357 | 4584 | caballero | |
358 | 4385 | fjp | for (int i=0; i < tm.getRowCount(); i++) |
359 | { |
||
360 | fieldsDesc[i] = new FieldDescription();
|
||
361 | fieldsDesc[i].setFieldName((String) tm.getValueAt(i,0)); |
||
362 | String strType = (String) tm.getValueAt(i,1); |
||
363 | if (strType.equals("String")) |
||
364 | fieldsDesc[i].setFieldType(Types.VARCHAR);
|
||
365 | if (strType.equals("Double")) |
||
366 | fieldsDesc[i].setFieldType(Types.DOUBLE);
|
||
367 | if (strType.equals("Integer")) |
||
368 | fieldsDesc[i].setFieldType(Types.INTEGER);
|
||
369 | if (strType.equals("Boolean")) |
||
370 | fieldsDesc[i].setFieldType(Types.BOOLEAN);
|
||
371 | if (strType.equals("Date")) |
||
372 | fieldsDesc[i].setFieldType(Types.DATE);
|
||
373 | 4534 | fjp | int fieldLength = Integer.parseInt((String) tm.getValueAt(i,2)); |
374 | fieldsDesc[i].setFieldLength(fieldLength); |
||
375 | 4584 | caballero | |
376 | 4385 | fjp | // TODO: HACERLO BIEN
|
377 | if (strType.equals("Double")) |
||
378 | fieldsDesc[i].setFieldDecimalCount(5);
|
||
379 | 4584 | caballero | |
380 | 4385 | fjp | } |
381 | 4584 | caballero | |
382 | 4385 | fjp | return fieldsDesc;
|
383 | } |
||
384 | 4380 | fjp | |
385 | 4385 | fjp | |
386 | |||
387 | 4364 | fjp | } // @jve:decl-index=0:visual-constraint="10,10" |