Ticket #19199: 19199.patch
| File 19199.patch, 5.5 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.ChangeListener; 29 30 30 31 import org.openstreetmap.josm.command.ChangeCommand; 31 32 import org.openstreetmap.josm.command.Command; … … 48 49 import org.openstreetmap.josm.tools.GBC; 49 50 import org.openstreetmap.josm.tools.ImageProvider; 50 51 import org.openstreetmap.josm.tools.Shortcut; 52 import org.openstreetmap.josm.tools.Utils; 51 53 52 54 /** 53 55 * Delete unnecessary nodes from a way … … 112 114 * @since 15419 113 115 */ 114 116 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) { 115 129 IPreferences s = Config.getPref(); 116 130 String key = "simplify-way." + (auto ? "auto." : ""); 117 131 String keyRemember = key + "remember"; … … 132 146 p.setBorder(BorderFactory.createEmptyBorder(5, 10, 10, 5)); 133 147 JPanel q = new JPanel(new GridBagLayout()); 134 148 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); 137 152 ((JSpinner.DefaultEditor) n.getEditor()).getTextField().setColumns(4); 138 153 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 } 139 167 q.setBorder(BorderFactory.createEmptyBorder(14, 0, 10, 0)); 140 168 p.add(q, GBC.eol()); 141 169 JCheckBox c = new JCheckBox(tr("Do not ask again")); … … 185 213 String lengthstr = SystemOfMeasurement.getSystemOfMeasurement().getDistText( 186 214 ways.stream().mapToDouble(Way::getLength).sum()); 187 215 188 double err = askSimplifyWays( trn(216 double err = askSimplifyWays(ways, trn( 189 217 "You are about to simplify {0} way with a total length of {1}.", 190 218 "You are about to simplify {0} ways with a total length of {1}.", 191 219 ways.size(), ways.size(), lengthstr), false); … … 244 272 * 245 273 * @param ways the ways to simplify 246 274 * @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 247 291 * @since 15419 248 292 */ 249 293 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) { 250 309 Collection<Command> allCommands = new LinkedList<>(); 251 310 for (Way way : ways) { 252 311 SequenceCommand simplifyCommand = createSimplifyCommand(way, threshold); … … 256 315 allCommands.add(simplifyCommand); 257 316 } 258 317 if (allCommands.isEmpty()) 259 return ;260 SequenceCommand rootCommand =new SequenceCommand(318 return null; 319 return new SequenceCommand( 261 320 trn("Simplify {0} way", "Simplify {0} ways", allCommands.size(), allCommands.size()), 262 321 allCommands); 263 UndoRedoHandler.getInstance().add(rootCommand);264 322 } 265 323 266 324 /**
