Revision 44705 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/FeatureSelectionModel.java

View differences:

FeatureSelectionModel.java
27 27
 */
28 28
package org.gvsig.fmap.mapcontrol.dal.feature.swing;
29 29

  
30
import javax.swing.DefaultListSelectionModel;
30 31
import javax.swing.ListSelectionModel;
31 32
import javax.swing.event.EventListenerList;
32 33
import javax.swing.event.ListSelectionEvent;
......
50 51
/**
51 52
 * @author 2010- C?sar Ordi?ana - gvSIG team
52 53
 */
54
@SuppressWarnings("Convert2Lambda")
53 55
public class FeatureSelectionModel implements ListSelectionModel, Observer {
54
    private static Logger LOG =
55
        LoggerFactory.getLogger(FeatureSelectionModel.class);
56 56

  
57
	protected EventListenerList listenerList = new EventListenerList();
57
  private static final Logger LOGGER= LoggerFactory.getLogger(FeatureSelectionModel.class);
58 58

  
59
	private final FeatureTableModel featureTableModel;
59
  protected EventListenerList listenerList = new EventListenerList();
60 60

  
61
	private int selectionMode = SINGLE_INTERVAL_SELECTION;
61
  private final FeatureTableModel featureTableModel;
62 62

  
63
	private boolean isAdjusting = false;
63
  private int selectionMode = SINGLE_INTERVAL_SELECTION;
64 64

  
65
	private int anchor = -1;
65
  private boolean isAdjusting = false;
66 66

  
67
	private int lead = -1;
67
  private int anchor = -1;
68 68

  
69
	private int currentFirst = -1;
70
	private int currentLast = -1;
69
  private int lead = -1;
71 70

  
72
	/**
73
	 * Creates a new {@link FeatureSelectionModel} with a
74
	 * {@link FeatureTableModel} used to get the {@link Feature}s by position in
75
	 * the table.
76
	 *
77
	 * @param featureTableModel
78
	 *            to get Features from
79
	 * @throws DataException
80
	 *             if there is an error getting the store selection
81
	 */
82
	public FeatureSelectionModel(FeatureTableModel featureTableModel)
83
			throws DataException {
84
		this.featureTableModel = featureTableModel;
85
		this.featureTableModel.getFeatureStore().addObserver(this);
86
	}
71
  private int currentFirst = -1;
72
  private int currentLast = -1;
87 73

  
88
	public int getAnchorSelectionIndex() {
89
		return anchor;
90
	}
74
  private ListSelectionModel selectionModelDelegated;
91 75

  
92
	public int getLeadSelectionIndex() {
93
		return lead;
94
	}
76
  /**
77
   * Creates a new {@link FeatureSelectionModel} with a
78
   * {@link FeatureTableModel} used to get the {@link Feature}s by position in
79
   * the table.
80
   *
81
   * @param featureTableModel to get Features from
82
   * @throws DataException if there is an error getting the store selection
83
   */
84
  @SuppressWarnings("LeakingThisInConstructor")
85
  public FeatureSelectionModel(FeatureTableModel featureTableModel)
86
          throws DataException {
87
    this.featureTableModel = featureTableModel;
88
    FeatureStore store = this.featureTableModel.getFeatureStore();
89
    store.addObserver(this);
90
    if( !store.getFeatureSelection().isAvailable() ) {
91
      this.selectionModelDelegated = new DefaultListSelectionModel();
92
    }
93
  }
95 94

  
96
	public int getMaxSelectionIndex() {
95
  @Override
96
  public int getAnchorSelectionIndex() {
97
    if( this.selectionModelDelegated!=null ) {
98
      return this.selectionModelDelegated.getAnchorSelectionIndex();
99
    }
100
    return anchor;
101
  }
97 102

  
98
	    int resp = this.getSelectionIndex(true);
99
	    return resp;
100
	    /*
103
  @Override
104
  public int getLeadSelectionIndex() {
105
    if( this.selectionModelDelegated!=null ) {
106
      return this.selectionModelDelegated.getLeadSelectionIndex();
107
    }
108
    return lead;
109
  }
110

  
111
  @Override
112
  public int getMaxSelectionIndex() {
113
    if( this.selectionModelDelegated!=null ) {
114
      return this.selectionModelDelegated.getMaxSelectionIndex();
115
    }
116

  
117
    int resp = this.getSelectionIndex(true);
118
    return resp;
119
    /*
101 120
	     *
102 121
	     * The call to "featureTableModel.getFeatureAt(i)"
103 122
	     * causes a lot of reloadPage all over
......
115 134
			throw new SelectionChangeException(e);
116 135
		}
117 136
		return -1;
118
		*/
119
	}
137
     */
138
  }
