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