svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretype / DefaultFeatureTypePanel.java @ 44154
History | View | Annotate | Download (13.5 KB)
1 | 44077 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.featuretype; |
---|---|---|---|
2 | |||
3 | 44096 | jjdelcerro | import org.gvsig.fmap.dal.swing.featuretype.FeatureTypePanel; |
4 | 44077 | jjdelcerro | import java.awt.BorderLayout; |
5 | 44085 | jjdelcerro | import java.awt.Dimension; |
6 | import java.awt.event.ActionEvent; |
||
7 | import java.awt.event.ActionListener; |
||
8 | import java.util.Date; |
||
9 | 44077 | jjdelcerro | import java.util.Objects; |
10 | import javax.swing.JComponent; |
||
11 | 44149 | jjdelcerro | import javax.swing.JOptionPane; |
12 | 44077 | jjdelcerro | import javax.swing.event.ListSelectionEvent; |
13 | import javax.swing.event.ListSelectionListener; |
||
14 | 44085 | jjdelcerro | import javax.swing.event.TableModelEvent; |
15 | 44077 | jjdelcerro | import javax.swing.table.AbstractTableModel; |
16 | 44149 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
17 | import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
||
18 | 44085 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
19 | 44077 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
20 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
21 | 44085 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
22 | 44077 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
23 | 44085 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
24 | import org.gvsig.tools.dataTypes.DataTypes; |
||
25 | import org.gvsig.tools.i18n.I18nManager; |
||
26 | 44128 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
27 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
28 | 44149 | jjdelcerro | import org.slf4j.Logger; |
29 | import org.slf4j.LoggerFactory; |
||
30 | 44077 | jjdelcerro | |
31 | /**
|
||
32 | *
|
||
33 | * @author jjdelcerro
|
||
34 | */
|
||
35 | public class DefaultFeatureTypePanel |
||
36 | extends FeatureTypePanelView
|
||
37 | implements FeatureTypePanel
|
||
38 | { |
||
39 | |||
40 | 44149 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureTypePanel.class); |
41 | |||
42 | 44077 | jjdelcerro | private class FeatureTypeTableModel extends AbstractTableModel { |
43 | |||
44 | private final FeatureType featureType; |
||
45 | private final String[] columnNames = new String[] { |
||
46 | "Name",
|
||
47 | "Type",
|
||
48 | "Size",
|
||
49 | "Precision",
|
||
50 | "Default value",
|
||
51 | 44085 | jjdelcerro | "Calculated"
|
52 | 44077 | jjdelcerro | }; |
53 | private final Class[] columnClasses = new Class[] { |
||
54 | String.class,
|
||
55 | String.class,
|
||
56 | Integer.class,
|
||
57 | Integer.class,
|
||
58 | String.class,
|
||
59 | Boolean.class
|
||
60 | }; |
||
61 | |||
62 | public FeatureTypeTableModel() {
|
||
63 | this.featureType = null; |
||
64 | } |
||
65 | |||
66 | public FeatureTypeTableModel(FeatureType featureType) {
|
||
67 | this.featureType = featureType;
|
||
68 | } |
||
69 | |||
70 | @Override
|
||
71 | public int getRowCount() { |
||
72 | if( this.featureType==null ) { |
||
73 | return 0; |
||
74 | } |
||
75 | return this.featureType.size(); |
||
76 | } |
||
77 | |||
78 | @Override
|
||
79 | public int getColumnCount() { |
||
80 | if( this.featureType==null ) { |
||
81 | return 0; |
||
82 | } |
||
83 | return this.columnNames.length; |
||
84 | } |
||
85 | |||
86 | @Override
|
||
87 | public String getColumnName(int columnIndex) { |
||
88 | if( this.featureType==null ) { |
||
89 | return ""; |
||
90 | } |
||
91 | return this.columnNames[columnIndex]; |
||
92 | } |
||
93 | |||
94 | @Override
|
||
95 | public Class<?> getColumnClass(int columnIndex) { |
||
96 | if( this.featureType==null ) { |
||
97 | return String.class; |
||
98 | } |
||
99 | return this.columnClasses[columnIndex]; |
||
100 | } |
||
101 | |||
102 | @Override
|
||
103 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
||
104 | return false; |
||
105 | } |
||
106 | |||
107 | @Override
|
||
108 | public Object getValueAt(int rowIndex, int columnIndex) { |
||
109 | if( this.featureType==null ) { |
||
110 | return ""; |
||
111 | } |
||
112 | FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(rowIndex);
|
||
113 | switch(columnIndex) {
|
||
114 | case 0: |
||
115 | return descriptor.getName();
|
||
116 | case 1: |
||
117 | return descriptor.getDataTypeName();
|
||
118 | case 2: |
||
119 | return descriptor.getSize();
|
||
120 | case 3: |
||
121 | return descriptor.getPrecision();
|
||
122 | case 4: |
||
123 | return Objects.toString(descriptor.getDefaultValue(),""); |
||
124 | case 5: |
||
125 | return descriptor.isComputed();
|
||
126 | default:
|
||
127 | return ""; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | @Override
|
||
132 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
||
133 | } |
||
134 | 44085 | jjdelcerro | |
135 | public void fireUpdateEvent() { |
||
136 | this.fireTableChanged(new TableModelEvent(this)); |
||
137 | } |
||
138 | 44077 | jjdelcerro | } |
139 | |||
140 | private DefaultFeatureTypeAttributePanel descriptorPanel;
|
||
141 | private FeatureType featureType;
|
||
142 | 44085 | jjdelcerro | private boolean editable; |
143 | private FeatureTypeTableModel tableModel;
|
||
144 | private FeatureStore store;
|
||
145 | 44149 | jjdelcerro | private int currentRow; |
146 | private boolean fieldSelectionAllowed; |
||
147 | 44077 | jjdelcerro | |
148 | public DefaultFeatureTypePanel() {
|
||
149 | 44085 | jjdelcerro | this.editable = true; |
150 | 44149 | jjdelcerro | this.currentRow = -1; |
151 | this.fieldSelectionAllowed = true; |
||
152 | 44077 | jjdelcerro | this.initComponents();
|
153 | } |
||
154 | |||
155 | private void initComponents() { |
||
156 | 44128 | jjdelcerro | ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
157 | |||
158 | swingManager.translate(this.btnDelete);
|
||
159 | swingManager.translate(this.btnFormFieldAccept);
|
||
160 | swingManager.translate(this.btnFormFieldDiscard);
|
||
161 | swingManager.translate(this.btnFormFieldModify);
|
||
162 | swingManager.translate(this.btnNew);
|
||
163 | |||
164 | 44085 | jjdelcerro | this.tableModel = new FeatureTypeTableModel(); |
165 | this.tblFields.setModel(this.tableModel); |
||
166 | 44077 | jjdelcerro | this.tblFields.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
167 | @Override
|
||
168 | public void valueChanged(ListSelectionEvent e) { |
||
169 | 44128 | jjdelcerro | if( e.getValueIsAdjusting() ) {
|
170 | return;
|
||
171 | } |
||
172 | 44149 | jjdelcerro | doChangeFieldSelection(); |
173 | 44077 | jjdelcerro | } |
174 | }); |
||
175 | this.descriptorPanel = new DefaultFeatureTypeAttributePanel(); |
||
176 | this.pnlField.setLayout(new BorderLayout()); |
||
177 | this.pnlField.add(this.descriptorPanel, BorderLayout.CENTER); |
||
178 | this.descriptorPanel.setEnabled(false); |
||
179 | 44085 | jjdelcerro | |
180 | 44128 | jjdelcerro | this.btnFormFieldModify.addActionListener(new ActionListener() { |
181 | @Override
|
||
182 | public void actionPerformed(ActionEvent e) { |
||
183 | doFormFieldModify(); |
||
184 | } |
||
185 | }); |
||
186 | 44085 | jjdelcerro | this.btnFormFieldDiscard.addActionListener(new ActionListener() { |
187 | @Override
|
||
188 | public void actionPerformed(ActionEvent e) { |
||
189 | 44149 | jjdelcerro | doFormFieldDiscard(); |
190 | 44085 | jjdelcerro | } |
191 | }); |
||
192 | this.btnFormFieldAccept.addActionListener(new ActionListener() { |
||
193 | @Override
|
||
194 | public void actionPerformed(ActionEvent e) { |
||
195 | 44149 | jjdelcerro | doFormFieldAccept(); |
196 | 44085 | jjdelcerro | } |
197 | }); |
||
198 | this.btnNew.addActionListener(new ActionListener() { |
||
199 | @Override
|
||
200 | public void actionPerformed(ActionEvent e) { |
||
201 | doNewField(); |
||
202 | } |
||
203 | }); |
||
204 | this.btnDelete.addActionListener(new ActionListener() { |
||
205 | @Override
|
||
206 | public void actionPerformed(ActionEvent e) { |
||
207 | doDeleteField(); |
||
208 | } |
||
209 | }); |
||
210 | |||
211 | 44128 | jjdelcerro | this.setPreferredSize(new Dimension(700, 400)); |
212 | if( this.tblFields.getRowCount()>0 ) { |
||
213 | this.tblFields.getSelectionModel().setSelectionInterval(0, 0); |
||
214 | doFormFieldPut(); |
||
215 | } |
||
216 | 44077 | jjdelcerro | } |
217 | |||
218 | 44152 | jjdelcerro | private boolean doFormFieldFetch() { |
219 | 44077 | jjdelcerro | int row = this.tblFields.getSelectedRow(); |
220 | if( row<0 ) { |
||
221 | 44152 | jjdelcerro | return true; |
222 | 44077 | jjdelcerro | } |
223 | FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
||
224 | 44085 | jjdelcerro | if( descriptor instanceof EditableFeatureAttributeDescriptor ) { |
225 | 44152 | jjdelcerro | if( this.descriptorPanel.fetch((EditableFeatureAttributeDescriptor) descriptor)==null ) { |
226 | return false; |
||
227 | } |
||
228 | 44085 | jjdelcerro | this.tableModel.fireUpdateEvent();
|
229 | } |
||
230 | 44152 | jjdelcerro | return true; |
231 | 44085 | jjdelcerro | } |
232 | |||
233 | private void doFormFieldPut() { |
||
234 | int row = this.tblFields.getSelectedRow(); |
||
235 | if( row<0 ) { |
||
236 | return;
|
||
237 | } |
||
238 | FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
||
239 | 44149 | jjdelcerro | this.currentRow = row;
|
240 | 44077 | jjdelcerro | this.descriptorPanel.put(descriptor);
|
241 | 44149 | jjdelcerro | updateButtonState(); |
242 | 44085 | jjdelcerro | if( descriptor instanceof EditableFeatureAttributeDescriptor ) { |
243 | 44128 | jjdelcerro | this.btnFormFieldModify.setEnabled(this.editable); |
244 | } else {
|
||
245 | this.btnFormFieldModify.setEnabled(false); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | 44149 | jjdelcerro | private void updateButtonState() { |
250 | this.descriptorPanel.setEnabled(true); |
||
251 | this.descriptorPanel.setEditable(false); |
||
252 | this.btnFormFieldAccept.setEnabled(false); |
||
253 | this.btnFormFieldDiscard.setEnabled(false); |
||
254 | this.btnFormFieldModify.setEnabled(this.editable); |
||
255 | 44152 | jjdelcerro | this.btnNew.setEnabled(true); |
256 | this.btnDelete.setEnabled(true); |
||
257 | 44149 | jjdelcerro | } |
258 | |||
259 | 44128 | jjdelcerro | private void doFormFieldModify() { |
260 | int row = this.tblFields.getSelectedRow(); |
||
261 | if( row<0 ) { |
||
262 | return;
|
||
263 | } |
||
264 | FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
||
265 | this.descriptorPanel.put(descriptor);
|
||
266 | this.descriptorPanel.setEnabled(true); |
||
267 | this.btnFormFieldAccept.setEnabled(this.editable); |
||
268 | this.btnFormFieldDiscard.setEnabled(this.editable); |
||
269 | this.btnFormFieldModify.setEnabled(false); |
||
270 | 44152 | jjdelcerro | this.btnNew.setEnabled(false); |
271 | this.btnDelete.setEnabled(false); |
||
272 | 44128 | jjdelcerro | if( descriptor instanceof EditableFeatureAttributeDescriptor ) { |
273 | 44085 | jjdelcerro | this.descriptorPanel.setEditable(this.editable); |
274 | } else {
|
||
275 | this.descriptorPanel.setEditable(false); |
||
276 | } |
||
277 | 44149 | jjdelcerro | this.fieldSelectionAllowed = false; |
278 | 44077 | jjdelcerro | } |
279 | |||
280 | 44149 | jjdelcerro | private void doFormFieldAccept() { |
281 | 44152 | jjdelcerro | int row = this.tblFields.getSelectedRow(); |
282 | if( row<0 ) { |
||
283 | return;
|
||
284 | } |
||
285 | if( doFormFieldFetch() ) {
|
||
286 | this.updateButtonState();
|
||
287 | this.tblFields.getSelectionModel().setSelectionInterval(row, row);
|
||
288 | this.tblFields.scrollRectToVisible(this.tblFields.getCellRect(row, 0, true)); |
||
289 | this.fieldSelectionAllowed = true; |
||
290 | } |
||
291 | 44149 | jjdelcerro | } |
292 | |||
293 | private void doFormFieldDiscard() { |
||
294 | doFormFieldPut(); |
||
295 | this.updateButtonState();
|
||
296 | this.fieldSelectionAllowed = true; |
||
297 | } |
||
298 | |||
299 | private void doChangeFieldSelection() { |
||
300 | int row = this.tblFields.getSelectedRow(); |
||
301 | if( row<0 ) { |
||
302 | return;
|
||
303 | } |
||
304 | if( !this.fieldSelectionAllowed ) { |
||
305 | if( row !=this.currentRow ) { |
||
306 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
307 | JOptionPane.showMessageDialog(
|
||
308 | this,
|
||
309 | i18n.getTranslation("_Before_changing_field_accept_or_discard_the_changes"),
|
||
310 | i18n.getTranslation("_Warning"),
|
||
311 | JOptionPane.WARNING_MESSAGE
|
||
312 | ); |
||
313 | this.tblFields.getSelectionModel().setSelectionInterval(
|
||
314 | this.currentRow,
|
||
315 | this.currentRow
|
||
316 | ); |
||
317 | } |
||
318 | return;
|
||
319 | } |
||
320 | doFormFieldPut(); |
||
321 | } |
||
322 | |||
323 | 44077 | jjdelcerro | @Override
|
324 | public JComponent asJComponent() { |
||
325 | return this; |
||
326 | } |
||
327 | |||
328 | 44085 | jjdelcerro | public void setEditable(boolean editable) { |
329 | this.editable = editable;
|
||
330 | this.btnNew.setEnabled(editable);
|
||
331 | this.btnDelete.setEnabled(editable);
|
||
332 | 44149 | jjdelcerro | updateButtonState(); |
333 | 44077 | jjdelcerro | } |
334 | |||
335 | @Override
|
||
336 | public EditableFeatureType fetch(EditableFeatureType type) {
|
||
337 | return type;
|
||
338 | } |
||
339 | |||
340 | @Override
|
||
341 | public void put(FeatureType type) { |
||
342 | this.featureType = type;
|
||
343 | 44085 | jjdelcerro | if( type == null ) { |
344 | this.store = null; |
||
345 | } else {
|
||
346 | // Nos quedamos una referencia para evitar que se destruya, ya que
|
||
347 | 44149 | jjdelcerro | // el featureType se guarda solo una WeakReference.
|
348 | 44085 | jjdelcerro | this.store = type.getStore();
|
349 | } |
||
350 | if( !(type instanceof EditableFeatureType) ) { |
||
351 | this.setEditable(false); |
||
352 | } |
||
353 | this.tableModel = new FeatureTypeTableModel(type); |
||
354 | this.tblFields.setModel(this.tableModel); |
||
355 | 44149 | jjdelcerro | if( type!=null && !type.isEmpty() ) { |
356 | 44077 | jjdelcerro | this.tblFields.getSelectionModel().setSelectionInterval(0, 0); |
357 | } |
||
358 | 44149 | jjdelcerro | updateButtonState(); |
359 | doFormFieldPut(); |
||
360 | 44077 | jjdelcerro | } |
361 | |||
362 | 44085 | jjdelcerro | private void doNewField() { |
363 | EditableFeatureType eft = (EditableFeatureType)this.featureType;
|
||
364 | EditableFeatureAttributeDescriptor descriptor = eft.add( |
||
365 | 44128 | jjdelcerro | getNewFieldName(this.featureType),
|
366 | 44152 | jjdelcerro | DataTypes.STRING, |
367 | 50
|
||
368 | 44085 | jjdelcerro | ); |
369 | this.tableModel.fireUpdateEvent();
|
||
370 | int row = descriptor.getIndex();
|
||
371 | this.tblFields.getSelectionModel().addSelectionInterval(row,row);
|
||
372 | 44152 | jjdelcerro | this.tblFields.scrollRectToVisible(this.tblFields.getCellRect(row, 0, true)); |
373 | 44149 | jjdelcerro | updateButtonState(); |
374 | 44152 | jjdelcerro | doFormFieldModify(); |
375 | 44085 | jjdelcerro | } |
376 | |||
377 | private void doDeleteField() { |
||
378 | int row = this.tblFields.getSelectedRow(); |
||
379 | if( row<0 ) { |
||
380 | return;
|
||
381 | } |
||
382 | 44149 | jjdelcerro | // FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
383 | 44085 | jjdelcerro | ((EditableFeatureType)this.featureType).remove(row);
|
384 | this.tableModel.fireUpdateEvent();
|
||
385 | if( row >= this.tblFields.getRowCount()) { |
||
386 | row = this.tblFields.getRowCount()-1; |
||
387 | } |
||
388 | this.tblFields.getSelectionModel().addSelectionInterval(row,row);
|
||
389 | 44149 | jjdelcerro | updateButtonState(); |
390 | doFormFieldPut(); |
||
391 | 44085 | jjdelcerro | } |
392 | |||
393 | 44128 | jjdelcerro | public static String getNewFieldName(FeatureType featureType) { |
394 | 44085 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
395 | String prefix = i18n.getTranslation("_Field"); |
||
396 | String fieldName;
|
||
397 | for (int i = 1; i < 1000; i++) { |
||
398 | fieldName = prefix +"-"+i;
|
||
399 | 44128 | jjdelcerro | if( featureType.get(fieldName)==null ) { |
400 | 44085 | jjdelcerro | return fieldName;
|
401 | } |
||
402 | } |
||
403 | fieldName = prefix + "-" + (new Date()).getTime(); |
||
404 | return fieldName;
|
||
405 | } |
||
406 | |||
407 | 44077 | jjdelcerro | } |