120 139

  
121
	public int getMinSelectionIndex() {
140
  @Override
141
  public int getMinSelectionIndex() {
142
    if( this.selectionModelDelegated!=null ) {
143
      return this.selectionModelDelegated.getMinSelectionIndex();
144
    }
122 145

  
123
	    int resp = this.getSelectionIndex(false);
124
	    return resp;
146
    int resp = this.getSelectionIndex(false);
147
    return resp;
125 148

  
126
	    /*
149
    /*
127 150
	     *
128 151
         * The call to "featureTableModel.getFeatureAt(i)"
129 152
         * causes a lot of reloadPage all over
......
141 164
		} catch (Exception e) {
142 165
			throw new SelectionChangeException(e);
143 166
		}
144
		*/
167
     */
168
  }
145 169

  
146
	}
170
  @Override
171
  @SuppressWarnings("UnnecessaryReturnStatement")
172
  public void insertIndexInterval(int index, int length, boolean before) {
173
    if( this.selectionModelDelegated!=null ) {
174
      this.selectionModelDelegated.insertIndexInterval(index, length, before);
175
      return;
176
    }
177
    // Nothing to do
178
  }
147 179

  
148
	public void insertIndexInterval(int index, int length, boolean before) {
149
		// Nothing to do
150
	}
180
  @Override
181
  @SuppressWarnings("UnnecessaryReturnStatement")
182
  public void removeIndexInterval(int index0, int index1) {
183
    if( this.selectionModelDelegated!=null ) {
184
      this.selectionModelDelegated.removeIndexInterval(index0, index1);
185
      return;
186
    }
187
    // Nothing to do
188
  }
151 189

  
152
	public void removeIndexInterval(int index0, int index1) {
153
		// Nothing to do
154
	}
190
  @Override
191
  public void setAnchorSelectionIndex(int index) {
192
    if( this.selectionModelDelegated!=null ) {
193
      this.selectionModelDelegated.setAnchorSelectionIndex(index);
194
      return;
195
    }
196
    this.anchor = index;
197
  }
155 198

  
156
	public void setAnchorSelectionIndex(int index) {
157
		this.anchor = index;
158
	}
199
  @Override
200
  public void setLeadSelectionIndex(int index) {
201
    if( this.selectionModelDelegated!=null ) {
202
      this.selectionModelDelegated.setLeadSelectionIndex(index);
203
      return;
204
    }
205
    this.lead = index;
206
  }
159 207

  
160
	public void setLeadSelectionIndex(int index) {
161
		this.lead = index;
162
	}
208
  @Override
