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 @ 3308

History | View | Annotate | Download (31.4 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.Date;
32
import java.util.HashMap;
33
import java.util.HashSet;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.List;
38
import java.util.Set;
39
import java.util.logging.Level;
40
import java.util.logging.Logger;
41
import javax.json.JsonObject;
42
import javax.swing.JButton;
43
import javax.swing.JLabel;
44
import javax.swing.JOptionPane;
45
import javax.swing.JTable;
46
import javax.swing.JTextField;
47
import javax.swing.ListSelectionModel;
48
import javax.swing.SwingUtilities;
49
import javax.swing.event.ChangeEvent;
50
import javax.swing.event.ListSelectionEvent;
51
import javax.swing.event.TableModelEvent;
52
import javax.swing.event.TableModelListener;
53
import javax.swing.table.DefaultTableCellRenderer;
54
import javax.swing.table.TableModel;
55
import org.apache.commons.lang3.StringUtils;
56
import org.apache.commons.lang3.mutable.MutableObject;
57
import org.gvsig.featureform.swing.CreateJFeatureFormException;
58
import org.gvsig.featureform.swing.JFeatureForm;
59
import org.gvsig.fmap.dal.feature.Feature;
60
import org.gvsig.fmap.dal.feature.FeatureStore;
61
import org.gvsig.fmap.dal.swing.DALSwingLocator;
62
import org.gvsig.fmap.dal.swing.DataSwingManager;
63
import org.gvsig.fmap.geom.Geometry;
64
import org.gvsig.tools.ToolsLocator;
65
import org.gvsig.tools.i18n.I18nManager;
66
import org.gvsig.tools.swing.api.ToolsSwingLocator;
67
import org.gvsig.tools.swing.api.ToolsSwingManager;
68
import org.gvsig.tools.swing.api.pickercontroller.DatePickerController;
69
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
70
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
71
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
72
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
73
import org.gvsig.topology.lib.api.TopologyLocator;
74
import org.gvsig.topology.lib.api.TopologyManager;
75
import org.gvsig.topology.lib.api.TopologyPlan;
76
import org.gvsig.topology.swing.api.JTopologyReport;
77
import org.gvsig.topology.swing.api.TopologySwingLocator;
78
import org.gvsig.topology.swing.api.TopologySwingManager;
79
import org.gvsig.vcsgis.lib.VCSGisChange;
80
import org.gvsig.vcsgis.lib.VCSGisEntity;
81
import org.gvsig.vcsgis.lib.VCSGisLocator;
82
import org.gvsig.vcsgis.lib.VCSGisManager;
83
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK;
84
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_ADD_ENTITY;
85
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE;
86
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
87
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChange;
88
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChanges;
89
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity;
90
import org.gvsig.vcsgis.swing.VCSGisSwingLocator;
91
import org.gvsig.vcsgis.swing.VCSGisSwingManager;
92
import org.gvsig.vcsgis.swing.VCSGisSwingServices;
93
import static org.gvsig.vcsgis.swing.VCSGisSwingServices.HIGHLIGHT_WORKSPACE;
94
import static org.gvsig.vcsgis.swing.VCSGisSwingServices.HIGHLIGHT_WORKSPACE_PREVIOUS;
95
import org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons;
96
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.cleanHighligthed;
97
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.notInSwingThreadInvokeLater;
98
import static org.gvsig.vcsgis.swing.impl.VCSGisSwingCommons.refreshDocument;
99

    
100
/**
101
 *
102
 * @author gvSIG Team
103
 */
104
public class LocalChangesController {
105

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

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

    
128
    private DatePickerController effectiveDatePicker;
129
    
130
    private final Set<String> entitiesToUnCheckAll;
131
    private TableModelListener tableModelListener;
132
    private boolean dissabledUncheckAllByEntities;
133
    private final JButton btnLocalCleanHighlighted;
134
    
135
    public LocalChangesController(
136
        VCSGisJChangesImpl context,
137
        PickerController<VCSGisWorkspace> workspacePicker,
138
        JTable tblLocalChanges,
139
        JButton btnLocalCheckAll,
140
        JButton btnLocalUnCheckAll,
141
        JButton btnLocalShowForm,
142
        JButton btnLocalRefresh,
143
        JButton btnLocalCommit,
144
        JButton btnLocalRevert,
145
        JLabel lblLocalEffectiveDate,
146
        JTextField txtLocalEffectiveDate,
147
        JTextField txtLocalComment,
148
        JButton btnLocalEffectiveDate,
149
        JLabel lblLocalComment,
150
        JButton btnLocalZoom,
151
        JButton btnLocalCenter,
152
        JButton btnLocalCleanHighlighted,
153
        JLabel lblLocalChangesCount
154
        
155
        ) {
156
        this.dissabledUncheckAllByEntities = false;
157

    
158
        this.context = context;
159
        this.workspacePicker = workspacePicker;
160
        this.tblLocalChanges = tblLocalChanges;
161
        this.btnLocalCheckAll = btnLocalCheckAll;
162
        this.btnLocalCommit = btnLocalCommit;
163
        this.btnLocalEffectiveDate = btnLocalEffectiveDate;
164
        this.btnLocalRefresh = btnLocalRefresh;
165
        this.btnLocalRevert = btnLocalRevert;
166
        this.btnLocalShowForm = btnLocalShowForm;
167
        this.btnLocalUnCheckAll = btnLocalUnCheckAll;
168
        this.lblLocalComment = lblLocalComment;
169
        this.lblLocalEffectiveDate = lblLocalEffectiveDate;
170
        this.txtLocalComment = txtLocalComment;
171
        this.txtLocalEffectiveDate = txtLocalEffectiveDate;
172
        this.btnLocalZoom = btnLocalZoom;
173
        this.btnLocalCenter = btnLocalCenter;
174
        this.btnLocalCleanHighlighted = btnLocalCleanHighlighted;
175
        this.lblLocalChangesCount= lblLocalChangesCount;
176

    
177
        this.entitiesToUnCheckAll = new HashSet<>();
178
                
179
        initComponents();
180
    }
181

    
182
    private void initComponents() {
183
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
184

    
185
        translate();
186

    
187
        this.btnLocalCheckAll.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
188
        this.btnLocalCommit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
189
        this.btnLocalEffectiveDate.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
190
        this.btnLocalRefresh.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
191
        this.btnLocalRevert.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
192
        this.btnLocalShowForm.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
193
        this.btnLocalUnCheckAll.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
194
        this.btnLocalZoom.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
195
        this.btnLocalCenter.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
196
        this.btnLocalCleanHighlighted.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
197
        
198
        this.workspacePicker.addChangeListener((ChangeEvent e) -> {
199
            doUpdateTableLocalChanges();
200
            doReloadRepositoryEntities();
201
        });
202
        
203
        this.tblLocalChanges.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
204

    
205
        this.tblLocalChanges.addKeyListener(new KeyAdapter() {
206
            @Override
207
            public void keyPressed(KeyEvent e) {
208
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
209
                    doToggleSelection();
210
                }
211
            }
212
        });
213

    
214
        this.tblLocalChanges.getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> {
215
            context.doUpdateComponents();
216
        });
217

    
218
        this.btnLocalCheckAll.addActionListener((ActionEvent e) -> {
219
            if (changes != null) {
220
                doCheckAll();
221
                doUpdateTableLocalChanges();
222
            }
223
        });
224

    
225
        this.btnLocalUnCheckAll.addActionListener((ActionEvent e) -> {
226
            if (changes != null) {
227
                doUnCheckAll();
228
                doUpdateTableLocalChanges();
229
            }
230
        });
231

    
232
        this.btnLocalShowForm.addActionListener((ActionEvent e) -> {
233
            doShowForm();
234
        });
235

    
236
        this.btnLocalCommit.addActionListener((ActionEvent e) -> {
237
            doCommit();
238
        });
239

    
240
        this.btnLocalRevert.addActionListener((ActionEvent e) -> {
241
            doRevert();
242
        });
243

    
244
        this.effectiveDatePicker = toolsSwingManager.createDatePickerController(
245
                this.txtLocalEffectiveDate,
246
                this.btnLocalEffectiveDate
247
        );
248

    
249
        this.effectiveDatePicker.set(new Date());
250
        
251
        this.btnLocalRefresh.addActionListener((ActionEvent e) -> {
252
            doUpdateTableLocalChanges();
253
        });
254

    
255
        this.btnLocalZoom.addActionListener((ActionEvent e) -> {
256
            doZoomToSelectedChange();
257
        });
258

    
259
        this.btnLocalCenter.addActionListener((ActionEvent e) -> {
260
            doCenterToSelectedChange();
261
        });
262

    
263
        this.btnLocalCleanHighlighted.addActionListener((ActionEvent e) -> {
264
            cleanHighligthed();
265
        });
266

    
267
        toolsSwingManager.addClearButton(this.txtLocalComment);
268
        toolsSwingManager.setDefaultPopupMenu(txtLocalComment);
269

    
270
        context.doUpdateComponents();
271
    }
