Skip to content

Commit

Permalink
fix #21655 - fix selection of changesets after sorting the table
Browse files Browse the repository at this point in the history
git-svn-id: https://josm.openstreetmap.de/svn/trunk@18338 0c6e7542-c601-0410-84e7-c038aed88b3b
  • Loading branch information
don-vip committed Dec 20, 2021
1 parent 9a4fc04 commit 1f7574f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static void destroyInstance() {
}

private ChangesetCacheManagerModel model;
private ChangesetCacheTableRowSorter sorter;
private JSplitPane spContent;
private boolean needsSplitPaneAdjustment;

Expand Down Expand Up @@ -271,7 +272,7 @@ protected JPanel buildChangesetTablePanel() {
new ChangesetCacheTableColumnModel(),
model.getSelectionModel()
);
tblChangesets.setRowSorter(new ChangesetCacheTableRowSorter(model));
tblChangesets.setRowSorter(sorter);
tblChangesets.addMouseListener(new MouseEventHandler());
InputMapUtils.addEnterAction(tblChangesets, new ShowDetailAction(model));
model.getSelectionModel().addListSelectionListener(new ChangesetDetailViewSynchronizer(model));
Expand Down Expand Up @@ -303,6 +304,8 @@ protected void build() {
cp.setLayout(new BorderLayout());

model = buildModel();
sorter = new ChangesetCacheTableRowSorter(model);
model.setChangesetCacheTableRowSorter(sorter);
actRemoveFromCacheAction = new RemoveFromCacheAction(model);
actCloseSelectedChangesetsAction = new CloseSelectedChangesetsAction(model);
actDownloadSelectedChangesets = new DownloadSelectedChangesetsAction(model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -31,6 +32,7 @@ public class ChangesetCacheManagerModel extends AbstractTableModel implements Ch

private final transient List<Changeset> data = new ArrayList<>();
private final DefaultListSelectionModel selectionModel;
private transient ChangesetCacheTableRowSorter sorter;
private transient Changeset changesetInDetailView;
private final PropertyChangeSupport support = new PropertyChangeSupport(this);

Expand Down Expand Up @@ -87,14 +89,8 @@ public boolean hasSelectedChangesets() {
* @return the list of selected changesets
*/
public List<Changeset> getSelectedChangesets() {
List<Changeset> ret = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
Changeset cs = data.get(i);
if (selectionModel.isSelectedIndex(i)) {
ret.add(cs);
}
}
return ret;
return Arrays.stream(TableHelper.getSelectedIndices(selectionModel))
.map(sorter::convertRowIndexToModel).mapToObj(data::get).collect(Collectors.toList());
}

/**
Expand Down Expand Up @@ -165,6 +161,10 @@ protected void sort() {
data.sort(Comparator.comparingInt(Changeset::getId).reversed());
}

void setChangesetCacheTableRowSorter(ChangesetCacheTableRowSorter sorter) {
this.sorter = sorter;
}

/* ------------------------------------------------------------------------------ */
/* interface ChangesetCacheListener */
/* ------------------------------------------------------------------------------ */
Expand Down

0 comments on commit 1f7574f

Please sign in to comment.