root / branches / v2_0_0_prep / libraries / org.gvsig.newlayer / org.gvsig.newlayer.lib / org.gvsig.newlayer.lib.impl / src / main / java / org / gvsig / newlayer / impl / FeatureTypePanel.java @ 38533
History | View | Annotate | Download (39.8 KB)
1 | 36022 | fdiaz | package org.gvsig.newlayer.impl; |
---|---|---|---|
2 | |||
3 | import java.awt.BorderLayout; |
||
4 | 36721 | fdiaz | import java.awt.Component; |
5 | 38533 | jldominguez | import java.awt.GridBagConstraints; |
6 | import java.awt.GridBagLayout; |
||
7 | import java.awt.Insets; |
||
8 | 36721 | fdiaz | import java.awt.event.ActionEvent; |
9 | import java.awt.event.ActionListener; |
||
10 | 38533 | jldominguez | import java.awt.event.ItemEvent; |
11 | import java.awt.event.ItemListener; |
||
12 | import java.lang.reflect.InvocationTargetException; |
||
13 | 36022 | fdiaz | import java.util.ArrayList; |
14 | import java.util.Iterator; |
||
15 | 36220 | fdiaz | import java.util.List; |
16 | 36022 | fdiaz | |
17 | 36721 | fdiaz | import javax.swing.ComboBoxModel; |
18 | 36022 | fdiaz | import javax.swing.DefaultCellEditor; |
19 | import javax.swing.JButton; |
||
20 | 36721 | fdiaz | import javax.swing.JCheckBox; |
21 | 36022 | fdiaz | import javax.swing.JComboBox; |
22 | import javax.swing.JLabel; |
||
23 | 38533 | jldominguez | import javax.swing.JOptionPane; |
24 | 36022 | fdiaz | import javax.swing.JPanel; |
25 | import javax.swing.JScrollPane; |
||
26 | import javax.swing.JTable; |
||
27 | 38533 | jldominguez | import javax.swing.JTextField; |
28 | 36022 | fdiaz | import javax.swing.ListSelectionModel; |
29 | 38533 | jldominguez | import javax.swing.SwingUtilities; |
30 | 36721 | fdiaz | import javax.swing.UIManager; |
31 | import javax.swing.event.ListDataListener; |
||
32 | 36022 | fdiaz | import javax.swing.event.ListSelectionEvent; |
33 | import javax.swing.event.ListSelectionListener; |
||
34 | 37807 | jpiera | import javax.swing.event.TableModelEvent; |
35 | import javax.swing.event.TableModelListener; |
||
36 | 38533 | jldominguez | import javax.swing.table.DefaultTableCellRenderer; |
37 | 36022 | fdiaz | import javax.swing.table.DefaultTableModel; |
38 | 36721 | fdiaz | import javax.swing.table.TableCellRenderer; |
39 | 36022 | fdiaz | import javax.swing.table.TableColumn; |
40 | 38533 | jldominguez | import javax.swing.table.TableModel; |
41 | 36022 | fdiaz | |
42 | 36721 | fdiaz | import org.cresques.cts.IProjection; |
43 | 37807 | jpiera | import org.slf4j.Logger; |
44 | import org.slf4j.LoggerFactory; |
||
45 | |||
46 | 36721 | fdiaz | import org.gvsig.app.gui.panels.CRSSelectPanel; |
47 | import org.gvsig.app.gui.panels.crs.ISelectCrsPanel; |
||
48 | import org.gvsig.fmap.dal.DALLocator; |
||
49 | import org.gvsig.fmap.dal.DataManager; |
||
50 | 36022 | fdiaz | import org.gvsig.fmap.dal.DataServerExplorer; |
51 | 38533 | jldominguez | import org.gvsig.fmap.dal.DataStoreProviderFactory; |
52 | import org.gvsig.fmap.dal.DataTypes; |
||
53 | 36022 | fdiaz | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
54 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
55 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
56 | 37333 | fdiaz | import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
57 | 38533 | jldominguez | import org.gvsig.fmap.dal.feature.FeatureType; |
58 | 36721 | fdiaz | import org.gvsig.fmap.geom.Geometry; |
59 | 38533 | jldominguez | import org.gvsig.fmap.geom.GeometryLocator; |
60 | import org.gvsig.fmap.geom.GeometryManager; |
||
61 | import org.gvsig.fmap.geom.type.GeometryType; |
||
62 | 36022 | fdiaz | import org.gvsig.gui.beans.wizard.panel.NotContinueWizardException; |
63 | import org.gvsig.gui.beans.wizard.panel.OptionPanel; |
||
64 | import org.gvsig.i18n.Messages; |
||
65 | 36721 | fdiaz | import org.gvsig.newlayer.NewLayerService; |
66 | import org.gvsig.tools.dataTypes.DataType; |
||
67 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
68 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
69 | 36022 | fdiaz | |
70 | 36721 | fdiaz | public class FeatureTypePanel extends JPanel implements OptionPanel { |
71 | 37807 | jpiera | private static final int COLUMN_FIELD = 0; |
72 | private static final int COLUMN_TYPE = 1; |
||
73 | private static final int COLUMN_LENGTH = 2; |
||
74 | private static final int COLUMN_GEOMETRYTYPE = 3; |
||
75 | private static final int COLUMN_GEOMETRYSUBTYPE = 4; |
||
76 | private static final int COLUMN_CRS = 5; |
||
77 | private static final int COLUMN_PRIMARYKEY = 6; |
||
78 | private static final int COLUMN_MANDATORY = 7; |
||
79 | |||
80 | 36022 | fdiaz | private static final long serialVersionUID = 263541873998597624L; |
81 | 38533 | jldominguez | |
82 | private FeatureType defaultFeatType = null; |
||
83 | |||
84 | private NewLayerService newLayerService = null; |
||
85 | private int inDefaultFeatTypeFieldCount = 0; |
||
86 | private String inStoreName = null; |
||
87 | private IProjection inProjection = null; |
||
88 | private FeatureStoreProviderFactory spFactory = null; |
||
89 | private boolean defaultHasGeometry = false; |
||
90 | |||
91 | 36721 | fdiaz | private static final Logger logger = LoggerFactory |
92 | .getLogger(FeatureTypePanel.class); |
||
93 | 36022 | fdiaz | |
94 | private JLabel jLabel = null; |
||
95 | private JScrollPane jScrollPane = null; |
||
96 | protected JTable jTable = null; |
||
97 | private JPanel jPanelEast = null; |
||
98 | 38533 | jldominguez | private JButton addFieldButton = null; |
99 | private JButton deleteFieldButton = null; |
||
100 | 36022 | fdiaz | protected int MAX_FIELD_LENGTH = 254; |
101 | 38367 | jldominguez | |
102 | // we dont need this list
|
||
103 | // we'll clear and rebuild featureType each time
|
||
104 | // the user clicks next
|
||
105 | // private List removedFields;
|
||
106 | 36022 | fdiaz | |
107 | 38533 | jldominguez | // private DataServerExplorer explorer = null;
|
108 | 36022 | fdiaz | |
109 | 38533 | jldominguez | public FeatureTypePanel(NewLayerService new_layer_service) {
|
110 | |||
111 | /*
|
||
112 | service.getFeatureType(),
|
||
113 | service.getStoreName(),
|
||
114 | service.getMapContext().getProjection();
|
||
115 | */
|
||
116 | newLayerService = new_layer_service; |
||
117 | inStoreName = newLayerService.getStoreName(); |
||
118 | inProjection = newLayerService.getMapContext().getProjection(); |
||
119 | |||
120 | DataStoreProviderFactory dspf = |
||
121 | DALLocator.getDataManager().getStoreProviderFactory(inStoreName); |
||
122 | spFactory = (FeatureStoreProviderFactory) dspf; |
||
123 | defaultFeatType = spFactory.createDefaultFeatureType(); |
||
124 | if (defaultFeatType != null) { |
||
125 | inDefaultFeatTypeFieldCount = |
||
126 | defaultFeatType.getAttributeDescriptors().length; |
||
127 | int def_geom_fld_index = defaultFeatType.getDefaultGeometryAttributeIndex();
|
||
128 | if (def_geom_fld_index >= 0 && |
||
129 | def_geom_fld_index < inDefaultFeatTypeFieldCount && |
||
130 | defaultFeatType. |
||
131 | getAttributeDescriptor(def_geom_fld_index). |
||
132 | getDataType().getType() == DataTypes.GEOMETRY) { |
||
133 | |||
134 | defaultHasGeometry = true;
|
||
135 | } |
||
136 | } |
||
137 | |||
138 | 36022 | fdiaz | } |
139 | |||
140 | 36721 | fdiaz | private void initializeComponents() { |
141 | jLabel = new JLabel(); |
||
142 | jLabel.setText(Messages.getText("define_fields"));
|
||
143 | this.setLayout(new BorderLayout(5, 5)); |
||
144 | this.setSize(new java.awt.Dimension(499, 232)); |
||
145 | this.add(jLabel, java.awt.BorderLayout.NORTH);
|
||
146 | this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
|
||
147 | 38533 | jldominguez | this.add(getJPanelSouth(), java.awt.BorderLayout.SOUTH);
|
148 | 36022 | fdiaz | |
149 | 36721 | fdiaz | } |
150 | |||
151 | 36022 | fdiaz | /**
|
152 | * This method initializes jScrollPane
|
||
153 | 36721 | fdiaz | *
|
154 | 36022 | fdiaz | * @return javax.swing.JScrollPane
|
155 | */
|
||
156 | private JScrollPane getJScrollPane() { |
||
157 | if (jScrollPane == null) { |
||
158 | jScrollPane = new JScrollPane(); |
||
159 | jScrollPane.setViewportView(getJTable()); |
||
160 | } |
||
161 | return jScrollPane;
|
||
162 | } |
||
163 | |||
164 | /**
|
||
165 | * This method initializes jTable
|
||
166 | 36721 | fdiaz | *
|
167 | 36022 | fdiaz | * @return javax.swing.JTable
|
168 | */
|
||
169 | private JTable getJTable() { |
||
170 | if (jTable == null) { |
||
171 | 37807 | jpiera | jTable = new JTable(){ |
172 | 38533 | jldominguez | public boolean isCellEditable(int row,int column){ |
173 | 37807 | jpiera | |
174 | 38533 | jldominguez | |
175 | if (row < inDefaultFeatTypeFieldCount) {
|
||
176 | // default fields
|
||
177 | Object o = getValueAt(row,COLUMN_TYPE);
|
||
178 | if (o instanceof ComboItemDataType) { |
||
179 | |||
180 | ComboItemDataType ot = (ComboItemDataType) o; |
||
181 | return (ot.dataType.getType() == DataTypes.GEOMETRY)
|
||
182 | && |
||
183 | (column == COLUMN_GEOMETRYTYPE || |
||
184 | column == COLUMN_GEOMETRYSUBTYPE); |
||
185 | |||
186 | } else {
|
||
187 | return false; |
||
188 | } |
||
189 | |||
190 | } else {
|
||
191 | // fields added by user
|
||
192 | if (column == COLUMN_PRIMARYKEY) {
|
||
193 | return spFactory.allowsPrimaryKeyAttributes();
|
||
194 | } |
||
195 | |||
196 | if (column == COLUMN_MANDATORY) {
|
||
197 | return spFactory.allowsMandatoryAttributes();
|
||
198 | } |
||
199 | |||
200 | if (column == COLUMN_LENGTH) {
|
||
201 | |||
202 | Object o = getValueAt(row,COLUMN_TYPE);
|
||
203 | if (o instanceof ComboItemDataType) { |
||
204 | |||
205 | ComboItemDataType ot = (ComboItemDataType) o; |
||
206 | return dataTypeNeedsSize(ot.dataType.getType());
|
||
207 | |||
208 | } else {
|
||
209 | return false; |
||
210 | } |
||
211 | |||
212 | } |
||
213 | |||
214 | if (defaultHasGeometry &&
|
||
215 | (column == COLUMN_GEOMETRYTYPE |
||
216 | || column == COLUMN_GEOMETRYSUBTYPE |
||
217 | || column == COLUMN_CRS)) { |
||
218 | // geom-related columns not editable
|
||
219 | return false; |
||
220 | } else {
|
||
221 | return true; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /*
|
||
226 | 37807 | jpiera | if ((column == COLUMN_GEOMETRYTYPE) ||
|
227 | (column == COLUMN_GEOMETRYSUBTYPE) ||
|
||
228 | (column == COLUMN_CRS)) {
|
||
229 | DataType dataType =
|
||
230 | ((ComboItemDataType) getValueAt(row, COLUMN_TYPE)).dataType;
|
||
231 | return dataType.getType() == DataTypes.GEOMETRY;
|
||
232 | }
|
||
233 | 38533 | jldominguez | */
|
234 | 37807 | jpiera | } |
235 | }; |
||
236 | 36022 | fdiaz | |
237 | DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
||
238 | tm.addColumn(Messages.getText("field"));
|
||
239 | |||
240 | tm.addColumn(Messages.getText("type"));
|
||
241 | tm.addColumn(Messages.getText("length"));
|
||
242 | 36721 | fdiaz | tm.addColumn(Messages.getText("geomType"));
|
243 | tm.addColumn(Messages.getText("geomSubtype"));
|
||
244 | tm.addColumn(Messages.getText("CRS"));
|
||
245 | tm.addColumn(Messages.getText("primaryKey"));
|
||
246 | tm.addColumn(Messages.getText("mandatory"));
|
||
247 | 36022 | fdiaz | |
248 | 36721 | fdiaz | // Ask to be notified of selection changes.
|
249 | 36022 | fdiaz | ListSelectionModel rowSM = jTable.getSelectionModel();
|
250 | rowSM.addListSelectionListener(new ListSelectionListener() { |
||
251 | 36721 | fdiaz | public void valueChanged(ListSelectionEvent e) { |
252 | // Ignore extra messages.
|
||
253 | if (e.getValueIsAdjusting())
|
||
254 | return;
|
||
255 | 36022 | fdiaz | |
256 | 36721 | fdiaz | ListSelectionModel lsm = (ListSelectionModel) e.getSource(); |
257 | if (lsm.isSelectionEmpty()) {
|
||
258 | // no rows are selected
|
||
259 | 38533 | jldominguez | deleteFieldButton.setEnabled(false);
|
260 | 36721 | fdiaz | } else {
|
261 | 38533 | jldominguez | int min_sele = lsm.getMinSelectionIndex();
|
262 | if (min_sele > -1 && |
||
263 | min_sele < inDefaultFeatTypeFieldCount) { |
||
264 | deleteFieldButton.setEnabled(false);
|
||
265 | } else {
|
||
266 | deleteFieldButton.setEnabled(allowEditableFeatureType()); |
||
267 | } |
||
268 | } |
||
269 | |||
270 | 36721 | fdiaz | } |
271 | 36022 | fdiaz | }); |
272 | jTable.getColumn(Messages.getText("field")).setWidth(180); |
||
273 | |||
274 | 37807 | jpiera | jTable.getModel().addTableModelListener(new TableModelListener() { |
275 | public void tableChanged(TableModelEvent e) { |
||
276 | if (e.getColumn() == COLUMN_TYPE){
|
||
277 | jTable.repaint(); |
||
278 | } |
||
279 | } |
||
280 | }); |
||
281 | |||
282 | 36721 | fdiaz | // Rellenado de la tabla
|
283 | 38533 | jldominguez | if (defaultFeatType != null) { |
284 | |||
285 | Iterator it = defaultFeatType.iterator();
|
||
286 | boolean some_error = false; |
||
287 | 36022 | fdiaz | while (it.hasNext()) {
|
288 | 36721 | fdiaz | FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) it |
289 | .next(); |
||
290 | 38533 | jldominguez | |
291 | if (!addRowToFieldTable(
|
||
292 | jTable, |
||
293 | null,
|
||
294 | inProjection, |
||
295 | descriptor)) { |
||
296 | |||
297 | some_error = true;
|
||
298 | } |
||
299 | /*
|
||
300 | 37807 | jpiera | tm.setValueAt(descriptor.getName(), row, COLUMN_FIELD);
|
301 | tm.setValueAt(descriptor.getType(), row, COLUMN_TYPE);
|
||
302 | tm.setValueAt(descriptor.getSize(), row, COLUMN_LENGTH);
|
||
303 | tm.setValueAt(descriptor.getGeometryType(), row, COLUMN_GEOMETRYTYPE);
|
||
304 | tm.setValueAt(descriptor.getGeometrySubType(), row, COLUMN_GEOMETRYSUBTYPE);
|
||
305 | tm.setValueAt(descriptor.getSRS(), row, COLUMN_CRS);
|
||
306 | tm.setValueAt(descriptor.isPrimaryKey(), row, COLUMN_PRIMARYKEY);
|
||
307 | tm.setValueAt(descriptor.isMandatory(), row, COLUMN_MANDATORY);
|
||
308 | 38533 | jldominguez | */
|
309 | 36022 | fdiaz | } |
310 | 38533 | jldominguez | |
311 | if (some_error) {
|
||
312 | String tit = Messages.getText("_Default_fields"); |
||
313 | String msg = Messages.getText("_Unable_to_add_fields"); |
||
314 | JOptionPane.showMessageDialog(
|
||
315 | jTable, msg, tit, |
||
316 | JOptionPane.ERROR_MESSAGE);
|
||
317 | } |
||
318 | 36022 | fdiaz | } |
319 | 36721 | fdiaz | |
320 | 36022 | fdiaz | } |
321 | 37333 | fdiaz | jTable.setEnabled(allowEditableFeatureType()); |
322 | 36022 | fdiaz | return jTable;
|
323 | } |
||
324 | |||
325 | /**
|
||
326 | * This method initializes jPanelWest
|
||
327 | 36721 | fdiaz | *
|
328 | 36022 | fdiaz | * @return javax.swing.JPanel
|
329 | */
|
||
330 | 38533 | jldominguez | private JPanel getJPanelSouth() { |
331 | 36022 | fdiaz | if (jPanelEast == null) { |
332 | jPanelEast = new JPanel(); |
||
333 | 38533 | jldominguez | jPanelEast.setLayout(new BorderLayout()); |
334 | |||
335 | JPanel south_east = new JPanel(new GridBagLayout()); |
||
336 | // south_east.setPreferredSize(new java.awt.Dimension(170, 50));
|
||
337 | GridBagConstraints c = new GridBagConstraints(); |
||
338 | c.fill = GridBagConstraints.HORIZONTAL;
|
||
339 | c.insets = new Insets(8,10,8,0); //top padding |
||
340 | c.gridx = 0;
|
||
341 | c.gridwidth = 1;
|
||
342 | c.gridy = 0;
|
||
343 | south_east.add(getJButtonAddField(), c); |
||
344 | c.gridx = 1;
|
||
345 | south_east.add(getJButtonDeleteField(), c); |
||
346 | |||
347 | jPanelEast.add(south_east, BorderLayout.EAST);
|
||
348 | 36022 | fdiaz | } |
349 | return jPanelEast;
|
||
350 | } |
||
351 | |||
352 | /**
|
||
353 | * This method initializes jButtonAddField
|
||
354 | 36721 | fdiaz | *
|
355 | 36022 | fdiaz | * @return javax.swing.JButton
|
356 | */
|
||
357 | private JButton getJButtonAddField() { |
||
358 | 38533 | jldominguez | if (addFieldButton == null) { |
359 | addFieldButton = new JButton(); |
||
360 | addFieldButton.setText(Messages.getText("add_field"));
|
||
361 | addFieldButton.setLocation(new java.awt.Point(7, 5)); |
||
362 | addFieldButton.setSize(new java.awt.Dimension(145, 23)); |
||
363 | addFieldButton.setPreferredSize(new java.awt.Dimension(100, 23)); |
||
364 | addFieldButton |
||
365 | 36721 | fdiaz | .addActionListener(new java.awt.event.ActionListener() {
|
366 | public void actionPerformed(java.awt.event.ActionEvent e) { |
||
367 | DefaultTableModel tm = (DefaultTableModel) jTable |
||
368 | .getModel(); |
||
369 | 36022 | fdiaz | |
370 | 36721 | fdiaz | // Figure out a suitable field name
|
371 | ArrayList fieldNames = new ArrayList(); |
||
372 | for (int i = 0; i < jTable.getRowCount(); i++) { |
||
373 | 37807 | jpiera | fieldNames.add(tm.getValueAt(i, COLUMN_FIELD)); |
374 | 36721 | fdiaz | } |
375 | String[] currentFieldNames = (String[]) fieldNames |
||
376 | .toArray(new String[0]); |
||
377 | String newField = Messages.getText("field") |
||
378 | .replaceAll(" +", "_"); |
||
379 | int index = 0; |
||
380 | for (int i = 0; i < currentFieldNames.length; i++) { |
||
381 | if (currentFieldNames[i].startsWith(newField)) {
|
||
382 | try {
|
||
383 | index = Integer
|
||
384 | .parseInt(currentFieldNames[i] |
||
385 | .replaceAll(newField, |
||
386 | ""));
|
||
387 | } catch (Exception ex) { /* we don't care */ |
||
388 | } |
||
389 | } |
||
390 | } |
||
391 | String newFieldName = newField + (++index);
|
||
392 | |||
393 | // Esto lo a?ado aqu? porque si no tiene registros,
|
||
394 | // no hace caso. (Por eso no
|
||
395 | // lo pongo en getJTable()
|
||
396 | 38533 | jldominguez | if (!addRowToFieldTable(
|
397 | jTable, |
||
398 | newFieldName, |
||
399 | inProjection, |
||
400 | null)) {
|
||
401 | 37807 | jpiera | |
402 | 38533 | jldominguez | String tit = Messages.getText("_Default_fields"); |
403 | String msg = Messages.getText("_Unable_to_add_fields"); |
||
404 | JOptionPane.showMessageDialog(
|
||
405 | jTable, msg, tit, |
||
406 | JOptionPane.ERROR_MESSAGE);
|
||
407 | } |
||
408 | 36022 | fdiaz | } |
409 | 36721 | fdiaz | }); |
410 | 36022 | fdiaz | |
411 | 36721 | fdiaz | } |
412 | 38533 | jldominguez | addFieldButton.setEnabled(allowEditableFeatureType()); |
413 | return addFieldButton;
|
||
414 | 36721 | fdiaz | } |
415 | 37333 | fdiaz | |
416 | private boolean allowEditableFeatureType(){ |
||
417 | 38533 | jldominguez | |
418 | 37333 | fdiaz | DataManager dataManager = DALLocator.getDataManager(); |
419 | 38533 | jldominguez | FeatureStoreProviderFactory factory = |
420 | (FeatureStoreProviderFactory) dataManager. |
||
421 | getStoreProviderFactory(inStoreName); |
||
422 | 37333 | fdiaz | if (factory.allowEditableFeatureType()==FeatureStoreProviderFactory.NO) {
|
423 | return false; |
||
424 | } |
||
425 | return true; |
||
426 | } |
||
427 | 36022 | fdiaz | |
428 | 36721 | fdiaz | private class ComboItemDataType { |
429 | private DataType dataType;
|
||
430 | 36022 | fdiaz | |
431 | 36721 | fdiaz | public ComboItemDataType(DataType dataType) {
|
432 | this.dataType = dataType;
|
||
433 | } |
||
434 | 36022 | fdiaz | |
435 | 36721 | fdiaz | @Override
|
436 | public String toString() { |
||
437 | return this.dataType.getName(); |
||
438 | } |
||
439 | } |
||
440 | 36022 | fdiaz | |
441 | 36721 | fdiaz | private class ComboModelDataType implements ComboBoxModel { |
442 | |||
443 | private List<ComboItemDataType> dataTypes; |
||
444 | private ComboItemDataType selected;
|
||
445 | |||
446 | 38533 | jldominguez | public ComboModelDataType(List<DataType> dataTypes, int sel_type) { |
447 | |||
448 | 36721 | fdiaz | this.dataTypes = new ArrayList<ComboItemDataType>(); |
449 | 38533 | jldominguez | ComboItemDataType item = null;
|
450 | 36721 | fdiaz | for (Iterator iterator = dataTypes.iterator(); iterator.hasNext();) { |
451 | DataType dataType = (DataType) iterator.next(); |
||
452 | 38533 | jldominguez | item = new ComboItemDataType(dataType);
|
453 | this.dataTypes.add(item);
|
||
454 | if (sel_type == dataType.getType()) {
|
||
455 | this.setSelectedItem(item);
|
||
456 | } |
||
457 | 36721 | fdiaz | } |
458 | } |
||
459 | |||
460 | public void addListDataListener(ListDataListener arg0) { |
||
461 | // DO NOTHING
|
||
462 | } |
||
463 | |||
464 | public Object getElementAt(int arg0) { |
||
465 | return this.dataTypes.get(arg0); |
||
466 | } |
||
467 | |||
468 | public int getSize() { |
||
469 | return this.dataTypes.size(); |
||
470 | } |
||
471 | |||
472 | public void removeListDataListener(ListDataListener arg0) { |
||
473 | // DO NOTHING
|
||
474 | } |
||
475 | |||
476 | public Object getSelectedItem() { |
||
477 | return this.selected; |
||
478 | } |
||
479 | |||
480 | public void setSelectedItem(Object anItem) { |
||
481 | this.selected = (ComboItemDataType) anItem;
|
||
482 | } |
||
483 | |||
484 | } |
||
485 | |||
486 | 37807 | jpiera | class CrsRenderer extends JButton implements TableCellRenderer { |
487 | 36721 | fdiaz | private static final long serialVersionUID = -573793294070515376L; |
488 | 37807 | jpiera | private JLabel label = null; |
489 | |||
490 | public CrsRenderer() {
|
||
491 | 36721 | fdiaz | setOpaque(true);
|
492 | 37807 | jpiera | label = new JLabel(Messages.getText("not_applicable")); |
493 | 36721 | fdiaz | } |
494 | |||
495 | public Component getTableCellRendererComponent(JTable table, |
||
496 | Object value, boolean isSelected, boolean hasFocus, int row, |
||
497 | int column) {
|
||
498 | 37807 | jpiera | |
499 | 38533 | jldominguez | ComboItemDataType item = |
500 | (ComboItemDataType) table.getValueAt(row, COLUMN_TYPE); |
||
501 | |||
502 | if (item == null) { |
||
503 | // happens sometimes. perhaps an early rendering?
|
||
504 | return new JLabel(); |
||
505 | } |
||
506 | |||
507 | DataType dataType = item.dataType; |
||
508 | 37807 | jpiera | |
509 | if (dataType.getType() != DataTypes.GEOMETRY) {
|
||
510 | return label;
|
||
511 | } |
||
512 | |||
513 | if (isSelected) {
|
||
514 | 36721 | fdiaz | setForeground(table.getSelectionForeground()); |
515 | setBackground(table.getSelectionBackground()); |
||
516 | } else {
|
||
517 | setForeground(table.getForeground()); |
||
518 | setBackground(UIManager.getColor("Button.background")); |
||
519 | } |
||
520 | setText((value == null) ? "" : ((IProjection) value).getFullCode()); |
||
521 | return this; |
||
522 | } |
||
523 | 37807 | jpiera | } |
524 | |||
525 | private class GeometryComboCellRenderer extends JLabel implements TableCellRenderer{ |
||
526 | 36721 | fdiaz | |
527 | 37807 | jpiera | public Component getTableCellRendererComponent(JTable table, |
528 | Object value, boolean isSelected, boolean hasFocus, int row, |
||
529 | int column) {
|
||
530 | |||
531 | 38533 | jldominguez | ComboItemDataType item = (ComboItemDataType) table.getValueAt(row, COLUMN_TYPE); |
532 | DataType dataType = item.dataType; |
||
533 | 37807 | jpiera | |
534 | if (dataType.getType() == DataTypes.GEOMETRY) {
|
||
535 | ComboItemGeomType geomType = (ComboItemGeomType)value; |
||
536 | if (geomType != null) { |
||
537 | this.setText(geomType.toString());
|
||
538 | } |
||
539 | } else {
|
||
540 | this.setText(Messages.getText("not_applicable")); |
||
541 | } |
||
542 | |||
543 | return this; |
||
544 | } |
||
545 | |||
546 | } |
||
547 | |||
548 | 36721 | fdiaz | class ButtonEditor extends DefaultCellEditor { |
549 | /**
|
||
550 | *
|
||
551 | */
|
||
552 | private static final long serialVersionUID = -2820892672026188802L; |
||
553 | |||
554 | protected JButton button; |
||
555 | |||
556 | private String label; |
||
557 | |||
558 | private boolean isPushed; |
||
559 | |||
560 | private Object value; |
||
561 | |||
562 | public ButtonEditor(JCheckBox checkBox) { |
||
563 | super(checkBox);
|
||
564 | button = new JButton(); |
||
565 | button.setOpaque(true);
|
||
566 | button.addActionListener(new ActionListener() { |
||
567 | public void actionPerformed(ActionEvent e) { |
||
568 | fireEditingStopped(); |
||
569 | 36022 | fdiaz | } |
570 | }); |
||
571 | 36721 | fdiaz | } |
572 | 36022 | fdiaz | |
573 | 36721 | fdiaz | public Component getTableCellEditorComponent(JTable table, |
574 | Object value, boolean isSelected, int row, int column) { |
||
575 | if (isSelected) {
|
||
576 | button.setForeground(table.getSelectionForeground()); |
||
577 | button.setBackground(table.getSelectionBackground()); |
||
578 | } else {
|
||
579 | button.setForeground(table.getForeground()); |
||
580 | button.setBackground(table.getBackground()); |
||
581 | } |
||
582 | label = (value == null) ? "" : ((IProjection) value).getFullCode();// .toString(); |
||
583 | button.setText(label); |
||
584 | isPushed = true;
|
||
585 | this.value = value;
|
||
586 | return button;
|
||
587 | 36022 | fdiaz | } |
588 | 36721 | fdiaz | |
589 | public Object getCellEditorValue() { |
||
590 | IProjection curProj = (IProjection) this.value;
|
||
591 | if (isPushed) {
|
||
592 | ISelectCrsPanel csSelect = CRSSelectPanel.getUIFactory() |
||
593 | .getSelectCrsPanel(curProj, true);
|
||
594 | WindowManager wm = ToolsSwingLocator.getWindowManager(); |
||
595 | wm.showWindow((JPanel) csSelect, "Projection", |
||
596 | WindowManager.MODE.DIALOG); |
||
597 | curProj = csSelect.getProjection(); |
||
598 | } |
||
599 | isPushed = false;
|
||
600 | return curProj;
|
||
601 | } |
||
602 | |||
603 | public boolean stopCellEditing() { |
||
604 | isPushed = false;
|
||
605 | return super.stopCellEditing(); |
||
606 | } |
||
607 | |||
608 | protected void fireEditingStopped() { |
||
609 | super.fireEditingStopped();
|
||
610 | } |
||
611 | 36022 | fdiaz | } |
612 | |||
613 | 36721 | fdiaz | private class ComboItemGeomType { |
614 | private int type; |
||
615 | private String name; |
||
616 | 36022 | fdiaz | |
617 | 36721 | fdiaz | public ComboItemGeomType(String name, int type) { |
618 | this.type = type;
|
||
619 | this.name = name;
|
||
620 | } |
||
621 | |||
622 | @Override
|
||
623 | public String toString() { |
||
624 | return this.name; |
||
625 | } |
||
626 | 38533 | jldominguez | |
627 | public int getGeomType() { |
||
628 | return type;
|
||
629 | } |
||
630 | 36721 | fdiaz | } |
631 | |||
632 | private class ComboModelGeomType implements ComboBoxModel { |
||
633 | |||
634 | private List<ComboItemGeomType> geomTypes; |
||
635 | private ComboItemGeomType selected;
|
||
636 | |||
637 | 38533 | jldominguez | public ComboModelGeomType(
|
638 | List<ComboItemGeomType> geomTypes, int sel_geom_type) { |
||
639 | |||
640 | 36721 | fdiaz | this.geomTypes = geomTypes;
|
641 | 38533 | jldominguez | int len = geomTypes.size();
|
642 | ComboItemGeomType item = null;
|
||
643 | for (int i=0; i<len; i++) { |
||
644 | item = geomTypes.get(i); |
||
645 | if (item.getGeomType() == sel_geom_type) {
|
||
646 | setSelectedItem(item); |
||
647 | } |
||
648 | } |
||
649 | 36721 | fdiaz | } |
650 | |||
651 | public void addListDataListener(ListDataListener arg0) { |
||
652 | // DO NOTHING
|
||
653 | } |
||
654 | |||
655 | public Object getElementAt(int arg0) { |
||
656 | return this.geomTypes.get(arg0); |
||
657 | } |
||
658 | |||
659 | public int getSize() { |
||
660 | return this.geomTypes.size(); |
||
661 | } |
||
662 | |||
663 | public void removeListDataListener(ListDataListener arg0) { |
||
664 | // DO NOTHING
|
||
665 | } |
||
666 | |||
667 | public Object getSelectedItem() { |
||
668 | return this.selected; |
||
669 | } |
||
670 | |||
671 | public void setSelectedItem(Object anItem) { |
||
672 | this.selected = (ComboItemGeomType) anItem;
|
||
673 | } |
||
674 | |||
675 | } |
||
676 | |||
677 | private List<ComboItemGeomType> getGeomTypes() { |
||
678 | List<ComboItemGeomType> geomTypes = new ArrayList<ComboItemGeomType>(); |
||
679 | geomTypes |
||
680 | .add(new ComboItemGeomType("GEOMETRY", Geometry.TYPES.GEOMETRY)); |
||
681 | geomTypes.add(new ComboItemGeomType("POINT", Geometry.TYPES.POINT)); |
||
682 | geomTypes.add(new ComboItemGeomType("CURVE", Geometry.TYPES.CURVE)); |
||
683 | geomTypes.add(new ComboItemGeomType("SURFACE", Geometry.TYPES.SURFACE)); |
||
684 | geomTypes.add(new ComboItemGeomType("SOLID", Geometry.TYPES.SOLID)); |
||
685 | geomTypes.add(new ComboItemGeomType("AGGREGATE", |
||
686 | Geometry.TYPES.AGGREGATE)); |
||
687 | geomTypes.add(new ComboItemGeomType("MULTIPOINT", |
||
688 | Geometry.TYPES.MULTIPOINT)); |
||
689 | geomTypes.add(new ComboItemGeomType("MULTICURVE", |
||
690 | Geometry.TYPES.MULTICURVE)); |
||
691 | geomTypes.add(new ComboItemGeomType("MULTISURFACE", |
||
692 | Geometry.TYPES.MULTISURFACE)); |
||
693 | geomTypes.add(new ComboItemGeomType("MULTISOLID", |
||
694 | Geometry.TYPES.MULTISOLID)); |
||
695 | |||
696 | return geomTypes;
|
||
697 | } |
||
698 | |||
699 | private List<ComboItemGeomType> getGeomSubtypes() { |
||
700 | List<ComboItemGeomType> geomSubtypes = new ArrayList<ComboItemGeomType>(); |
||
701 | geomSubtypes.add(new ComboItemGeomType("GEOM2D", |
||
702 | Geometry.SUBTYPES.GEOM2D)); |
||
703 | geomSubtypes.add(new ComboItemGeomType("GEOM2DM", |
||
704 | Geometry.SUBTYPES.GEOM2DM)); |
||
705 | geomSubtypes.add(new ComboItemGeomType("GEOM3D", |
||
706 | Geometry.SUBTYPES.GEOM3D)); |
||
707 | geomSubtypes.add(new ComboItemGeomType("GEOM3DM", |
||
708 | Geometry.SUBTYPES.GEOM3DM)); |
||
709 | geomSubtypes.add(new ComboItemGeomType("UNKNOWN", |
||
710 | Geometry.SUBTYPES.UNKNOWN)); |
||
711 | return geomSubtypes;
|
||
712 | } |
||
713 | |||
714 | class CheckBoxRenderer extends JCheckBox implements TableCellRenderer { |
||
715 | |||
716 | /**
|
||
717 | *
|
||
718 | */
|
||
719 | private static final long serialVersionUID = 5093448776058027505L; |
||
720 | |||
721 | 38533 | jldominguez | public CheckBoxRenderer() {
|
722 | 36721 | fdiaz | setHorizontalAlignment(JLabel.CENTER);
|
723 | } |
||
724 | |||
725 | public Component getTableCellRendererComponent(JTable table, |
||
726 | Object value, boolean isSelected, boolean hasFocus, int row, |
||
727 | int column) {
|
||
728 | if (isSelected) {
|
||
729 | setForeground(table.getSelectionForeground()); |
||
730 | setBackground(table.getSelectionBackground()); |
||
731 | } else {
|
||
732 | setForeground(table.getForeground()); |
||
733 | setBackground(table.getBackground()); |
||
734 | } |
||
735 | setSelected((value != null && ((Boolean) value).booleanValue())); |
||
736 | return this; |
||
737 | } |
||
738 | } |
||
739 | |||
740 | 36022 | fdiaz | /**
|
741 | * This method initializes jButton
|
||
742 | 36721 | fdiaz | *
|
743 | 36022 | fdiaz | * @return javax.swing.JButton
|
744 | */
|
||
745 | private JButton getJButtonDeleteField() { |
||
746 | 38533 | jldominguez | if (deleteFieldButton == null) { |
747 | deleteFieldButton = new JButton(); |
||
748 | deleteFieldButton.setText(Messages.getText("delete_field"));
|
||
749 | deleteFieldButton.setLocation(new java.awt.Point(7, 33)); |
||
750 | deleteFieldButton.setSize(new java.awt.Dimension(145, 23)); |
||
751 | deleteFieldButton.setEnabled(false);
|
||
752 | deleteFieldButton |
||
753 | 36721 | fdiaz | .addActionListener(new java.awt.event.ActionListener() {
|
754 | public void actionPerformed(java.awt.event.ActionEvent e) { |
||
755 | int[] selecteds = jTable.getSelectedRows(); |
||
756 | DefaultTableModel tm = (DefaultTableModel) jTable |
||
757 | .getModel(); |
||
758 | 36022 | fdiaz | |
759 | 36721 | fdiaz | for (int i = selecteds.length - 1; i >= 0; i--) { |
760 | 38367 | jldominguez | /*
|
761 | *
|
||
762 | 36721 | fdiaz | removedFields.add((String) tm.getValueAt(i, 0));
|
763 | 38367 | jldominguez | */
|
764 | 36721 | fdiaz | tm.removeRow(selecteds[i]); |
765 | } |
||
766 | } |
||
767 | }); |
||
768 | 36022 | fdiaz | } |
769 | 38533 | jldominguez | deleteFieldButton.setEnabled(false);
|
770 | return deleteFieldButton;
|
||
771 | 36022 | fdiaz | } |
772 | |||
773 | public String getPanelTitle() { |
||
774 | return Messages.getText("fields_definitions"); |
||
775 | } |
||
776 | |||
777 | public void nextPanel() throws NotContinueWizardException { |
||
778 | 37333 | fdiaz | if (allowEditableFeatureType()){
|
779 | fillFeatureType(); |
||
780 | } |
||
781 | 36220 | fdiaz | } |
782 | 36721 | fdiaz | |
783 | 38533 | jldominguez | private void fillFeatureType() throws NotContinueWizardException { |
784 | 36721 | fdiaz | /*
|
785 | 36220 | fdiaz | * Coge los datos del formulario y los guarda en
|
786 | 36022 | fdiaz | * this.service.getFeatureType()
|
787 | */
|
||
788 | 38533 | jldominguez | |
789 | EditableFeatureType edft = newLayerService.getFeatureType(); |
||
790 | |||
791 | 38367 | jldominguez | // Clean and rebuild featureType everytime we click next
|
792 | 38533 | jldominguez | removeAllFields(edft); |
793 | edft.setDefaultGeometryAttributeName(null);
|
||
794 | 36721 | fdiaz | |
795 | 38533 | jldominguez | GeometryManager gm = GeometryLocator.getGeometryManager(); |
796 | 36022 | fdiaz | DefaultTableModel tm = (DefaultTableModel) jTable.getModel(); |
797 | 38533 | jldominguez | |
798 | 36721 | fdiaz | for (int i = 0; i < tm.getRowCount(); i++) { |
799 | 37807 | jpiera | DataType dataType = ((ComboItemDataType) tm.getValueAt(i, COLUMN_TYPE)).dataType; |
800 | 36022 | fdiaz | |
801 | 38533 | jldominguez | int fieldLength = 20; |
802 | if (dataTypeNeedsSize(dataType.getType())) {
|
||
803 | fieldLength = |
||
804 | Integer.parseInt((String) tm.getValueAt(i, COLUMN_LENGTH)); |
||
805 | 36721 | fdiaz | } |
806 | 38367 | jldominguez | |
807 | 38533 | jldominguez | EditableFeatureAttributeDescriptor efad1 = |
808 | edft.add( |
||
809 | 37807 | jpiera | (String) tm.getValueAt(i, COLUMN_FIELD), dataType.getType(),
|
810 | 36721 | fdiaz | fieldLength); |
811 | 38533 | jldominguez | |
812 | if (dataType.getType() == DataTypes.DOUBLE ||
|
||
813 | dataType.getType() == DataTypes.FLOAT) { |
||
814 | // TODO Improve this?
|
||
815 | // Half of the size dedicated
|
||
816 | // to precision (at least 1)
|
||
817 | efad1.setPrecision(Math.max(1, fieldLength/2)); |
||
818 | } |
||
819 | |||
820 | 36721 | fdiaz | if (dataType.getType() == DataTypes.GEOMETRY) {
|
821 | 38533 | jldominguez | |
822 | int g_type = ((ComboItemGeomType) tm.getValueAt(i, COLUMN_GEOMETRYTYPE)).type;
|
||
823 | int g_subtype = ((ComboItemGeomType) tm.getValueAt(i, COLUMN_GEOMETRYSUBTYPE)).type;
|
||
824 | |||
825 | GeometryType gty = null;
|
||
826 | |||
827 | try {
|
||
828 | gty = gm.getGeometryType(g_type, g_subtype); |
||
829 | efad1.setGeometryType(gty); |
||
830 | // efad1.setGeometryType(g_type);
|
||
831 | } catch (Exception ex) { |
||
832 | String _msg = Messages.getText("_Invalid_geometry_type") |
||
833 | + " (" + g_type + ", " + g_subtype + ")"; |
||
834 | throw new NotContinueWizardException(_msg, ex, this); |
||
835 | } |
||
836 | |||
837 | 37807 | jpiera | efad1.setIsPrimaryKey(((Boolean) tm.getValueAt(i, COLUMN_PRIMARYKEY))
|
838 | 36721 | fdiaz | .booleanValue()); |
839 | 37807 | jpiera | efad1.setAllowNull(!((Boolean) tm.getValueAt(i, COLUMN_MANDATORY))
|
840 | 36721 | fdiaz | .booleanValue()); |
841 | 37807 | jpiera | efad1.setSRS((IProjection) tm.getValueAt(i, COLUMN_CRS)); |
842 | 38533 | jldominguez | |
843 | 37804 | jpiera | //If there is not a previous default geometry field, add it
|
844 | 38533 | jldominguez | // this will set the first geometry field
|
845 | // as the default geometry field
|
||
846 | if (edft.getDefaultGeometryAttribute() == null){ |
||
847 | edft.setDefaultGeometryAttributeName(efad1.getName()); |
||
848 | 37807 | jpiera | } |
849 | 36721 | fdiaz | } |
850 | 36022 | fdiaz | } |
851 | 36220 | fdiaz | } |
852 | 36022 | fdiaz | |
853 | 38367 | jldominguez | /**
|
854 | * @param featureType
|
||
855 | */
|
||
856 | private void removeAllFields(EditableFeatureType ft) { |
||
857 | |||
858 | FeatureAttributeDescriptor[] atts = ft.getAttributeDescriptors();
|
||
859 | int len = atts.length;
|
||
860 | FeatureAttributeDescriptor item = null;
|
||
861 | for (int i=0; i<len; i++) { |
||
862 | item = atts[i]; |
||
863 | ft.remove(item.getName()); |
||
864 | } |
||
865 | } |
||
866 | |||
867 | public void lastPanel() { |
||
868 | 36220 | fdiaz | // TODO Auto-generated method stub
|
869 | logger.info("FeatureTypePanel: lastPanel");
|
||
870 | 36022 | fdiaz | } |
871 | |||
872 | 36220 | fdiaz | public void updatePanel() { |
873 | initializeComponents(); |
||
874 | 36721 | fdiaz | |
875 | 36220 | fdiaz | } |
876 | |||
877 | public JPanel getJPanel() { |
||
878 | return this; |
||
879 | } |
||
880 | 38533 | jldominguez | |
881 | /**
|
||
882 | * If fad is null, it's an addition after user has clicked on 'Add field'.
|
||
883 | * If fad is not null, it's the description of a default field and the
|
||
884 | * field name will also be taken from there.
|
||
885 | *
|
||
886 | * @param the_table
|
||
887 | * @param new_fld_name
|
||
888 | * @param the_projection
|
||
889 | * @param fad
|
||
890 | *
|
||
891 | * @return true if field was actually added, false otherwise
|
||
892 | */
|
||
893 | private boolean addRowToFieldTable( |
||
894 | JTable the_table,
|
||
895 | String new_fld_name,
|
||
896 | IProjection the_projection, |
||
897 | FeatureAttributeDescriptor fad) { |
||
898 | |||
899 | if (fad == null && new_fld_name == null) { |
||
900 | logger.info("Error: unable to add field to table (no description and no name)");
|
||
901 | return false; |
||
902 | } |
||
903 | |||
904 | DefaultTableModel the_tm = (DefaultTableModel) the_table.getModel(); |
||
905 | Object[] newRow = new Object[the_tm.getColumnCount()]; |
||
906 | |||
907 | /*
|
||
908 | tm.setValueAt(descriptor.getName(), row, COLUMN_FIELD);
|
||
909 | tm.setValueAt(descriptor.getType(), row, COLUMN_TYPE);
|
||
910 | tm.setValueAt(descriptor.getSize(), row, COLUMN_LENGTH);
|
||
911 | tm.setValueAt(descriptor.getGeometryType(), row, COLUMN_GEOMETRYTYPE);
|
||
912 | tm.setValueAt(descriptor.getGeometrySubType(), row, COLUMN_GEOMETRYSUBTYPE);
|
||
913 | tm.setValueAt(descriptor.getSRS(), row, COLUMN_CRS);
|
||
914 | tm.setValueAt(descriptor.isPrimaryKey(), row, COLUMN_PRIMARYKEY);
|
||
915 | tm.setValueAt(descriptor.isMandatory(), row, COLUMN_MANDATORY);
|
||
916 | */
|
||
917 | |||
918 | // =============================== name
|
||
919 | if (fad == null) { |
||
920 | newRow[COLUMN_FIELD] = new_fld_name; |
||
921 | } else {
|
||
922 | newRow[COLUMN_FIELD] = fad.getName(); |
||
923 | } |
||
924 | // ===================================== field type
|
||
925 | TableColumn typeColumn = the_table.getColumnModel().getColumn(COLUMN_TYPE);
|
||
926 | List supp_types = spFactory.getSupportedDataTypes();
|
||
927 | |||
928 | DataManager daman = DALLocator.getDataManager(); |
||
929 | List all_types = daman.getDataTypes();
|
||
930 | |||
931 | |||
932 | ComboModelDataType model = null;
|
||
933 | if (fad == null) { |
||
934 | |||
935 | // this is a user defined column, remove geometry if in default feattype
|
||
936 | if (defaultHasGeometry) {
|
||
937 | supp_types = removeGeometryType(supp_types); |
||
938 | all_types = removeGeometryType(all_types); |
||
939 | } |
||
940 | |||
941 | if (supp_types != null && supp_types.size() > 0) { |
||
942 | model = new ComboModelDataType(
|
||
943 | supp_types, |
||
944 | ((DataType) supp_types.get(0)).getType());
|
||
945 | } else {
|
||
946 | model = new ComboModelDataType(
|
||
947 | all_types, |
||
948 | ((DataType) all_types.get(0)).getType());
|
||
949 | } |
||
950 | } else {
|
||
951 | model = new ComboModelDataType(supp_types, fad.getType());
|
||
952 | } |
||
953 | |||
954 | JComboBox comboBox = new JComboBox(model); |
||
955 | typeColumn.setCellEditor(new DefaultCellEditor(comboBox)); |
||
956 | newRow[COLUMN_TYPE] = model.getSelectedItem(); |
||
957 | |||
958 | // ============================= width
|
||
959 | TableColumn widthColumn =
|
||
960 | the_table.getColumnModel().getColumn(COLUMN_LENGTH); |
||
961 | if (fad == null) { |
||
962 | newRow[COLUMN_LENGTH] = "20";
|
||
963 | } else {
|
||
964 | newRow[COLUMN_LENGTH] = "" + fad.getSize();
|
||
965 | } |
||
966 | |||
967 | SimpleTextCellRenderer stcr = new SimpleTextCellRenderer();
|
||
968 | widthColumn.setCellEditor(new DefaultCellEditor(new JTextField())); |
||
969 | widthColumn.setCellRenderer(stcr); |
||
970 | |||
971 | |||
972 | // ============================= geom type
|
||
973 | TableColumn geomTypeColumn =
|
||
974 | the_table.getColumnModel().getColumn(COLUMN_GEOMETRYTYPE); |
||
975 | ComboModelGeomType geomTypeModel = null;
|
||
976 | |||
977 | List<ComboItemGeomType> supptd =
|
||
978 | leaveSupportedGeomTypes(getGeomTypes(), |
||
979 | spFactory.getSupportedGeometryTypesSubtypes(), 0);
|
||
980 | |||
981 | if (fad == null) { |
||
982 | geomTypeModel = new ComboModelGeomType(
|
||
983 | supptd, supptd.size() > 0 ? supptd.get(0).getGeomType() : -1); |
||
984 | } else {
|
||
985 | geomTypeModel = new ComboModelGeomType(
|
||
986 | supptd, fad.getGeomType().getType()); |
||
987 | } |
||
988 | |||
989 | JComboBox geomTypeComboBox = new JComboBox(geomTypeModel); |
||
990 | geomTypeColumn.setCellEditor(new DefaultCellEditor(geomTypeComboBox)); |
||
991 | |||
992 | GeometryComboCellRenderer gccr = new GeometryComboCellRenderer();
|
||
993 | geomTypeColumn.setCellRenderer(gccr); |
||
994 | newRow[COLUMN_GEOMETRYTYPE] = geomTypeModel.getSelectedItem(); |
||
995 | |||
996 | // ============================= geom sub type
|
||
997 | |||
998 | TableColumn geomSubTypeColumn =
|
||
999 | the_table.getColumnModel().getColumn(COLUMN_GEOMETRYSUBTYPE); |
||
1000 | ComboModelGeomType geomSubTypeModel = null;
|
||
1001 | |||
1002 | supptd = |
||
1003 | leaveSupportedGeomTypes(getGeomSubtypes(), |
||
1004 | spFactory.getSupportedGeometryTypesSubtypes(), 1);
|
||
1005 | |||
1006 | if (fad == null) { |
||
1007 | geomSubTypeModel = new ComboModelGeomType(
|
||
1008 | supptd, supptd.size() > 0 ? supptd.get(0).getGeomType() : -1); |
||
1009 | } else {
|
||
1010 | geomSubTypeModel = new ComboModelGeomType(
|
||
1011 | supptd, fad.getGeomType().getSubType()); |
||
1012 | } |
||
1013 | |||
1014 | JComboBox geomSubTypeComboBox = new JComboBox(geomSubTypeModel); |
||
1015 | geomSubTypeColumn.setCellEditor(new DefaultCellEditor( |
||
1016 | geomSubTypeComboBox)); |
||
1017 | geomSubTypeColumn.setCellRenderer(new GeometryComboCellRenderer());
|
||
1018 | newRow[COLUMN_GEOMETRYSUBTYPE] = geomSubTypeModel.getSelectedItem(); |
||
1019 | |||
1020 | // ============================= CRS
|
||
1021 | |||
1022 | TableColumn crsColumn = the_table.getColumnModel().getColumn(COLUMN_CRS);
|
||
1023 | crsColumn.setCellEditor(new ButtonEditor(new JCheckBox())); |
||
1024 | CrsRenderer crsButtonRenderer = new CrsRenderer();
|
||
1025 | crsColumn.setCellRenderer(crsButtonRenderer); |
||
1026 | newRow[COLUMN_CRS] = the_projection; |
||
1027 | |||
1028 | // ============================= PK
|
||
1029 | |||
1030 | TableColumn primaryKeyColumn =
|
||
1031 | the_table.getColumnModel().getColumn(COLUMN_PRIMARYKEY); |
||
1032 | JCheckBox primaryKeyCheckBox = new JCheckBox(); |
||
1033 | |||
1034 | if (fad == null) { |
||
1035 | primaryKeyCheckBox.setSelected(false);
|
||
1036 | } else {
|
||
1037 | primaryKeyCheckBox.setSelected(fad.isPrimaryKey()); |
||
1038 | } |
||
1039 | |||
1040 | primaryKeyColumn.setCellEditor( |
||
1041 | new DefaultCellEditor(primaryKeyCheckBox)); |
||
1042 | CheckBoxRenderer primaryKeyCheckBoxRenderer = new CheckBoxRenderer();
|
||
1043 | primaryKeyColumn.setCellRenderer(primaryKeyCheckBoxRenderer); |
||
1044 | newRow[COLUMN_PRIMARYKEY] = primaryKeyCheckBox.isSelected(); |
||
1045 | |||
1046 | // ================================== mandatory
|
||
1047 | |||
1048 | TableColumn mandatoryColumn =
|
||
1049 | the_table.getColumnModel().getColumn(COLUMN_MANDATORY); |
||
1050 | JCheckBox mandatoryCheckBox = new JCheckBox(); |
||
1051 | |||
1052 | if (fad == null) { |
||
1053 | mandatoryCheckBox.setSelected(false);
|
||
1054 | } else {
|
||
1055 | mandatoryCheckBox.setSelected(fad.isMandatory()); |
||
1056 | } |
||
1057 | |||
1058 | mandatoryColumn.setCellEditor(new DefaultCellEditor(mandatoryCheckBox)); |
||
1059 | CheckBoxRenderer mandatoryCheckBoxRenderer = new CheckBoxRenderer();
|
||
1060 | mandatoryColumn.setCellRenderer(mandatoryCheckBoxRenderer); |
||
1061 | newRow[COLUMN_MANDATORY] = mandatoryCheckBox.isSelected(); |
||
1062 | |||
1063 | // ==================================
|
||
1064 | |||
1065 | // Add a new row
|
||
1066 | the_tm.addRow(newRow); |
||
1067 | return true; |
||
1068 | } |
||
1069 | |||
1070 | /**
|
||
1071 | * @param supp_types
|
||
1072 | * @return
|
||
1073 | */
|
||
1074 | private List removeGeometryType(List dta_types) { |
||
1075 | List resp = new ArrayList(); |
||
1076 | |||
1077 | int len = dta_types.size();
|
||
1078 | Object item_o = null; |
||
1079 | DataType item_dt = null;
|
||
1080 | |||
1081 | for (int i=0; i<len; i++) { |
||
1082 | item_o = dta_types.get(i); |
||
1083 | if (item_o instanceof DataType) { |
||
1084 | item_dt = (DataType) item_o; |
||
1085 | if (item_dt.getType() != DataTypes.GEOMETRY) {
|
||
1086 | resp.add(item_dt); |
||
1087 | } |
||
1088 | } |
||
1089 | } |
||
1090 | return resp;
|
||
1091 | } |
||
1092 | |||
1093 | private List<ComboItemGeomType> leaveSupportedGeomTypes( |
||
1094 | List<ComboItemGeomType> all_types,
|
||
1095 | List<int[]> supported, |
||
1096 | int index /* 0 for geom types, 1 for geom subtypes */) { |
||
1097 | |||
1098 | List<ComboItemGeomType> resp = new ArrayList<ComboItemGeomType>(); |
||
1099 | int len = all_types.size();
|
||
1100 | ComboItemGeomType item = null;
|
||
1101 | for (int i=0; i<len; i++) { |
||
1102 | item = all_types.get(i); |
||
1103 | if (isInNthElement(item.getGeomType(), supported, index)) {
|
||
1104 | resp.add(item); |
||
1105 | } |
||
1106 | } |
||
1107 | return resp;
|
||
1108 | } |
||
1109 | |||
1110 | |||
1111 | private boolean isInNthElement(int val, List<int[]> arr_list, int index) { |
||
1112 | |||
1113 | int len = arr_list.size();
|
||
1114 | int[] item = null; |
||
1115 | for (int i=0; i<len; i++) { |
||
1116 | item = arr_list.get(i); |
||
1117 | if (item != null && item.length > index && item[index] == val) { |
||
1118 | return true; |
||
1119 | } |
||
1120 | } |
||
1121 | return false; |
||
1122 | } |
||
1123 | |||
1124 | private boolean dataTypeNeedsSize(int data_type) { |
||
1125 | |||
1126 | if (data_type == DataTypes.FLOAT
|
||
1127 | || data_type == DataTypes.INT |
||
1128 | || data_type == DataTypes.STRING) { |
||
1129 | return true; |
||
1130 | } else {
|
||
1131 | return false; |
||
1132 | } |
||
1133 | } |
||
1134 | |||
1135 | private class SimpleTextCellRenderer extends DefaultTableCellRenderer { |
||
1136 | |||
1137 | public SimpleTextCellRenderer() {
|
||
1138 | super();
|
||
1139 | } |
||
1140 | /* (non-Javadoc)
|
||
1141 | * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
|
||
1142 | */
|
||
1143 | public Component getTableCellRendererComponent( |
||
1144 | JTable table,
|
||
1145 | Object value, boolean isSelected, boolean hasFocus, int row, |
||
1146 | int column) {
|
||
1147 | |||
1148 | TableModel tm = table.getModel();
|
||
1149 | Object new_val = ""; |
||
1150 | Object type_obj = tm.getValueAt(row, COLUMN_TYPE);
|
||
1151 | if (type_obj instanceof ComboItemDataType) { |
||
1152 | ComboItemDataType type_item = (ComboItemDataType) type_obj; |
||
1153 | if (dataTypeNeedsSize(type_item.dataType.getType())) {
|
||
1154 | new_val = getCurrentOr(value, "");
|
||
1155 | } else {
|
||
1156 | new_val = Messages.getText("not_applicable");
|
||
1157 | } |
||
1158 | return super.getTableCellRendererComponent( |
||
1159 | table, new_val, isSelected, hasFocus, row, column); |
||
1160 | } else {
|
||
1161 | return super.getTableCellRendererComponent( |
||
1162 | table, value, isSelected, hasFocus, row, column); |
||
1163 | } |
||
1164 | } |
||
1165 | |||
1166 | private Object getCurrentOr(Object v, String d) { |
||
1167 | |||
1168 | if (v instanceof String) { |
||
1169 | String parse_str = (String) v; |
||
1170 | try {
|
||
1171 | Integer.parseInt(parse_str);
|
||
1172 | return v;
|
||
1173 | } catch (Exception ex) { |
||
1174 | // not a valid integer
|
||
1175 | return d;
|
||
1176 | } |
||
1177 | } else {
|
||
1178 | return v;
|
||
1179 | } |
||
1180 | } |
||
1181 | |||
1182 | } |
||
1183 | |||
1184 | private class EnableLengthItemListener implements ItemListener { |
||
1185 | |||
1186 | private TableModel theTableModel = null; |
||
1187 | private JTable theTable = null; |
||
1188 | |||
1189 | public EnableLengthItemListener(JTable t, boolean b) { |
||
1190 | theTable = t; |
||
1191 | theTableModel = t.getModel(); |
||
1192 | } |
||
1193 | |||
1194 | public void itemStateChanged(final ItemEvent e) { |
||
1195 | doUpdate(e); |
||
1196 | } |
||
1197 | |||
1198 | public void doUpdate(ItemEvent e) { |
||
1199 | |||
1200 | Object src_obj = e.getSource();
|
||
1201 | if (src_obj instanceof JComboBox) { |
||
1202 | |||
1203 | // one of the field type combos has changed
|
||
1204 | int len = theTableModel.getRowCount();
|
||
1205 | Object type_obj = null; |
||
1206 | ComboItemDataType type_item = null;
|
||
1207 | for (int i=0; i<len; i++) { |
||
1208 | type_obj = theTableModel.getValueAt(i, COLUMN_TYPE); |
||
1209 | if (type_obj instanceof ComboItemDataType) { |
||
1210 | type_item = (ComboItemDataType) type_obj; |
||
1211 | if (dataTypeNeedsSize(type_item.dataType.getType())) {
|
||
1212 | theTableModel.setValueAt( |
||
1213 | "20",
|
||
1214 | i, COLUMN_LENGTH); |
||
1215 | } else {
|
||
1216 | theTableModel.setValueAt( |
||
1217 | Messages.getText("not_applicable"),
|
||
1218 | i, COLUMN_LENGTH); |
||
1219 | } |
||
1220 | } |
||
1221 | } |
||
1222 | theTable.validate(); // .revalidate(); // .repaint();
|
||
1223 | } |
||
1224 | } |
||
1225 | |||
1226 | } |
||
1227 | |||
1228 | |||
1229 | |||
1230 | |||
1231 | |||
1232 | 36022 | fdiaz | } |