272

    
273
    private void translate() {
274
        ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager();
275

    
276
        swingManager.translate(this.btnLocalCheckAll);
277
        swingManager.translate(this.btnLocalCommit);
278
        swingManager.translate(this.btnLocalRefresh);
279
        swingManager.translate(this.btnLocalRevert);
280
        swingManager.translate(this.btnLocalShowForm);
281
        swingManager.translate(this.btnLocalUnCheckAll);
282

    
283
        swingManager.translate(this.lblLocalEffectiveDate);
284
        swingManager.translate(this.lblLocalComment);
285

    
286
        swingManager.translate(this.btnLocalCenter);
287
        swingManager.translate(this.btnLocalZoom);
288
        swingManager.translate(this.btnLocalCleanHighlighted);
289
    }
290

    
291
    private Iterator<Long> getSelectionIterator() {
292
        ListSelectionModel selection = this.tblLocalChanges.getSelectionModel();
293
        return new Iterator<Long>() {
294
            long n = selection.getMinSelectionIndex();
295
            @Override
296
            public boolean hasNext() {
297
                while (n <= selection.getMaxSelectionIndex()){
298
                    if(selection.isSelectedIndex((int) n)){
299
                       return true; 
300
                    }
301
                    n++;
302
                }
303
                return false;
304
            }
305

    
306
            @Override
307
            public Long next() {
308
                if(n > selection.getMaxSelectionIndex()){
309
                    throw new IllegalStateException();
310
                }
311
                return n++;
312
            }
313
        };
314
    }
