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 / changes / LocalChangesController.java @ 3630

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

    
24
import java.awt.Component;
25
import java.awt.Cursor;
26
import java.awt.Dimension;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.KeyAdapter;
29
import java.awt.event.KeyEvent;
30
import java.sql.Timestamp;
31
import java.util.ArrayList;
32
import java.util.Date;
33
import java.util.HashSet;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Set;
37
import javax.json.JsonObject;
38
import javax.swing.JButton;
39
import javax.swing.JLabel;
40
import javax.swing.JOptionPane;
41
import javax.swing.JTable;
42
import javax.swing.JTextField;
43
import javax.swing.JTree;
44
import javax.swing.ListSelectionModel;
45
import javax.swing.event.ChangeEvent;
46
import javax.swing.event.ListSelectionEvent;
47
import javax.swing.event.TableModelEvent;
48
import javax.swing.event.TableModelListener;
49
import javax.swing.table.DefaultTableCellRenderer;
50
import javax.swing.table.TableModel;
51
import org.apache.commons.collections.CollectionUtils;
52
import org.apache.commons.lang3.StringUtils;
53
import org.apache.commons.lang3.mutable.MutableObject;
54
import org.gvsig.featureform.swing.CreateJFeatureFormException;
55
import org.gvsig.featureform.swing.JFeatureForm;
56
import org.gvsig.fmap.dal.feature.Feature;
57
import org.gvsig.fmap.dal.feature.FeatureStore;
58
import org.gvsig.fmap.dal.swing.DALSwingLocator;
59
import org.gvsig.fmap.dal.swing.DataSwingManager;
60
import org.gvsig.fmap.geom.Geometry;
61
import org.gvsig.tools.ToolsLocator;
62
import org.gvsig.tools.i18n.I18nManager;
63
import org.gvsig.tools.swing.api.ToolsSwingLocator;
64
import org.gvsig.tools.swing.api.ToolsSwingManager;
65
import org.gvsig.tools.swing.api.pickercontroller.DatePickerController;
66
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
67
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
68
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
69
import org.gvsig.vcsgis.lib.VCSGisChange;
70
import org.gvsig.vcsgis.lib.VCSGisEntity;
71
import org.gvsig.vcsgis.lib.VCSGisLocator;
72
import org.gvsig.vcsgis.lib.VCSGisManager;
73
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK;
74
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_ADD_ENTITY;
75
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE;
76
import org.gvsig.vcsgis.lib.VCSGisRuntimeException;
77
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
78
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChange;
79
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChanges;
80
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity;
81
import org.gvsig.vcsgis.swing.VCSGisSwingLocator;
82
import org.gvsig.vcsgis.swing.VCSGisSwingManager;
83
import org.gvsig.vcsgis.swing.VCSGisSwingServices;
84
import static org.gvsig.vcsgis.swing.VCSGisSwingServices.HIGHLIGHT_WORKSPACE;
85
import static org.gvsig.vcsgis.swing.VCSGisSwingServices.HIGHLIGHT_WORKSPACE_PREVIOUS;
86
import org.gvsig.vcsgis.swing.VCSGisEntitySelectorController;
87
import org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons;
88
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.cleanHighligthed;
89
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.notInSwingThreadInvokeLater;
90
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.refreshDocument;
91
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.showAuthenticationErrors;
92
import org.slf4j.Logger;
93
import org.slf4j.LoggerFactory;
94

    
95
/**
96
 *
97
 * @author gvSIG Team
98
 */