209
  public void addSelectionInterval(int index0, int index1) {
210
    if( this.selectionModelDelegated!=null ) {
211
      this.selectionModelDelegated.addSelectionInterval(index0, index1);
212
      return;
213
    }
214
    if ( featureTableModel.isSelectionLocked() ) {
215
       return;
216
    }
217
    doWithSelection(new FeatureSelectionOperation() {
218
      @Override
219
      public void doWithSelection(FeatureSelection selection, int first,
220
              int last) throws DataException {
221
        for (int i = first; i <= last; i++) {
222
          Feature feature = getFeature(i);
223
          if (!selection.isSelected(feature)) {
224
            selection.select(feature);
225
          }
226
        }
227
      }
163 228

  
164
	public void addSelectionInterval(int index0, int index1) {
165
	    if (!featureTableModel.isSelectionLocked()){
166
	        doWithSelection(new FeatureSelectionOperation() {
229
    }, index0, index1, true);
230
  }
167 231

  
168
	            public void doWithSelection(FeatureSelection selection, int first,
169
	                int last) throws DataException {
170
	                for (int i = first; i <= last; i++) {
171
	                    Feature feature = getFeature(i);
172
	                    if (!selection.isSelected(feature)) {
173
	                        selection.select(feature);
174
	                    }
175
	                }
176
	            }
232
  @Override
233
  public void setSelectionInterval(int index0, int index1) {
234
    if( this.selectionModelDelegated!=null ) {
235
      this.selectionModelDelegated.setSelectionInterval(index0, index1);
236
      return;
237
    }
238
    if ( featureTableModel.isSelectionLocked() ) {
239
       return;
240
    }
241
    doWithSelection(new FeatureSelectionOperation() {
177 242

  
178
	        }, index0, index1, true);
179
	    }
180
	}
243
      @Override
244
      public void doWithSelection(FeatureSelection selection, int first,
245
              int last) throws DataException {
246
        selection.deselectAll();
247
        for (int i = first; i <= last; i++) {
248
          Feature feature = getFeature(i);
249
          selection.select(feature);
250
        }
251
      }
181 252

  
182
	public void setSelectionInterval(int index0, int index1) {
183
	    if (!featureTableModel.isSelectionLocked()){
184
	        doWithSelection(new FeatureSelectionOperation() {
253
    }, index0, index1, true);
254
  }
185 255

  
186
	            public void doWithSelection(FeatureSelection selection, int first,
187
	                int last) throws DataException {
188
	                selection.deselectAll();
189
	                for (int i = first; i <= last; i++) {
190
	                    Feature feature = getFeature(i);
191
	                    selection.select(feature);
192
	                }
193
	            }
256
  @Override
257
  public void removeSelectionInterval(int index0, int index1) {
258
    if( this.selectionModelDelegated!=null ) {
259
      this.selectionModelDelegated.removeSelectionInterval(index0, index1);
260
      return;
261
    }
262
    if ( featureTableModel.isSelectionLocked() ) {
263
       return;
264
    }
265
    doWithSelection(new FeatureSelectionOperation() {
194 266

  
195
	        }, index0, index1, true);
196
	    }
197
	}
267
      @Override
268
      public void doWithSelection(FeatureSelection selection, int first,
269
              int last) throws DataException {
270
        for (int i = first; i <= last; i++) {
271
          Feature feature = getFeature(i);
272
          if (selection.isSelected(feature)) {
273
            selection.deselect(feature);
274
          }
275
        }
276
      }
198 277

  
199
	public void removeSelectionInterval(int index0, int index1) {
200
	    if (!featureTableModel.isSelectionLocked()){
201
	        doWithSelection(new FeatureSelectionOperation() {
278
    }, index0, index1, false);
279
  }
202 280

  
203
	            public void doWithSelection(FeatureSelection selection, int first,
204
	                int last) throws DataException {
205
	                for (int i = first; i <= last; i++) {
206
	                    Feature feature = getFeature(i);
207
	                    if (selection.isSelected(feature)) {
208
	                        selection.deselect(feature);
209
	                    }
210
	                }
211
	            }
281
  @Override
282
  public void clearSelection() {
283
    if( this.selectionModelDelegated!=null ) {
284
      this.selectionModelDelegated.clearSelection();
285
      return;
286
    }
287
    if ( featureTableModel.isSelectionLocked() ) {
288
       return;
289
    }
290
    try {
291
      getFeatureSelection().deselectAll();
292
    } catch (DataException e) {
293
      throw new SelectionChangeException(e);
294
    }
295
  }
212 296

  
213
	        }, index0, index1, false);
214
	    }
215
	}
297
  @Override
298
  public boolean isSelectedIndex(int index) {
299
    if( this.selectionModelDelegated!=null ) {
300
      return this.selectionModelDelegated.isSelectedIndex(index);
301
    }
302
    if (index == -1) {
303
      return false;
304
    }
305
    Feature feature = featureTableModel.getFeatureAt(index);
306
    return getFeatureSelection().isSelected(feature);
307
  }
216 308

  
217
	public void clearSelection() {
218
	    if (!featureTableModel.isSelectionLocked()){
219
	        try {
220
	            getFeatureSelection().deselectAll();
221
	        } catch (DataException e) {
222
	            throw new SelectionChangeException(e);
223
	        }
224
	    }
225
	}
309
  @Override
310
  public boolean isSelectionEmpty() {
311
    if( this.selectionModelDelegated!=null ) {
312
      return this.selectionModelDelegated.isSelectionEmpty();
313
    }
314
    try {
315
      return getFeatureSelection().isEmpty();
316
    } catch (DataException ex) {
317
      throw new SelectionChangeException(ex);
318
    }
319
  }