315
    
316
    private void doToggleSelection() {
317
        Thread task = new Thread(() -> {
318
            try {
319
                context.processing = true;
320
                context.doUpdateComponents();
321
                Iterator<Long> rows = getSelectionIterator();
322

    
323
                this.changes.process(rows, (VCSGisWorkspaceChange change) -> {
324
                    if(change.getOperation() == OP_ADD_ENTITY) {
325
                        if(change.isSelected()){
326
                            entitiesToUnCheckAll.add(change.getEntityCode());
327
                        }
328
                    }
329
                    change.setSelected(!change.isSelected());
330
                    return true;
331
                });
332
                if(!entitiesToUnCheckAll.isEmpty()){
333
                    doUnCheckAllByEntities();
334
                }
335
            } finally {
336
                context.processing = false;
337
                context.doUpdateComponents();
338
            }
339
        }, "VCSGisToggleSelectionLocalChanges");
340
        task.start();
341
    }
342
    
343
    private Iterator<Long> getRowsByEntitiesToUnCheckIterator() {
344

    
345
        long size = changes.size64();
346
        TableModel model = this.tblLocalChanges.getModel();
347
        return new Iterator<Long>() {
348
            long n = 0;
349
            @Override
350
            public boolean hasNext() {
351
                while (n < size){
352
                    VCSGisWorkspaceChange row = changes.get64(n);
353
                    if(entitiesToUnCheckAll.contains(row.getEntityCode())){
354
                       return true; 
355
                    }
356
                    n++;
357
                }
358
                return false;
359
            }
360

    
361
            @Override
362
            public Long next() {
363
                return n++;
364
            }
365
        };
366
    }
367

    
368
    
369
    private void doUnCheckAllByEntities(){
370
        if(dissabledUncheckAllByEntities){
371
            return;
372
        }
373
        Thread task = new Thread(() -> {
374
            try {
375
                context.processing = true;
376
                context.doUpdateComponents();
377
                if(!entitiesToUnCheckAll.isEmpty()) {
378
                    Iterator<Long> rows = getRowsByEntitiesToUnCheckIterator();
379
                    this.changes.process(rows, (VCSGisWorkspaceChange change) -> {
380
                         if(change.getOperation() != OP_ADD_ENTITY) {
381
                            change.setSelected(false);
382
                         }
383
                        return true;
384
                    });
385
                    entitiesToUnCheckAll.clear();
386
                }
387
            } finally {
388
                context.processing = false;
389
                context.doUpdateComponents();
390
            }
391
        }, "VCSGisUnCheckAllByEntitiesLocalChanges");
392
        task.start();
393
    }