99
public class LocalChangesController {
100
    private static final Logger LOGGER = LoggerFactory.getLogger(LocalChangesController.class);
101

    
102
    private final JButton btnLocalCheckAllEntities;
103
    private final JButton btnLocalUnCheckAllEntities;
104
    private final JTable tblLocalChanges;
105
    private final JButton btnLocalCheckAll;
106
    private final JButton btnLocalUnCheckAll;
107
    private final JButton btnLocalShowForm;
108
    private final JButton btnLocalRefresh;
109
    private final JButton btnLocalCommit;
110
    private final JButton btnLocalRevert;
111
    private final JLabel lblLocalEffectiveDate;
112
    private final JTextField txtLocalEffectiveDate;
113
    private final JTextField txtLocalComment;
114
    private final JButton btnLocalEffectiveDate;
115
    private final JLabel lblLocalComment;
116
    private final PickerController<VCSGisWorkspace> workspacePicker;    
117
    private final VCSGisJChangesImpl context;
118
    private final JButton btnLocalZoom;
119
    private final JButton btnLocalCenter;
120
    private final JButton btnLocalHighlight;
121
    private final JLabel lblLocalChangesCount;
122

    
123
    private VCSGisWorkspaceChanges<VCSGisWorkspaceChange> changes;
124
    private ChangesTableModel localChangesTableModel;
125
    private List<VCSGisWorkspaceEntity> entitiesWithSelectedChanges = null;
126

    
127
    private DatePickerController effectiveDatePicker;
128
    
129
    private final Set<String> entitiesToUnCheckAll;
130
    private TableModelListener tableModelListener;
131
    private boolean dissabledUncheckAllByEntities;
132
    private final JButton btnLocalCleanHighlighted;
133
    private final JTextField txtLocalTablesFilter;
134
    private final JButton btnLocalTable;
135
    private final JTree treeLocalTables;
136
    private VCSGisEntitySelectorController entitySelector;
137
    private boolean isSelectionFromEntitiesEmpty;
138
    
139
    public LocalChangesController(
140
        VCSGisJChangesImpl context,
141
        PickerController<VCSGisWorkspace> workspacePicker,
142
        JButton btnLocalCheckAllEntities,
143
        JButton btnLocalUnCheckAllEntities,
144
        JTable tblLocalChanges,
145
        JButton btnLocalCheckAll,
146
        JButton btnLocalUnCheckAll,
147
        JButton btnLocalShowForm,
148
        JButton btnLocalRefresh,
149
        JButton btnLocalCommit,
150
        JButton btnLocalRevert,
151
        JLabel lblLocalEffectiveDate,
152
        JTextField txtLocalEffectiveDate,
153
        JTextField txtLocalComment,
154
        JButton btnLocalEffectiveDate,
155
        JLabel lblLocalComment,
156
        JButton btnLocalZoom,
157
        JButton btnLocalCenter,
158
        JButton btnLocalHighlight,
159
        JButton btnLocalCleanHighlighted,
160
        JLabel lblLocalChangesCount,
161
        JTextField txtLocalTablesFilter,
162
        JButton btnLocalTable,
163
        JTree treeLocalTables
164
        ) {
165
        this.dissabledUncheckAllByEntities = false;
166

    
167
        this.context = context;
168
        this.workspacePicker = workspacePicker;
169
        this.tblLocalChanges = tblLocalChanges;
170
        this.btnLocalCheckAll = btnLocalCheckAll;
171
        this.btnLocalCheckAllEntities = btnLocalCheckAllEntities;
172
        this.btnLocalCommit = btnLocalCommit;
173
        this.btnLocalEffectiveDate = btnLocalEffectiveDate;
174
        this.btnLocalRefresh = btnLocalRefresh;
175
        this.btnLocalRevert = btnLocalRevert;
176
        this.btnLocalShowForm = btnLocalShowForm;
177
        this.btnLocalUnCheckAll = btnLocalUnCheckAll;
178
        this.btnLocalUnCheckAllEntities = btnLocalUnCheckAllEntities;
179
        this.lblLocalComment = lblLocalComment;
180
        this.lblLocalEffectiveDate = lblLocalEffectiveDate;
181
        this.txtLocalComment = txtLocalComment;
182
        this.txtLocalEffectiveDate = txtLocalEffectiveDate;
183
        this.btnLocalZoom = btnLocalZoom;
184
        this.btnLocalCenter = btnLocalCenter;
185
        this.btnLocalHighlight = btnLocalHighlight;
186
        this.btnLocalCleanHighlighted = btnLocalCleanHighlighted;
187
        this.lblLocalChangesCount= lblLocalChangesCount;
188
        
189
        this.txtLocalTablesFilter = txtLocalTablesFilter;
190
        this.btnLocalTable = btnLocalTable;
191
        this.treeLocalTables = treeLocalTables;
192

    
193
        this.entitiesToUnCheckAll = new HashSet<>();
194
                
195
        initComponents();
196
    }
197

    
198
    private void initComponents() {
199
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
200

    
201
        translate();
202

    
203
        this.btnLocalCheckAllEntities.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
204
        this.btnLocalUnCheckAllEntities.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
205
        this.btnLocalCheckAll.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
206
        this.btnLocalCommit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
207
        this.btnLocalEffectiveDate.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
208
        this.btnLocalRefresh.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
209
        this.btnLocalRevert.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
210
        this.btnLocalShowForm.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
211
        this.btnLocalUnCheckAll.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
212
        this.btnLocalZoom.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
213
        this.btnLocalCenter.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
214
        this.btnLocalHighlight.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
215
        this.btnLocalCleanHighlighted.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
216
        
217
        this.btnLocalCheckAllEntities.addActionListener((ActionEvent e) -> {
218
            this.entitySelector.checkAll();
219
        });
220

    
221
        this.btnLocalUnCheckAllEntities.addActionListener((ActionEvent e) -> {
222
            this.entitySelector.clearChecks();
223
        });
224
        
225
        this.entitySelector = VCSGisSwingLocator.getVCSGisSwingManager().createEntitySelectorController(
226
                this.treeLocalTables, 
227
                txtLocalTablesFilter, 
228
                btnLocalTable
229
        );
230
        this.entitySelector.setViewFilter(VCSGisEntitySelectorController.LOCAL_ENTITIES);
231
        this.entitySelector.setFilter(VCSGisEntitySelectorController.LOCAL_ENTITIES);
232
        this.entitySelector.setChecksEnabled(true);
233
        this.entitySelector.addChangeListener((ChangeEvent e) -> {
234
            doUpdateTableLocalChanges();
235
        });
236
        this.entitySelector.addActionListener((ActionEvent e) -> {
237
            switch(e.getID()) {
238
//                case VCSGisEntitySelectorController.ACTION_SELECT_ID:
239
//                    break;
240
                case VCSGisEntitySelectorController.ACTION_CHECK_ID:
241
                    doUpdateTableLocalChanges();
242
                    doUpdateComponents();
243
                    break;
244
                case VCSGisEntitySelectorController.ACTION_RELOAD_ID:
245
                    doUpdateTableLocalChanges();
246
                    doUpdateComponents();
247
                    break;
248
            }
249
        });
250

    
251
        this.tblLocalChanges.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
252

    
253
        this.tblLocalChanges.addKeyListener(new KeyAdapter() {
254
            @Override
255
            public void keyPressed(KeyEvent e) {
256
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
257
                    doToggleSelection();
258
                }
259
            }
260
        });
261

    
262
        this.tblLocalChanges.getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> {
263
            doUpdateEntitiesWithSelectedChanges();
264
            doUpdateIsSelectionFromEntitiesEmpty();
265
            context.doUpdateComponents();
266
        });
