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 @ 44395
History | View | Annotate | Download (12 KB)
1 |
package org.gvsig.export.swing.impl.panels; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.awt.Font; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Comparator; |
7 |
import java.util.HashMap; |
8 |
import java.util.List; |
9 |
import javax.swing.DefaultCellEditor; |
10 |
import javax.swing.DefaultComboBoxModel; |
11 |
import javax.swing.JComboBox; |
12 |
import javax.swing.JComponent; |
13 |
import javax.swing.JLabel; |
14 |
import javax.swing.JList; |
15 |
import javax.swing.JOptionPane; |
16 |
import javax.swing.JTable; |
17 |
import javax.swing.ListCellRenderer; |
18 |
import javax.swing.table.DefaultTableModel; |
19 |
import javax.swing.table.TableModel; |
20 |
import org.apache.commons.lang.StringUtils; |
21 |
import org.gvsig.export.ExportAttributes; |
22 |
import org.gvsig.export.ExportParameters; |
23 |
import org.gvsig.export.swing.JExportProcessPanel; |
24 |
import org.gvsig.export.swing.spi.ExportPanel; |
25 |
import org.gvsig.export.swing.spi.ExportPanelValidationException; |
26 |
import org.gvsig.export.swing.spi.ExportPanelsManager; |
27 |
import org.gvsig.fmap.dal.DataTypes; |
28 |
import org.gvsig.tools.ToolsLocator; |
29 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
30 |
import org.gvsig.tools.i18n.I18nManager; |
31 |
import org.gvsig.tools.swing.api.ListElement; |
32 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
33 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
/**
|
38 |
*
|
39 |
* @author jjdelcerro
|
40 |
*/
|
41 |
public class SelectAttributesPanel |
42 |
extends SelectAttributesPanelView
|
43 |
implements ExportPanel {
|
44 |
|
45 |
private static final Logger LOG = LoggerFactory.getLogger(SelectAttributesPanel.class); |
46 |
|
47 |
public static final int COLUMN_FIELD_NAME = 0; |
48 |
public static final int COLUMN_FIELD_NEW_NAME = 1; |
49 |
public static final int COLUMN_FIELD_TYPE = 2; |
50 |
public static final int COLUMN_FIELD_SIZE = 3; |
51 |
public static final int COLUMN_FIELD_EXPORT = 4; |
52 |
|
53 |
private final ExportParameters parameters; |
54 |
private final JExportProcessPanel processPanel; |
55 |
private HashMap<String, String> layerAttributeDescriptor; |
56 |
|
57 |
public SelectAttributesPanel(
|
58 |
JExportProcessPanel processPanel, |
59 |
ExportParameters parameters |
60 |
) { |
61 |
this.processPanel = processPanel;
|
62 |
this.parameters = parameters;
|
63 |
this.parameters.getExportAttributes().setActive(true); |
64 |
this.initComponents();
|
65 |
} |
66 |
|
67 |
@Override
|
68 |
public String getIdPanel() { |
69 |
return ExportPanelsManager.PANEL_ATTRIBUTES_SELECTION;
|
70 |
} |
71 |
|
72 |
@Override
|
73 |
public String getTitlePanel() { |
74 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
75 |
return i18n.getTranslation("_Attribute_Selection"); |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public boolean validatePanel() throws ExportPanelValidationException { |
80 |
ExportAttributes expAttrs; |
81 |
expAttrs = this.parameters.getExportAttributes();
|
82 |
DataTypesManager dataManager = ToolsLocator.getDataTypesManager(); |
83 |
TableModel model = this.tblAttributes.getModel(); |
84 |
for (int i = 0; i < model.getRowCount(); i++) { |
85 |
String object = (String) model.getValueAt(i, COLUMN_FIELD_NAME); |
86 |
for (ExportAttributes.ExportAttribute expAttr : expAttrs) { // assign valuen from table to ExportAttribute |
87 |
if (object == null ? expAttr.getName() == null : object.equals(expAttr.getName())) { |
88 |
try {
|
89 |
expAttr.setNewName((String) model.getValueAt(i, COLUMN_FIELD_NEW_NAME));
|
90 |
Object value = model.getValueAt(i, COLUMN_FIELD_TYPE);
|
91 |
int dataType = dataManager.getType(value.toString());
|
92 |
expAttr.setNewType(dataType); |
93 |
int sizeValue;
|
94 |
try {
|
95 |
sizeValue = Integer.parseInt(model.getValueAt(i, COLUMN_FIELD_SIZE).toString());
|
96 |
} catch (NumberFormatException ex) { |
97 |
LOG.error("Not been able to set size value" + expAttr.getName() + ".Exception:" + ex); |
98 |
sizeValue = expAttr.getSize(); |
99 |
} |
100 |
expAttr.setSize(sizeValue); |
101 |
expAttr.setExported((boolean) model.getValueAt(i, COLUMN_FIELD_EXPORT));
|
102 |
} catch (Exception ex) { |
103 |
// TODO
|
104 |
// break for incorrect values
|
105 |
LOG.error("Not been able to set Export attribute for field" + expAttr.getName() + ".Exception:" + ex); |
106 |
|
107 |
} |
108 |
|
109 |
} |
110 |
|
111 |
} |
112 |
} |
113 |
|
114 |
if (!expAttrs.isAttributeNamesValid()) {
|
115 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
116 |
int as = JOptionPane.showConfirmDialog( |
117 |
this,
|
118 |
i18nManager.getTranslation("_Not_valid_attribute_names_Do_you_want_a_suggest_names?"),
|
119 |
i18nManager.getTranslation("_Not_valid_attriute_names"),
|
120 |
JOptionPane.YES_NO_OPTION
|
121 |
); |
122 |
if (as == JOptionPane.YES_OPTION) { |
123 |
expAttrs.fixAttributeNames(); |
124 |
this.fillTableWithAttributes();
|
125 |
} else if (as == JOptionPane.NO_OPTION) { |
126 |
|
127 |
} |
128 |
return false; |
129 |
} |
130 |
return true; |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public void enterPanel() { |
135 |
this.fillTableWithAttributes();
|
136 |
} |
137 |
|
138 |
@Override
|
139 |
public void previousPanel() { |
140 |
this.fillTableWithAttributes();
|
141 |
} |
142 |
|
143 |
@Override
|
144 |
public void nextPanel() { |
145 |
//get params are in the validate method
|
146 |
} |
147 |
|
148 |
@Override
|
149 |
public JComponent asJComponent() { |
150 |
return this; |
151 |
} |
152 |
|
153 |
private void fillTableWithAttributes() { |
154 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
155 |
String[] columnNames = new String[]{ |
156 |
i18nManager.getTranslation("_Field_name"),
|
157 |
i18nManager.getTranslation("_Change_field_name"),
|
158 |
i18nManager.getTranslation("_Change_field_type"),
|
159 |
i18nManager.getTranslation("_Change_field_size"),
|
160 |
i18nManager.getTranslation("_Check_export")};
|
161 |
|
162 |
DataTypesManager dataManager = ToolsLocator.getDataTypesManager(); |
163 |
List<ListElement<Integer>> fieldTypes = new ArrayList<>(); |
164 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.BOOLEAN), DataTypes.BOOLEAN));
|
165 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.DATE), DataTypes.DATE));
|
166 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.INT), DataTypes.INT));
|
167 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.DOUBLE), DataTypes.DOUBLE));
|
168 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.LONG), DataTypes.LONG));
|
169 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.STRING), DataTypes.STRING));
|
170 |
fieldTypes.add(new ListElement<>(dataManager.getTypeName(DataTypes.GEOMETRY), DataTypes.GEOMETRY));
|
171 |
|
172 |
fieldTypes.sort(new Comparator<ListElement<Integer>>() { |
173 |
@Override
|
174 |
public int compare(ListElement<Integer> o1, ListElement<Integer> o2) { |
175 |
return o1.toString().compareTo(o2.toString());
|
176 |
} |
177 |
}); |
178 |
|
179 |
// model = new ComboModelDataType(fieldTypes, fad.getType());
|
180 |
DefaultComboBoxModel<ListElement<Integer>> modelTypes = new DefaultComboBoxModel<>(); |
181 |
for (ListElement<Integer> element : fieldTypes) { |
182 |
modelTypes.addElement(element); |
183 |
} |
184 |
|
185 |
JComboBox comboBoxFieldTypes = new JComboBox(); |
186 |
comboBoxFieldTypes.setModel(modelTypes); |
187 |
|
188 |
// Values to field the table are taken from the parameter.getExportAttributes
|
189 |
Object[][] propertyFields = new Object[this.parameters.getExportAttributes().size()][5]; |
190 |
this.layerAttributeDescriptor = new HashMap<>(); |
191 |
for (int nAttr = 0; nAttr < this.parameters.getExportAttributes().size(); nAttr++) { |
192 |
//ExportAttributes.ExportAttribute exportAttr : this.parameters.getExportAttributes()) {
|
193 |
ExportAttributes.ExportAttribute exportAttr = this.parameters.getExportAttributes().get(nAttr);
|
194 |
propertyFields[nAttr][COLUMN_FIELD_NAME] = exportAttr.getName(); |
195 |
propertyFields[nAttr][COLUMN_FIELD_NEW_NAME] = exportAttr.getNewName(); |
196 |
propertyFields[nAttr][COLUMN_FIELD_TYPE] = dataManager.getTypeName(exportAttr.getDataType()); |
197 |
propertyFields[nAttr][COLUMN_FIELD_SIZE] = exportAttr.getSize(); |
198 |
propertyFields[nAttr][COLUMN_FIELD_EXPORT] = exportAttr.isExported(); |
199 |
|
200 |
this.layerAttributeDescriptor.put(
|
201 |
exportAttr.getName(), |
202 |
dataManager.getTypeName(exportAttr.getDataType()) |
203 |
); |
204 |
} |
205 |
|
206 |
DefaultTableModel model = new DefaultTableModel(propertyFields, columnNames) { |
207 |
|
208 |
private static final long serialVersionUID = 1L; |
209 |
|
210 |
@Override
|
211 |
public boolean isCellEditable(int row, int col) { |
212 |
return col != 0; |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public Class getColumnClass(int column) { |
217 |
switch (column) {
|
218 |
case 0: |
219 |
case 1: |
220 |
case 3: |
221 |
return String.class; |
222 |
case 2: |
223 |
return JComboBox.class; |
224 |
case 4: |
225 |
return Boolean.class; |
226 |
|
227 |
} |
228 |
return getValueAt(0, column).getClass(); |
229 |
} |
230 |
}; |
231 |
|
232 |
this.tblAttributes.setModel(model);
|
233 |
|
234 |
class TypeDefaultCellEditor extends DefaultCellEditor { |
235 |
|
236 |
private final HashMap<String, String> attributes; |
237 |
|
238 |
public TypeDefaultCellEditor(JComboBox comboBox, HashMap attributes) { |
239 |
super(comboBox);
|
240 |
this.attributes = attributes;
|
241 |
} |
242 |
|
243 |
@Override
|
244 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, |
245 |
int row, int column) { |
246 |
|
247 |
JComboBox editor = (JComboBox) super.getTableCellEditorComponent(table, value, isSelected, row, column); |
248 |
Object labelField = table.getModel().getValueAt(row, 0); |
249 |
String initialType = this.attributes.get(labelField.toString()); |
250 |
|
251 |
editor.getModel().setSelectedItem(value); |
252 |
|
253 |
class ComboBoxRenderer extends JLabel implements ListCellRenderer { |
254 |
|
255 |
private final String actualValue; |
256 |
private final String initialType; |
257 |
|
258 |
public ComboBoxRenderer(String value, String initialType) { |
259 |
this.actualValue = value;
|
260 |
this.initialType = initialType;
|
261 |
} |
262 |
|
263 |
@Override
|
264 |
public Component getListCellRendererComponent(JList list, |
265 |
Object value,
|
266 |
int index,
|
267 |
boolean isSelected,
|
268 |
boolean cellHasFocus) {
|
269 |
if (value != null) { |
270 |
this.setText(value.toString());
|
271 |
} |
272 |
Font f = this.getFont(); |
273 |
if (StringUtils.equals(value.toString(), initialType)) {
|
274 |
this.setFont(f.deriveFont(f.getStyle() | Font.BOLD)); |
275 |
} else {
|
276 |
this.setFont(f.deriveFont(f.getStyle() & ~Font.BOLD)); |
277 |
} |
278 |
|
279 |
return this; |
280 |
} |
281 |
} |
282 |
ListCellRenderer cmbRenderer = new ComboBoxRenderer(value.toString(), initialType); |
283 |
editor.setRenderer(cmbRenderer); |
284 |
|
285 |
return editor;
|
286 |
} |
287 |
} |
288 |
DefaultCellEditor cellEditor = new TypeDefaultCellEditor(comboBoxFieldTypes, this.layerAttributeDescriptor); |
289 |
this.tblAttributes.getColumnModel().getColumn(COLUMN_FIELD_TYPE).setCellEditor(cellEditor);
|
290 |
|
291 |
this.translate();
|
292 |
} |
293 |
|
294 |
private void initComponents() { |
295 |
// init default values
|
296 |
|
297 |
// init all UI
|
298 |
this.fillTableWithAttributes();
|
299 |
|
300 |
} |
301 |
|
302 |
private void translate() { |
303 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
304 |
manager.translate(this.lblHeader);
|
305 |
} |
306 |
|
307 |
} |