Ticket #19199: 19199.1.patch

File 19199.1.patch, 7.1 KB (added by taylor.smock, 6 years ago)

This was an attempt to allow the mapview display how the maximum error (threshold) actually affected the way. This does not work, since the EDT is blocked.

  • src/org/openstreetmap/josm/actions/SimplifyWayAction.java

     
    2626import javax.swing.JSpinner;
    2727import javax.swing.SpinnerNumberModel;
    2828import javax.swing.SwingUtilities;
     29import javax.swing.event.ChangeEvent;
     30import javax.swing.event.ChangeListener;
    2931
    3032import org.openstreetmap.josm.command.ChangeCommand;
    3133import org.openstreetmap.josm.command.Command;
     
    4850import org.openstreetmap.josm.tools.GBC;
    4951import org.openstreetmap.josm.tools.ImageProvider;
    5052import org.openstreetmap.josm.tools.Shortcut;
     53import org.openstreetmap.josm.tools.Utils;
    5154
    5255/**
    5356 * Delete unnecessary nodes from a way
     
    112115     * @since 15419
    113116     */
    114117    public static double askSimplifyWays(String text, boolean auto) {
     118        return askSimplifyWays(Collections.emptyList(), text, auto);
     119    }
     120
     121    /**
     122     * Asks the user for max-err value used to simplify ways, if not remembered before
     123     * @param ways the ways that are being simplified (to show estimated number of nodes to be removed)
     124     * @param text the text being shown
     125     * @param auto whether it's called automatically (conversion) or by the user
     126     * @return the max-err value or -1 if canceled
     127     * @since xxx
     128     */
     129    public static double askSimplifyWays(List<Way> ways, String text, boolean auto) {
    115130        IPreferences s = Config.getPref();
    116131        String key = "simplify-way." + (auto ? "auto." : "");
    117132        String keyRemember = key + "remember";
     
    132147        p.setBorder(BorderFactory.createEmptyBorder(5, 10, 10, 5));
    133148        JPanel q = new JPanel(new GridBagLayout());
    134149        q.add(new JLabel(tr("Maximum error (meters): ")));
    135         JSpinner n = new JSpinner(new SpinnerNumberModel(
    136                 s.getDouble(keyError, 3.0), 0.01, null, 0.5));
     150        SpinnerNumberModel errorModel = new SpinnerNumberModel(
     151                s.getDouble(keyError, 3.0), 0.01, null, 0.5);
     152        JSpinner n = new JSpinner(errorModel);
    137153        ((JSpinner.DefaultEditor) n.getEditor()).getTextField().setColumns(4);
    138154        q.add(n);
     155        JLabel estNodesToRemove = new JLabel();
     156        SimplifyChangeListener l = new SimplifyChangeListener(estNodesToRemove, errorModel, ways);
     157
     158        if (!ways.isEmpty()) {
     159            errorModel.addChangeListener(l);
     160            l.stateChanged(null);
     161            q.add(estNodesToRemove);
     162            errorModel.getChangeListeners();
     163        }
    139164        q.setBorder(BorderFactory.createEmptyBorder(14, 0, 10, 0));
    140165        p.add(q, GBC.eol());
    141166        JCheckBox c = new JCheckBox(tr("Do not ask again"));
     
    154179
    155180        int ret = ed.showDialog().getValue();
    156181        double val = (double) n.getValue();
     182        if (l.lastCommand != null && l.lastCommand.equals(UndoRedoHandler.getInstance().getLastCommand())) {
     183            UndoRedoHandler.getInstance().undo();
     184            l.lastCommand = null;
     185        }
    157186        if (ret == 1) {
    158187            s.putDouble(keyError, val);
    159188            if (c.isSelected()) {
     
    185214            String lengthstr = SystemOfMeasurement.getSystemOfMeasurement().getDistText(
    186215                    ways.stream().mapToDouble(Way::getLength).sum());
    187216
    188             double err = askSimplifyWays(trn(
     217            double err = askSimplifyWays(ways, trn(
    189218                    "You are about to simplify {0} way with a total length of {1}.",
    190219                    "You are about to simplify {0} ways with a total length of {1}.",
    191220                    ways.size(), ways.size(), lengthstr), false);
     
    244273     *
    245274     * @param ways the ways to simplify
    246275     * @param threshold the max error threshold
     276     * @return The number of nodes removed from the ways (does not double-count)
     277     * @since xxx
     278     */
     279    public static int simplifyWaysCountNodesRemoved(List<Way> ways, double threshold) {
     280        Command command = buildSimplifyWaysCommand(ways, threshold);
     281        if (command == null) {
     282            return 0;
     283        }
     284        return Utils.filteredCollection(new ArrayList<>(command.getParticipatingPrimitives()), Node.class).size();
     285    }
     286
     287    /**
     288     * Runs the commands to simplify the ways with the given threshold
     289     *
     290     * @param ways the ways to simplify
     291     * @param threshold the max error threshold
    247292     * @since 15419
    248293     */
    249294    public static void simplifyWays(List<Way> ways, double threshold) {
     295        Command command = buildSimplifyWaysCommand(ways, threshold);
     296        if (command != null) {
     297            UndoRedoHandler.getInstance().add(command);
     298        }
     299    }
     300
     301    /**
     302     * Creates the commands to simplify the ways with the given threshold
     303     *
     304     * @param ways the ways to simplify
     305     * @param threshold the max error threshold
     306     * @return The command to simplify ways
     307     * @since xxx (private)
     308     */
     309    private static SequenceCommand buildSimplifyWaysCommand(List<Way> ways, double threshold) {
    250310        Collection<Command> allCommands = new LinkedList<>();
    251311        for (Way way : ways) {
    252312            SequenceCommand simplifyCommand = createSimplifyCommand(way, threshold);
     
    256316            allCommands.add(simplifyCommand);
    257317        }
    258318        if (allCommands.isEmpty())
    259             return;
    260         SequenceCommand rootCommand = new SequenceCommand(
     319            return null;
     320        return new SequenceCommand(
    261321                trn("Simplify {0} way", "Simplify {0} ways", allCommands.size(), allCommands.size()),
    262322                allCommands);
    263         UndoRedoHandler.getInstance().add(rootCommand);
    264323    }
    265324
    266325    /**
     
    439498    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
    440499        updateEnabledStateOnModifiableSelection(selection);
    441500    }
     501
     502    private static class SimplifyChangeListener implements ChangeListener {
     503        Command lastCommand;
     504        private JLabel estNodesToRemove;
     505        private SpinnerNumberModel errorModel;
     506        private List<Way> ways;
     507        SimplifyChangeListener(JLabel estNodesToRemove, SpinnerNumberModel errorModel, List<Way> ways) {
     508            this.estNodesToRemove = estNodesToRemove;
     509            this.errorModel = errorModel;
     510            this.ways = ways;
     511        }
     512
     513        @Override
     514        public void stateChanged(ChangeEvent e) {
     515            if (UndoRedoHandler.getInstance().hasUndoCommands() && UndoRedoHandler.getInstance().getLastCommand().equals(lastCommand)) {
     516                UndoRedoHandler.getInstance().undo();
     517            }
     518            double threshold = errorModel.getNumber().doubleValue();
     519            int removeNodes = simplifyWaysCountNodesRemoved(ways, threshold);
     520            estNodesToRemove.setText(trn("(about {0} node to remove)",
     521                    "(about {0} nodes to remove)", removeNodes, removeNodes
     522                    ));
     523            lastCommand = SimplifyWayAction.buildSimplifyWaysCommand(ways, threshold);
     524            if (lastCommand != null) {
     525                UndoRedoHandler.getInstance().add(lastCommand);
     526            }
     527        }
     528    }
    442529}