394
    
395
    private Iterator<Long> getRowsIterator() {
396
        long size = changes.size64();
397
        return new Iterator<Long>() {
398
            long n = 0;
399
            @Override
400
            public boolean hasNext() {
401
                return (n < size);
402
            }
403

    
404
            @Override
405
            public Long next() {
406
                return n++;
407
            }
408
        };
409
    }
410

    
411

    
412
    private void doCheckAll() {
413
        Thread task = new Thread(() -> {
414
            try {
415
                context.processing = true;
416
                dissabledUncheckAllByEntities = true;
417
                context.doUpdateComponents();
418
                this.changes.setSelectionAll();
419
            } finally {
420
                context.processing = false;
421
                dissabledUncheckAllByEntities = false;
422
                context.doUpdateComponents();
423
            }
424
        }, "VCSGisCheckAllLocalChanges");
425
        task.start();
426
    }
427

    
428
    private void doUnCheckAll() {
429
        Thread task = new Thread(() -> {
430
            try {
431
                context.processing = true;
432
                dissabledUncheckAllByEntities = true;
433
                context.doUpdateComponents();
434
                this.changes.clearSelection();
435
            } finally {
436
                context.processing = false;
437
                dissabledUncheckAllByEntities = false;
438
                context.doUpdateComponents();
439
            }
440
        }, "VCSGisUncheckAllLocalChanges");
441
        task.start();
442
    }
443
    
444
    public VCSGisWorkspace getWorkspace() {
445
        return this.context.getWorkspace();
446
    }
447
    
448
    /* friend */void doUpdateComponents() {
449
        if (notInSwingThreadInvokeLater(() -> {doUpdateComponents();})) {
450
            return;
451
        }
452
        this.context.setVisibleStatus(context.processing);
453
        VCSGisWorkspace ws = getWorkspace();
454
        this.context.updateEntitiesFromRepository(false);
455
        this.effectiveDatePicker.setEnabled(!context.processing);
456
        this.workspacePicker.setEnabled(!context.processing);
457
        this.btnLocalCheckAll.setEnabled(!context.processing && changes!=null && changes.size64()>0);
458
        this.btnLocalUnCheckAll.setEnabled(!context.processing && changes!=null && changes.size64()>0);
459
        this.btnLocalRefresh.setEnabled(!context.processing && changes!=null);
460
        this.btnLocalRevert.setEnabled(ws!=null && 
461
                        !context.processing && 
462
                        changes!=null && 
463
                        changes.size64()>0 && 
464
                        entitiesWithChanges != null &&
465
                        !entitiesWithChanges.isEmpty()
466
        );      
467
        this.btnLocalRevert.setVisible(true);      
468
        this.btnLocalCommit.setEnabled(ws!=null && 
469
                !context.processing && 
470
                changes!=null && 
471
                !this.changes.isSelectionEmpty() && 
472
                ws.canCommit()
473
        );
474
        //Show form
475
        boolean enableShowForm = false;
476

    
477
        int row = this.tblLocalChanges.getSelectedRow();
478
        if(!context.processing && row>=0){
479
            VCSGisWorkspaceChange change = this.changes.get64(row);
480
            if(change.getOperation() != OP_ADD_ENTITY){
481
                enableShowForm = true;
482
            }
483
        }
484
        this.btnLocalShowForm.setEnabled(!context.processing && enableShowForm);
485
        
486
        this.tblLocalChanges.setEnabled(!context.processing);
487
        this.txtLocalComment.setEnabled(!context.processing);
488
        this.effectiveDatePicker.setEnabled(!context.processing);
489
        this.workspacePicker.setEnabled(!context.processing);
490

    
491
        Geometry geom = getGeometryOfSelectedChange();
492
        Geometry originalGeom = getOriginalGeometryOfSelectedChange();
493
        this.btnLocalCenter.setEnabled(!context.processing && (geom!=null || originalGeom!=null));
494
        this.btnLocalZoom.setEnabled(!context.processing && (geom!=null || originalGeom!=null));
495
        this.btnLocalCleanHighlighted.setEnabled(!context.processing);
496

    
497
    }
