Ticket #19199: 19199.1.patch
| File 19199.1.patch, 7.1 KB (added by , 6 years ago) |
|---|
-
src/org/openstreetmap/josm/actions/SimplifyWayAction.java
26 26 import javax.swing.JSpinner; 27 27 import javax.swing.SpinnerNumberModel; 28 28 import javax.swing.SwingUtilities; 29 import javax.swing.event.ChangeEvent; 30 import javax.swing.event.ChangeListener; 29 31 30 32 import org.openstreetmap.josm.command.ChangeCommand; 31 33 import org.openstreetmap.josm.command.Command; … … 48 50 import org.openstreetmap.josm.tools.GBC; 49 51 import org.openstreetmap.josm.tools.ImageProvider; 50 52 import org.openstreetmap.josm.tools.Shortcut; 53 import org.openstreetmap.josm.tools.Utils; 51 54 52 55 /** 53 56 * Delete unnecessary nodes from a way … … 112 115 * @since 15419 113 116 */ 114 117 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) { 115 130 IPreferences s = Config.getPref(); 116 131 String key = "simplify-way." + (auto ? "auto." : ""); 117 132 String keyRemember = key + "remember"; … … 132 147 p.setBorder(BorderFactory.createEmptyBorder(5, 10, 10, 5)); 133 148 JPanel q = new JPanel(new GridBagLayout()); 134 149 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); 137 153 ((JSpinner.DefaultEditor) n.getEditor()).getTextField().setColumns(4); 138 154 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 } 139 164 q.setBorder(BorderFactory.createEmptyBorder(14, 0, 10, 0)); 140 165 p.add(q, GBC.eol()); 141 166 JCheckBox c = new JCheckBox(tr("Do not ask again")); … … 154 179 155 180 int ret = ed.showDialog().getValue(); 156 181 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 } 157 186 if (ret == 1) { 158 187 s.putDouble(keyError, val); 159 188 if (c.isSelected()) { … … 185 214 String lengthstr = SystemOfMeasurement.getSystemOfMeasurement().getDistText( 186 215 ways.stream().mapToDouble(Way::getLength).sum()); 187 216 188 double err = askSimplifyWays( trn(217 double err = askSimplifyWays(ways, trn( 189 218 "You are about to simplify {0} way with a total length of {1}.", 190 219 "You are about to simplify {0} ways with a total length of {1}.", 191 220 ways.size(), ways.size(), lengthstr), false); … … 244 273 * 245 274 * @param ways the ways to simplify 246 275 * @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 247 292 * @since 15419 248 293 */ 249 294 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) { 250 310 Collection<Command> allCommands = new LinkedList<>(); 251 311 for (Way way : ways) { 252 312 SequenceCommand simplifyCommand = createSimplifyCommand(way, threshold); … … 256 316 allCommands.add(simplifyCommand); 257 317 } 258 318 if (allCommands.isEmpty()) 259 return ;260 SequenceCommand rootCommand =new SequenceCommand(319 return null; 320 return new SequenceCommand( 261 321 trn("Simplify {0} way", "Simplify {0} ways", allCommands.size(), allCommands.size()), 262 322 allCommands); 263 UndoRedoHandler.getInstance().add(rootCommand);264 323 } 265 324 266 325 /** … … 439 498 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 440 499 updateEnabledStateOnModifiableSelection(selection); 441 500 } 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 } 442 529 }
