Revision 9512 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

View differences:

RemoteChangesTableModel.java
22 22

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

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

  
37 41
/**
38 42
 *
......
58 62
        new ColumnDescriptor(String.class, "code", false)
59 63
    };
60 64
    
61
    private OnlineWorkingcopyChanges<OnlineRemoteChange> changes;
65
    private OnlineChanges<OnlineRemoteChange> changes;
62 66
    private final OnlineWorkingcopy ws;
67

  
63 68
    
64
    
65 69
    private static class ColumnDescriptor {
66 70
        final private Class<?> columnClass;
67 71
        final private String name;
......
90 94
        this(null, ws);
91 95
    }
92 96

  
93
    public RemoteChangesTableModel(OnlineWorkingcopyChanges<OnlineRemoteChange> changes, OnlineWorkingcopy ws) {
97
    public RemoteChangesTableModel(OnlineChanges<OnlineRemoteChange> changes, OnlineWorkingcopy ws) {
94 98
        this.ws = ws;
95 99
        this.changes = changes;
96 100
        if( changes!=null ) {
......
132 136
        return COLUMNS[columnIndex].isEditable();
133 137
    }
134 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
    
135 149
    @Override
136 150
    public Object getValueAt(int rowIndex, int columnIndex) {
137 151
        if(this.changes == null){
138 152
            return null;
139 153
        }
140 154
        I18nManager i18n = ToolsLocator.getI18nManager();
141
        OnlineRemoteChange row = this.changes.get64(rowIndex);
155
        OnlineRemoteChange row = getRow(rowIndex);
156
        if(row == null){
157
            return null;
158
        }
142 159
        switch(columnIndex){
143 160
            case COLUMN_SELECT:
144 161
                return row.isSelected();
......
151 168

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

  
156 178
            case COLUMN_LABEL:
157 179
                return row.getLabel();
......
172 194
        if(this.changes == null){
173 195
            return;
174 196
        }
175
        OnlineChange row = this.changes.get64(rowIndex);
197
//        OnlineChange row = this.changes.get64(rowIndex);
176 198
        switch(columnIndex){
177 199
            case COLUMN_SELECT:
178 200
                Boolean selected = (Boolean)aValue;
179 201
                if(selected) {
180
                    this.changes.addSelectionInterval(rowIndex, rowIndex);
202
                    this.changes.addSelected(rowIndex);
181 203
                } else {
182
                    this.changes.removeSelectionInterval(rowIndex, rowIndex);
204
                    this.changes.removeSelected(rowIndex);
183 205
                }
184 206
                break;
185 207
        }
186 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
    
187 320
}

Also available in: Unified diff