267

    
268
        this.btnLocalCheckAll.addActionListener((ActionEvent e) -> {
269
            if (changes != null) {
270
                doCheckAll();
271
                doUpdateTableLocalChanges();
272
            }
273
        });
274

    
275
        this.btnLocalUnCheckAll.addActionListener((ActionEvent e) -> {
276
            if (changes != null) {
277
                doUnCheckAll();
278
                doUpdateTableLocalChanges();
279
            }
280
        });
281

    
282
        this.btnLocalShowForm.addActionListener((ActionEvent e) -> {
283
            doShowForm();
284
        });
285

    
286
        this.btnLocalCommit.addActionListener((ActionEvent e) -> {
287
            doCommit();
288
        });
289

    
290
        this.btnLocalRevert.addActionListener((ActionEvent e) -> {
291
            doRevert();
292
        });
293

    
294
        this.effectiveDatePicker = toolsSwingManager.createDatePickerController(
295
                this.txtLocalEffectiveDate,
296
                this.btnLocalEffectiveDate
297
        );
298

    
299
        this.effectiveDatePicker.set(new Date());
300
        
301
        this.btnLocalRefresh.addActionListener((ActionEvent e) -> {
302
            doUpdateTableLocalChanges();
303
        });
304

    
305
        this.btnLocalZoom.addActionListener((ActionEvent e) -> {
306
            doZoomToSelectedChange();
307
        });
308

    
309
        this.btnLocalCenter.addActionListener((ActionEvent e) -> {
310
            doCenterToSelectedChange();
311
        });
312

    
313
        this.btnLocalHighlight.addActionListener((ActionEvent e) -> {
314
            doHighLightSelectedChange();
315
        });
316

    
317
        this.btnLocalCleanHighlighted.addActionListener((ActionEvent e) -> {
318
            cleanHighligthed();
319
        });
320

    
321
        toolsSwingManager.addClearButton(this.txtLocalComment);
322
        toolsSwingManager.setDefaultPopupMenu(txtLocalComment);
323

    
324
        context.doUpdateComponents();
325
    }
326

    
327
    public void doChangeWorkspace() {
328
        this.entitySelector.setWorkspace(getWorkspace());
329
//        doUpdateTableLocalChanges();
330
    }
331
    
332
    private void translate() {
333
        ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager();
334

    
335
        swingManager.translate(this.btnLocalCheckAll);
336
        swingManager.translate(this.btnLocalCommit);
337
        swingManager.translate(this.btnLocalRefresh);
338
        swingManager.translate(this.btnLocalRevert);
339
        swingManager.translate(this.btnLocalShowForm);
340
        swingManager.translate(this.btnLocalUnCheckAll);
341

    
342
        swingManager.translate(this.lblLocalEffectiveDate);
343
        swingManager.translate(this.lblLocalComment);
344

    
345
        swingManager.translate(this.btnLocalCenter);
346
        swingManager.translate(this.btnLocalHighlight);
347
        swingManager.translate(this.btnLocalZoom);
348
        swingManager.translate(this.btnLocalCleanHighlighted);
349
    }
