svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.impl / src / main / java / org / gvsig / export / swing / impl / panels / SelectAttributesPanel.java @ 44408
History | View | Annotate | Download (18.2 KB)
1 |
package org.gvsig.export.swing.impl.panels; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.awt.Font; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.KeyAdapter; |
8 |
import java.awt.event.KeyEvent; |
9 |
import java.net.URL; |
10 |
import java.util.ArrayList; |
11 |
import java.util.Comparator; |
12 |
import java.util.Iterator; |
13 |
import java.util.List; |
14 |
import java.util.Map; |
15 |
import java.util.Objects; |
16 |
import javax.swing.DefaultCellEditor; |
17 |
import javax.swing.DefaultComboBoxModel; |
18 |
import javax.swing.DefaultListCellRenderer; |
19 |
import javax.swing.ImageIcon; |
20 |
import javax.swing.JButton; |
21 |
import javax.swing.JCheckBox; |
22 |
import javax.swing.JComboBox; |
23 |
import javax.swing.JComponent; |
24 |
import javax.swing.JLabel; |
25 |
import javax.swing.JList; |
26 |
import javax.swing.JOptionPane; |
27 |
import javax.swing.JTable; |
28 |
import javax.swing.table.AbstractTableModel; |
29 |
import javax.swing.table.TableCellRenderer; |
30 |
import javax.swing.table.TableColumnModel; |
31 |
import javax.swing.table.TableModel; |
32 |
import javax.swing.text.JTextComponent; |
33 |
import org.apache.commons.io.FilenameUtils; |
34 |
import org.apache.commons.lang.StringUtils; |
35 |
import org.gvsig.export.ExportAttributes; |
36 |
import org.gvsig.export.ExportAttributes.ExportAttribute; |
37 |
import org.gvsig.export.ExportParameters; |
38 |
import org.gvsig.export.swing.JExportProcessPanel; |
39 |
import org.gvsig.export.swing.spi.ExportPanel; |
40 |
import org.gvsig.export.swing.spi.ExportPanelValidationException; |
41 |
import org.gvsig.export.swing.spi.ExportPanelsManager; |
42 |
import org.gvsig.fmap.dal.DataTypes; |
43 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
44 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 |
import org.gvsig.tools.ToolsLocator; |
46 |
import org.gvsig.tools.dataTypes.DataType; |
47 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
48 |
import org.gvsig.tools.i18n.I18nManager; |
49 |
import org.gvsig.tools.swing.api.FilteredTableModel; |
50 |
import org.gvsig.tools.swing.api.ListElement; |
51 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
52 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
53 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
54 |
import org.gvsig.tools.util.LabeledValue; |
55 |
import org.gvsig.tools.util.LabeledValueImpl; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
/**
|
60 |
*
|
61 |
* @author jjdelcerro
|
62 |
*/
|
63 |
@SuppressWarnings("UseSpecificCatch") |
64 |
public class SelectAttributesPanel |
65 |
extends SelectAttributesPanelView
|
66 |
implements ExportPanel {
|
67 |
|
68 |
private static final Logger LOGGER = LoggerFactory.getLogger(SelectAttributesPanel.class); |
69 |
|
70 |
private static final int COLUMN_FIELD_EXPORT = 0; |
71 |
private static final int COLUMN_FIELD_TYPE = 1; |
72 |
private static final int COLUMN_FIELD_SIZE = 2; |
73 |
private static final int COLUMN_FIELD_NAME = 3; |
74 |
private static final int COLUMN_FIELD_NEW_NAME = 4; |
75 |
|
76 |
private static final int COLUMN_FIELD_COUNT = 5; |
77 |
|
78 |
private static class AttributesTableModel |
79 |
extends AbstractTableModel |
80 |
implements TableModel { |
81 |
|
82 |
final private ExportAttributes attributes; |
83 |
private final String[] columnNames = new String[COLUMN_FIELD_COUNT]; |
84 |
private final Class[] columnClases = new Class[COLUMN_FIELD_COUNT]; |
85 |
private final DataTypesManager dataManager; |
86 |
|
87 |
public AttributesTableModel(ExportAttributes attributes) {
|
88 |
this.attributes = attributes;
|
89 |
this.dataManager = ToolsLocator.getDataTypesManager();
|
90 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
91 |
columnNames[COLUMN_FIELD_NAME] = i18nManager.getTranslation("_name");
|
92 |
columnNames[COLUMN_FIELD_NEW_NAME] = i18nManager.getTranslation("_new_name");
|
93 |
columnNames[COLUMN_FIELD_TYPE] = i18nManager.getTranslation("_type");
|
94 |
columnNames[COLUMN_FIELD_SIZE] = i18nManager.getTranslation("_size");
|
95 |
columnNames[COLUMN_FIELD_EXPORT] = "";
|
96 |
|
97 |
columnClases[COLUMN_FIELD_NAME] = String.class;
|
98 |
columnClases[COLUMN_FIELD_NEW_NAME] = String.class;
|
99 |
columnClases[COLUMN_FIELD_TYPE] = String.class;
|
100 |
columnClases[COLUMN_FIELD_SIZE] = Integer.class;
|
101 |
columnClases[COLUMN_FIELD_EXPORT] = Boolean.class;
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public int getRowCount() { |
106 |
return this.attributes.size(); |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public int getColumnCount() { |
111 |
return COLUMN_FIELD_COUNT;
|
112 |
} |
113 |
|
114 |
@Override
|
115 |
public String getColumnName(int columnIndex) { |
116 |
return this.columnNames[columnIndex]; |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public Class<?> getColumnClass(int columnIndex) { |
121 |
return this.columnClases[columnIndex]; |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
126 |
switch (columnIndex) {
|
127 |
case COLUMN_FIELD_NAME:
|
128 |
return false; |
129 |
default:
|
130 |
return true; |
131 |
} |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public Object getValueAt(int rowIndex, int columnIndex) { |
136 |
ExportAttribute attr = this.attributes.get(rowIndex);
|
137 |
switch (columnIndex) {
|
138 |
case COLUMN_FIELD_NAME:
|
139 |
return attr.getName();
|
140 |
case COLUMN_FIELD_NEW_NAME:
|
141 |
return attr.getNewName();
|
142 |
case COLUMN_FIELD_TYPE:
|
143 |
return dataManager.getTypeName(attr.getNewDataType());
|
144 |
case COLUMN_FIELD_SIZE:
|
145 |
return attr.getSize();
|
146 |
case COLUMN_FIELD_EXPORT:
|
147 |
return attr.isExported();
|
148 |
default:
|
149 |
return null; |
150 |
} |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
155 |
ExportAttribute attr = this.attributes.get(rowIndex);
|
156 |
switch (columnIndex) {
|
157 |
case COLUMN_FIELD_NAME:
|
158 |
break;
|
159 |
case COLUMN_FIELD_NEW_NAME:
|
160 |
attr.setNewName(Objects.toString(aValue, attr.getName())); |
161 |
break;
|
162 |
case COLUMN_FIELD_TYPE:
|
163 |
attr.setNewType(dataManager.getType(Objects.toString(aValue))); |
164 |
break;
|
165 |
case COLUMN_FIELD_SIZE:
|
166 |
try {
|
167 |
attr.setSize(Integer.parseInt(Objects.toString(aValue)));
|
168 |
} catch (Exception ex) { |
169 |
LOGGER.warn("Not been able to set size value of attribute " + attr.getName() + " (" + Objects.toString(aValue) + ").", ex); |
170 |
} |
171 |
break;
|
172 |
case COLUMN_FIELD_EXPORT:
|
173 |
try {
|
174 |
attr.setExported((boolean) dataManager.get(DataTypes.BOOLEAN).coerce(aValue));
|
175 |
} catch (Exception ex) { |
176 |
LOGGER.warn("Not been able to set exported value of attribute " + attr.getName() + " (" + Objects.toString(aValue) + ").", ex); |
177 |
} |
178 |
break;
|
179 |
default:
|
180 |
break;
|
181 |
} |
182 |
this.fireTableCellUpdated(rowIndex, columnIndex);
|
183 |
} |
184 |
} |
185 |
|
186 |
private static class TableFilterController { |
187 |
|
188 |
private final JTable jtable; |
189 |
private final JTextComponent jtext; |
190 |
private final JButton btnFilter; |
191 |
private final int filterColumn; |
192 |
|
193 |
private FilteredTableModel tableModel;
|
194 |
|
195 |
public TableFilterController(JTable jtable, JTextComponent jtext, JButton btnFilter, int filterColumn) { |
196 |
this.jtable = jtable;
|
197 |
this.filterColumn = filterColumn;
|
198 |
this.btnFilter = btnFilter;
|
199 |
this.jtext = jtext;
|
200 |
this.tableModel = null; |
201 |
ToolsSwingLocator.getToolsSwingManager().setDefaultPopupMenu(jtext); |
202 |
ToolsSwingLocator.getToolsSwingManager().addClearButton(jtext, new ActionListener() { |
203 |
@Override
|
204 |
public void actionPerformed(ActionEvent e) { |
205 |
if (tableModel != null) { |
206 |
tableModel.setFilter(null);
|
207 |
jtext.setText("");
|
208 |
} |
209 |
} |
210 |
}); |
211 |
this.jtext.addKeyListener(new KeyAdapter() { |
212 |
@Override
|
213 |
public void keyPressed(KeyEvent e) { |
214 |
if (e.getKeyChar() == '\n') { |
215 |
if (tableModel != null) { |
216 |
tableModel.setFilter(jtext.getText()); |
217 |
} |
218 |
} |
219 |
} |
220 |
}); |
221 |
this.btnFilter.addActionListener(new ActionListener() { |
222 |
@Override
|
223 |
public void actionPerformed(ActionEvent e) { |
224 |
if (tableModel != null) { |
225 |
tableModel.setFilter(jtext.getText()); |
226 |
} |
227 |
} |
228 |
}); |
229 |
} |
230 |
|
231 |
public void setModel(TableModel tableModel) { |
232 |
this.tableModel = ToolsSwingLocator.getToolsSwingManager().createFilteredTableModel(
|
233 |
tableModel, |
234 |
this.filterColumn
|
235 |
); |
236 |
this.jtable.setModel(this.tableModel); |
237 |
} |
238 |
} |
239 |
|
240 |
private class DataTypeCellRenderer |
241 |
extends JLabel |
242 |
implements TableCellRenderer { |
243 |
|
244 |
public DataTypeCellRenderer() {
|
245 |
} |
246 |
|
247 |
@Override
|
248 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
249 |
if (value != null) { |
250 |
String name = (String) table.getModel().getValueAt(row, COLUMN_FIELD_NAME); |
251 |
this.setText(value.toString());
|
252 |
Font f = this.getFont(); |
253 |
if (isOriginalType(row, value.toString())) {
|
254 |
this.setFont(f.deriveFont(f.getStyle() & ~Font.BOLD)); |
255 |
} else {
|
256 |
this.setFont(f.deriveFont(f.getStyle() | Font.BOLD)); |
257 |
} |
258 |
} else {
|
259 |
this.setText(""); |
260 |
} |
261 |
return this; |
262 |
} |
263 |
} |
264 |
|
265 |
private class DataTypeCellEditor extends DefaultCellEditor { |
266 |
|
267 |
private int currentRow = -1; |
268 |
|
269 |
private class DataTypeListCellRenderer extends DefaultListCellRenderer { |
270 |
|
271 |
@Override
|
272 |
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
273 |
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
274 |
if (value != null) { |
275 |
label.setText(value.toString()); |
276 |
Font f = label.getFont();
|
277 |
if (isOriginalType(currentRow, value.toString())) {
|
278 |
label.setFont(f.deriveFont(f.getStyle() | Font.BOLD));
|
279 |
} else {
|
280 |
label.setFont(f.deriveFont(f.getStyle() & ~Font.BOLD));
|
281 |
} |
282 |
} |
283 |
return label;
|
284 |
} |
285 |
} |
286 |
|
287 |
public DataTypeCellEditor() {
|
288 |
super(new JComboBox()); |
289 |
JComboBox combo = (JComboBox) this.getComponent(); |
290 |
combo.setRenderer(new DataTypeListCellRenderer());
|
291 |
combo.setModel(this.getDataTypeComboModel());
|
292 |
} |
293 |
|
294 |
@Override
|
295 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { |
296 |
this.currentRow = row;
|
297 |
JComboBox combo = (JComboBox) super.getTableCellEditorComponent(table, value, isSelected, row, column); |
298 |
ListElement.setSelected(combo, value); |
299 |
return combo;
|
300 |
} |
301 |
|
302 |
private DefaultComboBoxModel getDataTypeComboModel() { |
303 |
DataTypesManager dataManager = ToolsLocator.getDataTypesManager(); |
304 |
List<ListElement<Integer>> fieldTypes = new ArrayList<>(); |
305 |
Iterator<DataType> it = dataManager.iterator();
|
306 |
while (it.hasNext()) {
|
307 |
DataType dataType = it.next(); |
308 |
fieldTypes.add( new ListElement<>(dataType.getName(), dataType.getType()) );
|
309 |
} |
310 |
fieldTypes.sort(new Comparator<ListElement<Integer>>() { |
311 |
@Override
|
312 |
public int compare(ListElement<Integer> o1, ListElement<Integer> o2) { |
313 |
return o1.toString().compareTo(o2.toString());
|
314 |
} |
315 |
}); |
316 |
|
317 |
DefaultComboBoxModel<ListElement<Integer>> model = new DefaultComboBoxModel<>(); |
318 |
for (ListElement<Integer> element : fieldTypes) { |
319 |
model.addElement(element); |
320 |
} |
321 |
return model;
|
322 |
} |
323 |
} |
324 |
|
325 |
private final ExportParameters parameters; |
326 |
private final JExportProcessPanel processPanel; |
327 |
|
328 |
// private Map<String, String> fieldAttributeTypes;
|
329 |
// private FilteredTableModel filteredTableModel;
|
330 |
private TableModel tableModel; |
331 |
private TableFilterController tableFilterController;
|
332 |
private FeatureType targetFeatureType ;
|
333 |
|
334 |
public SelectAttributesPanel(
|
335 |
JExportProcessPanel processPanel, |
336 |
ExportParameters parameters |
337 |
) { |
338 |
this.processPanel = processPanel;
|
339 |
this.parameters = parameters;
|
340 |
this.parameters.getExportAttributes().setActive(true); |
341 |
this.initComponents();
|
342 |
} |
343 |
|
344 |
@Override
|
345 |
public String getIdPanel() { |
346 |
return ExportPanelsManager.PANEL_ATTRIBUTES_SELECTION;
|
347 |
} |
348 |
|
349 |
@Override
|
350 |
public String getTitlePanel() { |
351 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
352 |
return i18n.getTranslation("_Attribute_Selection"); |
353 |
} |
354 |
|
355 |
@Override
|
356 |
public boolean validatePanel() throws ExportPanelValidationException { |
357 |
ExportAttributes expAttrs = this.parameters.getExportAttributes();
|
358 |
if (!expAttrs.isAttributeNamesValid()) {
|
359 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
360 |
int as = JOptionPane.showConfirmDialog( |
361 |
this,
|
362 |
i18nManager.getTranslation("_Not_valid_attribute_names_Do_you_want_a_suggest_names?"),
|
363 |
i18nManager.getTranslation("_Not_valid_attriute_names"),
|
364 |
JOptionPane.YES_NO_OPTION
|
365 |
); |
366 |
if (as == JOptionPane.YES_OPTION) { |
367 |
expAttrs.fixAttributeNames(); |
368 |
this.fillTableWithAttributes();
|
369 |
} else if (as == JOptionPane.NO_OPTION) { |
370 |
|
371 |
} |
372 |
return false; |
373 |
} |
374 |
return true; |
375 |
} |
376 |
|
377 |
@Override
|
378 |
public void enterPanel() { |
379 |
this.fillTableWithAttributes();
|
380 |
} |
381 |
|
382 |
@Override
|
383 |
public void previousPanel() { |
384 |
this.fillTableWithAttributes();
|
385 |
} |
386 |
|
387 |
@Override
|
388 |
public void nextPanel() { |
389 |
//get params are in the validate method
|
390 |
} |
391 |
|
392 |
@Override
|
393 |
public JComponent asJComponent() { |
394 |
return this; |
395 |
} |
396 |
|
397 |
private boolean isOriginalType(int row, String type) { |
398 |
try {
|
399 |
if( this.targetFeatureType==null ) { |
400 |
return true; |
401 |
} |
402 |
TableModel model = this.tblAttributes.getModel(); |
403 |
String name = (String) model.getValueAt(row, COLUMN_FIELD_NAME); |
404 |
FeatureAttributeDescriptor attr = this.targetFeatureType.getAttributeDescriptor(name);
|
405 |
if (attr == null) { |
406 |
return true; |
407 |
} |
408 |
return StringUtils.equalsIgnoreCase(type, attr.getDataTypeName());
|
409 |
} catch (Exception ex) { |
410 |
return true; |
411 |
} |
412 |
|
413 |
} |
414 |
|
415 |
private ExportAttributes getExportAttributes() {
|
416 |
ExportAttributes attributes = this.parameters.getExportAttributes();
|
417 |
return attributes;
|
418 |
} |
419 |
|
420 |
private void fillTableWithAttributes() { |
421 |
ExportAttributes attributes = this.getExportAttributes();
|
422 |
if( this.targetFeatureType == null ) { |
423 |
this.targetFeatureType = this.getExportAttributes().getTargetFeatureType(); |
424 |
} |
425 |
|
426 |
|
427 |
this.tableModel = new AttributesTableModel(attributes); |
428 |
this.tblAttributes.setModel(this.tableModel); |
429 |
this.tableFilterController.setModel(this.tableModel); |
430 |
|
431 |
TableColumnModel columnModel = this.tblAttributes.getColumnModel(); |
432 |
columnModel.getColumn(COLUMN_FIELD_TYPE).setCellEditor(new DataTypeCellEditor());
|
433 |
columnModel.getColumn(COLUMN_FIELD_TYPE).setCellRenderer(new DataTypeCellRenderer());
|
434 |
|
435 |
columnModel.getColumn(COLUMN_FIELD_EXPORT).setPreferredWidth( new JCheckBox().getPreferredSize().width*2); |
436 |
columnModel.getColumn(COLUMN_FIELD_TYPE).setPreferredWidth( new JLabel("XXIntegerXX").getPreferredSize().width); |
437 |
columnModel.getColumn(COLUMN_FIELD_SIZE).setPreferredWidth( new JLabel("00000").getPreferredSize().width); |
438 |
|
439 |
} |
440 |
|
441 |
private void initComponents() { |
442 |
|
443 |
this.tblAttributes.setAutoCreateRowSorter(true); |
444 |
this.tblAttributes.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); |
445 |
|
446 |
this.tableFilterController = new TableFilterController( |
447 |
this.tblAttributes,
|
448 |
this.txtFilter,
|
449 |
this.btnFilter,
|
450 |
COLUMN_FIELD_NAME |
451 |
); |
452 |
this.btnCheckAll.addActionListener(new ActionListener() { |
453 |
@Override
|
454 |
public void actionPerformed(ActionEvent e) { |
455 |
doCheckAll(); |
456 |
} |
457 |
}); |
458 |
this.btnUncheckAll.addActionListener(new ActionListener() { |
459 |
@Override
|
460 |
public void actionPerformed(ActionEvent e) { |
461 |
doUncheckAll(); |
462 |
} |
463 |
}); |
464 |
|
465 |
this.translate();
|
466 |
} |
467 |
|
468 |
private void doUncheckAll() { |
469 |
TableModel model = this.tblAttributes.getModel(); |
470 |
for (int row = 0; row < model.getRowCount(); row++) { |
471 |
model.setValueAt(false, row, COLUMN_FIELD_EXPORT);
|
472 |
} |
473 |
} |
474 |
|
475 |
private void doCheckAll() { |
476 |
TableModel model = this.tblAttributes.getModel(); |
477 |
for (int row = 0; row < model.getRowCount(); row++) { |
478 |
model.setValueAt(true, row, COLUMN_FIELD_EXPORT);
|
479 |
} |
480 |
} |
481 |
|
482 |
private void translate() { |
483 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
484 |
manager.translate(this.lblHeader);
|
485 |
} |
486 |
|
487 |
@Override
|
488 |
public ImageIcon loadImage(String imageName) { |
489 |
String name = FilenameUtils.getBaseName(imageName);
|
490 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
491 |
if (theme.exists(name)) {
|
492 |
return theme.get(name);
|
493 |
} |
494 |
URL url = this.getClass().getResource(name + ".png"); |
495 |
if (url == null) { |
496 |
return null; |
497 |
} |
498 |
return new ImageIcon(url); |
499 |
} |
500 |
|
501 |
} |