Statistics
| Revision:

gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.swing / org.gvsig.online.swing.impl / src / main / java / org / gvsig / online / swing / impl / changes / RemoteChangesTableModel.java @ 9512

History | View | Annotate | Download (9.61 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

    
23
package org.gvsig.online.swing.impl.changes;
24

    
25
import java.util.Iterator;
26
import java.util.Objects;
27
import javax.swing.ListSelectionModel;
28
import javax.swing.SwingUtilities;
29
import javax.swing.event.ChangeEvent;
30
import javax.swing.table.AbstractTableModel;
31
import static org.gvsig.online.lib.api.OnlineManager.OP_INSERT;
32
import org.gvsig.online.lib.api.workingcopy.OnlineChange;
33
import org.gvsig.online.lib.api.workingcopy.OnlineEntity;
34
import org.gvsig.online.lib.api.workingcopy.OnlineRemoteChange;
35
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopy;
36
import org.gvsig.online.swing.impl.OnlineSwingCommons;
37
import org.gvsig.tools.ToolsLocator;
38
import org.gvsig.tools.i18n.I18nManager;
39
import org.gvsig.online.lib.api.workingcopy.OnlineChanges;
40

    
41
/**
42
 *
43
 * @author gvSIG Team
44
 */
45
public class RemoteChangesTableModel extends AbstractTableModel {
46

    
47
    private static final int COLUMN_SELECT = 0;
48
    private static final int COLUMN_TABLE = 1;
49
    private static final int COLUMN_OPERATION = 2;
50
    private static final int COLUMN_LABEL = 3;
51
    private static final int COLUMN_REVNUM = 4;
52
    private static final int COLUMN_DATE = 5;
53
    private static final int COLUMN_CODE = 6;
54
    
55
    private static final ColumnDescriptor[] COLUMNS = new ColumnDescriptor[] {
56
        new ColumnDescriptor(Boolean.class, "select", true),
57
        new ColumnDescriptor(String.class, "table", false),
58
        new ColumnDescriptor(String.class, "operation", false),
59
        new ColumnDescriptor(String.class, "label", false),
60
        new ColumnDescriptor(Long.class, "Rev.num.", false),
61
        new ColumnDescriptor(String.class, "Fecha", false),
62
        new ColumnDescriptor(String.class, "code", false)
63
    };
64
    
65
    private OnlineChanges<OnlineRemoteChange> changes;
66
    private final OnlineWorkingcopy ws;
67

    
68
    
69
    private static class ColumnDescriptor {
70
        final private Class<?> columnClass;
71
        final private String name;
72
        private final boolean editable;
73
        public ColumnDescriptor(Class<?> columnClass, String name, boolean editable) {
74
            this.name = name;
75
            this.columnClass = columnClass;
76
            this.editable = editable;
77
        }
78
        public Class<?> columnClass() {
79
            return this.columnClass;
80
        }
81
        public String name() {
82
            return this.name;
83
        }
84
        public String label() {
85
            I18nManager i18n = ToolsLocator.getI18nManager();
86
            return i18n.getTranslation(this.name);
87
        }
88
        public boolean isEditable() {
89
            return this.editable;
90
        }
91
    }
92
    
93
    public RemoteChangesTableModel(OnlineWorkingcopy ws) {
94
        this(null, ws);
95
    }
96

    
97
    public RemoteChangesTableModel(OnlineChanges<OnlineRemoteChange> changes, OnlineWorkingcopy ws) {
98
        this.ws = ws;
99
        this.changes = changes;
100
        if( changes!=null ) {
101
            this.changes.addChangeListener((ChangeEvent e) -> {
102
                fireTableDataChanged();
103
            });
104
        }
105
    }
106
    
107
    @Override
108
    public int getRowCount() {
109
        if(this.changes == null){
110
            return 0;
111
        }
112
        try {
113
            return (int) this.changes.size64();
114
        } catch (Exception e) {
115
            return 0;
116
        }
117
    }
118

    
119
    @Override
120
    public int getColumnCount() {
121
        return COLUMNS.length;
122
    }
123

    
124
    @Override
125
    public Class<?> getColumnClass(int columnIndex) {
126
        return COLUMNS[columnIndex].columnClass();
127
    }
128

    
129
    @Override
130
    public String getColumnName(int column) {
131
        return COLUMNS[column].label();
132
    }
133

    
134
    @Override
135
    public boolean isCellEditable(int rowIndex, int columnIndex) {
136
        return COLUMNS[columnIndex].isEditable();
137
    }
138

    
139
    public OnlineRemoteChange getRow(int rowIndex) {
140
        OnlineRemoteChange row = null;
141
        try {
142
            row = this.changes.get64(rowIndex);
143
        } catch (IndexOutOfBoundsException ex) {
144
            //Do nothing
145
        }
146
        return row;
147
    }
148
    
149
    @Override
150
    public Object getValueAt(int rowIndex, int columnIndex) {
151
        if(this.changes == null){
152
            return null;
153
        }
154
        I18nManager i18n = ToolsLocator.getI18nManager();
155
        OnlineRemoteChange row = getRow(rowIndex);
156
        if(row == null){
157
            return null;
158
        }
159
        switch(columnIndex){
160
            case COLUMN_SELECT:
161
                return row.isSelected();
162
            case COLUMN_TABLE:
163
                OnlineEntity entity = ws.getEntity(row.getEntityCode());
164
                if(entity==null){
165
                    return "unknown ("+row.getEntityCode()+")";
166
                }
167
                return ws.getEntity(row.getEntityCode()).getEntityName();
168

    
169
            case COLUMN_OPERATION:
170
                int op = row.getOperation();
171
                switch (op) {
172
                    case OP_INSERT:
173
                        return i18n.getTranslation("_Not_in_local");
174
                    default:
175
                        return i18n.getTranslation(OnlineSwingCommons.getOperationLabel(op));
176
                }
177

    
178
            case COLUMN_LABEL:
179
                return row.getLabel();
180
            case COLUMN_CODE:
181
                return row.getRelatedFeatureCode();
182
            case COLUMN_REVNUM:
183
                return row.getRevisionNumber();
184
            case COLUMN_DATE:
185
                return Objects.toString(row.getRevisionDate(), "");
186
            default:
187
                return "";
188
                
189
        }
190
    }
191

    
192
    @Override
193
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
194
        if(this.changes == null){
195
            return;
196
        }
197
//        OnlineChange row = this.changes.get64(rowIndex);
198
        switch(columnIndex){
199
            case COLUMN_SELECT:
200
                Boolean selected = (Boolean)aValue;
201
                if(selected) {
202
                    this.changes.addSelected(rowIndex);
203
                } else {
204
                    this.changes.removeSelected(rowIndex);
205
                }
206
                break;
207
        }
208
    }
209
    
210
    public synchronized void selectAll() {
211
        Iterator<Long> rows = getRowsIterator();
212

    
213
        this.changes.process(rows, (OnlineRemoteChange change) -> {
214
            change.setSelected(true);
215
            return true;
216
        });
217
        this.fireTableDataChanged();
218

    
219
    }
220

    
221
    public synchronized void deselectAll() {
222
        Iterator<Long> rows = getRowsIterator();
223

    
224
        this.changes.process(rows, (OnlineRemoteChange change) -> {
225
            change.setSelected(false);
226
            return true;
227
        });
228
        this.fireTableDataChanged();
229

    
230
    }
231

    
232
    public synchronized void toggleSelection(ListSelectionModel selectionModel) {
233
        Iterator<Long> rows = getRowsIterator(selectionModel);
234

    
235
        this.changes.process(rows, (OnlineRemoteChange change) -> {
236
            change.setSelected(!change.isSelected());
237
            return true;
238
        });
239
        this.fireTableDataChanged();
240

    
241
    }
242

    
243
    @Override
244
    public void fireTableDataChanged() {
245
        if(!SwingUtilities.isEventDispatchThread()){
246
            SwingUtilities.invokeLater(new Runnable() {
247
                @Override
248
                public void run() {
249
                    fireTableDataChanged();
250
                }
251
            });
252
            return;
253
        }
254
        super.fireTableDataChanged();
255
    }
256
    
257
    private Iterator<Long> getRowsIterator(ListSelectionModel selectionModel) {
258
        int size = Math.min((int)changes.size64(), selectionModel.getMaxSelectionIndex());
259
        return new Iterator<Long>() {
260
            int n = selectionModel.getMinSelectionIndex();
261

    
262
            @Override
263
            public boolean hasNext() {
264
                while( n <= size && !selectionModel.isSelectedIndex(n) ){
265
                    n++;
266
                }
267
                return (n <= size);
268
            }
269

    
270
            @Override
271
            public Long next() {
272
                if( !selectionModel.isSelectedIndex(n) ){
273
                    if(!hasNext()){
274
                        return null;
275
                    }
276
                }
277
                return (long)n++;
278
            }
279
        };
280
    }
281

    
282
    private Iterator<Long> getRowsIterator() {
283
        long size = changes.size64();
284
        return new Iterator<Long>() {
285
            long n = 0;
286

    
287
            @Override
288
            public boolean hasNext() {
289
                return (n < size);
290
            }
291

    
292
            @Override
293
            public Long next() {
294
                return n++;
295
            }
296
        };
297
    }
298

    
299
    public OnlineChanges<OnlineRemoteChange> getChanges() {
300
        return this.changes;
301
    }
302

    
303
    public boolean isEmpty() {
304
        return this.changes == null;
305
    }
306

    
307
    public boolean isSelectionEmpty() {
308
        return this.changes == null || this.changes.isSelectionEmpty();
309
    }
310

    
311
    public synchronized void removeRemoteChanges(String entityName) {
312
        ws.removeRemoteChanges(entityName);
313
        this.fireTableDataChanged();
314
        
315
    }
316

    
317
    
318
    
319
    
320
}