226 320

  
227
	public boolean isSelectedIndex(int index) {
228
		if (index == -1) {
229
			return false;
230
		}
231
		Feature feature = featureTableModel.getFeatureAt(index);
232
		return getFeatureSelection().isSelected(feature);
233
	}
321
  @Override
322
  public boolean getValueIsAdjusting() {
323
    if( this.selectionModelDelegated!=null ) {
324
      return this.selectionModelDelegated.getValueIsAdjusting();
325
    }
326
    return isAdjusting;
327
  }
234 328

  
235
	public boolean isSelectionEmpty() {
236
		try {
237
			return getFeatureSelection().isEmpty();
238
		} catch (DataException ex) {
239
			throw new SelectionChangeException(ex);
240
		}
241
	}
329
  @Override
330
  public void setValueIsAdjusting(boolean valueIsAdjusting) {
331
    if( this.selectionModelDelegated!=null ) {
332
      this.selectionModelDelegated.setValueIsAdjusting(valueIsAdjusting);
333
      return;
334
    }
335
    if (this.isAdjusting != valueIsAdjusting) {
336
      this.isAdjusting = valueIsAdjusting;
337
      if (this.isAdjusting) {
338
        getFeatureSelection().beginComplexNotification();
339
      } else {
340
        getFeatureSelection().endComplexNotification();
341
      }
342
    }
343
  }
242 344

  
243
	public boolean getValueIsAdjusting() {
244
		return isAdjusting;
245
	}
345
  @Override
346
  public int getSelectionMode() {
347
    if( this.selectionModelDelegated!=null ) {
348
      return this.selectionModelDelegated.getSelectionMode();
349
    }
350
    return selectionMode;
351
  }
246 352

  
247
	public void setValueIsAdjusting(boolean valueIsAdjusting) {
248
		if (this.isAdjusting != valueIsAdjusting) {
249
			this.isAdjusting = valueIsAdjusting;
250
			if (this.isAdjusting) {
251
				getFeatureSelection().beginComplexNotification();
252
			} else {
253
				getFeatureSelection().endComplexNotification();
254
			}
255
		}
256
	}
353
  @Override
354
  public void setSelectionMode(int selectionMode) {
355
    if( this.selectionModelDelegated!=null ) {
356
      this.selectionModelDelegated.setSelectionMode(selectionMode);
357
      return ;
358
    }
359
    this.selectionMode = selectionMode;
360
  }
257 361

  
258
	public int getSelectionMode() {
259
		return selectionMode;
260
	}
362
  @Override
363
  public void addListSelectionListener(ListSelectionListener listener) {
364
    if( this.selectionModelDelegated!=null ) {
365
      this.selectionModelDelegated.addListSelectionListener(listener);
366
      return ;
367
    }
368
    listenerList.add(ListSelectionListener.class, listener);
369
  }
261 370

  
262
	public void setSelectionMode(int selectionMode) {
263
		this.selectionMode = selectionMode;
264
	}
371
  @Override
372
  public void removeListSelectionListener(ListSelectionListener listener) {
373
    if( this.selectionModelDelegated!=null ) {
374
      this.selectionModelDelegated.removeListSelectionListener(listener);
375
      return ;
376
    }
377
    listenerList.remove(ListSelectionListener.class, listener);
378
  }
265 379

  
266
	public void addListSelectionListener(ListSelectionListener listener) {
267
		listenerList.add(ListSelectionListener.class, listener);
268
	}
380
  @Override
381
  public void update(Observable observable, Object notification) {
382
    if (notification instanceof FeatureStoreNotification) {
383
      FeatureStoreNotification fnotification
384
              = (FeatureStoreNotification) notification;
385
      if (!fnotification.getSource().equals(getFeatureStore())) {
386
        return;
387
      }
388
      if (FeatureStoreNotification.SELECTION_CHANGE.equals(fnotification.getType())) {
389
        try {
390
          fireValueChanged(-1, -1, false);
391
        } catch (ConcurrentDataModificationException e) {
392
          LOGGER.warn("The store has been updated and the selection can not be refreshed", e);
393
        }
394
      }
395
    }
396
  }
