Changeset 17773 in josm


Ignore:
Timestamp:
2021-04-13T22:20:22+02:00 (3 years ago)
Author:
simon04
Message:

see #20745 - Introduce TableHelper.selectedIndices

Location:
trunk/src/org/openstreetmap/josm
Files:
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/RestorePropertyAction.java

    r17034 r17773  
    66import java.awt.event.ActionEvent;
    77import java.util.Collections;
    8 import java.util.HashMap;
     8import java.util.Map;
    99import java.util.function.IntFunction;
    1010import java.util.function.Supplier;
     11import java.util.stream.Collectors;
    1112
    1213import javax.swing.AbstractAction;
     
    5455        if (primitive == null) return;
    5556
    56         HashMap<String, String> changes = new HashMap<>();
    57         for (int index : TableHelper.getSelectedIndices(selectionModel)) {
    58             changes.put(keyFn.apply(index), valueFn.apply(index));
    59         }
     57        Map<String, String> changes = TableHelper.selectedIndices(selectionModel).boxed()
     58                .collect(Collectors.toMap(keyFn::apply, valueFn::apply, (a, b) -> b));
    6059        ChangePropertyCommand command = new ChangePropertyCommand(Collections.singleton(primitive), changes);
    6160        UndoRedoHandler.getInstance().add(command);
  • trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java

    r17626 r17773  
    9090import org.openstreetmap.josm.gui.widgets.ScrollableTable;
    9191import org.openstreetmap.josm.spi.preferences.Config;
    92 import org.openstreetmap.josm.tools.ArrayUtils;
    9392import org.openstreetmap.josm.tools.ImageProvider;
    9493import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
     
    892891         */
    893892        public List<Integer> getSelectedRows() {
    894             return ArrayUtils.toList(TableHelper.getSelectedIndices(selectionModel));
     893            return TableHelper.selectedIndices(selectionModel).boxed().collect(Collectors.toList());
    895894        }
    896895
     
    905904            layer.removePropertyChangeListener(this);
    906905            final int size = getRowCount();
    907             final int[] rows = TableHelper.getSelectedIndices(selectionModel);
    908 
    909             if (rows.length == 0 && size > 0) {
     906
     907            if (selectionModel.isSelectionEmpty() && size > 0) {
    910908                selectionModel.setSelectionInterval(size-1, size-1);
    911909            }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableModel.java

    r17423 r17773  
    33
    44import java.util.ArrayList;
    5 import java.util.Arrays;
    65import java.util.BitSet;
    76import java.util.Collection;
     
    5150import org.openstreetmap.josm.gui.util.SortableTableModel;
    5251import org.openstreetmap.josm.gui.widgets.OsmPrimitivesTableModel;
    53 import org.openstreetmap.josm.tools.ArrayUtils;
    5452import org.openstreetmap.josm.tools.JosmRuntimeException;
    5553import org.openstreetmap.josm.tools.bugreport.BugReport;
     
    513511     */
    514512    public Collection<RelationMember> getSelectedMembers() {
    515         return Arrays.stream(getSelectedIndices())
     513        return selectedIndices()
    516514                .mapToObj(members::get)
    517515                .collect(Collectors.toList());
     
    692690        addToSelectedMembers(selected);
    693691        getSelectionModel().setValueIsAdjusting(false);
    694         int[] selectedIndices = getSelectedIndices();
    695         if (selectedIndices.length > 0) {
    696             fireMakeMemberVisible(selectedIndices[0]);
    697         }
     692        selectedIndices().findFirst().ifPresent(this::fireMakeMemberVisible);
    698693    }
    699694
     
    723718        } else {
    724719            sortedMembers = relationSorter.sortMembers(selectedMembers);
    725             List<Integer> selectedIndices = ArrayUtils.toList(getSelectedIndices());
     720            List<Integer> selectedIndices = selectedIndices().boxed().collect(Collectors.toList());
    726721            newMembers = new ArrayList<>();
    727722            boolean inserted = false;
     
    784779    @Override
    785780    public void reverse() {
    786         List<Integer> selectedIndices = ArrayUtils.toList(getSelectedIndices());
    787         List<Integer> selectedIndicesReversed = ArrayUtils.toList(getSelectedIndices());
     781        List<Integer> selectedIndices = selectedIndices().boxed().collect(Collectors.toList());
     782        List<Integer> selectedIndicesReversed = selectedIndices().boxed().collect(Collectors.toList());
    788783
    789784        if (selectedIndices.size() <= 1) {
  • trunk/src/org/openstreetmap/josm/gui/history/SelectionSynchronizer.java

    r16609 r17773  
    22package org.openstreetmap.josm.gui.history;
    33
    4 import java.util.Arrays;
    54import java.util.HashSet;
    65import java.util.Objects;
     
    6160        preventRecursion = true;
    6261        ListSelectionModel referenceModel = (ListSelectionModel) e.getSource();
    63         int[] selectedIndices = TableHelper.getSelectedIndices(referenceModel);
    6462        for (ListSelectionModel model : participants) {
    6563            if (model == referenceModel) {
     
    6765            }
    6866            TableHelper.setSelectedIndices(model,
    69                     Arrays.stream(selectedIndices).flatMap(i -> selectionIndexMapper.apply(i, referenceModel)));
     67                    TableHelper.selectedIndices(referenceModel).flatMap(i -> selectionIndexMapper.apply(i, referenceModel)));
    7068        }
    7169        preventRecursion = false;
  • trunk/src/org/openstreetmap/josm/gui/util/ReorderableTableModel.java

    r16601 r17773  
    33
    44import java.util.Arrays;
     5import java.util.stream.IntStream;
    56
    67import javax.swing.JList;
     
    3839     * Returns an array of all of the selected indices in the selection model, in increasing order.
    3940     * @return an array of all of the selected indices in the selection model, in increasing order
     41     * @see #selectedIndices()
    4042     */
    4143    default int[] getSelectedIndices() {
    4244        return TableHelper.getSelectedIndices(getSelectionModel());
     45    }
     46
     47    /**
     48     * Returns a stream of all of the selected indices in the selection model, in increasing order.
     49     * @return a stream of all of the selected indices in the selection model, in increasing order
     50     * @since 17773
     51     */
     52    default IntStream selectedIndices() {
     53        return TableHelper.selectedIndices(getSelectionModel());
    4354    }
    4455
  • trunk/src/org/openstreetmap/josm/gui/util/TableHelper.java

    r16960 r17773  
    123123     * Unfortunately this method is not available in OpenJDK before version 11, see
    124124     * https://bugs.openjdk.java.net/browse/JDK-8199395
    125      * Code taken from OpenJDK 11. To be removed when we switch to Java 11 or later.
     125     *
     126     * To be removed when we switch to Java 11 or later.
    126127     *
    127128     * @param selectionModel list selection model.
     
    130131     *         or an empty array if nothing is selected
    131132     * @since 15226
     133     * @see #selectedIndices(ListSelectionModel)
    132134     */
    133135    public static int[] getSelectedIndices(ListSelectionModel selectionModel) {
    134         int iMin = selectionModel.getMinSelectionIndex();
    135         int iMax = selectionModel.getMaxSelectionIndex();
     136        return selectedIndices(selectionModel).toArray();
     137    }
    136138
    137         if (iMin < 0 || iMax < 0) {
    138             return new int[0];
     139    /**
     140     * Returns a stream of all of the selected indices in the selection model, in increasing order.
     141     *
     142     * @param selectionModel list selection model.
     143     *
     144     * @return all of the selected indices, in increasing order,
     145     *         or an empty stream if nothing is selected
     146     * @since 17773
     147     */
     148    public static IntStream selectedIndices(ListSelectionModel selectionModel) {
     149        if (selectionModel.isSelectionEmpty()) {
     150            return IntStream.empty();
    139151        }
    140 
    141         int[] rvTmp = new int[1 + iMax - iMin];
    142         int n = 0;
    143         for (int i = iMin; i <= iMax; i++) {
    144             if (selectionModel.isSelectedIndex(i)) {
    145                 rvTmp[n++] = i;
    146             }
    147         }
    148         int[] rv = new int[n];
    149         System.arraycopy(rvTmp, 0, rv, 0, n);
    150         return rv;
     152        return IntStream.rangeClosed(selectionModel.getMinSelectionIndex(), selectionModel.getMaxSelectionIndex())
     153                .filter(selectionModel::isSelectedIndex);
    151154    }
    152155
Note: See TracChangeset for help on using the changeset viewer.