Changeset 16566 in josm for trunk


Ignore:
Timestamp:
2020-06-07T22:40:18+02:00 (4 years ago)
Author:
simon04
Message:

fix #19199 - SimplifyWayAction: display how many nodes are going to be removed (patch by taylor.smock, modified)

File:
1 edited

Legend:

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

    r16438 r16566  
    2828import javax.swing.SpinnerNumberModel;
    2929import javax.swing.SwingUtilities;
     30import javax.swing.event.ChangeEvent;
     31import javax.swing.event.ChangeListener;
    3032
    3133import org.openstreetmap.josm.command.ChangeCommand;
     
    115117     */
    116118    public static double askSimplifyWays(String text, boolean auto) {
     119        return askSimplifyWays(Collections.emptyList(), text, auto);
     120    }
     121
     122    /**
     123     * Asks the user for max-err value used to simplify ways, if not remembered before
     124     * @param ways the ways that are being simplified (to show estimated number of nodes to be removed)
     125     * @param text the text being shown
     126     * @param auto whether it's called automatically (conversion) or by the user
     127     * @return the max-err value or -1 if canceled
     128     * @since 16566
     129     */
     130    public static double askSimplifyWays(List<Way> ways, String text, boolean auto) {
    117131        IPreferences s = Config.getPref();
    118132        String key = "simplify-way." + (auto ? "auto." : "");
     
    135149        JPanel q = new JPanel(new GridBagLayout());
    136150        q.add(new JLabel(tr("Maximum error (meters): ")));
    137         JSpinner n = new JSpinner(new SpinnerNumberModel(
    138                 s.getDouble(keyError, 3.0), 0.01, null, 0.5));
     151        SpinnerNumberModel errorModel = new SpinnerNumberModel(
     152                s.getDouble(keyError, 3.0), 0.01, null, 0.5);
     153        JSpinner n = new JSpinner(errorModel);
    139154        ((JSpinner.DefaultEditor) n.getEditor()).getTextField().setColumns(4);
    140155        q.add(n);
     156
     157        JLabel nodesToRemove = new JLabel();
     158        SimplifyChangeListener l = new SimplifyChangeListener(nodesToRemove, errorModel, ways);
     159        if (!ways.isEmpty()) {
     160            errorModel.addChangeListener(l);
     161            l.stateChanged(null);
     162            q.add(nodesToRemove, GBC.std().insets(5, 0, 0, 0));
     163            errorModel.getChangeListeners();
     164        }
     165
    141166        q.setBorder(BorderFactory.createEmptyBorder(14, 0, 10, 0));
    142167        p.add(q, GBC.eol());
     
    157182        int ret = ed.showDialog().getValue();
    158183        double val = (double) n.getValue();
     184        if (l.lastCommand != null && l.lastCommand.equals(UndoRedoHandler.getInstance().getLastCommand())) {
     185            UndoRedoHandler.getInstance().undo();
     186            l.lastCommand = null;
     187        }
    159188        if (ret == 1) {
    160189            s.putDouble(keyError, val);
     
    188217                    ways.stream().mapToDouble(Way::getLength).sum());
    189218
    190             double err = askSimplifyWays(trn(
     219            double err = askSimplifyWays(ways, trn(
    191220                    "You are about to simplify {0} way with a total length of {1}.",
    192221                    "You are about to simplify {0} ways with a total length of {1}.",
     
    244273     * @param ways the ways to simplify
    245274     * @param threshold the max error threshold
     275     * @return The number of nodes removed from the ways (does not double-count)
     276     * @since 16566
     277     */
     278    public static int simplifyWaysCountNodesRemoved(List<Way> ways, double threshold) {
     279        Command command = buildSimplifyWaysCommand(ways, threshold);
     280        if (command == null) {
     281            return 0;
     282        }
     283        return (int) command.getParticipatingPrimitives().stream()
     284                .filter(Node.class::isInstance)
     285                .count();
     286    }
     287
     288    /**
     289     * Runs the commands to simplify the ways with the given threshold
     290     *
     291     * @param ways the ways to simplify
     292     * @param threshold the max error threshold
    246293     * @since 15419
    247294     */
    248295    public static void simplifyWays(List<Way> ways, double threshold) {
     296        Command command = buildSimplifyWaysCommand(ways, threshold);
     297        if (command != null) {
     298            UndoRedoHandler.getInstance().add(command);
     299        }
     300    }
     301
     302    /**
     303     * Creates the commands to simplify the ways with the given threshold
     304     *
     305     * @param ways the ways to simplify
     306     * @param threshold the max error threshold
     307     * @return The command to simplify ways
     308     * @since 16566 (private)
     309     */
     310    private static SequenceCommand buildSimplifyWaysCommand(List<Way> ways, double threshold) {
    249311        Collection<Command> allCommands = ways.stream()
    250312                .map(way -> createSimplifyCommand(way, threshold))
     
    252314                .collect(StreamUtils.toUnmodifiableList());
    253315        if (allCommands.isEmpty())
    254             return;
    255         SequenceCommand rootCommand = new SequenceCommand(
     316            return null;
     317        return new SequenceCommand(
    256318                trn("Simplify {0} way", "Simplify {0} ways", allCommands.size(), allCommands.size()),
    257319                allCommands);
    258         UndoRedoHandler.getInstance().add(rootCommand);
    259320    }
    260321
     
    435496        updateEnabledStateOnModifiableSelection(selection);
    436497    }
     498
     499    private static class SimplifyChangeListener implements ChangeListener {
     500        Command lastCommand;
     501        private final JLabel nodesToRemove;
     502        private final SpinnerNumberModel errorModel;
     503        private final List<Way> ways;
     504
     505        SimplifyChangeListener(JLabel nodesToRemove, SpinnerNumberModel errorModel, List<Way> ways) {
     506            this.nodesToRemove = nodesToRemove;
     507            this.errorModel = errorModel;
     508            this.ways = ways;
     509        }
     510
     511        @Override
     512        public void stateChanged(ChangeEvent e) {
     513            if (Objects.equals(UndoRedoHandler.getInstance().getLastCommand(), lastCommand)) {
     514                UndoRedoHandler.getInstance().undo();
     515            }
     516            double threshold = errorModel.getNumber().doubleValue();
     517            int removeNodes = simplifyWaysCountNodesRemoved(ways, threshold);
     518            nodesToRemove.setText(trn(
     519                    "(about {0} node to remove)",
     520                    "(about {0} nodes to remove)", removeNodes, removeNodes));
     521            lastCommand = SimplifyWayAction.buildSimplifyWaysCommand(ways, threshold);
     522            if (lastCommand != null) {
     523                UndoRedoHandler.getInstance().add(lastCommand);
     524            }
     525        }
     526    }
    437527}
Note: See TracChangeset for help on using the changeset viewer.