269 397

  
270
	public void removeListSelectionListener(ListSelectionListener listener) {
271
		listenerList.remove(ListSelectionListener.class, listener);
272
	}
398
  private FeatureSelection getFeatureSelection() {
399
    try {
400
      return (FeatureSelection) getFeatureStore().getSelection();
401
    } catch (DataException ex) {
402
      throw new SelectionChangeException(ex);
403
    }
404
  }
273 405

  
274
	public void update(Observable observable, Object notification) {
275
		if (notification instanceof FeatureStoreNotification) {
276
			FeatureStoreNotification fnotification =
277
					(FeatureStoreNotification) notification;
278
			if (!fnotification.getSource().equals(getFeatureStore())) {
279
				return;
280
			}
281
			if (FeatureStoreNotification.SELECTION_CHANGE.equals(fnotification.getType())) {
282
				try{
283
				    fireValueChanged(-1, -1, false);
284
				}catch(ConcurrentDataModificationException e){
285
				    LOG.warn("The store has been updated and the selection can not be refreshed", e);
286
				}
287
			}
288
		}
289
	}
406
  /**
407
   * @param operation
408
   * @param index0
409
   * @param index1
410
   */
411
  private void doWithSelection(FeatureSelectionOperation operation,
412
          int index0, int index1, boolean select) {
413
    // Set the anchor and lead
414
    anchor = index0;
415
    lead = index1;
290 416

  
291
	private FeatureSelection getFeatureSelection() {
292
		try {
293
			return (FeatureSelection) getFeatureStore().getSelection();
294
		} catch (DataException ex) {
295
			throw new SelectionChangeException(ex);
296
		}
297
	}
417
    // As index0 <= index1 is no guaranteed, calculate the first and second
418
    // values
419
    int first = (index0 <= index1) ? index0 : index1;
420
    int last = (index0 <= index1) ? index1 : index0;
298 421

  
299
	/**
300
	 * @param operation
301
	 * @param index0
302
	 * @param index1
303
	 */
304
	private void doWithSelection(FeatureSelectionOperation operation,
305
			int index0, int index1, boolean select) {
306
		// Set the anchor and lead
307
		anchor = index0;
308
		lead = index1;
422
    //If the new selection is not updated don't continue
423
    if ((currentFirst == first) && (currentLast == last)) {
424
      return;
425
    }
309 426

  
310
		// As index0 <= index1 is no guaranteed, calculate the first and second
311
		// values
312
		int first = (index0 <= index1) ? index0 : index1;
313
		int last = (index0 <= index1) ? index1 : index0;
427
    int oldFirst = currentFirst;
428
    int oldLast = currentLast;
429
    currentFirst = first;
430
    currentLast = last;
314 431

  
315
		//If the new selection is not updated don't continue
316
		if ((currentFirst == first) && (currentLast == last)){
317
		    return;
318
		}
432
    FeatureSelection selection = getFeatureSelection();
319 433

  
320
		int oldFirst = currentFirst;
321
		int oldLast = currentLast;
322
		currentFirst = first;
323
		currentLast = last;
434
    // Perform the selection operation into a complex notification
435
    selection.beginComplexNotification();
436
    try {
437
      // Is a full select or deselect
438
      if (first == 00 && last == featureTableModel.getRowCount() - 1) {
439
        if (select) {
440
          selection.selectAll();
441
        } else {
442
          selection.deselectAll();
443
        }
444
      } else {
445
        operation.doWithSelection(selection, first, last);
446
      }
447
    } catch (DataException e) {
448
      throw new SelectionChangeException(e);
449
    } finally {
450
      selection.endComplexNotification();
451
    }
324 452

  
325
		FeatureSelection selection = getFeatureSelection();
326

  
327
		// Perform the selection operation into a complex notification
328
		selection.beginComplexNotification();
329
		try {
330
			// Is a full select or deselect
331
			if (first == 00 && last == featureTableModel.getRowCount() - 1) {
332
				if (select) {
333
					selection.selectAll();
334
				} else {
335
					selection.deselectAll();
336
				}
337
			} else {
338
				operation.doWithSelection(selection, first, last);
339
			}
340
		} catch (DataException e) {
341
			throw new SelectionChangeException(e);
342
		} finally {
343
			selection.endComplexNotification();
344
		}
345

  
346
		// Para no lanzar dos veces el fireValueChanged sobre las mismas filas de la selecci?n
347
		if(oldFirst<=first){
348
		    if(oldLast<first){ //No intersectan los rangos
349
		        fireValueChanged(oldFirst, oldLast, isAdjusting);
350
		        fireValueChanged(first, last, isAdjusting);
351
		    } else { // Intersectan los rangos oldFirst - first - ...
352
                if (last > oldLast) { // Intersectan los rangos oldFirst - first - oldLast - last
353
                    fireValueChanged(oldFirst, last, isAdjusting);
354
                } else { // Intersectan los rangos oldFirst - first - last - oldLast
355
                    fireValueChanged(oldFirst, oldLast, isAdjusting);
356
                }
357
		    }
358
		}
359
        if(first<=oldFirst){
360
            if(last<oldFirst){ //No intersectan los rangos
361
                fireValueChanged(oldFirst, oldLast, isAdjusting);
362
                fireValueChanged(first, last, isAdjusting);
363
            } else { // Intersectan los rangos first - oldFirst - ...
364
                if (last > oldLast) { // Intersectan los rangos first - oldFirst - oldLast - last
365
                    fireValueChanged(first, last, isAdjusting);
366
                } else { // Intersectan los rangos first - oldFirst - last - oldLast
367
                    fireValueChanged(first, oldLast, isAdjusting);
368
                }
369
            }
453
    // Para no lanzar dos veces el fireValueChanged sobre las mismas filas de la selecci?n
454
    if (oldFirst <= first) {
455
      if (oldLast < first) { //No intersectan los rangos
456
        fireValueChanged(oldFirst, oldLast, isAdjusting);
457
        fireValueChanged(first, last, isAdjusting);
458
      } else { // Intersectan los rangos oldFirst - first - ...
459
        if (last > oldLast) { // Intersectan los rangos oldFirst - first - oldLast - last
460
          fireValueChanged(oldFirst, last, isAdjusting);
461
        } else { // Intersectan los rangos oldFirst - first - last - oldLast
462
          fireValueChanged(oldFirst, oldLast, isAdjusting);
370 463
        }
371
	}
464
      }
465
    }
466
    if (first <= oldFirst) {
467
      if (last < oldFirst) { //No intersectan los rangos
468
        fireValueChanged(oldFirst, oldLast, isAdjusting);
469
        fireValueChanged(first, last, isAdjusting);
470
      } else { // Intersectan los rangos first - oldFirst - ...
471
        if (last > oldLast) { // Intersectan los rangos first - oldFirst - oldLast - last
472
          fireValueChanged(first, last, isAdjusting);
473
        } else { // Intersectan los rangos first - oldFirst - last - oldLast
474
          fireValueChanged(first, oldLast, isAdjusting);
475
        }
476
      }
477
    }
478
  }
372 479

  
373
	/**
374
	 * Returns a Feature by table row position.
375
	 */
376
	private Feature getFeature(int index) {
377
		return featureTableModel.getFeatureAt(index);
378
	}
480
  /**
481
   * Returns a Feature by table row position.
482
   */
483
  private Feature getFeature(int index) {
484
    return featureTableModel.getFeatureAt(index);
485
  }
379 486

  
380
	/**
381
	 * Returns the FeatureStore.
382
	 */
383
	private FeatureStore getFeatureStore() {
384
		return featureTableModel.getFeatureStore();
385
	}
487
  /**
488
   * Returns the FeatureStore.
489
   */
490
  private FeatureStore getFeatureStore() {
491
    return featureTableModel.getFeatureStore();
492
  }
386 493

  
387
	/**
388
	 * @param firstIndex
389
	 *            the first index in the interval
390
	 * @param lastIndex
391
	 *            the last index in the interval
392
	 * @param isAdjusting
393
	 *            true if this is the final change in a series of adjustments
394
	 * @see EventListenerList
395
	 */
396
	protected void fireValueChanged(int firstIndex, int lastIndex,
397
			boolean isAdjusting) {
398
		Object[] listeners = listenerList.getListenerList();
399
		ListSelectionEvent e = null;
494
  /**
495
   * @param firstIndex the first index in the interval
496
   * @param lastIndex the last index in the interval
497
   * @param isAdjusting true if this is the final change in a series of
498
   * adjustments
499
   * @see EventListenerList
500
   */
501
  protected void fireValueChanged(int firstIndex, int lastIndex,
502
          boolean isAdjusting) {
503
    Object[] listeners = listenerList.getListenerList();
504
    ListSelectionEvent e = null;
400 505

  
401
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
402
			if (listeners[i] == ListSelectionListener.class) {
403
				if (e == null) {
404
					e =
405
							new ListSelectionEvent(this, firstIndex, lastIndex,
406
									isAdjusting);
407
				}
408
				((ListSelectionListener) listeners[i + 1]).valueChanged(e);
409
			}
410
		}
411
	}
506
    for (int i = listeners.length - 2; i >= 0; i -= 2) {
507
      if (listeners[i] == ListSelectionListener.class) {
508
        if (e == null) {
509
          e
510
                  = new ListSelectionEvent(this, firstIndex, lastIndex,
511
                          isAdjusting);
512
        }
513
        ((ListSelectionListener) listeners[i + 1]).valueChanged(e);
514
      }
515
    }
516
  }