350

    
351
    private Iterator<Long> getSelectionIterator() {
352
        ListSelectionModel selection = this.tblLocalChanges.getSelectionModel();
353
        return new Iterator<Long>() {
354
            long n = selection.getMinSelectionIndex();
355
            @Override
356
            public boolean hasNext() {
357
                while (n <= selection.getMaxSelectionIndex()){
358
                    if(selection.isSelectedIndex((int) n)){
359
                       return true; 
360
                    }
361
                    n++;
362
                }
363
                return false;
364
            }
365

    
366
            @Override
367
            public Long next() {
368
                if(n > selection.getMaxSelectionIndex()){
369
                    throw new IllegalStateException();
370
                }
371
                return n++;
372
            }
373
        };
374
    }
375
    
376
    private void doToggleSelection() {
377
        Thread task = new Thread(() -> {
378
            try {
379
                context.processing = true;
380
                context.doUpdateComponents();
381
                Iterator<Long> rows = getSelectionIterator();
382

    
383
                this.changes.process(rows, (VCSGisWorkspaceChange change) -> {
384
                    if(change.getOperation() == OP_ADD_ENTITY) {
385
                        if(change.isSelected()){
386
                            entitiesToUnCheckAll.add(change.getEntityCode());
387
                        }
388
                    }
389
                    change.setSelected(!change.isSelected());
390
                    return true;
391
                });
392
                if(!entitiesToUnCheckAll.isEmpty()){
393
                    doUnCheckAllByEntities();
394
                }
395
            } finally {
396
                doUpdateEntitiesWithSelectedChanges();
397
                doUpdateIsSelectionFromEntitiesEmpty();
398
                context.processing = false;
399
                context.doUpdateComponents();
400
            }
401
        }, "VCSGisToggleSelectionLocalChanges");
402
        task.start();
403
    }
404
    
405
    private Iterator<Long> getRowsByEntitiesToUnCheckIterator() {
406

    
407
        long size = changes.size64();
408
        TableModel model = this.tblLocalChanges.getModel();
409
        return new Iterator<Long>() {
410
            long n = 0;
411
            @Override
412
            public boolean hasNext() {
413
                while (n < size){
414
                    VCSGisWorkspaceChange row = changes.get64(n);
415
                    if(entitiesToUnCheckAll.contains(row.getEntityCode())){
416
                       return true; 
417
                    }
418
                    n++;
419
                }
420
                return false;
421
            }
422

    
423
            @Override
424
            public Long next() {
425
                return n++;
426
            }
427
        };
428
    }
429

    
430
    
431
    private void doUnCheckAllByEntities(){
432
        if(dissabledUncheckAllByEntities){
433
            return;
434
        }
435
        Thread task = new Thread(() -> {
436
            try {
437
                context.processing = true;
438
                context.doUpdateComponents();
439
                if(!entitiesToUnCheckAll.isEmpty()) {
440
                    Iterator<Long> rows = getRowsByEntitiesToUnCheckIterator();
441
                    this.changes.process(rows, (VCSGisWorkspaceChange change) -> {
442
                         if(change.getOperation() != OP_ADD_ENTITY) {
443
                            change.setSelected(false);
444
                         }
445
                        return true;
446
                    });
447
                    entitiesToUnCheckAll.clear();
448
                }
449
            } finally {
450
                doUpdateEntitiesWithSelectedChanges();
451
                context.processing = false;
452
                context.doUpdateComponents();
453
            }
454
        }, "VCSGisUnCheckAllByEntitiesLocalChanges");
455
        task.start();
456
    }
457
    
458
    private Iterator<Long> getRowsIterator() {
459
        long size = changes.size64();
460
        return new Iterator<Long>() {
461
            long n = 0;
462
            @Override
463
            public boolean hasNext() {
464
                return (n < size);
465
            }
466

    
467
            @Override
468
            public Long next() {
469
                return n++;
470
            }
471
        };
472
    }
473

    
474

    
475
    private void doCheckAll() {
476
        Thread task = new Thread(() -> {
477
            try {
478
                context.processing = true;
479
                dissabledUncheckAllByEntities = true;
480
                context.doUpdateComponents();
481
                this.changes.setSelectionAll();
482
            } finally {
483
                context.processing = false;
484
                dissabledUncheckAllByEntities = false;
485
                context.doUpdateComponents();
486
            }
487
        }, "VCSGisCheckAllLocalChanges");
488
        task.start();
489
    }
490

    
491
    private void doUnCheckAll() {
492
        Thread task = new Thread(() -> {
493
            try {
494
                context.processing = true;
495
                dissabledUncheckAllByEntities = true;
496
                context.doUpdateComponents();
497
                this.changes.clearSelection();
498
            } finally {
499
                context.processing = false;
500
                dissabledUncheckAllByEntities = false;
501
                context.doUpdateComponents();
502
            }
503
        }, "VCSGisUncheckAllLocalChanges");
504
        task.start();
505
    }
