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

History | View | Annotate | Download (14.7 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.Dimension;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.awt.event.KeyAdapter;
28
import java.awt.event.KeyEvent;
29
import java.sql.Timestamp;
30
import java.util.Date;
31
import java.util.HashSet;
32
import java.util.Iterator;
33
import java.util.Set;
34
import java.util.logging.Level;
35
import java.util.logging.Logger;
36
import javax.swing.JButton;
37
import javax.swing.JLabel;
38
import javax.swing.JTable;
39
import javax.swing.JTextField;
40
import javax.swing.ListSelectionModel;
41
import javax.swing.event.ChangeEvent;
42
import javax.swing.event.ListSelectionEvent;
43
import javax.swing.event.TableModelEvent;
44
import javax.swing.event.TableModelListener;
45
import javax.swing.table.TableModel;
46
import org.gvsig.featureform.swing.CreateJFeatureFormException;
47
import org.gvsig.featureform.swing.JFeatureForm;
48
import org.gvsig.fmap.dal.feature.Feature;
49
import org.gvsig.fmap.dal.swing.DALSwingLocator;
50
import org.gvsig.fmap.dal.swing.DataSwingManager;
51
import org.gvsig.tools.swing.api.ToolsSwingLocator;
52
import org.gvsig.tools.swing.api.ToolsSwingManager;
53
import org.gvsig.tools.swing.api.pickercontroller.DatePickerController;
54
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
55
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
56
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_ADD_ENTITY;
57
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE;
58
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
59
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChange;
60
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChange;
61
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChanges;
62

    
63
/**
64
 *
65
 * @author gvSIG Team
66
 */
67
public class LocalChangesController {
68

    
69
    private final JTable tblLocalChanges;
70
    private final JButton btnLocalCheckAll;
71
    private final JButton btnLocalUnCheckAll;
72
    private final JButton btnLocalShowForm;
73
    private final JButton btnLocalRefresh;
74
    private final JButton btnLocalCommit;
75
    private final JButton btnLocalRevert;
76
    private final JLabel lblLocalEffectiveDate;
77
    private final JTextField txtLocalEffectiveDate;
78
    private final JTextField txtLocalComment;
79
    private final JButton btnLocalEffectiveDate;
80
    private final JLabel lblLocalComment;
81
    private final PickerController<VCSGisWorkspace> workspacePicker;    
82

    
83
    private VCSGisWorkspaceChanges<VCSGisWorkspaceChange> changes;
84
    private ChangesTableModel localChangesTableModel;
85
    
86
    private DatePickerController effectiveDatePicker;
87
    
88
    private final Set<String> entitiesToUnCheckAll;
89
    private TableModelListener tableModelListener;
90
    
91
    public LocalChangesController(
92
        PickerController<VCSGisWorkspace> workspacePicker,
93
        JTable tblLocalChanges,
94
        JButton btnLocalCheckAll,
95
        JButton btnLocalUnCheckAll,
96
        JButton btnLocalShowForm,
97
        JButton btnLocalRefresh,
98
        JButton btnLocalCommit,
99
        JButton btnLocalRevert,
100
        JLabel lblLocalEffectiveDate,
101
        JTextField txtLocalEffectiveDate,
102
        JTextField txtLocalComment,
103
        JButton btnLocalEffectiveDate,
104
        JLabel lblLocalComment
105
        ) {
106
        this.workspacePicker = workspacePicker;
107
        this.tblLocalChanges = tblLocalChanges;
108
        this.btnLocalCheckAll = btnLocalCheckAll;
109
        this.btnLocalCommit = btnLocalCommit;
110
        this.btnLocalEffectiveDate = btnLocalEffectiveDate;
111
        this.btnLocalRefresh = btnLocalRefresh;
112
        this.btnLocalRevert = btnLocalRevert;
113
        this.btnLocalShowForm = btnLocalShowForm;
114
        this.btnLocalUnCheckAll = btnLocalUnCheckAll;
115
        this.lblLocalComment = lblLocalComment;
116
        this.lblLocalEffectiveDate = lblLocalEffectiveDate;
117
        this.txtLocalComment = txtLocalComment;
118
        this.txtLocalEffectiveDate = txtLocalEffectiveDate;
119

    
120
        this.entitiesToUnCheckAll = new HashSet<>();
121
                
122
        initComponents();
123
    }
124

    
125
    private void initComponents() {
126
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
127

    
128
        translate();
129
        
130
        this.workspacePicker.addChangeListener((ChangeEvent e) -> {
131
            doUpdateTableLocalChanges();
132
        });
133
        
134
        this.tblLocalChanges.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
135

    
136
        this.tblLocalChanges.addKeyListener(new KeyAdapter() {
137
            @Override
138
            public void keyPressed(KeyEvent e) {
139
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
140
                    doToggleSelection();
141
                }
142
            }
143
        });
144

    
145
        this.tblLocalChanges.getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> {
146
            doUpdateEnabledComponents();
147
        });
148

    
149
        this.btnLocalCheckAll.addActionListener((ActionEvent e) -> {
150
            if (changes != null) {
151
                doCheckAll();
152
                doUpdateTableLocalChanges();
153
            }
154
        });
155

    
156
        this.btnLocalUnCheckAll.addActionListener((ActionEvent e) -> {
157
            if (changes != null) {
158
                doUnCheckAll();
159
                doUpdateTableLocalChanges();
160
            }
161
        });
162

    
163
        this.btnLocalShowForm.addActionListener((ActionEvent e) -> {
164
            doShowForm();
165
        });
166

    
167
        this.btnLocalCommit.addActionListener((ActionEvent e) -> {
168
            doCommit();
169
        });
170

    
171
        this.effectiveDatePicker = toolsSwingManager.createDatePickerController(
172
                this.txtLocalEffectiveDate,
173
                this.btnLocalEffectiveDate
174
        );
175

    
176
        this.effectiveDatePicker.set(new Date());
177
        
178
        this.btnLocalRefresh.addActionListener(new ActionListener() {
179
            @Override
180
            public void actionPerformed(ActionEvent e) {
181
                doUpdateTableLocalChanges();
182
            }
183
        });
184

    
185
        toolsSwingManager.addClearButton(this.txtLocalComment);
186
        toolsSwingManager.setDefaultPopupMenu(txtLocalComment);
187

    
188
        doUpdateEnabledComponents();
189
    }
