Changeset 16601 in josm


Ignore:
Timestamp:
2020-06-11T14:19:10+02:00 (4 years ago)
Author:
simon04
Message:

Add TableHelper.setSelectedIndices

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/conflict/pair/AbstractListMergeModel.java

    r16553 r16601  
    1313import java.beans.PropertyChangeListener;
    1414import java.util.ArrayList;
     15import java.util.Arrays;
    1516import java.util.EnumMap;
    1617import java.util.HashSet;
     
    4041import org.openstreetmap.josm.gui.help.HelpUtil;
    4142import org.openstreetmap.josm.gui.util.ChangeNotifier;
     43import org.openstreetmap.josm.gui.util.TableHelper;
    4244import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTableModel;
    4345import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    522524        }
    523525        fireModelDataChanged();
    524         mergedEntriesSelectionModel.setValueIsAdjusting(true);
    525         mergedEntriesSelectionModel.clearSelection();
    526         for (int row: rows) {
    527             mergedEntriesSelectionModel.addSelectionInterval(row-1, row-1);
    528         }
    529         mergedEntriesSelectionModel.setValueIsAdjusting(false);
     526        TableHelper.setSelectedIndices(mergedEntriesSelectionModel, Arrays.stream(rows).map(row -> row - 1));
    530527    }
    531528
     
    550547        }
    551548        fireModelDataChanged();
    552         mergedEntriesSelectionModel.setValueIsAdjusting(true);
    553         mergedEntriesSelectionModel.clearSelection();
    554         for (int row: rows) {
    555             mergedEntriesSelectionModel.addSelectionInterval(row+1, row+1);
    556         }
    557         mergedEntriesSelectionModel.setValueIsAdjusting(false);
     549        TableHelper.setSelectedIndices(mergedEntriesSelectionModel, Arrays.stream(rows).map(row -> row + 1));
    558550    }
    559551
  • trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

    r16563 r16601  
    6969import org.openstreetmap.josm.gui.util.GuiHelper;
    7070import org.openstreetmap.josm.gui.util.StayOpenPopupMenu;
     71import org.openstreetmap.josm.gui.util.TableHelper;
    7172import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    7273import org.openstreetmap.josm.gui.widgets.FileChooserManager;
     
    334335            int[] pos = tblStyles.getSelectedRows();
    335336            MapPaintStyles.toggleStyleActive(pos);
    336             selectionModel.setValueIsAdjusting(true);
    337             selectionModel.clearSelection();
    338             for (int p: pos) {
    339                 selectionModel.addSelectionInterval(p, p);
    340             }
    341             selectionModel.setValueIsAdjusting(false);
     337            TableHelper.setSelectedIndices(selectionModel, Arrays.stream(pos));
    342338        }
    343339    }
     
    371367            int[] sel = tblStyles.getSelectedRows();
    372368            MapPaintStyles.moveStyles(sel, increment);
    373 
    374             selectionModel.setValueIsAdjusting(true);
    375             selectionModel.clearSelection();
    376             for (int row: sel) {
    377                 selectionModel.addSelectionInterval(row + increment, row + increment);
    378             }
    379             selectionModel.setValueIsAdjusting(false);
     369            TableHelper.setSelectedIndices(selectionModel, Arrays.stream(sel).map(row -> row + increment));
    380370            model.ensureSelectedIsVisible();
    381371        }
     
    414404            final int[] rows = tblStyles.getSelectedRows();
    415405            MapPaintStyleLoader.reloadStyles(rows);
    416             MainApplication.worker.submit(() -> SwingUtilities.invokeLater(() -> {
    417                 selectionModel.setValueIsAdjusting(true);
    418                 selectionModel.clearSelection();
    419                 for (int r: rows) {
    420                     selectionModel.addSelectionInterval(r, r);
    421                 }
    422                 selectionModel.setValueIsAdjusting(false);
    423             }));
     406            MainApplication.worker.submit(() -> SwingUtilities.invokeLater(() ->
     407                    TableHelper.setSelectedIndices(selectionModel, Arrays.stream(rows))));
    424408        }
    425409    }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java

    r16512 r16601  
    8282import org.openstreetmap.josm.gui.util.AbstractTag2LinkPopupListener;
    8383import org.openstreetmap.josm.gui.util.HighlightHelper;
     84import org.openstreetmap.josm.gui.util.TableHelper;
    8485import org.openstreetmap.josm.gui.widgets.CompileSearchTextDecorator;
    8586import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField;
     
    309310        } else {
    310311            model.setSelectedRelations(relations);
    311             Integer i = model.getVisibleRelationIndex(relations.iterator().next());
    312             if (i != null) {
     312            int i = model.getVisibleRelations().indexOf(relations.iterator().next());
     313            if (i >= 0) {
    313314                // Not all relations have to be in the list
    314315                // (for example when the relation list is hidden, it's not updated with new relations)
     
    584585         */
    585586        public void setSelectedRelations(Collection<? extends IRelation<?>> sel) {
    586             selectionModel.setValueIsAdjusting(true);
    587             selectionModel.clearSelection();
    588587            if (sel != null && !sel.isEmpty()) {
    589588                if (!getVisibleRelations().containsAll(sel)) {
    590589                    resetFilter();
    591590                }
    592                 for (IRelation<?> r: sel) {
    593                     Integer i = getVisibleRelationIndex(r);
    594                     if (i != null) {
    595                         selectionModel.addSelectionInterval(i, i);
    596                     }
    597                 }
    598             }
    599             selectionModel.setValueIsAdjusting(false);
    600         }
    601 
    602         private Integer getVisibleRelationIndex(IRelation<?> rel) {
    603             int i = getVisibleRelations().indexOf(rel);
    604             if (i < 0)
    605                 return null;
    606             return i;
     591                TableHelper.setSelectedIndices(selectionModel, sel.stream().mapToInt(getVisibleRelations()::indexOf));
     592            } else {
     593                TableHelper.setSelectedIndices(selectionModel, IntStream.empty());
     594            }
    607595        }
    608596
  • trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

    r16495 r16601  
    7979import org.openstreetmap.josm.gui.util.GuiHelper;
    8080import org.openstreetmap.josm.gui.util.HighlightHelper;
     81import org.openstreetmap.josm.gui.util.TableHelper;
    8182import org.openstreetmap.josm.gui.widgets.ListPopupMenu;
    8283import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
     
    578579         */
    579580        public synchronized void setSelected(Collection<OsmPrimitive> sel) {
    580             selectionModel.setValueIsAdjusting(true);
    581             selectionModel.clearSelection();
    582             if (sel != null) {
    583                 for (OsmPrimitive p: sel) {
    584                     int i = selection.indexOf(p);
    585                     if (i >= 0) {
    586                         selectionModel.addSelectionInterval(i, i);
    587                     }
    588                 }
    589             }
    590             selectionModel.setValueIsAdjusting(false);
     581            TableHelper.setSelectedIndices(selectionModel,
     582                    sel != null ? sel.stream().mapToInt(selection::indexOf) : IntStream.empty());
    591583        }
    592584
  • trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManagerModel.java

    r16436 r16601  
    1010import java.util.Set;
    1111import java.util.stream.Collectors;
     12import java.util.stream.IntStream;
    1213
    1314import javax.swing.DefaultListSelectionModel;
     
    1920import org.openstreetmap.josm.data.osm.ChangesetCacheListener;
    2021import org.openstreetmap.josm.gui.util.GuiHelper;
     22import org.openstreetmap.josm.gui.util.TableHelper;
    2123
    2224/**
     
    111113     */
    112114    public void setSelectedChangesets(Collection<Changeset> selected) {
    113         selectionModel.setValueIsAdjusting(true);
    114         selectionModel.clearSelection();
    115         if (selected != null) {
    116             for (Changeset cs: selected) {
    117                 final int idx = data.indexOf(cs);
    118                 if (idx >= 0) {
    119                     selectionModel.addSelectionInterval(idx, idx);
    120                 }
    121             }
    122         }
    123         GuiHelper.runInEDTAndWait(() -> selectionModel.setValueIsAdjusting(false));
     115        GuiHelper.runInEDTAndWait(() -> TableHelper.setSelectedIndices(selectionModel,
     116                selected != null ? selected.stream().mapToInt(data::indexOf) : IntStream.empty()));
    124117    }
    125118
  • trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetListModel.java

    r16436 r16601  
    2222import org.openstreetmap.josm.data.osm.Storage;
    2323import org.openstreetmap.josm.gui.util.GuiHelper;
     24import org.openstreetmap.josm.gui.util.TableHelper;
    2425
    2526/**
     
    6263     */
    6364    public synchronized void setSelectedChangesets(Collection<Changeset> changesets) {
    64         selectionModel.setValueIsAdjusting(true);
    65         selectionModel.clearSelection();
    66         if (changesets != null) {
    67             for (Changeset cs: changesets) {
    68                 int idx = data.indexOf(cs);
    69                 if (idx >= 0) {
    70                     selectionModel.addSelectionInterval(idx, idx);
    71                 }
    72             }
    73         }
    74         selectionModel.setValueIsAdjusting(false);
     65        TableHelper.setSelectedIndices(selectionModel,
     66                changesets != null ? changesets.stream().mapToInt(data::indexOf) : IntStream.empty());
    7567    }
    7668
  • trunk/src/org/openstreetmap/josm/gui/history/SelectionSynchronizer.java

    r16600 r16601  
    22package org.openstreetmap.josm.gui.history;
    33
     4import java.util.Arrays;
    45import java.util.HashSet;
    56import java.util.Set;
     
    5354        preventRecursion = true;
    5455        DefaultListSelectionModel referenceModel = (DefaultListSelectionModel) e.getSource();
     56        int[] selectedIndices = TableHelper.getSelectedIndices(referenceModel);
    5557        for (ListSelectionModel model : participants) {
    5658            if (model == e.getSource()) {
    5759                continue;
    5860            }
    59             model.clearSelection();
    60             for (int i : TableHelper.getSelectedIndices(referenceModel)) {
    61                 model.addSelectionInterval(i, i);
    62             }
     61            TableHelper.setSelectedIndices(model, Arrays.stream(selectedIndices));
    6362        }
    6463        preventRecursion = false;
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/ChooseTrackVisibilityAction.java

    r16436 r16601  
    2121import java.util.Optional;
    2222import java.util.stream.Collectors;
     23import java.util.stream.IntStream;
    2324
    2425import javax.swing.AbstractAction;
     
    4647import org.openstreetmap.josm.gui.layer.GpxLayer;
    4748import org.openstreetmap.josm.gui.preferences.display.GPXSettingsPanel;
     49import org.openstreetmap.josm.gui.util.TableHelper;
    4850import org.openstreetmap.josm.gui.util.WindowGeometry;
    4951import org.openstreetmap.josm.tools.GBC;
     
    219221        }
    220222        ListSelectionModel s = table.getSelectionModel();
    221         s.setValueIsAdjusting(true);
    222         s.clearSelection();
    223         for (int i = 0; i < layer.trackVisibility.length; i++) {
    224             if (layer.trackVisibility[i]) {
    225                 s.addSelectionInterval(i, i);
    226             }
    227         }
    228         s.setValueIsAdjusting(false);
     223        TableHelper.setSelectedIndices(s,
     224                IntStream.range(0, layer.trackVisibility.length).filter(i -> layer.trackVisibility[i]));
    229225    }
    230226
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java

    r16553 r16601  
    745745            }
    746746            fireTableDataChanged();
    747             selectionModel.setValueIsAdjusting(true);
    748             selectionModel.clearSelection();
    749             for (ExtendedSourceEntry info: sources) {
    750                 int pos = data.indexOf(info);
    751                 if (pos >= 0) {
    752                     selectionModel.addSelectionInterval(pos, pos);
    753                 }
    754             }
    755             selectionModel.setValueIsAdjusting(false);
     747            TableHelper.setSelectedIndices(selectionModel, sources.stream().mapToInt(data::indexOf));
    756748        }
    757749
  • trunk/src/org/openstreetmap/josm/gui/util/ReorderableTableModel.java

    r16416 r16601  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.util;
     3
     4import java.util.Arrays;
    35
    46import javax.swing.JList;
     
    135137            return false;
    136138        final ListSelectionModel selectionModel = getSelectionModel();
    137         selectionModel.setValueIsAdjusting(true);
    138         selectionModel.clearSelection();
    139         for (int row: selectedRows) {
    140             selectionModel.addSelectionInterval(row + delta, row + delta);
    141         }
    142         selectionModel.setValueIsAdjusting(false);
     139        TableHelper.setSelectedIndices(selectionModel, Arrays.stream(selectedRows).map(i -> i + delta));
    143140        return true;
    144141    }
  • trunk/src/org/openstreetmap/josm/gui/util/TableHelper.java

    r15226 r16601  
    33
    44import java.awt.Component;
     5import java.util.stream.IntStream;
    56
    67import javax.swing.JTable;
     
    147148        return rv;
    148149    }
     150
     151    /**
     152     * Selects the given indices in the selection model
     153     * @param selectionModel list selection model.
     154     * @param indices the indices to select
     155     * @see ListSelectionModel#addSelectionInterval(int, int)
     156     * @since 16601
     157     */
     158    public static void setSelectedIndices(ListSelectionModel selectionModel, IntStream indices) {
     159        selectionModel.setValueIsAdjusting(true);
     160        selectionModel.clearSelection();
     161        indices.filter(i -> i >= 0).forEach(i -> selectionModel.addSelectionInterval(i, i));
     162        selectionModel.setValueIsAdjusting(false);
     163    }
    149164}
Note: See TracChangeset for help on using the changeset viewer.