506
    
507
    public VCSGisWorkspace getWorkspace() {
508
        return this.context.getWorkspace();
509
    }
510
    
511
    /* friend */ void doUpdateComponents() {
512
        if (notInSwingThreadInvokeLater(() -> {
513
            doUpdateComponents();
514
        })) {
515
            return;
516
        }
517
        try {
518
            this.context.setVisibleStatus(context.processing);
519
            VCSGisWorkspace ws = getWorkspace();
520
            this.context.updateEntitiesFromRepository(false);
521
            this.entitySelector.setEnabled(!context.processing);
522
            this.effectiveDatePicker.setEnabled(!context.processing);
523
            this.workspacePicker.setEnabled(!context.processing);
524
            this.btnLocalCheckAll.setEnabled(!context.processing && changes != null && changes.size64() > 0);
525
            this.btnLocalUnCheckAll.setEnabled(!context.processing && changes != null && changes.size64() > 0);
526
            List<VCSGisEntity> checkedEntities = entitySelector.getCheckedEntities();
527
            this.btnLocalRefresh.setEnabled(!context.processing && changes != null && CollectionUtils.isNotEmpty(checkedEntities));
528
            this.btnLocalRevert.setEnabled(ws != null
529
                    && !context.processing
530
                    && changes != null
531
                    && changes.size64() > 0
532
                    && CollectionUtils.isNotEmpty(checkedEntities)
533
                    && entitiesWithSelectedChanges != null
534
                    && !entitiesWithSelectedChanges.isEmpty()
535
                    && !isSelectionFromEntitiesEmpty
536
            );
537
            this.btnLocalRevert.setVisible(true);
538
            this.btnLocalCommit.setEnabled(ws != null
539
                    && !context.processing
540
                    && changes != null
541
                    && !this.changes.isSelectionEmpty()
542
                    && CollectionUtils.isNotEmpty(checkedEntities)
543
                    && ws.canCommit(null, getEntityCodes(checkedEntities))
544
            );
545
            //Show form
546
            boolean enableShowForm = false;
547

    
548
            int row = this.tblLocalChanges.getSelectedRow();
549
            if (!context.processing && row >= 0) {
550
                VCSGisWorkspaceChange change = this.changes.get64(row);
551
                if (change.getOperation() != OP_ADD_ENTITY) {
552
                    enableShowForm = true;
553
                }
554
            }
555
            this.btnLocalShowForm.setEnabled(!context.processing && enableShowForm);
556

    
557
            this.tblLocalChanges.setEnabled(!context.processing);
558
            this.txtLocalComment.setEnabled(!context.processing);
559
            this.effectiveDatePicker.setEnabled(!context.processing);
560
            this.workspacePicker.setEnabled(!context.processing);
561

    
562
            Geometry geom = getGeometryOfSelectedChange();
563
            Geometry originalGeom = getOriginalGeometryOfSelectedChange();
564
            this.btnLocalCenter.setEnabled(!context.processing && (geom != null || originalGeom != null));
565
            this.btnLocalHighlight.setEnabled(!context.processing && (geom != null || originalGeom != null));
566
            this.btnLocalZoom.setEnabled(!context.processing && (geom != null || originalGeom != null));
567
            this.btnLocalCleanHighlighted.setEnabled(!context.processing);
568
        } catch (VCSGisRuntimeException e1) {
569
            LOGGER.warn("Can't updating components.", e1);
570
            if (showAuthenticationErrors("_VCS_Changes", e1)) {
571
                this.workspacePicker.set(null);
572
                doChangeWorkspace();
573
            }
574
        } catch (Exception e2) {
575
            LOGGER.warn("Can't updating components.", e2);
576
        }
577

    
578
    }
579

    
580
    public void doReloadTables() {
581
        this.entitySelector.reloadEntities();
582
    }
583

    
584
    public void doUpdateEntitiesWithSelectedChanges() {
585
        VCSGisWorkspace ws = this.getWorkspace();
586
        if (ws != null) {
587
            entitiesWithSelectedChanges = ws.getEntitiesWithSelectedChanges();
588
        }
589
    }
590

    
591
    private void doUpdateIsSelectionFromEntitiesEmpty() {
592
        VCSGisWorkspace ws = this.getWorkspace();
593
        if (ws != null) {
594
            List<VCSGisEntity> checkedEntities = entitySelector.getCheckedEntities();
595
            this.isSelectionFromEntitiesEmpty = changes.isSelectionFromEntitiesEmpty(checkedEntities);
596
        }
597
    }
598

    
599
    
