- Timestamp:
- 2020-06-07T22:40:18+02:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java
r16438 r16566 28 28 import javax.swing.SpinnerNumberModel; 29 29 import javax.swing.SwingUtilities; 30 import javax.swing.event.ChangeEvent; 31 import javax.swing.event.ChangeListener; 30 32 31 33 import org.openstreetmap.josm.command.ChangeCommand; … … 115 117 */ 116 118 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) { 117 131 IPreferences s = Config.getPref(); 118 132 String key = "simplify-way." + (auto ? "auto." : ""); … … 135 149 JPanel q = new JPanel(new GridBagLayout()); 136 150 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); 139 154 ((JSpinner.DefaultEditor) n.getEditor()).getTextField().setColumns(4); 140 155 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 141 166 q.setBorder(BorderFactory.createEmptyBorder(14, 0, 10, 0)); 142 167 p.add(q, GBC.eol()); … … 157 182 int ret = ed.showDialog().getValue(); 158 183 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 } 159 188 if (ret == 1) { 160 189 s.putDouble(keyError, val); … … 188 217 ways.stream().mapToDouble(Way::getLength).sum()); 189 218 190 double err = askSimplifyWays( trn(219 double err = askSimplifyWays(ways, trn( 191 220 "You are about to simplify {0} way with a total length of {1}.", 192 221 "You are about to simplify {0} ways with a total length of {1}.", … … 244 273 * @param ways the ways to simplify 245 274 * @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 246 293 * @since 15419 247 294 */ 248 295 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) { 249 311 Collection<Command> allCommands = ways.stream() 250 312 .map(way -> createSimplifyCommand(way, threshold)) … … 252 314 .collect(StreamUtils.toUnmodifiableList()); 253 315 if (allCommands.isEmpty()) 254 return ;255 SequenceCommand rootCommand =new SequenceCommand(316 return null; 317 return new SequenceCommand( 256 318 trn("Simplify {0} way", "Simplify {0} ways", allCommands.size(), allCommands.size()), 257 319 allCommands); 258 UndoRedoHandler.getInstance().add(rootCommand);259 320 } 260 321 … … 435 496 updateEnabledStateOnModifiableSelection(selection); 436 497 } 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 } 437 527 }
Note:
See TracChangeset
for help on using the changeset viewer.