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 @ 44270
History | View | Annotate | Download (14.6 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.util.ArrayList; |
8 |
import java.util.Comparator; |
9 |
import java.util.HashMap; |
10 |
import java.util.Iterator; |
11 |
import java.util.List; |
12 |
import javax.swing.ComboBoxModel; |
13 |
import javax.swing.DefaultCellEditor; |
14 |
import javax.swing.DefaultComboBoxModel; |
15 |
import javax.swing.DefaultListModel; |
16 |
import javax.swing.JComboBox; |
17 |
import javax.swing.JComponent; |
18 |
import javax.swing.JLabel; |
19 |
import javax.swing.JList; |
20 |
import javax.swing.JOptionPane; |
21 |
import javax.swing.JTable; |
22 |
import javax.swing.JTextField; |
23 |
import javax.swing.ListCellRenderer; |
24 |
import javax.swing.SwingConstants; |
25 |
import javax.swing.event.ListDataListener; |
26 |
import javax.swing.event.ListSelectionEvent; |
27 |
import javax.swing.event.ListSelectionListener; |
28 |
import javax.swing.table.DefaultTableModel; |
29 |
import javax.swing.table.TableModel; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
import org.gvsig.export.ExportAttributes; |
32 |
import org.gvsig.export.ExportLocator; |
33 |
import org.gvsig.export.ExportManager; |
34 |
import org.gvsig.export.ExportManager.ExportFilter; |
35 |
import org.gvsig.export.ExportParameters; |
36 |
import org.gvsig.export.spi.AttributeNamesTranslator; |
37 |
import org.gvsig.export.swing.JExportProcessPanel; |
38 |
import org.gvsig.export.swing.spi.ExportPanel; |
39 |
import org.gvsig.export.swing.spi.ExportPanelValidationException; |
40 |
import org.gvsig.export.swing.spi.ExportPanelsManager; |
41 |
import org.gvsig.fmap.dal.DALLocator; |
42 |
import org.gvsig.fmap.dal.DataManager; |
43 |
import org.gvsig.fmap.dal.DataTypes; |
44 |
import org.gvsig.fmap.dal.exception.InitializeException; |
45 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
46 |
import org.gvsig.fmap.dal.feature.FeatureType; |
47 |
import org.gvsig.tools.ToolsLocator; |
48 |
import org.gvsig.tools.dataTypes.DataType; |
49 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
50 |
import org.gvsig.tools.dynobject.DynField; |
51 |
import org.gvsig.tools.dynobject.DynObject; |
52 |
import org.gvsig.tools.dynobject.DynObjectManager; |
53 |
import org.gvsig.tools.evaluator.Evaluator; |
54 |
import org.gvsig.tools.evaluator.EvaluatorData; |
55 |
import org.gvsig.tools.evaluator.EvaluatorException; |
56 |
import org.gvsig.tools.i18n.I18nManager; |
57 |
import org.gvsig.tools.swing.api.ListElement; |
58 |
import org.slf4j.Logger; |
59 |
import org.slf4j.LoggerFactory; |
60 |
|
61 |
/**
|
62 |
*
|
63 |
* @author jjdelcerro
|
64 |
*/
|
65 |
public class SelectAttributesPanel |
66 |
extends SelectAttributesPanelView
|
67 |
implements ExportPanel {
|
68 |
|
69 |
private static final Logger LOG = LoggerFactory.getLogger(SelectAttributesPanel.class); |
70 |
|
71 |
public static final int COLUMN_FIELD_NAME = 0; |
72 |
public static final int COLUMN_FIELD_NEW_NAME = 1; |
73 |
public static final int COLUMN_FIELD_TYPE = 2; |
74 |
public static final int COLUMN_FIELD_SIZE = 3; |
75 |
public static final int COLUMN_FIELD_EXPORT = 4; |
76 |
|
77 |
private final ExportParameters parameters; |
78 |
private final JExportProcessPanel processPanel; |
79 |
private HashMap<String, String> layerAttributeDescriptor; |
80 |
|
81 |
public SelectAttributesPanel(
|
82 |
JExportProcessPanel processPanel, |
83 |
ExportParameters parameters |
84 |
) { |
85 |
this.processPanel = processPanel;
|
86 |
this.parameters = parameters;
|
87 |
this.initComponents();
|
88 |
} |
89 |
|
90 |
@Override
|
91 |
public String getIdPanel() { |
92 |
return ExportPanelsManager.PANEL_ATTRIBUTES_SELECTION;
|
93 |
} |
94 |
|
95 |
@Override
|
96 |
public String getTitlePanel() { |
97 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
98 |
return i18n.getTranslation("_Attribute_Selection"); |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public boolean validatePanel() throws ExportPanelValidationException { |
103 |
checkPanel(true);
|
104 |
return true; |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public void enterPanel() { |
109 |
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public void previousPanel() { |
114 |
|
115 |
} |
116 |
|
117 |
@Override
|
118 |
public void nextPanel() { |
119 |
ExportAttributes expAttrs = ExportLocator.getServiceManager() |
120 |
.createExportAttributes(this.parameters.getSourceFeatureType());
|
121 |
|
122 |
DataTypesManager dataManager = ToolsLocator.getDataTypesManager(); |
123 |
TableModel model = this.tblAttributes.getModel(); |
124 |
for (int i = 0; i < model.getRowCount(); i++) { |
125 |
String object = (String) model.getValueAt(i, COLUMN_FIELD_NAME); |
126 |
for (ExportAttributes.ExportAttribute expAttr : expAttrs) {
|
127 |
if (object == null ? expAttr.getName() == null : object.equals(expAttr.getName())) { |
128 |
try {
|
129 |
expAttr.setNewName((String) model.getValueAt(i, COLUMN_FIELD_NEW_NAME));
|
130 |
Object value = model.getValueAt(i, COLUMN_FIELD_TYPE);
|
131 |
int dataType = dataManager.getType(value.toString());
|
132 |
expAttr.setNewType(dataType); |
133 |
int sizeValue;
|
134 |
try {
|
135 |
sizeValue = Integer.parseInt(model.getValueAt(i, COLUMN_FIELD_SIZE).toString());
|
136 |
} catch (NumberFormatException ex) { |
137 |
LOG.error("Not been able to set size value" + expAttr.getName() + ".Exception:" + ex); |
138 |
sizeValue = expAttr.getSize(); |
139 |
} |
140 |
expAttr.setSize(sizeValue); |
141 |
expAttr.setExported((boolean) model.getValueAt(i, COLUMN_FIELD_EXPORT));
|
142 |
} catch (Exception ex) { |
143 |
// TODO
|
144 |
// break for incorrect values
|
145 |
LOG.error("Not been able to set Export attribute for field" + expAttr.getName() + ".Exception:" + ex); |
146 |
|
147 |
} |
148 |
|
149 |
} |
150 |
|
151 |
} |
152 |
} |
153 |
|
154 |
// AttributeNamesTranslator nameTranslator = expAttr.getNamesTranslator();
|
155 |
this.parameters.setExportAttributes(expAttrs);
|
156 |
} |
157 |
|
158 |
@Override
|
159 |
public JComponent asJComponent() { |
160 |
return this; |
161 |
} |
162 |
|
163 |
private void initComponents() { |
164 |
// init default values
|
165 |
|
166 |
// init all UI
|
167 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
168 |
String[] columnNames = new String[]{ |
169 |
i18nManager.getTranslation("_Field_name"),
|
170 |
i18nManager.getTranslation("_Change_field_name"),
|
171 |
i18nManager.getTranslation("_Change_field_type"),
|
172 |
i18nManager.getTranslation("_Change_field_size"),
|
173 |
i18nManager.getTranslation("_Check_export")};
|
174 |
|
175 |
DataTypesManager dataManager = ToolsLocator.getDataTypesManager(); |
176 |
List<ListElement<Integer>> fieldTypes = new ArrayList<>(); |
177 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.BOOLEAN), DataTypes.BOOLEAN));
|
178 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.DATE), DataTypes.DATE));
|
179 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.INT), DataTypes.INT));
|
180 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.DOUBLE), DataTypes.DOUBLE));
|
181 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.LONG), DataTypes.LONG));
|
182 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.STRING), DataTypes.STRING));
|
183 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.GEOMETRY), DataTypes.GEOMETRY));
|
184 |
|
185 |
fieldTypes.sort(new Comparator<ListElement<Integer>>() { |
186 |
@Override
|
187 |
public int compare(ListElement<Integer> o1, ListElement<Integer> o2) { |
188 |
return o1.toString().compareTo(o2.toString());
|
189 |
} |
190 |
}); |
191 |
|
192 |
// model = new ComboModelDataType(fieldTypes, fad.getType());
|
193 |
DefaultComboBoxModel<ListElement<Integer>> modelTypes = new DefaultComboBoxModel<>(); |
194 |
for (ListElement<Integer> element : fieldTypes) { |
195 |
modelTypes.addElement(element); |
196 |
} |
197 |
|
198 |
JComboBox comboBoxFieldTypes = new JComboBox(); |
199 |
comboBoxFieldTypes.setModel(modelTypes); |
200 |
|
201 |
// Values to field the table are taken from the parameter.getExportAttributes
|
202 |
Object[][] propertyFields = new Object[this.parameters.getExportAttributes().size()][5]; |
203 |
this.layerAttributeDescriptor = new HashMap<String, String>(); |
204 |
for (int nAttr = 0; nAttr < this.parameters.getExportAttributes().size(); nAttr++) { |
205 |
//ExportAttributes.ExportAttribute exportAttr : this.parameters.getExportAttributes()) {
|
206 |
ExportAttributes.ExportAttribute exportAttr = this.parameters.getExportAttributes().get(nAttr);
|
207 |
propertyFields[nAttr][COLUMN_FIELD_NAME] = exportAttr.getName(); |
208 |
propertyFields[nAttr][COLUMN_FIELD_NEW_NAME] = exportAttr.getNewName(); |
209 |
propertyFields[nAttr][COLUMN_FIELD_TYPE] = dataManager.getTypeName(exportAttr.getDataType()); |
210 |
propertyFields[nAttr][COLUMN_FIELD_SIZE] = exportAttr.getSize(); |
211 |
propertyFields[nAttr][COLUMN_FIELD_EXPORT] = true;
|
212 |
|
213 |
this.layerAttributeDescriptor.put(
|
214 |
exportAttr.getName(), |
215 |
dataManager.getTypeName(exportAttr.getDataType()) |
216 |
); |
217 |
} |
218 |
|
219 |
DefaultTableModel model = new DefaultTableModel(propertyFields, columnNames) { |
220 |
|
221 |
private static final long serialVersionUID = 1L; |
222 |
|
223 |
@Override
|
224 |
public boolean isCellEditable(int row, int col) { |
225 |
return col != 0; |
226 |
} |
227 |
|
228 |
public Class getColumnClass(int column) { |
229 |
switch (column) {
|
230 |
case 0: |
231 |
case 1: |
232 |
case 3: |
233 |
return String.class; |
234 |
case 2: |
235 |
return JComboBox.class; |
236 |
case 4: |
237 |
return Boolean.class; |
238 |
|
239 |
} |
240 |
return getValueAt(0, column).getClass(); |
241 |
} |
242 |
}; |
243 |
|
244 |
this.tblAttributes.setModel(model);
|
245 |
|
246 |
class TypeDefaultCellEditor extends DefaultCellEditor { |
247 |
|
248 |
private final HashMap<String, String> attributes; |
249 |
|
250 |
public TypeDefaultCellEditor(JComboBox comboBox, HashMap attributes) { |
251 |
super(comboBox);
|
252 |
this.attributes = attributes;
|
253 |
} |
254 |
|
255 |
@Override
|
256 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, |
257 |
int row, int column) { |
258 |
|
259 |
JComboBox editor = (JComboBox) super.getTableCellEditorComponent(table, value, isSelected, row, column); |
260 |
Object labelField = table.getModel().getValueAt(row, 0); |
261 |
String initialType = this.attributes.get(labelField.toString()); |
262 |
|
263 |
editor.getModel().setSelectedItem(value); |
264 |
|
265 |
class ComboBoxRenderer extends JLabel implements ListCellRenderer { |
266 |
|
267 |
private final String actualValue; |
268 |
private final String initialType; |
269 |
|
270 |
public ComboBoxRenderer(String value, String initialType) { |
271 |
this.actualValue = value;
|
272 |
this.initialType = initialType;
|
273 |
} |
274 |
|
275 |
@Override
|
276 |
public Component getListCellRendererComponent(JList list, |
277 |
Object value,
|
278 |
int index,
|
279 |
boolean isSelected,
|
280 |
boolean cellHasFocus) {
|
281 |
if (value != null) { |
282 |
this.setText(value.toString());
|
283 |
} |
284 |
Font f = this.getFont(); |
285 |
if (value.toString() == initialType) {
|
286 |
this.setFont(f.deriveFont(f.getStyle() | Font.BOLD)); |
287 |
} else {
|
288 |
this.setFont(f.deriveFont(f.getStyle() & ~Font.BOLD)); |
289 |
} |
290 |
|
291 |
return this; |
292 |
} |
293 |
} |
294 |
ListCellRenderer cmbRenderer = new ComboBoxRenderer(value.toString(), initialType); |
295 |
editor.setRenderer(cmbRenderer); |
296 |
|
297 |
return editor;
|
298 |
} |
299 |
} |
300 |
DefaultCellEditor cellEditor = new TypeDefaultCellEditor(comboBoxFieldTypes, this.layerAttributeDescriptor); |
301 |
this.tblAttributes.getColumnModel().getColumn(COLUMN_FIELD_TYPE).setCellEditor(cellEditor);
|
302 |
|
303 |
this.translate();
|
304 |
} |
305 |
|
306 |
private void translate() { |
307 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
308 |
this.lblHeader.setText(i18nManager.getTranslation("_Select_attributes_to_export_and_its_name")); |
309 |
|
310 |
} |
311 |
|
312 |
// private static class Data implements EvaluatorData {
|
313 |
//
|
314 |
// private final DynObject data;
|
315 |
//
|
316 |
// public Data(DynObject data) {
|
317 |
// this.data = data;
|
318 |
// }
|
319 |
//
|
320 |
// @Override
|
321 |
// public Object getDataValue(String name) {
|
322 |
// if (this.data.getDynClass().getDynField(name) != null) {
|
323 |
// return this.data.getDynValue(name);
|
324 |
// }
|
325 |
// if ("defaultgeometry".equalsIgnoreCase(name)) {
|
326 |
// // FIXME: deberia crear una geometris de agun tipo
|
327 |
// return null;
|
328 |
// }
|
329 |
// throw new RuntimeException("Identifier '" + name + "'not defined.");
|
330 |
// }
|
331 |
//
|
332 |
// @Override
|
333 |
// public Object getContextValue(String name) {
|
334 |
// throw new UnsupportedOperationException("Not supported yet.");
|
335 |
// }
|
336 |
//
|
337 |
// @Override
|
338 |
// public Iterator getDataValues() {
|
339 |
// throw new UnsupportedOperationException("Not supported yet.");
|
340 |
// }
|
341 |
//
|
342 |
// @Override
|
343 |
// public Iterator getDataNames() {
|
344 |
// throw new UnsupportedOperationException("Not supported yet.");
|
345 |
// }
|
346 |
//
|
347 |
// @Override
|
348 |
// public boolean hasDataValue(String name) {
|
349 |
// if ("defaultgeometry".equalsIgnoreCase(name)) {
|
350 |
// return true;
|
351 |
// }
|
352 |
// return this.data.hasDynValue(name);
|
353 |
// }
|
354 |
//
|
355 |
// @Override
|
356 |
// public boolean hasContextValue(String name) {
|
357 |
// throw new UnsupportedOperationException("Not supported yet.");
|
358 |
// }
|
359 |
// }
|
360 |
protected void doTest() { |
361 |
try {
|
362 |
this.checkPanel(false); |
363 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
364 |
JOptionPane.showMessageDialog(this, |
365 |
i18nManager.getTranslation("_Expresion_correcta"),
|
366 |
i18nManager.getTranslation("_Filter"),
|
367 |
JOptionPane.INFORMATION_MESSAGE
|
368 |
); |
369 |
} catch (ExportPanelValidationException ex) {
|
370 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
371 |
JOptionPane.showMessageDialog(this, |
372 |
ex.getMessage(), |
373 |
i18nManager.getTranslation("_Warning"),
|
374 |
JOptionPane.WARNING_MESSAGE
|
375 |
); |
376 |
} |
377 |
} |
378 |
|
379 |
public void checkPanel(boolean save) throws ExportPanelValidationException { |
380 |
|
381 |
} |
382 |
} |