600
    public void doUpdateTableLocalChanges() {
601
        if( context.processing ) {
602
            return;
603
        }
604
        I18nManager i18n = ToolsLocator.getI18nManager();
605
        VCSGisWorkspace ws = this.getWorkspace();
606
        
607
        if(ws != null){
608
            
609
            doUpdateEntitiesWithSelectedChanges();
610

    
611
            List<VCSGisEntity> checkedEntities = entitySelector.getCheckedEntities();
612
            if(CollectionUtils.isEmpty(checkedEntities)) {
613
                localChangesTableModel = new ChangesTableModel(ws);
614
                this.tblLocalChanges.setModel(localChangesTableModel);
615
                return;
616
            }
617
            
618
            changes = ws.getLocalChanges(checkedEntities);
619

    
620
            localChangesTableModel = new ChangesTableModel(changes, ws);
621
            this.tblLocalChanges.setModel(localChangesTableModel);
622

    
623
            int columns = this.tblLocalChanges.getColumnModel().getColumnCount();
624
            for (int i = 1; i < columns; i++) {
625
                this.tblLocalChanges.getColumnModel().getColumn(i).setCellRenderer(new DefaultTableCellRenderer() {
626
                    @Override
627
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
628
                        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
629
                        VCSGisChange changeRow = changes.get64(row);
630
                        VCSGisSwingCommons.setColorCompoment(c, changeRow.getStatus());
631
                        return c;
632
                    }
633
                });
634
            }
635
            this.tableModelListener = null;
636
            localChangesTableModel.addTableModelListener(getTableModelListener());
637
            context.message(i18n.getTranslation(
638
                    "_Workspace_changes_list_updated",
639
                    new String[]{String.valueOf(changes.size64())}
640
            )
641
            );
642
            this.lblLocalChangesCount.setText(String.valueOf(changes.size64()));
643
        }
644
        doUpdateIsSelectionFromEntitiesEmpty();
645
        context.doUpdateComponents();
646
    }
647
    
648
    private TableModelListener getTableModelListener() {
649
        if(tableModelListener == null){
650
            tableModelListener =  (TableModelEvent e) -> {
651
                if (e.getType() == TableModelEvent.UPDATE) {
652
                    TableModel model = ((TableModel) (e.getSource()));
653
                    VCSGisWorkspaceChange change = changes.get64(e.getFirstRow());
654
                    if(change.getOperation() == OP_ADD_ENTITY) {
655
                        if(!change.isSelected()) {
656
                            if (entitiesToUnCheckAll.add(change.getEntityCode())) {
657
                                doUnCheckAllByEntities();
658
                            }
659
                        }
660
                    }
661
                }
662
            };
663
        }
664
        return tableModelListener;
665
    }
666
    
667
    private void doShowForm() {
668

    
669
        int row = this.tblLocalChanges.getSelectedRow();
670
        VCSGisWorkspaceChange change = this.changes.get64(row);
671
        if(change.getOperation() == OP_DELETE || change.getOperation() == OP_ADD_ENTITY){
672
            return;
673
        }
674
        Feature feature = change.getRelatedFeature();
675
        if(feature == null){
676
            return;
677
        }
678
        try {
679
            DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
680
            JFeatureForm form;
681
            form = dataSwingManager.createJFeatureForm(feature);
682
            WindowManager winManager = ToolsSwingLocator.getWindowManager();
683
            form.asJComponent().setPreferredSize(new Dimension(400,200));
684
            winManager.showWindow(form.asJComponent(), "Local change: "+change.getLabel(), WindowManager.MODE.WINDOW);
685
        } catch (CreateJFeatureFormException ex) {
686
            LOGGER.warn("Can't show form", ex);
687
        }
688

    
689
    }
690
    
691
    private void doCommit() {
692
        I18nManager i18n = ToolsLocator.getI18nManager();
693
        ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
694
        
695
        VCSGisWorkspace ws = getWorkspace();
696
        if( ws==null ) {
697
            // TODO: Mensaje al usuario ??
698
            return;
699
        }
700
        List<VCSGisEntity> entities = entitySelector.getCheckedEntities();
701
        List<String> entityCodes = getEntityCodes(entities);
702
        if( CollectionUtils.isEmpty(entities)){
703
            // TODO: Mensaje al usuario ??
704
            return;
705
        }
706
        this.context.updateEntitiesFromRepository(false);
707
        MutableObject<String> message = new MutableObject<>();
708
        if( !ws.canCommit(message, entityCodes) ) {
709
            dialogs.messageDialog(
710
                    i18n.getTranslation("_Cant_commit") + "\n" + message.getValue(),
711
                    i18n.getTranslation("_Commit"), 
712
                    JOptionPane.WARNING_MESSAGE
713
            );
714
            return;
715
        }
716
        Timestamp effectiveDate = new Timestamp(effectiveDatePicker.get().getTime());
717
        this.tblLocalChanges.setModel(new ChangesTableModel(ws));
718
        
719
        Thread task = new Thread(() -> {
720
            try {
721
                context.processing = true;
722
                context.doUpdateComponents();
723
                if( !executeTopologyPlans(ws, entities) ) {
724
                    dialogs.messageDialog(
725
                            i18n.getTranslation("_Cant_pass_topology_plans"),
726
                            i18n.getTranslation("_Commit"), 
727
                            JOptionPane.WARNING_MESSAGE
728
                    );
729
                    return;
730
                }
731
                int r = ws.commit(
732
                        entityCodes,
733
                        null,
734
                        effectiveDate, 
735
                        this.txtLocalComment.getText(), 
736
                        this.context.getTaskStatusController().getSimpleTaskStatus()
737
                );
738
                doPostCommit(r);
739
            } finally {
740
                context.processing = false;
741
                context.doUpdateComponents();
742
            }
743
        }, "VCSGisCommit");
744

    
745
        context.processing = true;
746
        context.doUpdateComponents();
747
        task.start();
748
    }
