svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / TableInfoController.java @ 45635
History | View | Annotate | Download (15.9 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.geodb; |
7 |
|
8 |
import java.awt.Cursor; |
9 |
import java.awt.event.ActionEvent; |
10 |
import java.awt.event.ItemEvent; |
11 |
import javax.swing.DefaultComboBoxModel; |
12 |
import javax.swing.DefaultListModel; |
13 |
import javax.swing.DefaultListSelectionModel; |
14 |
import javax.swing.JButton; |
15 |
import javax.swing.JComboBox; |
16 |
import javax.swing.JLabel; |
17 |
import javax.swing.JList; |
18 |
import javax.swing.JTextField; |
19 |
import javax.swing.event.ChangeEvent; |
20 |
import javax.swing.event.ChangeListener; |
21 |
import org.apache.commons.lang3.StringUtils; |
22 |
import org.cresques.cts.IProjection; |
23 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
24 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
25 |
import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
26 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
27 |
import org.gvsig.fmap.dal.feature.FeatureType; |
28 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
29 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
30 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
31 |
import org.gvsig.fmap.dal.swing.ProjectionPickerController; |
32 |
import org.gvsig.fmap.dal.swing.dataStoreParameters.DataStoreParametersPanel; |
33 |
import org.gvsig.fmap.dal.swing.dataStoreParameters.DataStoreParametersPanelManager; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.tools.ToolsLocator; |
36 |
import org.gvsig.tools.i18n.I18nManager; |
37 |
import org.gvsig.tools.locator.LocatorException; |
38 |
import org.gvsig.tools.swing.api.JListWithCheckbox; |
39 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
40 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
41 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
42 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
43 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
44 |
|
45 |
/**
|
46 |
*
|
47 |
* @author fdiaz
|
48 |
*/
|
49 |
public class TableInfoController { |
50 |
|
51 |
private final JButton btnDeselectAllColumns; |
52 |
private final JButton btnSelectAllColumns; |
53 |
private final JList lstColumns; |
54 |
private final JTextField txtName; |
55 |
private final JComboBox cboIdField; |
56 |
private final JComboBox cboGeometryField; |
57 |
private final JTextField txtProjection; |
58 |
private final JButton btnProjection; |
59 |
private final JTextField txtFilter; |
60 |
private final JButton btnFilter; |
61 |
private final JButton btnFilterBookmarks; |
62 |
private final JButton btnFilterHistory; |
63 |
private final JButton btnAdvancedProperties; |
64 |
private final JLabel lblReadOnlyNotification; |
65 |
private JListWithCheckbox lwcColumns;
|
66 |
private ProjectionPickerController pickerProjection;
|
67 |
private ExpressionPickerController pickerFilter;
|
68 |
private TableInfo tableInfo;
|
69 |
|
70 |
public TableInfoController(
|
71 |
JButton btnDeselectAllColumns,
|
72 |
JButton btnSelectAllColumns,
|
73 |
JList lstColumns,
|
74 |
JTextField txtName,
|
75 |
JComboBox cboIdField,
|
76 |
JComboBox cboGeometryField,
|
77 |
JTextField txtProjection,
|
78 |
JButton btnProjection,
|
79 |
JTextField txtFilter,
|
80 |
JButton btnFilter,
|
81 |
JButton btnFilterBookmarks,
|
82 |
JButton btnFilterHistory,
|
83 |
JButton btnAdvancedProperties,
|
84 |
JLabel lblReadOnlyNotification
|
85 |
) { |
86 |
|
87 |
this.btnDeselectAllColumns = btnDeselectAllColumns;
|
88 |
this.btnSelectAllColumns = btnSelectAllColumns;
|
89 |
this.lstColumns = lstColumns;
|
90 |
this.txtName = txtName;
|
91 |
this.cboIdField = cboIdField;
|
92 |
this.cboGeometryField = cboGeometryField;
|
93 |
this.txtProjection = txtProjection;
|
94 |
this.btnProjection = btnProjection;
|
95 |
this.txtFilter = txtFilter;
|
96 |
this.btnFilter = btnFilter;
|
97 |
this.btnFilterBookmarks = btnFilterBookmarks;
|
98 |
this.btnFilterHistory = btnFilterHistory;
|
99 |
this.btnAdvancedProperties = btnAdvancedProperties;
|
100 |
this.lblReadOnlyNotification = lblReadOnlyNotification;
|
101 |
|
102 |
initComponents(); |
103 |
} |
104 |
|
105 |
private void initComponents() { |
106 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
107 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
108 |
ExpressionEvaluatorSwingManager expressionSwingManager = ExpressionEvaluatorSwingLocator.getManager(); |
109 |
|
110 |
toolsSwingManager.translate(this.btnDeselectAllColumns);
|
111 |
toolsSwingManager.translate(this.btnSelectAllColumns);
|
112 |
toolsSwingManager.translate(this.btnAdvancedProperties);
|
113 |
|
114 |
toolsSwingManager.addClearButton(this.txtName);
|
115 |
toolsSwingManager.setDefaultPopupMenu(this.txtName);
|
116 |
|
117 |
this.lwcColumns = toolsSwingManager.createJListWithCheckbox(
|
118 |
this.lstColumns
|
119 |
); |
120 |
|
121 |
this.pickerProjection = dataSwingManager.createProjectionPickerController(
|
122 |
this.txtProjection,
|
123 |
this.btnProjection
|
124 |
); |
125 |
this.pickerFilter = expressionSwingManager.createExpressionPickerController(
|
126 |
this.txtFilter,
|
127 |
this.btnFilter,
|
128 |
this.btnFilterBookmarks,
|
129 |
this.btnFilterHistory
|
130 |
); |
131 |
this.pickerProjection.addChangeListener(new ChangeListener() { |
132 |
@Override
|
133 |
public void stateChanged(ChangeEvent e) { |
134 |
doChangeProjection(); |
135 |
} |
136 |
}); |
137 |
|
138 |
this.cboIdField.addItemListener((ItemEvent e) -> { |
139 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
140 |
doChangeIdField(); |
141 |
} |
142 |
}); |
143 |
this.cboGeometryField.addItemListener((ItemEvent e) -> { |
144 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
145 |
doChangeGeometryField(); |
146 |
updateLabel(); |
147 |
} |
148 |
}); |
149 |
this.btnDeselectAllColumns.addActionListener((ActionEvent e) -> { |
150 |
doDeselectAllColumns(); |
151 |
}); |
152 |
this.btnSelectAllColumns.addActionListener((ActionEvent e) -> { |
153 |
doSelectAllColumns(); |
154 |
}); |
155 |
this.btnAdvancedProperties.addActionListener((ActionEvent e) -> { |
156 |
doShowAdvancedProperties(); |
157 |
}); |
158 |
|
159 |
this.btnSelectAllColumns.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
160 |
this.btnDeselectAllColumns.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
161 |
this.btnProjection.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
162 |
this.btnFilter.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
163 |
this.btnFilterBookmarks.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
164 |
this.btnFilterHistory.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
165 |
this.btnAdvancedProperties.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
166 |
|
167 |
} |
168 |
|
169 |
private void doChangeIdField() { |
170 |
final TableInfo info = this.getCurrentTableInfo(); |
171 |
if (info == null) { |
172 |
return;
|
173 |
} |
174 |
info.setIdFieldSelected(this.cboIdField.getSelectedIndex());
|
175 |
updateLabel(); |
176 |
} |
177 |
|
178 |
private void doChangeProjection() { |
179 |
final TableInfo info = this.getCurrentTableInfo(); |
180 |
if (info == null) { |
181 |
return;
|
182 |
} |
183 |
info.setProjection(this.pickerProjection.get());
|
184 |
updateLabel(); |
185 |
} |
186 |
|
187 |
private void doChangeGeometryField() { |
188 |
TableInfo info = this.getCurrentTableInfo();
|
189 |
if (info == null) { |
190 |
return;
|
191 |
} |
192 |
String attrName = (String) this.cboGeometryField.getSelectedItem(); |
193 |
|
194 |
info.setGeomFieldSelected(this.cboGeometryField.getSelectedIndex());
|
195 |
|
196 |
if (StringUtils.isBlank(attrName)) {
|
197 |
this.pickerProjection.set(null); |
198 |
return;
|
199 |
} |
200 |
|
201 |
FeatureAttributeDescriptor attr = info.getAttributeDescriptor(attrName); |
202 |
IProjection srs = attr.getSRS(); |
203 |
if (srs == null) { |
204 |
return;
|
205 |
} |
206 |
this.pickerProjection.set(srs);
|
207 |
tableInfo.setProjection(this.pickerProjection.get());
|
208 |
} |
209 |
|
210 |
private void doDeselectAllColumns() { |
211 |
TableInfo info = this.getCurrentTableInfo();
|
212 |
if (info == null) { |
213 |
return;
|
214 |
} |
215 |
info.getColumnChecksModel().clearSelection(); |
216 |
} |
217 |
|
218 |
private void doSelectAllColumns() { |
219 |
TableInfo info = this.getCurrentTableInfo();
|
220 |
if (info == null) { |
221 |
return;
|
222 |
} |
223 |
info.getColumnChecksModel().setSelectionInterval(0, info.getAttributeDescriptors().size());
|
224 |
} |
225 |
|
226 |
private void doShowAdvancedProperties() { |
227 |
final TableInfo info = this.getCurrentTableInfo(); |
228 |
if (info == null) { |
229 |
return;
|
230 |
} |
231 |
this.fetch(info);
|
232 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
233 |
WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
234 |
DataStoreParametersPanelManager paramsPanelManager = DALSwingLocator.getDataStoreParametersPanelManager(); |
235 |
|
236 |
final DataStoreParametersPanel panel = paramsPanelManager.createDataStoreParametersPanel(info.getParameters());
|
237 |
final Dialog dialog = winmgr.createDialog( |
238 |
panel.asJComponent(), |
239 |
i18n.getTranslation("_Connection_parameters"),
|
240 |
null,
|
241 |
WindowManager_v2.BUTTONS_OK_CANCEL |
242 |
); |
243 |
dialog.addActionListener((ActionEvent e) -> {
|
244 |
if (dialog.getAction() == WindowManager_v2.BUTTON_OK) {
|
245 |
JDBCStoreParameters parameters = info.getParameters(); |
246 |
panel.fetchParameters(parameters); |
247 |
info.fetch(parameters); |
248 |
put(info); |
249 |
} |
250 |
}); |
251 |
dialog.show(WindowManager.MODE.DIALOG); |
252 |
} |
253 |
|
254 |
public void put(TableInfo tableInfo) { |
255 |
this.tableInfo = tableInfo;
|
256 |
|
257 |
if(tableInfo == null) { |
258 |
this.lwcColumns.setModel(new DefaultListModel()); |
259 |
this.lwcColumns.setCheckedModel(new DefaultListSelectionModel()); |
260 |
this.txtName.setText(""); |
261 |
this.cboGeometryField.setModel(new DefaultComboBoxModel()); |
262 |
this.cboIdField.setModel(new DefaultComboBoxModel()); |
263 |
this.pickerProjection.set(null); |
264 |
this.pickerFilter.set(null); |
265 |
if(this.lblReadOnlyNotification != null){ |
266 |
this.lblReadOnlyNotification.setText(""); |
267 |
} |
268 |
return;
|
269 |
} |
270 |
|
271 |
this.lwcColumns.setModel(tableInfo.getColumnsListModel());
|
272 |
this.lwcColumns.setCheckedModel(tableInfo.getColumnChecksModel());
|
273 |
|
274 |
this.txtName.setText(tableInfo.getDocumentName());
|
275 |
|
276 |
this.cboGeometryField.setModel(tableInfo.getGeomFieldComboModel());
|
277 |
this.cboGeometryField.setSelectedIndex(tableInfo.getGeomFieldSelected());
|
278 |
|
279 |
this.cboIdField.setModel(tableInfo.getIdFieldComboModel());
|
280 |
this.cboIdField.setSelectedIndex(tableInfo.getIdFieldSelected());
|
281 |
|
282 |
this.pickerProjection.set(tableInfo.getProjection());
|
283 |
this.pickerFilter.set(tableInfo.getFilter());
|
284 |
|
285 |
updateLabel(); |
286 |
} |
287 |
|
288 |
private void updateLabel() throws LocatorException { |
289 |
if (this.lblReadOnlyNotification != null) { |
290 |
StringBuilder builder = new StringBuilder(); |
291 |
builder.append("<html>");
|
292 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
293 |
builder.append("<font color='#00008B'>");
|
294 |
if (tableInfo.isView()) {
|
295 |
builder.append(i18n.getTranslation("_View"));
|
296 |
} else {
|
297 |
builder.append(i18n.getTranslation("_Table"));
|
298 |
} |
299 |
boolean readOnly = tableInfo.isReadOnly();
|
300 |
if (readOnly) {
|
301 |
builder.append(" - <b>");
|
302 |
builder.append(i18n.getTranslation("_Read_only"));
|
303 |
builder.append("</b>");
|
304 |
} |
305 |
if (StringUtils.isBlank(this.tableInfo.getFieldId())) { |
306 |
builder.append(" - ");
|
307 |
builder.append(i18n.getTranslation("_PrimaryKey_field_recommended"));
|
308 |
} |
309 |
if (this.tableInfo.requireGeometry()) { |
310 |
FeatureType featType = this.tableInfo.getFeatureType();
|
311 |
if (StringUtils.isBlank(this.tableInfo.getGeomField())) { |
312 |
builder.append(" - <b>");
|
313 |
builder.append(i18n.getTranslation("_Geometry_field_required"));
|
314 |
builder.append("</b>");
|
315 |
} |
316 |
JDBCStoreParameters params = this.tableInfo.getParameters();
|
317 |
if(params.getGeometryType() == Geometry.TYPES.NULL || params.getGeometryType() == Geometry.TYPES.UNKNOWN){
|
318 |
builder.append(" - <b>");
|
319 |
builder.append(i18n.getTranslation("_GeometryType_required"));
|
320 |
builder.append("</b>");
|
321 |
} |
322 |
if(params.getGeometrySubtype() == Geometry.SUBTYPES.UNKNOWN){
|
323 |
builder.append(" - <b>");
|
324 |
builder.append(i18n.getTranslation("_GeometrySubType_required"));
|
325 |
builder.append("</b>");
|
326 |
} |
327 |
if(this.tableInfo.getProjection()==null){ |
328 |
builder.append(" - ");
|
329 |
builder.append(i18n.getTranslation("_Projection_recommended"));
|
330 |
} |
331 |
} else { //Not require geometry |
332 |
FeatureType featType = this.tableInfo.getFeatureType();
|
333 |
if (StringUtils.isNotBlank(this.tableInfo.getGeomField())) { |
334 |
JDBCStoreParameters params = this.tableInfo.getParameters();
|
335 |
if (params.getGeometryType() == Geometry.TYPES.NULL || params.getGeometryType() == Geometry.TYPES.UNKNOWN) {
|
336 |
builder.append(" - ");
|
337 |
builder.append(i18n.getTranslation("_GeometryType_recommended"));
|
338 |
} |
339 |
if (params.getGeometrySubtype() == Geometry.SUBTYPES.UNKNOWN) {
|
340 |
builder.append(" - ");
|
341 |
builder.append(i18n.getTranslation("_GeometrySubType_recommended"));
|
342 |
} |
343 |
if (StringUtils.isNotBlank(this.tableInfo.getGeomField()) && this.tableInfo.getProjection() == null) { |
344 |
builder.append(" - ");
|
345 |
builder.append(i18n.getTranslation("_Projection_recommended"));
|
346 |
} |
347 |
} |
348 |
} |
349 |
builder.append("</font>");
|
350 |
builder.append("</html>");
|
351 |
this.lblReadOnlyNotification.setText(builder.toString());
|
352 |
} |
353 |
} |
354 |
|
355 |
public void fetch(TableInfo tableInfo) { |
356 |
tableInfo.setIdFieldSelected(this.cboIdField.getSelectedIndex());
|
357 |
tableInfo.setGeomFieldSelected(this.cboGeometryField.getSelectedIndex());
|
358 |
tableInfo.setFilter(this.pickerFilter.get());
|
359 |
tableInfo.setProjection(this.pickerProjection.get());
|
360 |
tableInfo.setDocumentName(this.txtName.getText());
|
361 |
} |
362 |
|
363 |
private TableInfo getCurrentTableInfo() {
|
364 |
return this.tableInfo; |
365 |
} |
366 |
|
367 |
public void setEditable(boolean enable) { |
368 |
this.lstColumns.setEnabled(enable);
|
369 |
this.txtName.setEditable(enable);
|
370 |
this.cboGeometryField.setEnabled(enable);
|
371 |
this.cboIdField.setEnabled(enable);
|
372 |
this.pickerProjection.setEditable(enable);
|
373 |
this.pickerFilter.setEditable(enable);
|
374 |
this.btnDeselectAllColumns.setEnabled(enable);
|
375 |
this.btnSelectAllColumns.setEnabled(enable);
|
376 |
this.btnAdvancedProperties.setEnabled(enable);
|
377 |
} |
378 |
|
379 |
public void setEnabled(boolean enable) { |
380 |
this.lstColumns.setEnabled(enable);
|
381 |
this.txtName.setEnabled(enable);
|
382 |
this.cboGeometryField.setEnabled(enable);
|
383 |
this.cboIdField.setEnabled(enable);
|
384 |
this.pickerProjection.setEnabled(enable);
|
385 |
this.pickerFilter.setEnabled(enable);
|
386 |
this.btnDeselectAllColumns.setEnabled(enable);
|
387 |
this.btnSelectAllColumns.setEnabled(enable);
|
388 |
this.btnAdvancedProperties.setEnabled(enable);
|
389 |
} |
390 |
|
391 |
public String getId(){ |
392 |
if(tableInfo == null) { |
393 |
return null; |
394 |
} |
395 |
return this.getCurrentTableInfo().getId(); |
396 |
} |
397 |
|
398 |
public void clear() { |
399 |
this.put(null); |
400 |
} |
401 |
|
402 |
|
403 |
} |