498
    
499
    public void doUpdateTableLocalChanges() {
500
        if( context.processing ) {
501
            return;
502
        }
503
        I18nManager i18n = ToolsLocator.getI18nManager();
504
        VCSGisWorkspace ws = this.getWorkspace();
505
        
506
        if(ws != null){
507
            
508
            entitiesWithChanges = ws.getEntitiesWithChanges();
509
            
510
            changes = ws.getLocalChanges();
511
            localChangesTableModel = new ChangesTableModel(changes, ws);
512
            this.tblLocalChanges.setModel(localChangesTableModel);
513

    
514
            int columns = this.tblLocalChanges.getColumnModel().getColumnCount();
515
            for (int i = 1; i < columns; i++) {
516
                this.tblLocalChanges.getColumnModel().getColumn(i).setCellRenderer(new DefaultTableCellRenderer() {
517
                    @Override
518
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
519
                        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
520
                        VCSGisChange changeRow = changes.get64(row);
521
                        VCSGisSwingCommons.setColorCompoment(c, changeRow.getStatus());
522
                        return c;
523
                    }
524
                });
525
            }
526

    
527
            this.tableModelListener = null;
528
            localChangesTableModel.addTableModelListener(getTableModelListener());
529
            context.message(i18n.getTranslation(
530
                    "_Workspace_changes_list_updated", 
531
                    new String[] { String.valueOf(changes.size64()) }
532
                )
533
            );
534
            this.lblLocalChangesCount.setText(String.valueOf(changes.size64()));
535
        }
536
        context.doUpdateComponents();
537
    }
538
    
539
    private void doReloadRepositoryEntities() {
540
        VCSGisWorkspace ws = this.getWorkspace();
541
        if (ws != null) {
542
            Thread task = new Thread(() -> {
543
                try {
544
                    context.processing = true;
545
                    context.doUpdateComponents();
546
                    ws.reloadRepositoryEntities(null);
547
                } finally {
548
                    context.processing = false;
549
                    context.doUpdateComponents();
550
                }
551
            }, "VCSGisReloadRepositoryEntities");
552

    
553
            context.processing = true;
554
            context.doUpdateComponents();
555
            task.start();
556
        }
557
    }
558

    
559
    private TableModelListener getTableModelListener() {
560
        if(tableModelListener == null){
561
            tableModelListener =  (TableModelEvent e) -> {
562
                if (e.getType() == TableModelEvent.UPDATE) {
563
                    TableModel model = ((TableModel) (e.getSource()));
564
                    VCSGisWorkspaceChange change = changes.get64(e.getFirstRow());
565
                    if(change.getOperation() == OP_ADD_ENTITY) {
566
                        if(!change.isSelected()) {
567
                            if (entitiesToUnCheckAll.add(change.getEntityCode())) {
568
                                doUnCheckAllByEntities();
569
                            }
570
                        }
571
                    }
572
                }
573
            };
574
        }
575
        return tableModelListener;
576
    }
577
    
578
    private void doShowForm() {
579

    
580
        int row = this.tblLocalChanges.getSelectedRow();
581
        VCSGisWorkspaceChange change = this.changes.get64(row);
582
        if(change.getOperation() == OP_DELETE || change.getOperation() == OP_ADD_ENTITY){
583
            return;
584
        }
585
        Feature feature = change.getRelatedFeature();
586
        if(feature == null){
587
            return;
588
        }
589
        try {
590
            DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
591
            JFeatureForm form;
592
            form = dataSwingManager.createJFeatureForm(feature);
593
            WindowManager winManager = ToolsSwingLocator.getWindowManager();
594
            form.asJComponent().setPreferredSize(new Dimension(400,200));
595
            winManager.showWindow(form.asJComponent(), "Local change: "+change.getLabel(), WindowManager.MODE.WINDOW);
596
        } catch (CreateJFeatureFormException ex) {
597
            Logger.getLogger(VCSGisJChangesImpl.class.getName()).log(Level.SEVERE, null, ex);
598
        }
599

    
600
    }
601
    