749

    
750
    private boolean executeTopologyPlans(VCSGisWorkspace ws, List<VCSGisEntity> entities) {
751
        boolean topologyRequired = false;
752
        for (VCSGisEntity entity : entities) {
753
            String x = ws.getTopologyPlan(entity.getTopologyPlanCode());
754
            if( StringUtils.isNotBlank(x) ) {
755
                topologyRequired= true;
756
                break;
757
            }
758
        }
759
        if (!topologyRequired){
760
            return true;
761
        }
762
        try {
763
            TopologyPlansExecutor executor = new TopologyPlansExecutor();
764
            return executor.execute(ws, entities);
765
        }catch(Exception ex){
766
            return false;
767
        }
768
    }
769
    
770
    private void doPostCommit(int commitStatus) {
771
        if (notInSwingThreadInvokeLater(() -> {doPostCommit(commitStatus);})) {
772
            return;
773
        }
774
        if( commitStatus == ERR_OK) {
775
            this.context.setVisibleStatus(false);
776
        } else {
777
            showAuthenticationErrors("_VCS_Commit", commitStatus);
778
        }
779
        context.updateLocalChangesTable();
780
        cleanHighligthed();
781
    }
782

    
783
    private void doRevert() {
784
        VCSGisWorkspace ws = getWorkspace();
785
        if( ws==null ) {
786
            // TODO: Mensaje al usuario
787
            return;
788
        }
789
        this.context.updateEntitiesFromRepository(false);
790
        
791
        Thread task = new Thread(() -> {
792
            try {
793
                context.processing = true;
794
                context.doUpdateComponents();
795
                List<VCSGisWorkspaceEntity> entities = ws.getEntitiesWithSelectedChanges();
796
                List<String> selectedEntityCodes = getEntityCodes(entitySelector.getCheckedEntities());
797
                int r = ERR_OK;
798
                if(!entities.isEmpty() && CollectionUtils.isNotEmpty(selectedEntityCodes)){
799
                    for (VCSGisWorkspaceEntity entity : entities) {
800
                        if(selectedEntityCodes.contains(entity.getEntityCode())){
801
                            r = ws.revert(entity.getEntityCode(), 
802
                                    this.context.getTaskStatusController().getSimpleTaskStatus()
803
                            );
804
                        }
805
                        if(r != ERR_OK){
806
                            break;
807
                        }
808
                    }
809
                }
810
                doPostRevert(r, entities);
811
            } finally {
812
                context.processing = false;
813
                context.doUpdateComponents();
814
            }
815
        }, "VCSGisRevert");
816

    
817
        context.processing = true;
818
        context.doUpdateComponents();
819
        task.start();
820
    }
821
    
822
    private void doPostRevert(int revertStatus, List<VCSGisWorkspaceEntity> entities) {
823
        if (notInSwingThreadInvokeLater(() -> {doPostRevert(revertStatus, entities);})) {
824
            return;
825
        }
826
        if( revertStatus == ERR_OK) {
827
            this.context.setVisibleStatus(false);
828
        }
829
        for (VCSGisWorkspaceEntity entity : entities) {
830
            FeatureStore store = getWorkspace().getFeatureStore(entity.getEntityName());
831
            refreshDocument(store);
832
        }
833
        context.updateLocalChangesTable();
834
        cleanHighligthed();
835

    
836
    }
837
    
838
    private Geometry getGeometryOfSelectedChange() {
839
        Geometry geom = null;
840
        int selected = this.tblLocalChanges.getSelectedRow();
841
        if(selected >= 0){
842
            VCSGisWorkspaceChange change = changes.get64(selected);
843
            try {
844
                Feature feature = change.getRelatedFeature();
845
                geom = feature.getDefaultGeometry();
846
            } catch (Exception ex){
847
                
848
            }
849
        }
850
        return geom;
851
    }
