gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.swing / org.gvsig.vcsgis.swing.impl / src / main / java / org / gvsig / vcsgis / swing / impl / export / VCSGisJExportImpl.java @ 3319
History | View | Annotate | Download (18.5 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
18 |
*
|
19 |
* For any additional information, do not hesitate to contact us
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
21 |
*/
|
22 |
package org.gvsig.vcsgis.swing.impl.export; |
23 |
|
24 |
import java.awt.Cursor; |
25 |
import java.awt.Dimension; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.sql.Timestamp; |
28 |
import java.util.Date; |
29 |
import javax.swing.ComboBoxModel; |
30 |
import javax.swing.JComponent; |
31 |
import javax.swing.SwingUtilities; |
32 |
import javax.swing.event.ChangeEvent; |
33 |
import static jdk.nashorn.internal.codegen.OptimisticTypesPersistence.store; |
34 |
import org.apache.commons.lang3.StringUtils; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import org.gvsig.fmap.dal.feature.FeatureType; |
37 |
import org.gvsig.tools.ToolsLocator; |
38 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
39 |
import org.gvsig.tools.dispose.DisposeUtils; |
40 |
import org.gvsig.tools.i18n.I18nManager; |
41 |
import org.gvsig.tools.swing.api.Component; |
42 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
43 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
44 |
import org.gvsig.tools.swing.api.pickercontroller.DatePickerController; |
45 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
46 |
import org.gvsig.tools.swing.api.task.TaskStatusController; |
47 |
import org.gvsig.tools.swing.api.task.TaskStatusSwingManager; |
48 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
49 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
50 |
import org.gvsig.tools.util.LabeledValue; |
51 |
import org.gvsig.vcsgis.lib.VCSGisEntity; |
52 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CHECKOUT; |
53 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK; |
54 |
import org.gvsig.vcsgis.lib.VCSGisRevision; |
55 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
56 |
import org.gvsig.vcsgis.swing.VCSGisJExport; |
57 |
import org.gvsig.vcsgis.swing.VCSGisSwingLocator; |
58 |
import org.gvsig.vcsgis.swing.VCSGisSwingManager; |
59 |
import org.gvsig.vcsgis.swing.VCSGisSwingServices; |
60 |
import org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController; |
61 |
import static org.gvsig.vcsgis.swing.impl.VCSGisEntitySelectorController.REMOTE_ENTITIES; |
62 |
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.notInSwingThreadInvokeLater; |
63 |
import org.gvsig.vcsgis.swing.impl.revisions.VCSGisRevisionPickerControllerImpl; |
64 |
import org.slf4j.LoggerFactory; |
65 |
|
66 |
/**
|
67 |
*
|
68 |
* @author gvSIG Team
|
69 |
*/
|
70 |
public class VCSGisJExportImpl extends VCSGisJExportView |
71 |
implements Component, VCSGisJExport { |
72 |
|
73 |
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisJExportImpl.class); |
74 |
|
75 |
private Dialog dialog; |
76 |
|
77 |
private PickerController<VCSGisWorkspace> workspacePicker;
|
78 |
private DatePickerController efectivePicker;
|
79 |
// private FilteredListController filteredList;
|
80 |
private VCSGisEntitySelectorController entitySelector;
|
81 |
private VCSGisRevisionPickerControllerImpl revisionPicker;
|
82 |
private boolean processing; |
83 |
private TaskStatusController taskStatusController;
|
84 |
|
85 |
public VCSGisJExportImpl() {
|
86 |
translate(); |
87 |
processing = false;
|
88 |
initComponents(); |
89 |
} |
90 |
|
91 |
private void initComponents() { |
92 |
//TODO: remove next line in next build (>3208)
|
93 |
this.btnEffectiveDate.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
94 |
|
95 |
TaskStatusSwingManager taskStatusManager = ToolsSwingLocator.getTaskStatusSwingManager(); |
96 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
97 |
VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager(); |
98 |
VCSGisSwingServices services = swingManager.getDefaultServices(); |
99 |
|
100 |
this.workspacePicker = swingManager.createWorkspacePickerController(
|
101 |
cboWorkspaces, |
102 |
btnInitWorkspace |
103 |
); |
104 |
|
105 |
this.workspacePicker.addChangeListener((ChangeEvent e) -> { |
106 |
doChangeWorkspace(); |
107 |
doUpdateEnableComponents(); |
108 |
}); |
109 |
|
110 |
this.taskStatusController = taskStatusManager.createTaskStatusController(
|
111 |
this.lblStatusTitle,
|
112 |
this.lblStatusMessages,
|
113 |
this.pbStatus
|
114 |
); |
115 |
this.taskStatusController.setShowCancelButton(false); |
116 |
this.taskStatusController.setShowRemoveTaskButton(false); |
117 |
this.taskStatusController.bind(ToolsLocator.getTaskStatusManager());
|
118 |
SwingUtilities.invokeLater(() -> {
|
119 |
this.taskStatusController.setVisible(false); |
120 |
}); |
121 |
|
122 |
this.entitySelector = VCSGisEntitySelectorController.create(lstTables, txtFilter, btnTable);
|
123 |
this.entitySelector.setFilter(REMOTE_ENTITIES);
|
124 |
this.entitySelector.setViewFilter(REMOTE_ENTITIES);
|
125 |
this.entitySelector.setChecksEnabled(false); |
126 |
this.entitySelector.addActionListener((ActionEvent e) -> { |
127 |
doChangeTable(); |
128 |
doUpdateEnableComponents(); |
129 |
}); |
130 |
this.entitySelector.addChangeListener((ChangeEvent e) -> { |
131 |
doUpdateEnableComponents(); |
132 |
}); |
133 |
|
134 |
this.efectivePicker = toolsSwingManager.createDatePickerController(txtEffectiveDate, btnEffectiveDate);
|
135 |
|
136 |
this.revisionPicker = new VCSGisRevisionPickerControllerImpl(txtRevision, btnRevision); |
137 |
|
138 |
ComboBoxModel<LabeledValue> viewDocumentsModel = services.getViewDocumentsComboBoxModel();
|
139 |
cboView.setModel(viewDocumentsModel); |
140 |
cboView.addActionListener((ActionEvent e) -> {
|
141 |
doUpdateEnableComponents(); |
142 |
}); |
143 |
|
144 |
this.rdbDontAddToProject.addActionListener((ActionEvent e) -> { |
145 |
doUpdateEnableComponents(); |
146 |
}); |
147 |
|
148 |
this.rdbAddLayerToView.addActionListener((ActionEvent e) -> { |
149 |
doUpdateEnableComponents(); |
150 |
}); |
151 |
|
152 |
this.rdbAddTableToProject.addActionListener((ActionEvent e) -> { |
153 |
doUpdateEnableComponents(); |
154 |
}); |
155 |
|
156 |
this.setPreferredSize(new Dimension(520, 400)); |
157 |
doUpdateEnableComponents(); |
158 |
|
159 |
} |
160 |
|
161 |
@Override
|
162 |
public JComponent asJComponent() { |
163 |
return this; |
164 |
} |
165 |
|
166 |
@Override
|
167 |
public void setDialog(Dialog dialog) { |
168 |
this.dialog = dialog;
|
169 |
this.doUpdateEnableComponents();
|
170 |
} |
171 |
|
172 |
private void doChangeWorkspace() { |
173 |
VCSGisWorkspace workspace = workspacePicker.get(); |
174 |
if (workspace == null) { |
175 |
return;
|
176 |
} |
177 |
this.entitySelector.setWorkspace(workspace);
|
178 |
this.revisionPicker.setWorkspace(workspace);
|
179 |
|
180 |
Thread task = new Thread( |
181 |
() -> { |
182 |
try {
|
183 |
LOGGER.info("Entro en el thread");
|
184 |
processing = true;
|
185 |
doUpdateEnableComponents(); |
186 |
workspace.getRepositoryEntities(); |
187 |
}catch (Exception ex){ |
188 |
LOGGER.warn("Can't retrieve repository entities", ex);
|
189 |
}finally{
|
190 |
LOGGER.info("Salgo del thread");
|
191 |
processing = false;
|
192 |
doUpdateEnableComponents(); |
193 |
} |
194 |
}, |
195 |
"VCSGisExportGetRepositoryEntities"
|
196 |
); |
197 |
this.processing = true; |
198 |
LOGGER.info("Creando thread");
|
199 |
doUpdateEnableComponents(); |
200 |
task.start(); |
201 |
} |
202 |
|
203 |
private void doChangeTable() { |
204 |
VCSGisEntity selectedTable = this.entitySelector.getSelectedEntity();
|
205 |
Timestamp now = DataTypeUtils.toTimestamp(new Date()); |
206 |
this.revisionPicker.setEntity(selectedTable);
|
207 |
this.txtTableName.setText(selectedTable.getEntityName() + "_" + now.toString()); |
208 |
|
209 |
} |
210 |
|
211 |
@Override
|
212 |
public VCSGisWorkspace getWorkspace() {
|
213 |
return this.workspacePicker.get(); |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public VCSGisEntity getTable() {
|
218 |
return this.entitySelector.getSelectedEntity(); |
219 |
} |
220 |
|
221 |
private void doUpdateEnableComponents() { |
222 |
if (notInSwingThreadInvokeLater(this::doUpdateEnableComponents)) { |
223 |
return;
|
224 |
} |
225 |
boolean isProcessing = this.processing || this.entitySelector.isProcessing(); |
226 |
|
227 |
VCSGisWorkspace workspace = getWorkspace(); |
228 |
VCSGisEntity selectedTable = this.entitySelector.getSelectedEntity();
|
229 |
|
230 |
String tableName = this.txtTableName.getText(); |
231 |
|
232 |
boolean exists = workspace != null |
233 |
&& selectedTable != null
|
234 |
&& existInDataBase(); |
235 |
|
236 |
boolean canExport = !isProcessing && workspace != null |
237 |
&& (selectedTable != null)
|
238 |
&& StringUtils.isNotBlank(tableName) |
239 |
&& !exists |
240 |
&& workspace.getEntity(tableName) == null;
|
241 |
|
242 |
LOGGER.info("canExport = "+canExport+", processing = "+processing+", ws = "+workspace); |
243 |
|
244 |
this.entitySelector.setEnabled(!isProcessing() && workspace != null); |
245 |
|
246 |
this.revisionPicker.setEnabled(canExport);
|
247 |
this.rdbRevision.setEnabled(canExport);
|
248 |
|
249 |
this.efectivePicker.setEnabled(false); |
250 |
this.rdbEffectiveDate.setEnabled(false); |
251 |
|
252 |
this.rdbDontAddToProject.setEnabled(canExport);
|
253 |
this.rdbAddTableToProject.setEnabled(canExport);
|
254 |
if (canExport) {
|
255 |
if (isLayer(selectedTable)) {
|
256 |
this.rdbAddLayerToView.setEnabled(canExport);
|
257 |
this.cboView.setEnabled(canExport);
|
258 |
} else {
|
259 |
this.rdbAddLayerToView.setEnabled(false); |
260 |
this.cboView.setEnabled(false); |
261 |
} |
262 |
} else {
|
263 |
this.rdbAddLayerToView.setEnabled(false); |
264 |
this.cboView.setEnabled(false); |
265 |
} |
266 |
|
267 |
this.workspacePicker.setEnabled(!isProcessing);
|
268 |
this.entitySelector.setEnabled(!isProcessing && workspace != null); |
269 |
this.lblTableName.setEnabled(canExport);
|
270 |
this.txtTableName.setEnabled(canExport);
|
271 |
LOGGER.info("isProcessing = "+isProcessing+", processing = "+processing+", ws = "+workspace); |
272 |
|
273 |
this.pbStatus.setVisible(isProcessing);
|
274 |
this.lblStatusTitle.setVisible(isProcessing);
|
275 |
|
276 |
if (dialog != null) { |
277 |
LabeledValue selectedView = (LabeledValue) cboView.getSelectedItem(); |
278 |
boolean dontAddNothing = rdbDontAddToProject.isSelected();
|
279 |
boolean addLayerToView = rdbAddLayerToView.isSelected() && selectedView != null && selectedView.getValue() != null; |
280 |
boolean addTableToProject = rdbAddTableToProject.isSelected();
|
281 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK,
|
282 |
canExport && (dontAddNothing || (addLayerToView || addTableToProject)) |
283 |
); |
284 |
// this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK,false);
|
285 |
} |
286 |
} |
287 |
|
288 |
private boolean isLayer(VCSGisEntity entity) { |
289 |
return !StringUtils.isBlank(entity.getGeometryFieldName());
|
290 |
} |
291 |
|
292 |
private boolean isLayer(FeatureStore store) { |
293 |
FeatureType ft = store.getDefaultFeatureTypeQuietly(); |
294 |
if(ft == null){ |
295 |
return false; |
296 |
} |
297 |
return ft.getDefaultGeometryAttribute()!=null; |
298 |
} |
299 |
|
300 |
@Override
|
301 |
public int export() { |
302 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
303 |
|
304 |
VCSGisEntity entity = entitySelector.getSelectedEntity(); |
305 |
final VCSGisWorkspace workspace = this.getWorkspace(); |
306 |
|
307 |
VCSGisRevision revision = this.revisionPicker.get();
|
308 |
if (workspace != null && entity != null) { |
309 |
try {
|
310 |
processing = true;
|
311 |
doUpdateEnableComponents(); |
312 |
|
313 |
showMessage(i18n.getTranslation("_Processing"), null); |
314 |
int res = ERR_OK;
|
315 |
String tableName = this.txtTableName.getText(); |
316 |
|
317 |
res = workspace.export(entity.getEntityName(), |
318 |
tableName, |
319 |
(revision == null) ? null : revision.getRevisionCode(), |
320 |
DataTypeUtils.toTimestamp(efectivePicker.get()), |
321 |
null
|
322 |
); |
323 |
postExport(res, tableName); |
324 |
showMessage(i18n.getTranslation("_Ended_process"), null); |
325 |
return res;
|
326 |
} finally {
|
327 |
processing = false;
|
328 |
doUpdateEnableComponents(); |
329 |
} |
330 |
} |
331 |
return ERR_CANT_CHECKOUT;
|
332 |
} |
333 |
|
334 |
private void translate() { |
335 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
336 |
swingManager.translate(this.lblWorkspace);
|
337 |
swingManager.translate(this.lblTable);
|
338 |
swingManager.translate(this.rdbEffectiveDate);
|
339 |
swingManager.translate(this.rdbRevision);
|
340 |
swingManager.translate(this.rdbDontAddToProject);
|
341 |
swingManager.translate(this.rdbAddLayerToView);
|
342 |
swingManager.translate(this.rdbAddTableToProject);
|
343 |
swingManager.translate(this.lblTableName);
|
344 |
swingManager.translate(this.lblAddTableToProject);
|
345 |
} |
346 |
|
347 |
@Override
|
348 |
public boolean isProcessing() { |
349 |
return this.processing; |
350 |
} |
351 |
|
352 |
private void postExport(int checkoutStatus, String tableName) { |
353 |
if (notInSwingThreadInvokeLater(() -> {
|
354 |
postExport(checkoutStatus, tableName); |
355 |
})) { |
356 |
return;
|
357 |
} |
358 |
VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager(); |
359 |
VCSGisSwingServices services = swingManager.getDefaultServices(); |
360 |
final VCSGisWorkspace workspace = this.getWorkspace(); |
361 |
|
362 |
if (checkoutStatus == ERR_OK) {
|
363 |
FeatureStore store = workspace.openFeatureStore(tableName); |
364 |
if (!rdbDontAddToProject.isSelected()) {
|
365 |
if (rdbAddLayerToView.isSelected()) {
|
366 |
if (isLayer(store)) {
|
367 |
// String layerName = store.getName();
|
368 |
String newLayerName = tableName;
|
369 |
services.addLayerToView(store, (LabeledValue) cboView.getSelectedItem(), newLayerName); |
370 |
} |
371 |
} |
372 |
if (rdbAddTableToProject.isSelected()) {
|
373 |
// String layerName = store.getName();
|
374 |
String newLayerName = this.txtTableName.getText(); |
375 |
services.addTableToProject(workspace, store, newLayerName); |
376 |
} |
377 |
} |
378 |
this.setVisibleStatus(false); |
379 |
} else {
|
380 |
this.setVisibleStatus(true); |
381 |
} |
382 |
this.doUpdateEnableComponents();
|
383 |
|
384 |
} |
385 |
|
386 |
// private void doUpdateComponents() {
|
387 |
// if( notInSwingThreadInvokeLater(this::doUpdateComponents) ) {
|
388 |
// return;
|
389 |
// }
|
390 |
// boolean isProcessing = this.processing || this.entitySelector.isProcessing();
|
391 |
//
|
392 |
// I18nManager i18n = ToolsLocator.getI18nManager();
|
393 |
// VCSGisWorkspace workspace = getWorkspace();
|
394 |
// VCSGisEntity selectedTable = this.entitySelector.getSelectedEntity();
|
395 |
//
|
396 |
// boolean exists = workspace!=null &&
|
397 |
// workspace.existsInWorkspace(selectedTable);
|
398 |
//
|
399 |
//// this.chkOverwriteTable.setEnabled(!isProcessing && exists);
|
400 |
//
|
401 |
// boolean canExport = !isProcessing &&
|
402 |
// workspace!=null &&
|
403 |
// selectedTable != null;
|
404 |
//
|
405 |
// if (selectedTable == null || canExport) {
|
406 |
// showMessage("", null);
|
407 |
// } else {
|
408 |
// showMessage(
|
409 |
// i18n.getTranslation("_The_selected_table_is_already_in_the_working_copy"),
|
410 |
// i18n.getTranslation("_To_download_updated_data_use_show_changes_tool")
|
411 |
// );
|
412 |
// }
|
413 |
// this.revisionPicker.setEnabled(canExport);
|
414 |
// this.rdbRevision.setEnabled(canExport);
|
415 |
//
|
416 |
//// this.effectivDatePicker.setEnabled(canExport);
|
417 |
//// this.rdbEffectiveDate.setEnabled(canExport);
|
418 |
//
|
419 |
// this.rdbDontAddToProject.setEnabled(canExport);
|
420 |
// this.rdbAddTableToProject.setEnabled(canExport);
|
421 |
// if (canExport) {
|
422 |
// if (isLayer(selectedTable)) {
|
423 |
// this.rdbAddLayerToView.setEnabled(canExport);
|
424 |
// this.cboView.setEnabled(canExport);
|
425 |
// } else {
|
426 |
// this.rdbAddLayerToView.setEnabled(false);
|
427 |
// this.cboView.setEnabled(false);
|
428 |
// }
|
429 |
// } else {
|
430 |
// this.rdbAddLayerToView.setEnabled(false);
|
431 |
// this.cboView.setEnabled(false);
|
432 |
// }
|
433 |
//
|
434 |
// this.workspacePicker.setEnabled(!isProcessing);
|
435 |
// this.entitySelector.setEnabled(!isProcessing && workspace!=null );
|
436 |
//
|
437 |
// this.pbStatus.setVisible(isProcessing);
|
438 |
// this.lblStatusTitle.setVisible(isProcessing);
|
439 |
//
|
440 |
// if (dialog != null) {
|
441 |
// LabeledValue selectedView = (LabeledValue) cboView.getSelectedItem();
|
442 |
// boolean dontAddNothing = rdbDontAddToProject.isSelected();
|
443 |
// boolean addLayerToView = rdbAddLayerToView.isSelected() && selectedView != null && selectedView.getValue() != null;
|
444 |
// boolean addTableToProject = rdbAddTableToProject.isSelected();
|
445 |
// this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK,
|
446 |
// canExport && (dontAddNothing || (addLayerToView || addTableToProject))
|
447 |
// );
|
448 |
// }
|
449 |
// }
|
450 |
public void setVisibleStatus(boolean visible) { |
451 |
if (notInSwingThreadInvokeLater(() -> {
|
452 |
setVisibleStatus(visible); |
453 |
})) { |
454 |
return;
|
455 |
} |
456 |
this.lblStatusTitle.setVisible(visible);
|
457 |
this.pbStatus.setVisible(visible);
|
458 |
this.lblStatusMessages.setVisible(true); |
459 |
} |
460 |
|
461 |
private void showMessage(String message, String toolTipText) { |
462 |
if (notInSwingThreadInvokeLater(() -> {
|
463 |
showMessage(message, toolTipText); |
464 |
})) { |
465 |
return;
|
466 |
} |
467 |
this.lblStatusMessages.setText(message);
|
468 |
this.lblStatusMessages.setToolTipText(toolTipText);
|
469 |
} |
470 |
|
471 |
private boolean existInDataBase() { |
472 |
String tableName = this.txtTableName.getText(); |
473 |
if (StringUtils.isBlank(tableName)) {
|
474 |
return true; |
475 |
} |
476 |
VCSGisWorkspace ws = getWorkspace(); |
477 |
FeatureStore store = null;
|
478 |
try {
|
479 |
store = ws.openFeatureStore(tableName); |
480 |
return store != null; |
481 |
} catch (Exception ex) { |
482 |
return false; |
483 |
} finally {
|
484 |
DisposeUtils.disposeQuietly(store); |
485 |
} |
486 |
|
487 |
} |
488 |
|
489 |
} |