190

    
191
    private void translate() {
192
        ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager();
193

    
194
        swingManager.translate(this.btnLocalCheckAll);
195
        swingManager.translate(this.btnLocalCommit);
196
        swingManager.translate(this.btnLocalRefresh);
197
        swingManager.translate(this.btnLocalRevert);
198
        swingManager.translate(this.btnLocalShowForm);
199
        swingManager.translate(this.btnLocalUnCheckAll);
200

    
201
        swingManager.translate(this.lblLocalEffectiveDate);
202
        swingManager.translate(this.lblLocalComment);
203
    }
204

    
205
    private Iterator<Long> getSelectionIterator() {
206
        ListSelectionModel selection = this.tblLocalChanges.getSelectionModel();
207
        return new Iterator<Long>() {
208
            long n = selection.getMinSelectionIndex();
209
            @Override
210
            public boolean hasNext() {
211
                while (n <= selection.getMaxSelectionIndex()){
212
                    if(selection.isSelectedIndex((int) n)){
213
                       return true; 
214
                    }
215
                    n++;
216
                }
217
                return false;
218
            }
219

    
220
            @Override
221
            public Long next() {
222
                if(n > selection.getMaxSelectionIndex()){
223
                    throw new IllegalStateException();
224
                }
225
                return n++;
226
            }
227
        };
228
    }
229
    
230
    private void doToggleSelection() {
231
        Iterator<Long> rows = getSelectionIterator();
232

    
233
        this.changes.process(rows, (VCSGisWorkspaceChange change) -> {
234
            if(change.getOperation() == OP_ADD_ENTITY) {
235
                if(change.isSelected()){
236
                    entitiesToUnCheckAll.add(change.getEntityCode());
237
                }
238
            }
239
            change.setSelected(!change.isSelected());
240
            return true;
241
        });
242
        if(!entitiesToUnCheckAll.isEmpty()){
243
            doUnCheckAllByEntities();
244
        }
245
    }
246
    
247
    private Iterator<Long> getRowsByEntitiesToUnCheckIterator() {
248

    
249
        long size = changes.size64();
250
        TableModel model = this.tblLocalChanges.getModel();
251
        return new Iterator<Long>() {
252
            long n = 0;
253
            @Override
254
            public boolean hasNext() {
255
                while (n < size){
256
                    VCSGisWorkspaceChange row = changes.get64(n);
257
                    if(entitiesToUnCheckAll.contains(row.getEntityCode())){
258
                       return true; 
259
                    }
260
                    n++;
261
                }
262
                return false;
263
            }
264

    
265
            @Override
266
            public Long next() {
267
                return n++;
268
            }
269
        };
270
    }
271

    
272
    
273
    private void doUnCheckAllByEntities(){
274
        if(!entitiesToUnCheckAll.isEmpty()) {
275
            Iterator<Long> rows = getRowsByEntitiesToUnCheckIterator();
276
            this.changes.process(rows, (VCSGisWorkspaceChange change) -> {
277
                 if(change.getOperation() != OP_ADD_ENTITY) {
278
                    change.setSelected(false);
279
                 }
280
                return true;
281
            });
282
            entitiesToUnCheckAll.clear();
283
        }
284
    }
285
    
286
    private Iterator<Long> getRowsIterator() {
287
        long size = changes.size64();
288
        return new Iterator<Long>() {
289
            long n = 0;
290
            @Override
291
            public boolean hasNext() {
292
                return (n < size);
293
            }
294

    
295
            @Override
296
            public Long next() {
297
                return n++;
298
            }
299
        };
300
    }
