/** * Constrain all the lists' selections such that the relative differences between them stay the * same no matter how the user shifts any of the selections upwards/downwards. * * <p>Only works if the lists' selection models are instances of {@link * BoundedListSelectionModel}. */ private void updateSelectionBounds() { if (listsAndSelectionIndices.isEmpty()) { return; } if (!(listsAndSelectionIndices.keySet().iterator().next().getSelectionModel() instanceof BoundedListSelectionModel)) { return; } int minSelIndex = Integer.MAX_VALUE; int minHeadroom = Integer.MAX_VALUE; for (Entry<ImageListView, Integer> e : listsAndSelectionIndices.entrySet()) { ImageListView list = e.getKey(); Integer i = e.getValue(); if (i == null) { continue; } if (i < minSelIndex) { minSelIndex = i; } int length = list.getLength(); if (length - 1 - i < minHeadroom) { minHeadroom = length - 1 - i; } } for (ImageListView l : listsAndSelectionIndices.keySet()) { ListSelectionModel sm = l.getSelectionModel(); if (!(sm instanceof BoundedListSelectionModel)) { continue; } BoundedListSelectionModel bsm = (BoundedListSelectionModel) sm; Integer idx = listsAndSelectionIndices.get(l); if (idx == null) { bsm.disableBounds(); l.disableVisibilityLimits(); continue; } int lower = idx - minSelIndex; int upper = idx + minHeadroom; if (lower < 0 || lower > upper) { System.err.println( "shouldn't happen: trying to set selection bounds of list (length=" + l.getLength() + " to [" + lower + "," + upper + "]"); continue; } bsm.setLowerBound(lower); l.setLowerVisibilityLimit(lower); bsm.setUpperBound(upper); l.setUpperVisibilityLimit(upper); } }
private void clearSelectionBounds() { if (listsAndSelectionIndices.isEmpty()) { return; } for (ImageListView l : listsAndSelectionIndices.keySet()) { ListSelectionModel sm = l.getSelectionModel(); if (!(sm instanceof BoundedListSelectionModel)) { continue; } BoundedListSelectionModel bsm = (BoundedListSelectionModel) sm; bsm.disableBounds(); l.disableVisibilityLimits(); } }