852

    
853
    private Geometry getOriginalGeometryOfSelectedChange() {
854
        Geometry geom = null;
855
        int selected = this.tblLocalChanges.getSelectedRow();
856
        if(selected >= 0){
857
            VCSGisWorkspaceChange change = changes.get64(selected);
858
            try {
859
                VCSGisWorkspace ws = getWorkspace();
860
                if(ws!=null) {
861
                    JsonObject json = change.getDataAsJson();
862
                    String entityCode = change.getEntityCode();
863
                    VCSGisEntity entity = ws.getEntity(entityCode);
864
                    FeatureStore store = ws.getFeatureStore(entity.getEntityName());
865
                    Feature feature = store.createNewFeature(json);
866
                    geom = feature.getDefaultGeometry();
867
                }
868
            } catch (Exception ex){
869
                
870
            }
871
        }
872
        return geom;
873
    }
874
    
875
    private FeatureStore getStoreOfSelectedChange() {
876
        FeatureStore store = null;
877
        int selected = this.tblLocalChanges.getSelectedRow();
878
        if(selected >= 0){
879
            VCSGisWorkspaceChange change = changes.get64(selected);
880
            try {
881
                VCSGisWorkspace ws = getWorkspace();
882
                if(ws!=null) {
883
                    String entityCode = change.getEntityCode();
884
                    VCSGisEntity entity = ws.getEntity(entityCode);
885
                    store = ws.getFeatureStore(entity.getEntityName());
886
                }
887
            } catch (Exception ex){
888
                
889
            }
890
        }
891
        return store;
892
    }
893
    
894
    private void doZoomToSelectedChange() {
895
        cleanHighligthed();
896
        VCSGisManager manager = VCSGisLocator.getVCSGisManager();
897
        VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager();
898
        VCSGisSwingServices services =swingManager.getDefaultServices();
899
        FeatureStore store = getStoreOfSelectedChange();
900
        Geometry geom = getGeometryOfSelectedChange();
901
        Geometry originalGeom = getOriginalGeometryOfSelectedChange();
902
        services.zoomViewsHavingAStoreToGeometry(store, VCSGisSwingCommons.createBBox(geom, originalGeom));
903
        if(geom != null){
904
            services.highlight(HIGHLIGHT_WORKSPACE, geom, store);
905
        }
906
        if(originalGeom != null){
907
            services.highlight(HIGHLIGHT_WORKSPACE_PREVIOUS, originalGeom, store);
908
        }
909
    }
910

    
911
    private void doCenterToSelectedChange() {
912
        cleanHighligthed();
913
        VCSGisManager manager = VCSGisLocator.getVCSGisManager();
914
        VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager();
915
        VCSGisSwingServices services =swingManager.getDefaultServices();
916
        FeatureStore store = getStoreOfSelectedChange();
917
        Geometry geom = getGeometryOfSelectedChange();
918
        Geometry originalGeom = getOriginalGeometryOfSelectedChange();
919
        services.centerViewsHavingAStoreToGeometry(store, VCSGisSwingCommons.createBBox(geom, originalGeom));
920
        if(geom != null){
921
            services.highlight(HIGHLIGHT_WORKSPACE, geom, store);
922
        }
923
        if(originalGeom != null){
924
            services.highlight(HIGHLIGHT_WORKSPACE_PREVIOUS, originalGeom, store);
925
        }
926

    
927
    }
928
    
929
    private void doHighLightSelectedChange() {
930
        cleanHighligthed();
931
        VCSGisManager manager = VCSGisLocator.getVCSGisManager();
932
        VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager();
933
        VCSGisSwingServices services =swingManager.getDefaultServices();
934
        FeatureStore store = getStoreOfSelectedChange();
935
        Geometry geom = getGeometryOfSelectedChange();
936
        Geometry originalGeom = getOriginalGeometryOfSelectedChange();
937
        if(geom != null){
938
            services.highlight(HIGHLIGHT_WORKSPACE, geom, store);
939
        }
940
        if(originalGeom != null){
941
            services.highlight(HIGHLIGHT_WORKSPACE_PREVIOUS, originalGeom, store);
942
        }
943

    
944
    }
945
    
946
    private List<String> getEntityCodes(List<VCSGisEntity> entities) {
947
        List<String> res = new ArrayList();
948
        for (VCSGisEntity entity : entities) {
949
            res.add(entity.getEntityCode());
950
        }
951
        return res;
952
    }
953
    
954
    public void selectOnlyAEntity(VCSGisEntity entity){
955
        entitySelector.clearChecks();
956
        entitySelector.check(entity);
957
    }
958

    
959
}