301

    
302

    
303
    private void doCheckAll() {
304
        Iterator<Long> rows = getRowsIterator();
305

    
306
        this.changes.process(rows, (VCSGisWorkspaceChange change) -> {
307
            change.setSelected(true);
308
            return true;
309
        });
310
    }
311

    
312
    private void doUnCheckAll() {
313
        Iterator<Long> rows = getRowsIterator();
314

    
315
        this.changes.process(rows, (VCSGisWorkspaceChange change) -> {
316
             change.setSelected(false);
317
            return true;
318
        });
319
    }
320
    
321
    public VCSGisWorkspace getWorkspace() {
322
        return this.workspacePicker.get();
323
    }
324
    
325
    private void doUpdateEnabledComponents() {
326
        int selectedRowCount = this.tblLocalChanges.getSelectedRowCount();
327
        btnLocalCheckAll.setEnabled(changes!=null && changes.size64()>0);
328
        btnLocalUnCheckAll.setEnabled(changes!=null && changes.size64()>0);
329
        btnLocalRefresh.setEnabled(changes!=null);
330
        btnLocalRevert.setEnabled(false);      
331
        btnLocalRevert.setVisible(false);      
332
        btnLocalCommit.setEnabled(changes!=null && !this.changes.isSelectionEmpty());
333
        //Show form
334
        int row = this.tblLocalChanges.getSelectedRow();
335
        if(row>=0){
336
            VCSGisWorkspaceChange change = this.changes.get64(row);
337
            if(change.getOperation() != OP_ADD_ENTITY){
338
                btnLocalShowForm.setEnabled(true);
339
                return;
340
            }
341
        }
342
        btnLocalShowForm.setEnabled(false);
343
        
344
    }
345
    
346
    private void doUpdateTableLocalChanges() {
347
        VCSGisWorkspace ws = this.getWorkspace();
348
        
349
        if(ws != null){
350
            changes = ws.getLocalChanges();
351
            localChangesTableModel = new ChangesTableModel(changes, ws);
352
            this.tblLocalChanges.setModel(localChangesTableModel);
353
            this.tableModelListener = null;
354

    
355
            localChangesTableModel.addTableModelListener(getTableModelListener());
356
            
357
            this.btnLocalCheckAll.setEnabled(true);
358
            this.btnLocalUnCheckAll.setEnabled(true);
359
            this.btnLocalCommit.setEnabled(true);
360
            this.btnLocalRefresh.setEnabled(true);
361
            this.btnLocalShowForm.setEnabled(true);
362
        } else {
363
            this.btnLocalCheckAll.setEnabled(false);
364
            this.btnLocalUnCheckAll.setEnabled(false);
365
            this.btnLocalCommit.setEnabled(false);
366
            this.btnLocalRefresh.setEnabled(false);
367
            this.btnLocalShowForm.setEnabled(false);
368
        }
369
        doUpdateEnabledComponents();
370
    }
371

    
372
    private TableModelListener getTableModelListener() {
373
        if(tableModelListener == null){
374
            tableModelListener =  (TableModelEvent e) -> {
375
                if (e.getType() == TableModelEvent.UPDATE) {
376
                    TableModel model = ((TableModel) (e.getSource()));
377
                    VCSGisWorkspaceChange change = changes.get64(e.getFirstRow());
378
                    if(change.getOperation() == OP_ADD_ENTITY) {
379
                        if(!change.isSelected()) {
380
                            if (entitiesToUnCheckAll.add(change.getEntityCode())) {
381
                                doUnCheckAllByEntities();
382
                            }
383
                        }
384
                    }
385
                }
386
            };
387
        }
388
        return tableModelListener;
389
    }
390
    
391
    private void doShowForm() {
392

    
393
        int row = this.tblLocalChanges.getSelectedRow();
394
        VCSGisWorkspaceChange change = this.changes.get64(row);
395
        if(change.getOperation() == OP_DELETE || change.getOperation() == OP_ADD_ENTITY){
396
            return;
397
        }
398
        Feature feature = change.getRelatedFeature();
399
        if(feature == null){
400
            return;
401
        }
402
        try {
403
            DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
404
            JFeatureForm form;
405
            form = dataSwingManager.createJFeatureForm(feature);
406
            WindowManager winManager = ToolsSwingLocator.getWindowManager();
407
            form.asJComponent().setPreferredSize(new Dimension(400,200));
408
            winManager.showWindow(form.asJComponent(), "Local change: "+change.getLabel(), WindowManager.MODE.WINDOW);
409
        } catch (CreateJFeatureFormException ex) {
410
            Logger.getLogger(VCSGisJChangesImpl.class.getName()).log(Level.SEVERE, null, ex);
411
        }
412

    
413
    }
414
    
415
    private void doCommit() {
416
        Timestamp effectiveDate = new Timestamp(effectiveDatePicker.get().getTime());
417
        getWorkspace().commit(effectiveDate, this.txtLocalComment.getText(), null);
418
        doUpdateTableLocalChanges();
419
    }
420
}