602
    private void doCommit() {
603
        I18nManager i18n = ToolsLocator.getI18nManager();
604
        ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
605
        
606
        VCSGisWorkspace ws = getWorkspace();
607
        if( ws==null ) {
608
            // TODO: Mensaje al usuario
609
            return;
610
        }
611
        this.context.updateEntitiesFromRepository(false);
612
        MutableObject<String> message = new MutableObject<>();
613
        if( !ws.canCommit(message) ) {
614
            dialogs.messageDialog(
615
                    i18n.getTranslation("_Cant_commit") + "\n" + message.getValue(),
616
                    i18n.getTranslation("_Commit"), 
617
                    JOptionPane.WARNING_MESSAGE
618
            );
619
            return;
620
        }
621
        Timestamp effectiveDate = new Timestamp(effectiveDatePicker.get().getTime());
622
        this.tblLocalChanges.setModel(new ChangesTableModel(ws));
623
        
624
        List<VCSGisEntity> entities = ws.getEntitiesOfLocalChanges();
625
        Thread task = new Thread(() -> {
626
            try {
627
                context.processing = true;
628
                context.doUpdateComponents();
629
                if( !executeTopologyPlans(ws, entities) ) {
630
                    dialogs.messageDialog(
631
                            i18n.getTranslation("_Cant_pass_topology_plans"),
632
                            i18n.getTranslation("_Commit"), 
633
                            JOptionPane.WARNING_MESSAGE
634
                    );
635
                    return;
636
                }
637
                int r = ws.commit(
638
                        effectiveDate, 
639
                        this.txtLocalComment.getText(), 
640
                        this.context.getTaskStatusController().getSimpleTaskStatus()
641
                );
642
                doPostCommit(r);
643
            } finally {
644
                context.processing = false;
645
                context.doUpdateComponents();
646
            }
647
        }, "VCSGisCommit");
648

    
649
        context.processing = true;
650
        context.doUpdateComponents();
651
        task.start();
652
    }
653

    
654
    private boolean executeTopologyPlans(VCSGisWorkspace ws, List<VCSGisEntity> entities) {
655
        final I18nManager i18n = ToolsLocator.getI18nManager();
656
        final TopologyManager topologyManager = TopologyLocator.getTopologyManager();
657
        final TopologySwingManager swingManager = TopologySwingLocator.getTopologySwingManager();
658
        final WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
659
        
660
        Map<String, TopologyPlan> topologyPlans = new HashMap<>();
661
        for (VCSGisEntity entity : entities) {
662
            String x = ws.getTopologyPlan(entity.getTopologyPlanCode());
663
            if( StringUtils.isBlank(x) ) {
664
                continue;
665
            }
666
            TopologyPlan plan = topologyManager.createTopologyPlan();
667
            plan.fromJSON(x);
668
            if( plan.hasRules() ) {
669
                topologyPlans.put(plan.getName(), plan);
670
            }
671
        }
672
        if( topologyPlans.isEmpty() ) {
673
            return true;
674
        }
675
        for (TopologyPlan plan : topologyPlans.values()) {
676
            JTopologyReport panel = swingManager.createJTopologyReport(plan);
677
            panel.put(plan);
678
            SwingUtilities.invokeLater(() -> {
679
                winManager.showWindow(
680
                        panel.asJComponent(),
681
                        i18n.getTranslation("_Topology_plan_error_inspector") + " ("+plan.getName()+")",
682
                        WindowManager.MODE.TOOL
683
                );
684
            });
685
            try {
686
                plan.execute();
687
                if( plan.getReport().size()>0 ) {
688
                    return false;
689
                }
690
            } catch(Exception ex) {
691
                return false;
692
            }
693
        }
694
        return true;
695
    }
696
    
697
    private void doPostCommit(int commitStatus) {
698
        if (notInSwingThreadInvokeLater(() -> {doPostCommit(commitStatus);})) {
699
            return;
700
        }
701
        if( commitStatus ==ERR_OK) {
702
            this.context.setVisibleStatus(false);
703
        }
704
        context.updateLocalChangesTable();
705
    }
