Statistics
| Revision:

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 / checkout / VCSGisJCheckoutImpl.java @ 8713

History | View | Annotate | Download (38.8 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.checkout;
23

    
24
import java.awt.event.ActionEvent;
25
import java.sql.Timestamp;
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.Comparator;
29
import java.util.Date;
30
import java.util.List;
31
import javax.swing.ComboBoxModel;
32
import javax.swing.ImageIcon;
33
import javax.swing.JComponent;
34
import javax.swing.JOptionPane;
35
import javax.swing.SwingUtilities;
36
import javax.swing.event.ChangeEvent;
37
import javax.swing.event.ChangeListener;
38
import org.apache.commons.collections.CollectionUtils;
39
import org.apache.commons.lang3.StringUtils;
40
import org.gvsig.fmap.dal.feature.FeatureStore;
41
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
42
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
43
import org.gvsig.fmap.geom.primitive.Envelope;
44
import org.gvsig.fmap.mapcontrol.MapControl;
45
import org.gvsig.fmap.mapcontrol.MapControlLocator;
46
import org.gvsig.fmap.mapcontrol.MapControlManager;
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.dispose.DisposeUtils;
49
import org.gvsig.tools.i18n.I18nManager;
50
import org.gvsig.tools.swing.api.Component;
51
import org.gvsig.tools.swing.api.ToolsSwingLocator;
52
import org.gvsig.tools.swing.api.ToolsSwingManager;
53
import org.gvsig.tools.swing.api.ToolsSwingUtils;
54
import org.gvsig.tools.swing.api.pickercontroller.DatePickerController;
55
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
56
import org.gvsig.tools.swing.api.task.TaskStatusController;
57
import org.gvsig.tools.swing.api.task.TaskStatusSwingManager;
58
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
59
import org.gvsig.tools.swing.api.windowmanager.Dialog;
60
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
61
import org.gvsig.tools.task.SimpleTaskStatus;
62
import org.gvsig.tools.task.TaskStatus;
63
import org.gvsig.tools.util.LabeledValue;
64
import org.gvsig.vcsgis.lib.UserCancelledException;
65
import org.gvsig.vcsgis.lib.VCSGisEntity;
66
import org.gvsig.vcsgis.lib.VCSGisLocator;
67
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANCELLED_BY_USER;
68
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CHECKOUT;
69
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_ENTITY_NOT_EXISTS;
70
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK;
71
import org.gvsig.vcsgis.lib.VCSGisRevision;
72
import org.gvsig.vcsgis.lib.VCSGisRuntimeException;
73
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
74
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity;
75
import org.gvsig.vcsgis.swing.VCSGisJCheckout;
76
import org.gvsig.vcsgis.swing.VCSGisSwingLocator;
77
import org.gvsig.vcsgis.swing.VCSGisSwingManager;
78
import org.gvsig.vcsgis.swing.VCSGisSwingServices;
79
import org.gvsig.vcsgis.swing.VCSGisEntitySelectorController;
80
import static org.gvsig.vcsgis.swing.VCSGisEntitySelectorController.ACTION_CHECK_ID;
81
import static org.gvsig.vcsgis.swing.VCSGisEntitySelectorController.ACTION_RELOAD_ID;
82
import static org.gvsig.vcsgis.swing.VCSGisEntitySelectorController.REMOTE_ENTITIES;
83
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.notInSwingThreadInvokeLater;
84
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.refreshDocument;
85
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.showAuthenticationErrors;
86
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.showAuthenticationRequiredMessage;
87
import org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl;
88
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.ICON_GROUP_NAME;
89
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.ICON_PROVIDER_NAME;
90
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingManagerImpl.registerIcons;
91
import org.gvsig.vcsgis.swing.impl.revisions.VCSGisRevisionPickerControllerImpl;
92
import org.slf4j.LoggerFactory;
93

    
94
/**
95
 *
96
 * @author gvSIG Team
97
 */
98
@SuppressWarnings("UseSpecificCatch")
99
public class VCSGisJCheckoutImpl extends VCSGisJCheckoutView
100
        implements Component, VCSGisJCheckout {
101

    
102
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(VCSGisJCheckoutImpl.class);
103

    
104
    private static final int NUM_TABLAS_TO_SHOW_WARN = 20;
105
    
106
    private Dialog dialog;
107

    
108
    private boolean processing;
109
    private PickerController<VCSGisWorkspace> workspacePicker;
110
    private TaskStatusController taskStatusController;
111
    private VCSGisRevisionPickerControllerImpl revisionPicker;
112
    private VCSGisEntitySelectorController entitySelector;
113
    private DatePickerController efectivePicker;
114
    private PickerController<Envelope> extentPicker;
115
    private List<String> defaultTablesSelection;
116

    
117
    public VCSGisJCheckoutImpl() {
118
        this(null, null, null);
119
    }
120
    
121
    public VCSGisJCheckoutImpl(VCSGisWorkspace workspace, List<String> tableNames, Boolean overwrite) {
122
        processing = false;
123
        translate();
124
        if( tableNames==null ) {
125
            this.defaultTablesSelection = Collections.EMPTY_LIST;
126
        } else {
127
            this.defaultTablesSelection = tableNames;
128
        }
129
        initComponents();
130
        SwingUtilities.invokeLater(() -> {
131
            this.workspacePicker.set(workspace);
132
            if( overwrite!=null ) {
133
                this.chkOverwriteTable.setSelected(overwrite);
134
            }
135
        });
136
    }
137

    
138
    private void initComponents() {
139
        MapControlManager mapControlManager = MapControlLocator.getMapControlManager();
140
        TaskStatusSwingManager taskStatusManager = ToolsSwingLocator.getTaskStatusSwingManager();
141
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
142
        VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager();
143
        VCSGisSwingServices services = swingManager.getDefaultServices();
144

    
145
        this.chkUseLabelInsteadName.setSelected(true);
146
        
147
        this.entitySelector = VCSGisSwingLocator.getVCSGisSwingManager().createEntitySelectorController(treeTables, txtFilter, btnTable);
148
        this.entitySelector.setFilter(REMOTE_ENTITIES);
149
        this.entitySelector.setViewFilter(REMOTE_ENTITIES);
150
        this.entitySelector.setChecksEnabled(true);
151
        this.entitySelector.addActionListener((ActionEvent e) -> {
152
            switch(e.getID()){
153
                case ACTION_RELOAD_ID:
154
//                case ACTION_BEGIN_PROCESSING_ID:
155
//                case ACTION_END_PROCESSING_ID:
156
                    doUpdateComponents();
157
                    break;
158
                case ACTION_CHECK_ID:
159
                    doChangeSelectedEntities();
160
                    break;
161
            }
162
        });
163
        this.entitySelector.addChangeListener((ChangeEvent e) -> {
164
            doUpdateComponents();
165
        });
166
            
167
        this.taskStatusController = taskStatusManager.createTaskStatusController(
168
                null,
169
                this.lblStatusTitle,
170
                this.lblStatusMessages,
171
                this.pbStatus,
172
                this.btnStatusCancel,
173
                null
174
        );
175
        this.taskStatusController.setShowCancelButton(false);
176
        this.taskStatusController.setShowRemoveTaskButton(false);
177
//        this.taskStatusController.bind(ToolsLocator.getTaskStatusManager());
178
        SwingUtilities.invokeLater(() -> {this.setVisibleStatus(false); });
179

    
180
        this.workspacePicker = swingManager.createWorkspacePickerController(
181
                cboWorkspaces,
182
                btnInitWorkspace
183
        );
184

    
185
        this.workspacePicker.addChangeListener((ChangeEvent e) -> {
186
            this.clearMessage();
187
            doChangeWorkspace();
188
            doUpdateComponents();
189
        });
190

    
191
        this.rdblastRevision.addActionListener((ActionEvent e) -> {
192
            this.clearMessage();
193
            doUpdateComponents();
194
        });
195

    
196
        this.rdbEffectiveDate.addActionListener((ActionEvent e) -> {
197
            this.clearMessage();
198
            doUpdateComponents();
199
        });
200
        this.efectivePicker = toolsSwingManager.createDatePickerController(txtEffectiveDate, btnEffectiveDate);
201
        this.efectivePicker.addChangeListener((ChangeEvent e) -> {
202
            this.clearMessage();
203
            doUpdateComponents();
204
        });
205
        
206
        this.chkCheckoutOnlyOneArea.addChangeListener((ChangeEvent e) -> {
207
            this.clearMessage();
208
            doUpdateComponents();
209
        });
210
                
211
        this.rdbRevision.addActionListener((ActionEvent e) -> {
212
            this.clearMessage();
213
            doUpdateComponents();
214
        });
215
        this.revisionPicker = new VCSGisRevisionPickerControllerImpl(txtRevision, btnRevision);
216
        this.revisionPicker.addChangeListener((ChangeEvent e) -> {
217
            this.clearMessage();
218
            doUpdateComponents();
219
        });
220
        
221
        ComboBoxModel<LabeledValue> viewDocumentsModel = services.getViewDocumentsComboBoxModel();
222

    
223
        cboView.setModel(viewDocumentsModel);
224
        cboView.addActionListener((ActionEvent e) -> {
225
            this.clearMessage();
226
            doUpdateComponents();
227
        });
228

    
229
        this.rdbDontAddToProject.addActionListener((ActionEvent e) -> {
230
            this.clearMessage();
231
            doUpdateComponents();
232
        });
233

    
234
        this.rdbAddLayerToView.addActionListener((ActionEvent e) -> {
235
            this.clearMessage();
236
            doUpdateComponents();
237
        });
238

    
239
        this.rdbAddTableToProject.addActionListener((ActionEvent e) -> {
240
            this.clearMessage();
241
            doUpdateComponents();
242
        });
243
        
244
        this.chkOverwriteTable.addActionListener((ActionEvent e) -> {
245
            this.clearMessage();
246
            doUpdateComponents();
247
        });
248
        this.btnCheckAll.addActionListener((ActionEvent e) -> {
249
            this.entitySelector.checkAll();
250
        });
251
        this.btnUnCheckAll.addActionListener((ActionEvent e) -> {
252
            this.entitySelector.clearChecks();
253
        });
254
        
255
        this.btnTablesCollapseAll.addActionListener((ActionEvent e) -> {
256
            this.entitySelector.collapseAll();
257
        });
258
        this.btnTablesExpandAll.addActionListener((ActionEvent e) -> {
259
            this.entitySelector.expandAll();
260
        });
261
        
262
        this.rdbDontAddToProject.setSelected(true);
263
        
264
        this.chkReplaceLayerIfExists.setSelected(true);
265

    
266
        this.chkCheckoutOnlyOneArea.addActionListener((ActionEvent e) -> {
267
            this.extentPicker.setEnabled(this.chkCheckoutOnlyOneArea.isSelected());
268
        });
269
        MapControl mapControl = services.getActiveMapControl();
270
        this.extentPicker = mapControlManager.createEnvelopePickerController(
271
                mapControl, 
272
                txtExtent, 
273
                btnExtentFromView, 
274
                btnExtentCapture
275
        );
276
        this.extentPicker.addChangeListener((ChangeEvent e) -> {
277
            this.clearMessage();
278
            doUpdateComponents();
279
        });
280

    
281
        showMessage(" ", null);
282

    
283
        doUpdateComponents();
284
        ToolsSwingUtils.ensureRowsCols(this, 16, 60, 22, 100);
285

    
286
    }
287

    
288
    private void clearMessage() {
289
        showMessage(" ", null);
290
    }
291

    
292
    @Override
293
    public JComponent asJComponent() {
294
        return this;
295
    }
296

    
297
    public void setVisibleStatus(boolean visible) {
298
        if( notInSwingThreadInvokeLater(() -> {setVisibleStatus(visible);}) ) {
299
            return;
300
        }
301
        this.lblStatusTitle.setVisible(visible);
302
        this.pbStatus.setVisible(visible);
303
        this.lblStatusMessages.setVisible(true);
304
    }
305

    
306
    @Override
307
    public void setDialog(Dialog dialog) {
308
        this.dialog = dialog;
309
        this.doUpdateComponents();
310
    }
311

    
312
    private void doChangeWorkspace() {
313
        try {
314
            VCSGisWorkspace ws = this.getWorkspace();
315
            if (ws == null) {
316
                this.entitySelector.setWorkspace(ws);
317
                this.revisionPicker.setWorkspace(ws);
318
                return;
319
            }
320
            if (ws.isOffline() || ws.authenticate(null)) {
321
                this.entitySelector.setWorkspace(ws);
322
                this.revisionPicker.setWorkspace(ws);
323
                for (String tableName : defaultTablesSelection) {
324
                    VCSGisWorkspaceEntity entity = ws.getWorkspaceEntity(tableName);
325
                    if( entity!=null ) {
326
                        this.entitySelector.check(entity);
327
                    }
328
                }
329
                return;
330
            }
331
            showAuthenticationRequiredMessage("_VCS_Checkout");
332
            this.workspacePicker.set(null);
333
            doChangeWorkspace();
334
        } catch (VCSGisRuntimeException e1) {
335
            LOGGER.warn("Can't set workspace.", e1);
336
            if (showAuthenticationErrors("_VCS_Checkout", e1)) {
337
                this.workspacePicker.set(null);
338
                doChangeWorkspace();
339
            }
340
        } catch (Exception e2) {
341
            LOGGER.warn("Can't set workspace.", e2);
342
        }
343
    }
344

    
345
    @Override
346
    public VCSGisWorkspace getWorkspace() {
347
        return this.workspacePicker.get();
348
    }
349

    
350
    private boolean inDoUpdateComponents = false;
351
    private void doUpdateComponents() {
352
        if( notInSwingThreadInvokeLater(this::doUpdateComponents) ) {
353
            return;
354
        }
355
        if( this.inDoUpdateComponents ) {
356
            return;
357
        }
358
        try {
359
            this.inDoUpdateComponents = true;
360
            boolean isProcessing = this.processing || this.entitySelector.isProcessing();
361
            showMessage("");
362
            I18nManager i18n = ToolsLocator.getI18nManager();
363
            VCSGisWorkspace workspace = getWorkspace();
364
            List<VCSGisEntity> selectedTables = this.entitySelector.getCheckedEntities();
365
            VCSGisEntity selectedTable;
366
            boolean canCheckout = !VCSGisLocator.getVCSGisManager().IsThereConflictingStoresInEdition();
367
            if( !canCheckout ) {
368
                showMessage(
369
                        i18n.getTranslation("_There_is_some_table_that_can_be_conflicting_in_editing_mode")
370
                );
371
            }
372
            canCheckout = canCheckout && !isProcessing && workspace!=null;
373
            if( selectedTables.isEmpty() ) {
374
                this.workspacePicker.setEnabled(!isProcessing);
375
                this.entitySelector.setEnabled(!isProcessing && workspace!=null );
376
                this.btnCheckAll.setEnabled(this.entitySelector.isEnabled());
377
                this.btnUnCheckAll.setEnabled(this.entitySelector.isEnabled());
378
                this.rdbDontAddToProject.setEnabled(false);
379
                this.rdbAddTableToProject.setEnabled(false);
380
                this.rdbAddLayerToView.setEnabled(false);
381
                this.cboView.setEnabled(false);
382
                this.chkReplaceLayerIfExists.setEnabled(false);
383
                this.chkUseLabelInsteadName.setEnabled(false);
384

    
385
                this.rdbRevision.setEnabled(false);
386
                this.rdbEffectiveDate.setEnabled(false);
387
                this.rdblastRevision.setEnabled(false);
388

    
389
                this.revisionPicker.setEnabled(false);
390
                this.efectivePicker.setEnabled(false);
391
            } else if( selectedTables.size()==1 ) {
392
                boolean exists = false;
393
                selectedTable = selectedTables.get(0);
394
                if( workspace!=null ) {
395
                    if( workspace.existsInWorkspace(selectedTable) ) {
396
                        exists = true;
397
                    } else {
398
                        // Ojo. esta comprobacion es lenta.
399
                        JDBCServerExplorer explorer = workspace.getExplorer();
400
                        JDBCStoreParameters parameters = explorer.get(selectedTable.getEntityName());
401
                        if( explorer.exists(parameters) ) {
402
                            exists = true;
403
                        }
404
                    }
405
                }
406
                this.chkOverwriteTable.setEnabled(!isProcessing && exists);
407

    
408
                canCheckout = selectedTable != null && 
409
                        (!exists || this.chkOverwriteTable.isSelected());
410

    
411
                if (selectedTable == null || canCheckout) {
412
                    showMessage("", null);
413
                } else {
414
                    showMessage(
415
                            i18n.getTranslation("_The_selected_table_is_already_in_the_working_copy"),
416
                            i18n.getTranslation("_To_download_updated_data_use_show_changes_tool")
417
                    );
418
                }
419
                this.rdbRevision.setEnabled(canCheckout);
420
                this.rdbEffectiveDate.setEnabled(canCheckout);
421
                this.rdblastRevision.setEnabled(canCheckout);
422
                this.revisionPicker.setEnabled(canCheckout && this.rdbRevision.isSelected());
423
                this.efectivePicker.setEnabled(canCheckout && this.rdbEffectiveDate.isSelected());
424
                this.chkCheckoutOnlyOneArea.setEnabled(canCheckout && this.rdbEffectiveDate.isSelected());
425
                this.extentPicker.setEnabled(canCheckout && this.rdbEffectiveDate.isSelected());
426
                if(!this.rdbEffectiveDate.isSelected()){
427
                    this.chkCheckoutOnlyOneArea.setSelected(false);
428
                    this.extentPicker.set(null);
429
                }
430

    
431
                this.rdbDontAddToProject.setEnabled(canCheckout);
432
                this.rdbAddTableToProject.setEnabled(canCheckout);
433
                if (canCheckout) {
434
                    if (isLayer(selectedTable)) {
435
                        this.rdbAddLayerToView.setEnabled(canCheckout);
436
                        this.cboView.setEnabled(canCheckout && this.rdbAddLayerToView.isSelected());
437
                        this.chkReplaceLayerIfExists.setEnabled(canCheckout && this.rdbAddLayerToView.isSelected());
438
                        this.chkUseLabelInsteadName.setEnabled(canCheckout && this.rdbAddLayerToView.isSelected());
439
                    } else {
440
                        this.rdbAddLayerToView.setEnabled(false);
441
                        this.cboView.setEnabled(false);
442
                        this.chkReplaceLayerIfExists.setEnabled(false);
443
                        this.chkUseLabelInsteadName.setEnabled(false);
444
                    }
445
                } else {
446
                    this.rdbAddLayerToView.setEnabled(false);
447
                    this.cboView.setEnabled(false);
448
                    this.chkReplaceLayerIfExists.setEnabled(false);
449
                    this.chkUseLabelInsteadName.setEnabled(false);
450
                }
451
                this.workspacePicker.setEnabled(!isProcessing);
452
                this.entitySelector.setEnabled(!isProcessing && workspace!=null );
453
                this.btnCheckAll.setEnabled(this.entitySelector.isEnabled());
454
                this.btnUnCheckAll.setEnabled(this.entitySelector.isEnabled());
455

    
456
                this.chkCheckoutOnlyOneArea.setEnabled(this.rdbEffectiveDate.isSelected());
457
                this.extentPicker.setEnabled(chkCheckoutOnlyOneArea.isSelected());
458

    
459
                if( canCheckout &&
460
                        this.rdbEffectiveDate.isEnabled() && 
461
                        this.rdbEffectiveDate.isSelected() && 
462
                        this.chkCheckoutOnlyOneArea.isSelected() ) {
463
                    if( this.extentPicker.isEmpty() ) {
464
                        canCheckout = false;
465
                        showMessage(
466
                                i18n.getTranslation("_Clipping_area_is_empty"),
467
                                i18n.getTranslation("_Clipping_area_is_empty")
468
                        );
469
                        
470
                    }
471
                }
472
                
473
                if( canCheckout && this.rdbRevision.isSelected() ) {
474
                    if( this.revisionPicker.get()==null ) {
475
                        canCheckout = false;
476
                        showMessage(
477
                                i18n.getTranslation("_Revision_not_selected"),
478
                                i18n.getTranslation("_Revision_not_selected")
479
                        );
480
                    }
481
                }
482

    
483
                if (dialog != null) {
484
                    LabeledValue selectedView = (LabeledValue) cboView.getSelectedItem();
485
                    boolean dontAddNothing = rdbDontAddToProject.isSelected();
486
                    boolean addLayerToView = rdbAddLayerToView.isSelected() && selectedView != null && selectedView.getValue() != null;
487
                    boolean addTableToProject = rdbAddTableToProject.isSelected();
488
                    boolean enableCheckout = canCheckout && (dontAddNothing || (addLayerToView || addTableToProject));
489
                    this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK,enableCheckout);
490
                }
491
            } else {
492
                this.rdbRevision.setEnabled(false);
493
                this.rdbEffectiveDate.setEnabled(canCheckout);
494
                this.rdblastRevision.setEnabled(canCheckout);
495

    
496

    
497
                this.revisionPicker.setEnabled(canCheckout && this.rdbRevision.isSelected());
498
                this.efectivePicker.setEnabled(canCheckout && this.rdbEffectiveDate.isSelected());
499
                
500
                this.chkCheckoutOnlyOneArea.setEnabled(canCheckout && this.rdbEffectiveDate.isSelected());
501
                this.extentPicker.setEnabled(canCheckout && this.rdbEffectiveDate.isSelected());
502
                if(!this.rdbEffectiveDate.isSelected()){
503
                    this.chkCheckoutOnlyOneArea.setSelected(false);
504
                    this.extentPicker.set(null);
505
                }
506

    
507
                this.chkOverwriteTable.setEnabled(false);
508

    
509
                this.rdbDontAddToProject.setEnabled(canCheckout);
510
                this.rdbAddTableToProject.setEnabled(canCheckout);
511
                if( canCheckout ) {
512
                    if( areAllCheckedLayers() ) {
513
                        this.rdbAddLayerToView.setEnabled(canCheckout);
514
                        this.cboView.setEnabled(canCheckout && this.rdbAddLayerToView.isSelected());
515
                        this.chkReplaceLayerIfExists.setEnabled(canCheckout && this.rdbAddLayerToView.isSelected());
516
                        this.chkUseLabelInsteadName.setEnabled(canCheckout && this.rdbAddLayerToView.isSelected());
517
                    } else {
518
                        this.rdbAddLayerToView.setEnabled(false);
519
                        this.cboView.setEnabled(false);
520
                        this.chkReplaceLayerIfExists.setEnabled(false);
521
                        this.chkUseLabelInsteadName.setEnabled(false);
522
                    }
523
                } else {
524
                    this.rdbAddLayerToView.setEnabled(false);
525
                    this.cboView.setEnabled(false);
526
                    this.chkReplaceLayerIfExists.setEnabled(false);
527
                    this.chkUseLabelInsteadName.setEnabled(false);
528
                }
529
                this.workspacePicker.setEnabled(!isProcessing);
530
                this.entitySelector.setEnabled(!isProcessing && workspace!=null );
531
                this.btnCheckAll.setEnabled(this.entitySelector.isEnabled());
532
                this.btnUnCheckAll.setEnabled(this.entitySelector.isEnabled());
533
                
534
                this.chkCheckoutOnlyOneArea.setEnabled(this.rdbEffectiveDate.isSelected());
535
                this.extentPicker.setEnabled(chkCheckoutOnlyOneArea.isSelected());
536

    
537
                if( canCheckout &&
538
                        this.rdbEffectiveDate.isEnabled() && 
539
                        this.rdbEffectiveDate.isSelected() && 
540
                        this.chkCheckoutOnlyOneArea.isSelected() ) {
541
                    if( this.extentPicker.isEmpty() ) {
542
                        canCheckout = false;
543
                        showMessage(
544
                                i18n.getTranslation("_Clipping_area_is_empty"),
545
                                i18n.getTranslation("_Clipping_area_is_empty")
546
                        );
547
                        
548
                    }
549
                }
550
//                this.chkOverwriteTable.setEnabled(false);
551
//                this.revisionPicker.setEnabled(false);
552
//                this.lblRevisions.setEnabled(false);
553
//                this.rdbDontAddToProject.setEnabled(false);
554
//                this.rdbAddTableToProject.setEnabled(false);
555
//                this.rdbAddLayerToView.setEnabled(false);
556
//                this.cboView.setEnabled(false);
557
                if (dialog != null ) {
558
                    this.dialog.setButtonEnabled(
559
                            WindowManager_v2.BUTTON_OK,
560
                            canCheckout && selectedTables.size()>1
561
                    );
562
                }
563
            }
564
            
565
        } catch (VCSGisRuntimeException e1) {
566
            LOGGER.warn("Can't updating components.", e1);
567
            if (showAuthenticationErrors("_VCS_Checkout", e1)) {
568
                this.workspacePicker.set(null);
569
                doChangeWorkspace();
570
                doUpdateComponents();
571
            }
572
        } catch (Exception e2) {
573
            LOGGER.warn("Can't updating components.", e2);
574
        } finally {
575
            this.inDoUpdateComponents = false;
576
        }
577
    }
578

    
579
    private boolean isLayer(VCSGisEntity entity) {
580
        return entity != null && !StringUtils.isBlank(entity.getGeometryFieldName());
581
    }
582

    
583
    private boolean areAllCheckedLayers() {
584
        List<VCSGisEntity> entities = entitySelector.getCheckedEntities();
585
        for (VCSGisEntity entity : entities) {
586
            if( entity != null && StringUtils.isBlank(entity.getGeometryFieldName()) ) {
587
                return false;
588
            }
589
        }
590
        return true;
591
    }
592

    
593
    @Override
594
    public int checkout() {
595
        I18nManager i18n = ToolsLocator.getI18nManager();
596
        ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
597

    
598
        if( this.rdbEffectiveDate.isSelected() && !this.chkCheckoutOnlyOneArea.isSelected() ) {
599
            // _You_have_selected_to_get_the_data_to_an_effective_date_but_you_have_not_indicated_a_clipping_area_Do_you_want_to_continue_anyway
600
            // You have selected to get the data to an effective date but you have not indicated a clipping area.\nDo you want to continue anyway ?
601
            // Tiene seleccionado obtener los datos a una fecha de entrada en vigor pero no ha indicado un area de recorte.\n¿ Desea continuar de todas maneras ?
602
            int n = dialogs.confirmDialog(
603
                    i18n.getTranslation("_You_have_selected_to_get_the_data_to_an_effective_date_but_you_have_not_indicated_a_clipping_area_Do_you_want_to_continue_anyway"),
604
                    i18n.getTranslation("_VCS_Checkout"), 
605
                    JOptionPane.YES_NO_OPTION,
606
                    JOptionPane.QUESTION_MESSAGE
607
            );
608
            if( n == JOptionPane.NO_OPTION ) {
609
                setVisibleStatus(false);
610
                return ERR_CANCELLED_BY_USER;
611
            }
612
        }
613
        
614
        List<VCSGisEntity> entities = entitySelector.getCheckedEntities();
615
        if( entities.size()>NUM_TABLAS_TO_SHOW_WARN ) {
616
            int n = dialogs.confirmDialog(
617
                    i18n.getTranslation("_Are_selected_XTablesX_Are_you_sure_to_continue",new String[] {String.valueOf(entities.size())}),
618
                    i18n.getTranslation("_VCS_Checkout"), 
619
                    JOptionPane.YES_NO_OPTION,
620
                    JOptionPane.QUESTION_MESSAGE
621
            );
622
            if( n == JOptionPane.NO_OPTION ) {
623
                setVisibleStatus(false);
624
                return ERR_CANCELLED_BY_USER;
625
            }
626
        }
627
        final VCSGisWorkspace workspace = this.getWorkspace();
628
        boolean replaceLayerIfExists = this.chkReplaceLayerIfExists.isSelected();
629
        boolean useLabelInsteadName = this.chkUseLabelInsteadName.isSelected();
630
        
631
        Envelope bbox = null;
632
        if( this.chkCheckoutOnlyOneArea.isSelected() ) {
633
            bbox = this.extentPicker.get();
634
        }
635
        
636
        VCSGisRevision revision = null;
637
        Timestamp efectiveDate = null;
638
        if( this.rdbRevision.isSelected() ) {
639
            revision = this.revisionPicker.get();
640
            efectiveDate = null;
641
        }
642
        if( this.rdblastRevision.isSelected() ) {
643
            revision = null;
644
            efectiveDate = null;
645
        }
646
        if( this.rdbEffectiveDate.isSelected() ) {
647
            revision = null;
648
            Date d = this.efectivePicker.get();
649
            if( d!=null ) {
650
                efectiveDate = new Timestamp(d.getTime());
651
            }
652
        }
653
        if (workspace != null && !entities.isEmpty() ) {
654
            SimpleTaskStatus taskStatus = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Download changes");
655
            try {
656
                taskStatus.setAutoremove(true);
657
                taskStatus.add();
658
                taskStatus.setRangeOfValues(0, entities.size());
659
                taskStatus.setCurValue(0);
660
                this.taskStatusController.bind(taskStatus);
661
                processing = true;
662
                doUpdateComponents();
663
                
664
                List<String>resourcesEntityNames = new ArrayList<>();
665
                for (VCSGisEntity entity : entities) {
666
                    String resourcesTable = entity.getResources();
667
                    if( StringUtils.isNotBlank(resourcesTable) ) {
668
                        resourcesEntityNames.add(resourcesTable);
669
                    }
670
                }
671
                if( CollectionUtils.isNotEmpty(resourcesEntityNames) ) {
672
                    Collections.sort(entities, new Comparator<VCSGisEntity>() {
673
                        @Override
674
                        public int compare(VCSGisEntity o1, VCSGisEntity o2) {
675
                            if( o1!=null && resourcesEntityNames.contains(o1.getResources()) ) {
676
                                return -1;
677
                            }
678
                            if( o2!=null && resourcesEntityNames.contains(o2.getResources()) ) {
679
                                return 1;
680
                            }
681
                            return 0;
682
                        }
683
                    });
684
                }
685

    
686
                int res = ERR_OK;
687
                for (VCSGisEntity entity : entities) {
688
                    taskStatus.setTitle("Checkout" + " - " + entity.getLabel() + taskStatus.getProgressLabel());
689
                    showMessage(i18n.getTranslation("_Processing"), null);
690
                    if(chkOverwriteTable.isSelected()){
691
                        res = workspace.removeEntity(entity);
692
                    }
693
                    if( !(res == ERR_OK || res == ERR_ENTITY_NOT_EXISTS) ){
694
                        break;
695
                    }
696
                    try {
697
                        FeatureStore store = workspace.openFeatureStore(entity.getEntityName(), true);
698
                        if( store!=null ) {
699
                            workspace.getExplorer().remove(store.getParameters());
700
                        }
701
                    } catch(Exception ex) {
702
                        LOGGER.warn("Can't remove table '"+entity.getEntityName()+"'",ex);
703
                    }
704
                    res = workspace.checkout(
705
                            entity.getEntityName(),
706
                            (revision == null) ? null : revision.getRevisionCode(),
707
                            efectiveDate,
708
                            bbox,
709
                            taskStatus
710
                    );
711
                    taskStatus.incrementCurrentValue();
712
                    if( taskStatus.isCancellationRequested() ) {
713
                        break;
714
                    }
715
                }
716
                if(res == ERR_OK){
717
                    postCheckout(workspace, res, entities, replaceLayerIfExists, useLabelInsteadName);                   
718
                }
719
                showMessage(i18n.getTranslation("_Ended_process"), null);
720
                if( taskStatus.isCancellationRequested() ) {
721
                    taskStatus.cancel();
722
                } else {
723
                    taskStatus.terminate();
724
                }
725
                return res;
726
            } catch(UserCancelledException ex) {
727
                LOGGER.warn("User cancelled");
728
                taskStatus.cancel();
729
            } catch(Exception ex) {
730
                LOGGER.warn("Can't checkout", ex);
731
                taskStatus.abort();
732
            } finally {
733
                processing = false;
734
                doUpdateComponents();
735
            }
736
        }
737
        return ERR_CANT_CHECKOUT;
738
    }
739

    
740
    private void postCheckout(VCSGisWorkspace workspace, int checkoutStatus, List<VCSGisEntity> entities, boolean replaceLayerIfExists, boolean useLabelInsteadName) {
741
        if( notInSwingThreadInvokeLater(() -> {postCheckout(workspace, checkoutStatus,entities, replaceLayerIfExists, useLabelInsteadName);}) ) {
742
            return;
743
        }
744
        VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager();
745
        VCSGisSwingServices services = swingManager.getDefaultServices();
746
        TaskStatus taskStatus = this.taskStatusController.getTaskStatus();
747
        if (!taskStatus.isCancellationRequested() && checkoutStatus == ERR_OK) {
748
            FeatureStore store = null;
749
            for (VCSGisEntity entity : entities) {
750
                try {
751
                    store = workspace.openFeatureStore(entity.getEntityName(),false);
752
                    if (!rdbDontAddToProject.isSelected()) {
753
                        if (rdbAddLayerToView.isSelected()) {
754
                            if (isLayer(entity)) {
755
                                String layerName = store.getName();
756
                                if( useLabelInsteadName ) {
757
                                    layerName = entity.getLabelOrName();
758
                                }
759
                                String groupName = entity.getFirstCategory();
760
                                services.addLayerToView(
761
                                        store, 
762
                                        (LabeledValue) cboView.getSelectedItem(), 
763
                                        groupName, 
764
                                        layerName, 
765
                                        replaceLayerIfExists
766
                                );
767
                            }
768
                        }
769
                        if (rdbAddTableToProject.isSelected()) {
770
                            services.addTableToProject(workspace, store);
771
                        }
772
                    }
773
                    if (chkOverwriteTable.isSelected()) {
774
                        refreshDocument(store);
775
                    }
776
                } finally {
777
                    DisposeUtils.disposeQuietly(store);
778
                }
779
            }
780
            this.entitySelector.clearChecks();
781
            this.setVisibleStatus(false);
782
        } else {
783
            this.setVisibleStatus(true);
784
        }
785
        this.entitySelector.reloadEntities();
786
        this.doUpdateComponents();
787
    }
788

    
789
    private void showMessage(String message) {
790
        showMessage(message, null);
791
    }
792

    
793
    private void showMessage(String message, String toolTipText) {
794
        if( notInSwingThreadInvokeLater(() -> {showMessage(message, toolTipText);}) ) {
795
            return;
796
        }
797
        this.lblStatusMessages.setText(message);
798
        this.lblStatusMessages.setToolTipText(toolTipText);
799
    }
800

    
801
    private void translate() {
802
        ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager();
803
        swingManager.translate(this.lblWorkspace);
804
        swingManager.translate(this.tabCheckout);
805
        swingManager.translate(this.rdblastRevision);
806
        swingManager.translate(this.rdbEffectiveDate);
807
        swingManager.translate(this.rdbRevision);
808
        swingManager.translate(this.chkCheckoutOnlyOneArea);        
809
        swingManager.translate(this.rdbDontAddToProject);
810
        swingManager.translate(this.rdbAddLayerToView);
811
        swingManager.translate(this.rdbAddTableToProject);
812
        swingManager.translate(this.chkOverwriteTable);
813
        swingManager.translate(this.btnCheckAll);
814
        swingManager.translate(this.btnUnCheckAll);
815
        swingManager.translate(this.chkReplaceLayerIfExists);
816
        swingManager.translate(this.chkUseLabelInsteadName);
817
        swingManager.translate(this.btnTablesCollapseAll);
818
        swingManager.translate(this.btnTablesExpandAll);
819
    }
820

    
821
    @Override
822
    public boolean isProcessing() {
823
        return processing;
824
    }
825

    
826
    public static void selfRegister() {
827
        String[][] iconsInfo = new String[][]{
828
            new String[]{ICON_PROVIDER_NAME, ICON_GROUP_NAME, "vcsgis-common-checkout"},};
829
        registerIcons(iconsInfo);
830
    }
831

    
832
    private void doChangeSelectedEntities() {
833
        VCSGisWorkspace workspace = getWorkspace();
834
        List<VCSGisEntity> selectedTables = this.entitySelector.getCheckedEntities();
835
        if( selectedTables.size()==1 ) {
836
            VCSGisEntity selectedTable = selectedTables.get(0);
837
            boolean isLayer = isLayer(selectedTable);
838
            this.rdbAddLayerToView.setSelected(isLayer && !this.rdbDontAddToProject.isSelected());
839
            this.rdbAddTableToProject.setSelected(!isLayer && !this.rdbDontAddToProject.isSelected());
840
            if(isLayer) {
841
                if (this.cboView.getModel().getSize() > 0) {
842
                    this.cboView.setEnabled(true);
843
                    if (this.cboView.getModel().getSize() == 2) {
844
                        this.cboView.setSelectedIndex(1);
845
                    } else {
846
                        this.cboView.setSelectedIndex(0);
847
                    }
848
                } else {
849
                    this.cboView.setEnabled(false);
850
                }
851
            }
852
            this.revisionPicker.setEntity(selectedTable);
853
            this.chkOverwriteTable.setSelected(workspace!=null && workspace.existsInWorkspace(selectedTable));
854
        } else if( selectedTables.size()>1 ) {
855
            this.revisionPicker.set(null);
856
            this.chkOverwriteTable.setSelected(true);
857
            // No esta claro si cuando hay seleccion multiple dejar tocar estas 
858
            // cosas o no. Siempre me ha parecido que al dejarlos activos
859
            // el usuario puede crear que las cosas se aplican a la tabla que 
860
            // seleccionan en cada momento, como hace el añadir-capa-de-bbdd,
861
            // y podia llevar a confusion.
862
            // Me han pedido (5/1/22) que se active y que se apliquen las opciones
863
            // seleccionadas a todas las tablas.
864
//            this.rdbDontAddToProject.setSelected(true);
865
//            this.rdbAddLayerToView.setSelected(false);
866
//            this.rdbAddTableToProject.setSelected(false);
867
//            this.cboView.setSelectedIndex(0);
868
        }
869
        doUpdateComponents();
870
    }
871

    
872
    @Override
873
    public VCSGisEntity getTable() {
874
        return this.entitySelector.getSelectedEntity();
875
    }
876

    
877
    @Override
878
    public ImageIcon loadImage(String imageName) {
879
        return VCSGisSwingManagerImpl.loadImage(imageName);
880
    }
881
 }