Ticket #19199: 19199.patch

File 19199.patch, 5.5 KB (added by taylor.smock, 6 years ago)
  • src/org/openstreetmap/josm/actions/SimplifyWayAction.java

     
    2626import javax.swing.JSpinner;
    2727import javax.swing.SpinnerNumberModel;
    2828import javax.swing.SwingUtilities;
     29import javax.swing.event.ChangeListener;
    2930
    3031import org.openstreetmap.josm.command.ChangeCommand;
    3132import org.openstreetmap.josm.command.Command;
     
    4849import org.openstreetmap.josm.tools.GBC;
    4950import org.openstreetmap.josm.tools.ImageProvider;
    5051import org.openstreetmap.josm.tools.Shortcut;
     52import org.openstreetmap.josm.tools.Utils;
    5153
    5254/**
    5355 * Delete unnecessary nodes from a way
     
    112114     * @since 15419
    113115     */
    114116    public static double askSimplifyWays(String text, boolean auto) {
     117        return askSimplifyWays(Collections.emptyList(), text, auto);
     118    }
     119
     120    /**
     121     * Asks the user for max-err value used to simplify ways, if not remembered before
     122     * @param ways the ways that are being simplified (to show estimated number of nodes to be removed)
     123     * @param text the text being shown
     124     * @param auto whether it's called automatically (conversion) or by the user
     125     * @return the max-err value or -1 if canceled
     126     * @since xxx
     127     */
     128    public static double askSimplifyWays(List<Way> ways, String text, boolean auto) {
    115129        IPreferences s = Config.getPref();
    116130        String key = "simplify-way." + (auto ? "auto." : "");
    117131        String keyRemember = key + "remember";
     
    132146        p.setBorder(BorderFactory.createEmptyBorder(5, 10, 10, 5));
    133147        JPanel q = new JPanel(new GridBagLayout());
    134148        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));
     149        SpinnerNumberModel errorModel = new SpinnerNumberModel(
     150                s.getDouble(keyError, 3.0), 0.01, null, 0.5);
     151        JSpinner n = new JSpinner(errorModel);
    137152        ((JSpinner.DefaultEditor) n.getEditor()).getTextField().setColumns(4);
    138153        q.add(n);
     154        if (!ways.isEmpty()) {
     155            JLabel estNodesToRemove = new JLabel();
     156            ChangeListener l = e -> {
     157                int removeNodes = simplifyWaysCountNodesRemoved(ways, errorModel.getNumber().doubleValue());
     158                estNodesToRemove.setText(trn("(about {0} node to remove)",
     159                        "(about {0} nodes to remove)", removeNodes, removeNodes
     160                        ));
     161            };
     162            errorModel.addChangeListener(l);
     163            l.stateChanged(null);
     164            q.add(estNodesToRemove);
     165            errorModel.getChangeListeners();
     166        }
    139167        q.setBorder(BorderFactory.createEmptyBorder(14, 0, 10, 0));
    140168        p.add(q, GBC.eol());
    141169        JCheckBox c = new JCheckBox(tr("Do not ask again"));
     
    185213            String lengthstr = SystemOfMeasurement.getSystemOfMeasurement().getDistText(
    186214                    ways.stream().mapToDouble(Way::getLength).sum());
    187215
    188             double err = askSimplifyWays(trn(
     216            double err = askSimplifyWays(ways, trn(
    189217                    "You are about to simplify {0} way with a total length of {1}.",
    190218                    "You are about to simplify {0} ways with a total length of {1}.",
    191219                    ways.size(), ways.size(), lengthstr), false);
     
    244272     *
    245273     * @param ways the ways to simplify
    246274     * @param threshold the max error threshold
     275     * @return The number of nodes removed from the ways (does not double-count)
     276     * @since xxx
     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 Utils.filteredCollection(new ArrayList<>(command.getParticipatingPrimitives()), Node.class).size();
     284    }
     285
     286    /**
     287     * Runs the commands to simplify the ways with the given threshold
     288     *
     289     * @param ways the ways to simplify
     290     * @param threshold the max error threshold
    247291     * @since 15419
    248292     */
    249293    public static void simplifyWays(List<Way> ways, double threshold) {
     294        Command command = buildSimplifyWaysCommand(ways, threshold);
     295        if (command != null) {
     296            UndoRedoHandler.getInstance().add(command);
     297        }
     298    }
     299
     300    /**
     301     * Creates the commands to simplify the ways with the given threshold
     302     *
     303     * @param ways the ways to simplify
     304     * @param threshold the max error threshold
     305     * @return The command to simplify ways
     306     * @since xxx (private)
     307     */
     308    private static SequenceCommand buildSimplifyWaysCommand(List<Way> ways, double threshold) {
    250309        Collection<Command> allCommands = new LinkedList<>();
    251310        for (Way way : ways) {
    252311            SequenceCommand simplifyCommand = createSimplifyCommand(way, threshold);
     
    256315            allCommands.add(simplifyCommand);
    257316        }
    258317        if (allCommands.isEmpty())
    259             return;
    260         SequenceCommand rootCommand = new SequenceCommand(
     318            return null;
     319        return new SequenceCommand(
    261320                trn("Simplify {0} way", "Simplify {0} ways", allCommands.size(), allCommands.size()),
    262321                allCommands);
    263         UndoRedoHandler.getInstance().add(rootCommand);
    264322    }
    265323
    266324    /**