706

    
707
    private void doRevert() {
708
        VCSGisWorkspace ws = getWorkspace();
709
        if( ws==null ) {
710
            // TODO: Mensaje al usuario
711
            return;
712
        }
713
        this.context.updateEntitiesFromRepository(false);
714
        
715
        Thread task = new Thread(() -> {
716
            try {
717
                context.processing = true;
718
                context.doUpdateComponents();
719
                List<VCSGisWorkspaceEntity> entities = ws.getEntitiesWithChanges();
720
                int r = ERR_OK;
721
                if(!entities.isEmpty()){
722
                    for (VCSGisWorkspaceEntity entity : entities) {
723
                        r = ws.revert(entity.getEntityCode(), 
724
                                this.context.getTaskStatusController().getSimpleTaskStatus()
725
                        );
726
                        if(r != ERR_OK){
727
                            break;
728
                        }
729
                    }
730
                }
731
                doPostRevert(r, entities);
732
            } finally {
733
                context.processing = false;
734
                context.doUpdateComponents();
735
            }
736
        }, "VCSGisRevert");
737

    
738
        context.processing = true;
739
        context.doUpdateComponents();
740
        task.start();
741
    }
742
    
743
    private void doPostRevert(int revertStatus, List<VCSGisWorkspaceEntity> entities) {
744
        if (notInSwingThreadInvokeLater(() -> {doPostRevert(revertStatus, entities);})) {
745
            return;
746
        }
747
        if( revertStatus == ERR_OK) {
748
            this.context.setVisibleStatus(false);
749
        }
750
        for (VCSGisWorkspaceEntity entity : entities) {
751
            FeatureStore store = getWorkspace().getFeatureStore(entity.getEntityName());
752
            refreshDocument(store);
753
        }
754
        context.updateLocalChangesTable();
755
    }
756
    
757
    private Geometry getGeometryOfSelectedChange() {
758
        Geometry geom = null;
759
        int selected = this.tblLocalChanges.getSelectedRow();
760
        if(selected >= 0){
761
            VCSGisWorkspaceChange change = changes.get64(selected);
762
            try {
763
                Feature feature = change.getRelatedFeature();
764
                geom = feature.getDefaultGeometry();
765
            } catch (Exception ex){
766
                
767
            }
768
        }
769
        return geom;
770
    }
771

    
772
    private Geometry getOriginalGeometryOfSelectedChange() {
773
        Geometry geom = null;
774
        int selected = this.tblLocalChanges.getSelectedRow();
775
        if(selected >= 0){
776
            VCSGisWorkspaceChange change = changes.get64(selected);
777
            try {
778
                VCSGisWorkspace ws = getWorkspace();
779
                if(ws!=null) {
780
                    JsonObject json = change.getDataAsJson();
781
                    String entityCode = change.getEntityCode();
782
                    VCSGisEntity entity = ws.getEntity(entityCode);
783
                    FeatureStore store = ws.getFeatureStore(entity.getEntityName());
784
                    Feature feature = store.createNewFeature(json);
785
                    geom = feature.getDefaultGeometry();
786
                }
787
            } catch (Exception ex){
788
                
789
            }
790
        }
791
        return geom;
792
    }
793
    
794
    private void doZoomToSelectedChange() {
795
        cleanHighligthed();
796
        VCSGisManager manager = VCSGisLocator.getVCSGisManager();
797
        VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager();
798
        VCSGisSwingServices services =swingManager.getDefaultServices();
799
        Geometry geom = getGeometryOfSelectedChange();
800
        Geometry originalGeom = getOriginalGeometryOfSelectedChange();
801
        services.zoomActiveViewToGeometry(VCSGisSwingCommons.createBBox(geom, originalGeom));
802
        if(geom != null){
803
            services.highlight(HIGHLIGHT_WORKSPACE, geom);
804
        }
805
        if(originalGeom != null){
806
            services.highlight(HIGHLIGHT_WORKSPACE_PREVIOUS, originalGeom);
807
        }
808
    }
809

    
810
    private void doCenterToSelectedChange() {
811
        cleanHighligthed();
812
        VCSGisManager manager = VCSGisLocator.getVCSGisManager();
813
        VCSGisSwingManager swingManager = VCSGisSwingLocator.getVCSGisSwingManager();
814
        VCSGisSwingServices services =swingManager.getDefaultServices();
815
        Geometry geom = getGeometryOfSelectedChange();
816
        Geometry originalGeom = getOriginalGeometryOfSelectedChange();
817
        services.centerActiveViewToGeometry(VCSGisSwingCommons.createBBox(geom, originalGeom));
818
        if(geom != null){
819
            services.highlight(HIGHLIGHT_WORKSPACE, geom);
820
        }
821
        if(originalGeom != null){
822
            services.highlight(HIGHLIGHT_WORKSPACE_PREVIOUS, originalGeom);
823
        }
824

    
825
    }
826
}