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 @ 44149
History | View | Annotate | Download (12.9 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 | 44085 | jjdelcerro | private void doFormFieldFetch() { |
219 | 44077 | jjdelcerro | int row = this.tblFields.getSelectedRow(); |
220 | if( row<0 ) { |
||
221 | return;
|
||
222 | } |
||
223 | FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
||
224 | 44085 | jjdelcerro | if( descriptor instanceof EditableFeatureAttributeDescriptor ) { |
225 | this.descriptorPanel.fetch((EditableFeatureAttributeDescriptor) descriptor);
|
||
226 | this.tableModel.fireUpdateEvent();
|
||
227 | } |
||
228 | } |
||
229 | |||
230 | private void doFormFieldPut() { |
||
231 | int row = this.tblFields.getSelectedRow(); |
||
232 | if( row<0 ) { |
||
233 | return;
|
||
234 | } |
||
235 | FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
||
236 | 44149 | jjdelcerro | this.currentRow = row;
|
237 | 44077 | jjdelcerro | this.descriptorPanel.put(descriptor);
|
238 | 44149 | jjdelcerro | updateButtonState(); |
239 | 44085 | jjdelcerro | if( descriptor instanceof EditableFeatureAttributeDescriptor ) { |
240 | 44128 | jjdelcerro | this.btnFormFieldModify.setEnabled(this.editable); |
241 | } else {
|
||
242 | this.btnFormFieldModify.setEnabled(false); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | 44149 | jjdelcerro | private void updateButtonState() { |
247 | this.descriptorPanel.setEnabled(true); |
||
248 | this.descriptorPanel.setEditable(false); |
||
249 | this.btnFormFieldAccept.setEnabled(false); |
||
250 | this.btnFormFieldDiscard.setEnabled(false); |
||
251 | this.btnFormFieldModify.setEnabled(this.editable); |
||
252 | } |
||
253 | |||
254 | 44128 | jjdelcerro | private void doFormFieldModify() { |
255 | int row = this.tblFields.getSelectedRow(); |
||
256 | if( row<0 ) { |
||
257 | return;
|
||
258 | } |
||
259 | FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
||
260 | this.descriptorPanel.put(descriptor);
|
||
261 | this.descriptorPanel.setEnabled(true); |
||
262 | this.btnFormFieldAccept.setEnabled(this.editable); |
||
263 | this.btnFormFieldDiscard.setEnabled(this.editable); |
||
264 | this.btnFormFieldModify.setEnabled(false); |
||
265 | if( descriptor instanceof EditableFeatureAttributeDescriptor ) { |
||
266 | 44085 | jjdelcerro | this.descriptorPanel.setEditable(this.editable); |
267 | } else {
|
||
268 | this.descriptorPanel.setEditable(false); |
||
269 | } |
||
270 | 44149 | jjdelcerro | this.fieldSelectionAllowed = false; |
271 | 44077 | jjdelcerro | } |
272 | |||
273 | 44149 | jjdelcerro | private void doFormFieldAccept() { |
274 | doFormFieldFetch(); |
||
275 | this.updateButtonState();
|
||
276 | this.fieldSelectionAllowed = true; |
||
277 | } |
||
278 | |||
279 | private void doFormFieldDiscard() { |
||
280 | doFormFieldPut(); |
||
281 | this.updateButtonState();
|
||
282 | this.fieldSelectionAllowed = true; |
||
283 | } |
||
284 | |||
285 | private void doChangeFieldSelection() { |
||
286 | int row = this.tblFields.getSelectedRow(); |
||
287 | if( row<0 ) { |
||
288 | return;
|
||
289 | } |
||
290 | if( !this.fieldSelectionAllowed ) { |
||
291 | if( row !=this.currentRow ) { |
||
292 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
293 | JOptionPane.showMessageDialog(
|
||
294 | this,
|
||
295 | i18n.getTranslation("_Before_changing_field_accept_or_discard_the_changes"),
|
||
296 | i18n.getTranslation("_Warning"),
|
||
297 | JOptionPane.WARNING_MESSAGE
|
||
298 | ); |
||
299 | this.tblFields.getSelectionModel().setSelectionInterval(
|
||
300 | this.currentRow,
|
||
301 | this.currentRow
|
||
302 | ); |
||
303 | } |
||
304 | return;
|
||
305 | } |
||
306 | doFormFieldPut(); |
||
307 | } |
||
308 | |||
309 | 44077 | jjdelcerro | @Override
|
310 | public JComponent asJComponent() { |
||
311 | return this; |
||
312 | } |
||
313 | |||
314 | 44085 | jjdelcerro | public void setEditable(boolean editable) { |
315 | this.editable = editable;
|
||
316 | this.btnNew.setEnabled(editable);
|
||
317 | this.btnDelete.setEnabled(editable);
|
||
318 | 44149 | jjdelcerro | updateButtonState(); |
319 | 44077 | jjdelcerro | } |
320 | |||
321 | @Override
|
||
322 | public EditableFeatureType fetch(EditableFeatureType type) {
|
||
323 | return type;
|
||
324 | } |
||
325 | |||
326 | @Override
|
||
327 | public void put(FeatureType type) { |
||
328 | this.featureType = type;
|
||
329 | 44085 | jjdelcerro | if( type == null ) { |
330 | this.store = null; |
||
331 | } else {
|
||
332 | // Nos quedamos una referencia para evitar que se destruya, ya que
|
||
333 | 44149 | jjdelcerro | // el featureType se guarda solo una WeakReference.
|
334 | 44085 | jjdelcerro | this.store = type.getStore();
|
335 | } |
||
336 | if( !(type instanceof EditableFeatureType) ) { |
||
337 | this.setEditable(false); |
||
338 | } |
||
339 | this.tableModel = new FeatureTypeTableModel(type); |
||
340 | this.tblFields.setModel(this.tableModel); |
||
341 | 44149 | jjdelcerro | if( type!=null && !type.isEmpty() ) { |
342 | 44077 | jjdelcerro | this.tblFields.getSelectionModel().setSelectionInterval(0, 0); |
343 | } |
||
344 | 44149 | jjdelcerro | updateButtonState(); |
345 | doFormFieldPut(); |
||
346 | 44077 | jjdelcerro | } |
347 | |||
348 | 44085 | jjdelcerro | private void doNewField() { |
349 | EditableFeatureType eft = (EditableFeatureType)this.featureType;
|
||
350 | EditableFeatureAttributeDescriptor descriptor = eft.add( |
||
351 | 44128 | jjdelcerro | getNewFieldName(this.featureType),
|
352 | 44085 | jjdelcerro | DataTypes.STRING |
353 | ); |
||
354 | this.tableModel.fireUpdateEvent();
|
||
355 | int row = descriptor.getIndex();
|
||
356 | this.tblFields.getSelectionModel().addSelectionInterval(row,row);
|
||
357 | 44149 | jjdelcerro | updateButtonState(); |
358 | doFormFieldPut(); |
||
359 | 44085 | jjdelcerro | } |
360 | |||
361 | private void doDeleteField() { |
||
362 | int row = this.tblFields.getSelectedRow(); |
||
363 | if( row<0 ) { |
||
364 | return;
|
||
365 | } |
||
366 | 44149 | jjdelcerro | // FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(row);
|
367 | 44085 | jjdelcerro | ((EditableFeatureType)this.featureType).remove(row);
|
368 | this.tableModel.fireUpdateEvent();
|
||
369 | if( row >= this.tblFields.getRowCount()) { |
||
370 | row = this.tblFields.getRowCount()-1; |
||
371 | } |
||
372 | this.tblFields.getSelectionModel().addSelectionInterval(row,row);
|
||
373 | 44149 | jjdelcerro | updateButtonState(); |
374 | doFormFieldPut(); |
||
375 | 44085 | jjdelcerro | } |
376 | |||
377 | 44128 | jjdelcerro | public static String getNewFieldName(FeatureType featureType) { |
378 | 44085 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
379 | String prefix = i18n.getTranslation("_Field"); |
||
380 | String fieldName;
|
||
381 | for (int i = 1; i < 1000; i++) { |
||
382 | fieldName = prefix +"-"+i;
|
||
383 | 44128 | jjdelcerro | if( featureType.get(fieldName)==null ) { |
384 | 44085 | jjdelcerro | return fieldName;
|
385 | } |
||
386 | } |
||
387 | fieldName = prefix + "-" + (new Date()).getTime(); |
||
388 | return fieldName;
|
||
389 | } |
||
390 | |||
391 | 44077 | jjdelcerro | } |