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 |
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 |
|
28 |
/**
|
29 |
* @author fjp
|
30 |
*
|
31 |
* 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 |
private static final int MAX_FIELD_LENGTH = 254; |
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 |
|
46 |
|
47 |
public JPanelFieldDefinition(JWizardComponents wizardComponents) {
|
48 |
super(wizardComponents, null); |
49 |
initialize(); |
50 |
// TODO Auto-generated constructor stub
|
51 |
} |
52 |
|
53 |
|
54 |
/* (non-Javadoc)
|
55 |
* @see jwizardcomponent.JWizardPanel#next()
|
56 |
*/
|
57 |
public void next() { |
58 |
DefaultTableModel tm=(DefaultTableModel) jTable.getModel(); |
59 |
boolean valid=true; |
60 |
for (int i = 0;i<tm.getRowCount();i++) { |
61 |
String s=(String)tm.getValueAt(i,0); |
62 |
valid=validateFieldName(s); |
63 |
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 |
"\n"+PluginServices.getText(this, "length_of_field")+ " '"+ s + "' " + PluginServices.getText(this, "will_be_truncated")); |
68 |
tm.setValueAt(String.valueOf(MAX_FIELD_LENGTH),i,2); |
69 |
} |
70 |
|
71 |
} |
72 |
|
73 |
//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 |
if (valid)
|
85 |
super.next();
|
86 |
if (!((FileBasedPanel)getWizardComponents().getWizardPanel(2)).getPath().equals("")) |
87 |
setFinishButtonEnabled(true);
|
88 |
else
|
89 |
setFinishButtonEnabled(false);
|
90 |
} |
91 |
|
92 |
|
93 |
|
94 |
private boolean validateFieldName(String s) { |
95 |
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 |
/**
|
108 |
* This method initializes this
|
109 |
*
|
110 |
*/
|
111 |
private void initialize() { |
112 |
jLabel = new JLabel(); |
113 |
jLabel.setText(PluginServices.getText(this,"define_fields")); |
114 |
this.setLayout(new BorderLayout(5,5)); |
115 |
this.setSize(new java.awt.Dimension(437,232)); |
116 |
this.add(jLabel, java.awt.BorderLayout.NORTH);
|
117 |
this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
|
118 |
this.add(getJPanelEast(), java.awt.BorderLayout.EAST);
|
119 |
} |
120 |
|
121 |
|
122 |
/**
|
123 |
* This method initializes jScrollPane
|
124 |
*
|
125 |
* @return javax.swing.JScrollPane
|
126 |
*/
|
127 |
private JScrollPane getJScrollPane() { |
128 |
if (jScrollPane == null) { |
129 |
jScrollPane = new JScrollPane(); |
130 |
jScrollPane.setViewportView(getJTable()); |
131 |
} |
132 |
return jScrollPane;
|
133 |
} |
134 |
|
135 |
|
136 |
/**
|
137 |
* This method initializes jTable
|
138 |
*
|
139 |
* @return javax.swing.JTable
|
140 |
*/
|
141 |
private JTable getJTable() { |
142 |
if (jTable == null) { |
143 |
jTable = new JTable(); |
144 |
DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
145 |
tm.addColumn(PluginServices.getText(this,"field")); |
146 |
|
147 |
// TableColumn fieldTypeColumn = new TableColumn(1);
|
148 |
// fieldTypeColumn.setHeaderValue("Type");
|
149 |
// jTable.addColumn(fieldTypeColumn);
|
150 |
tm.addColumn(PluginServices.getText(this,"type")); |
151 |
// MIRAR EL C?DIGO DEL BOT?N DE A?ADIR CAMPO PARA VER EL CellEditor con comboBox
|
152 |
|
153 |
|
154 |
/* TableColumn fieldLengthColumn = new TableColumn(2);
|
155 |
fieldLengthColumn.setHeaderValue("Length");
|
156 |
// fieldLengthColumn.setCellRenderer(new DefaultTableCellRenderer());
|
157 |
jTable.addColumn(fieldLengthColumn); */
|
158 |
tm.addColumn(PluginServices.getText(this,"length")); |
159 |
|
160 |
// 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 |
|
181 |
} |
182 |
return jTable;
|
183 |
} |
184 |
|
185 |
|
186 |
/**
|
187 |
* This method initializes jPanelWest
|
188 |
*
|
189 |
* @return javax.swing.JPanel
|
190 |
*/
|
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 |
|
202 |
|
203 |
/**
|
204 |
* This method initializes jButtonAddField
|
205 |
*
|
206 |
* @return javax.swing.JButton
|
207 |
*/
|
208 |
private JButton getJButtonAddField() { |
209 |
if (jButtonAddField == null) { |
210 |
jButtonAddField = new JButton(); |
211 |
jButtonAddField.setText(PluginServices.getText(this,"add_field")); |
212 |
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 |
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 |
// Add a new row
|
235 |
Object[] newRow = new Object[tm.getColumnCount()]; |
236 |
newRow[0] = newFieldName;
|
237 |
newRow[1] = "String"; |
238 |
newRow[2] = "20"; |
239 |
tm.addRow(newRow); |
240 |
|
241 |
// 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 |
|
252 |
TableColumn widthColumn = jTable.getColumnModel().getColumn(2); |
253 |
|
254 |
// tm.setValueAt("NewField", tm.getRowCount()-1, 0);
|
255 |
} |
256 |
}); |
257 |
|
258 |
} |
259 |
return jButtonAddField;
|
260 |
} |
261 |
|
262 |
|
263 |
/**
|
264 |
* This method initializes jButton
|
265 |
*
|
266 |
* @return javax.swing.JButton
|
267 |
*/
|
268 |
private JButton getJButtonDeleteField() { |
269 |
if (jButtonDeleteField == null) { |
270 |
jButtonDeleteField = new JButton(); |
271 |
jButtonDeleteField.setText(PluginServices.getText(this,"delete_field")); |
272 |
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 |
|
279 |
for (int i=selecteds.length-1; i >=0; i--) |
280 |
tm.removeRow(selecteds[i]); |
281 |
} |
282 |
}); |
283 |
} |
284 |
return jButtonDeleteField;
|
285 |
} |
286 |
|
287 |
|
288 |
/**
|
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 |
|
297 |
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 |
int fieldLength = Integer.parseInt((String) tm.getValueAt(i,2)); |
313 |
|
314 |
|
315 |
fieldsDesc[i].setFieldLength(fieldLength); |
316 |
|
317 |
// TODO: HACERLO BIEN
|
318 |
if (strType.equals("Double")) |
319 |
fieldsDesc[i].setFieldDecimalCount(5);
|
320 |
|
321 |
} |
322 |
|
323 |
return fieldsDesc;
|
324 |
} |
325 |
|
326 |
|
327 |
|
328 |
} // @jve:decl-index=0:visual-constraint="10,10"
|