412 517

  
413
	private interface FeatureSelectionOperation {
414
		void doWithSelection(FeatureSelection selection, int first, int last)
415
				throws DataException;
416
	}
518
  private interface FeatureSelectionOperation {
417 519

  
418
	/**
419
	 *
420
	 * Return the index of the the first (last) selected feature
421
	 *
422
	 * @param last whether to return the index of the last selected feature
423
	 * @return
424
	 */
425
	private int getSelectionIndex(boolean last) {
520
    void doWithSelection(FeatureSelection selection, int first, int last)
521
            throws DataException;
522
  }
426 523

  
427
        int ind = -1;
428
        int resp = -1;
524
  /**
525
   *
526
   * Return the index of the the first (last) selected feature
527
   *
528
   * @param last whether to return the index of the last selected feature
529
   * @return
530
   */
531
  private int getSelectionIndex(boolean last) {
429 532

  
430
        FeatureSet fs = null;
431
        DisposableIterator diter = null;
533
    int ind = -1;
534
    int resp = -1;
432 535

  
433
        try {
434
            FeatureSelection selection = getFeatureSelection();
435
            if (!selection.isEmpty()) {
436
                FeatureStore store = getFeatureStore();
437
                FeatureQuery query = store.createFeatureQuery();
438
                query.addEssentialAttributeNames(store);
439
                fs = store.getFeatureSet(query);
440
                diter = fs.fastIterator();
441
                Feature feat = null;
442
                while (diter.hasNext()) {
443
                    ind++;
444
                    feat = (Feature) diter.next();
445
                    if (selection.isSelected(feat)) {
446
                        resp = ind;
447
                        if (!last) {
448
                            break;
449
                        }
450
                    }
451
                }
536
    FeatureSet fs = null;
537
    DisposableIterator diter = null;
452 538

  
539
    try {
540
      FeatureSelection selection = getFeatureSelection();
541
      if (!selection.isEmpty()) {
542
        FeatureStore store = getFeatureStore();
543
        FeatureQuery query = store.createFeatureQuery();
544
        query.addEssentialAttributeNames(store);
545
        fs = store.getFeatureSet(query);
546
        diter = fs.fastIterator();
547
        Feature feat;
548
        while (diter.hasNext()) {
549
          ind++;
550
          feat = (Feature) diter.next();
551
          if (selection.isSelected(feat)) {
552
            resp = ind;
553
            if (!last) {
554
              break;
453 555
            }
454
        } catch (DataException e) {
455
            throw new SelectionChangeException(e);
456
        } finally {
457
            if (diter != null) {
458
                diter.dispose();
459
            }
460

  
461
            if (fs != null) {
462
                fs.dispose();
463
            }
556
          }
464 557
        }
465
        return resp;
466
	}
467 558

  
468
}
559
      }
560
    } catch (DataException e) {
561
      throw new SelectionChangeException(e);
562
    } finally {
563
      if (diter != null) {
564
        diter.dispose();
565
      }
566

  
567
      if (fs != null) {
568
        fs.dispose();
569
      }
570
    }
571
    return resp;
572
  }
573

  
574
}

Also available in: Unified diff