Ticket #7630: 7630.2.patch
File 7630.2.patch, 14.2 KB (added by , 9 years ago) |
---|
-
src/org/openstreetmap/josm/actions/AutoScaleAction.java
7 7 8 8 import java.awt.event.ActionEvent; 9 9 import java.awt.event.KeyEvent; 10 import java.awt.geom.Area; 11 import java.util.ArrayList; 10 12 import java.util.Arrays; 11 13 import java.util.Collection; 12 14 import java.util.Collections; 13 15 import java.util.HashSet; 14 16 import java.util.List; 17 import java.util.ListIterator; 18 import java.util.concurrent.TimeUnit; 15 19 16 20 import javax.swing.JOptionPane; 17 21 import javax.swing.event.ListSelectionEvent; … … 21 25 22 26 import org.openstreetmap.josm.Main; 23 27 import org.openstreetmap.josm.data.Bounds; 28 import org.openstreetmap.josm.data.DataSource; 24 29 import org.openstreetmap.josm.data.conflict.Conflict; 25 30 import org.openstreetmap.josm.data.osm.OsmPrimitive; 26 31 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; … … 30 35 import org.openstreetmap.josm.gui.MapView; 31 36 import org.openstreetmap.josm.gui.dialogs.LayerListDialog; 32 37 import org.openstreetmap.josm.gui.dialogs.ValidatorDialog.ValidatorBoundingXYVisitor; 33 import org.openstreetmap.josm.gui.download.DownloadDialog;34 38 import org.openstreetmap.josm.gui.layer.Layer; 35 39 import org.openstreetmap.josm.tools.Shortcut; 36 40 … … 41 45 public class AutoScaleAction extends JosmAction { 42 46 43 47 public static final Collection<String> MODES = Collections.unmodifiableList(Arrays.asList( 44 marktr(/* ICON(dialogs/autoscale/) */ "data"), 45 marktr(/* ICON(dialogs/autoscale/) */ "layer"), 46 marktr(/* ICON(dialogs/autoscale/) */ "selection"), 47 marktr(/* ICON(dialogs/autoscale/) */ "conflict"), 48 marktr(/* ICON(dialogs/autoscale/) */ "download"), 49 marktr(/* ICON(dialogs/autoscale/) */ "problem"), 50 marktr(/* ICON(dialogs/autoscale/) */ "previous"), 51 marktr(/* ICON(dialogs/autoscale/) */ "next"))); 48 marktr(/* ICON(dialogs/autoscale/) */"data"), marktr(/* ICON(dialogs/autoscale/) */"layer"), 49 marktr(/* ICON(dialogs/autoscale/) */"selection"), marktr(/* ICON(dialogs/autoscale/) */"conflict"), 50 marktr(/* ICON(dialogs/autoscale/) */"download"), marktr(/* ICON(dialogs/autoscale/) */"problem"), 51 marktr(/* ICON(dialogs/autoscale/) */"previous"), marktr(/* ICON(dialogs/autoscale/) */"next"))); 52 52 53 53 private final String mode; 54 54 55 55 protected ZoomChangeAdapter zoomChangeAdapter; 56 56 protected MapFrameAdapter mapFrameAdapter; 57 protected DataSource previousDataSource; 58 protected Collection<DataSource> dataSources; 59 protected ArrayList<DataSource> dataSourcesList; 60 protected int firstPass = 0; 61 protected long lastZoomTime = -1; 57 62 63 private long timeDifference; 64 65 private long seconds; 66 58 67 /** 59 68 * Zooms the current map view to the currently selected primitives. 60 69 * Does nothing if there either isn't a current map view or if there isn't a current data … … 62 71 * 63 72 */ 64 73 public static void zoomToSelection() { 65 if (Main.main == null || !Main.main.hasEditLayer()) return; 74 if (Main.main == null || !Main.main.hasEditLayer()) 75 return; 66 76 Collection<OsmPrimitive> sel = Main.main.getEditLayer().data.getSelected(); 67 77 if (sel.isEmpty()) { 68 JOptionPane.showMessageDialog( 69 Main.parent, 70 tr("Nothing selected to zoom to."), 71 tr("Information"), 72 JOptionPane.INFORMATION_MESSAGE 73 ); 78 JOptionPane.showMessageDialog(Main.parent, tr("Nothing selected to zoom to."), tr("Information"), 79 JOptionPane.INFORMATION_MESSAGE); 74 80 return; 75 81 } 76 82 zoomTo(sel); … … 97 103 98 104 // TODO: convert this to switch/case and make sure the parsing still works 99 105 /* leave as single line for shortcut overview parsing! */ 100 if (mode.equals("data")) { shortcut = KeyEvent.VK_1; } 101 else if (mode.equals("layer")) { shortcut = KeyEvent.VK_2; } 102 else if (mode.equals("selection")) { shortcut = KeyEvent.VK_3; } 103 else if (mode.equals("conflict")) { shortcut = KeyEvent.VK_4; } 104 else if (mode.equals("download")) { shortcut = KeyEvent.VK_5; } 105 else if (mode.equals("problem")) { shortcut = KeyEvent.VK_6; } 106 else if (mode.equals("previous")) { shortcut = KeyEvent.VK_8; } 107 else if (mode.equals("next")) { shortcut = KeyEvent.VK_9; } 106 if (mode.equals("data")) { 107 shortcut = KeyEvent.VK_1; 108 } else if (mode.equals("layer")) { 109 shortcut = KeyEvent.VK_2; 110 } else if (mode.equals("selection")) { 111 shortcut = KeyEvent.VK_3; 112 } else if (mode.equals("conflict")) { 113 shortcut = KeyEvent.VK_4; 114 } else if (mode.equals("download")) { 115 shortcut = KeyEvent.VK_5; 116 } else if (mode.equals("problem")) { 117 shortcut = KeyEvent.VK_6; 118 } else if (mode.equals("previous")) { 119 shortcut = KeyEvent.VK_8; 120 } else if (mode.equals("next")) { 121 shortcut = KeyEvent.VK_9; 122 } 108 123 109 124 return shortcut; 110 125 } … … 124 139 * @param mode The autoscale mode (one of {@link AutoScaleAction#MODES}) 125 140 */ 126 141 public AutoScaleAction(final String mode) { 127 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)), 128 Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.DIRECT),129 true, null, false);142 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)), Shortcut 143 .registerShortcut("view:zoom" + mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), 144 getModeShortcut(mode), Shortcut.DIRECT), true, null, false); 130 145 String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1); 131 146 putValue("help", "Action/AutoScale/" + modeHelp); 132 147 this.mode = mode; … … 156 171 putValue("help", ht("/Action/ZoomToNext")); 157 172 break; 158 173 default: 159 throw new IllegalArgumentException("Unknown mode: " +mode);174 throw new IllegalArgumentException("Unknown mode: " + mode); 160 175 } 161 176 installAdapters(); 162 177 } 163 178 164 public void autoScale() 179 public void autoScale() { 165 180 if (Main.isDisplayingMapView()) { 166 switch (mode) {181 switch (mode) { 167 182 case "previous": 168 183 Main.map.mapView.zoomPrevious(); 169 184 break; … … 194 209 */ 195 210 protected Layer getFirstSelectedLayer() { 196 211 List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers(); 197 if (layers.isEmpty()) return null; 212 if (layers.isEmpty()) 213 return null; 198 214 return layers.get(0); 199 215 } 200 216 … … 201 217 private BoundingXYVisitor getBoundingBox() { 202 218 BoundingXYVisitor v = "problem".equals(mode) ? new ValidatorBoundingXYVisitor() : new BoundingXYVisitor(); 203 219 204 switch (mode) {220 switch (mode) { 205 221 case "problem": 206 222 TestError error = Main.map.validatorDialog.getSelectedError(); 207 if (error == null) return null; 223 if (error == null) 224 return null; 208 225 ((ValidatorBoundingXYVisitor) v).visit(error); 209 if (v.getBounds() == null) return null; 226 if (v.getBounds() == null) 227 return null; 210 228 v.enlargeBoundingBox(Main.pref.getDouble("validator.zoom-enlarge-bbox", 0.0002)); 211 229 break; 212 230 case "data": … … 219 237 return null; 220 238 // try to zoom to the first selected layer 221 239 Layer l = getFirstSelectedLayer(); 222 if (l == null) return null; 240 if (l == null) 241 return null; 223 242 l.visitBoundingBox(v); 224 243 break; 225 244 case "selection": … … 236 255 } 237 256 } 238 257 if (sel.isEmpty()) { 239 JOptionPane.showMessageDialog( 240 Main.parent, 241 ("selection".equals(mode) ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")), 242 tr("Information"), 243 JOptionPane.INFORMATION_MESSAGE 244 ); 258 JOptionPane 259 .showMessageDialog(Main.parent, ("selection".equals(mode) ? tr("Nothing selected to zoom to.") 260 : tr("No conflicts to zoom to")), tr("Information"), JOptionPane.INFORMATION_MESSAGE); 245 261 return null; 246 262 } 247 263 for (OsmPrimitive osm : sel) { … … 255 271 v.enlargeToMinSize(Main.pref.getDouble("zoom_to_selection_min_size_in_meter", 100)); 256 272 break; 257 273 case "download": 258 Bounds bounds = DownloadDialog.getSavedDownloadBounds(); 274 275 if (lastZoomTime == -1) { 276 seconds = 0; 277 } else { 278 timeDifference = System.currentTimeMillis() - lastZoomTime; 279 seconds = TimeUnit.MILLISECONDS.toSeconds(timeDifference); 280 } 281 282 Bounds bounds; 283 dataSources = Main.main.getCurrentDataSet().getDataSources(); 284 dataSourcesList = new ArrayList<>(dataSources); 285 System.out.println(seconds); 286 //Calculate time difference. If greater than thirty seconds reset to last download 287 if (seconds > 30) { 288 previousDataSource = dataSourcesList.listIterator(dataSourcesList.size()).previous(); 289 v.visit(previousDataSource.bounds); 290 lastZoomTime = System.currentTimeMillis(); 291 return v; 292 } 293 294 if (firstPass == 0) { 295 previousDataSource = dataSourcesList.listIterator(dataSourcesList.size()).previous(); 296 firstPass = 2; 297 } 298 if (firstPass == 3) { 299 final Area dataSourceArea = Main.main.getCurrentDataSet().getDataSourceArea(); 300 bounds = new Bounds(dataSourceArea.getBounds2D()); 301 firstPass = 1; 302 } else { 303 getPreviousData(); 304 bounds = previousDataSource.bounds; 305 } 306 307 if (previousDataSource == dataSourcesList.listIterator(1).previous()) { 308 firstPass++; 309 } 310 259 311 if (bounds != null) { 260 312 try { 261 313 v.visit(bounds); 314 lastZoomTime = System.currentTimeMillis(); 262 315 } catch (Exception e) { 263 316 Main.warn(e); 264 317 } … … 268 321 return v; 269 322 } 270 323 324 public void getPreviousData() { 325 for (ListIterator<DataSource> iterator = dataSourcesList.listIterator(dataSourcesList.size()); iterator 326 .hasPrevious();) { 327 DataSource currentSource = iterator.previous(); 328 if (currentSource.equals(previousDataSource)) { 329 if (iterator.hasPrevious()) { 330 previousDataSource = iterator.previous(); 331 } else { 332 previousDataSource = dataSourcesList.listIterator(dataSourcesList.size()).previous(); 333 } 334 return; 335 } 336 } 337 } 338 271 339 @Override 272 340 protected void updateEnabledState() { 273 switch (mode) {341 switch (mode) { 274 342 case "selection": 275 setEnabled(getCurrentDataSet() != null && ! 343 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty()); 276 344 break; 277 345 case "layer": 278 346 if (!Main.isDisplayingMapView() || Main.map.mapView.getAllLayersAsList().isEmpty()) { … … 295 363 setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasZoomRedoEntries()); 296 364 break; 297 365 default: 298 setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasLayers() 299 ); 366 setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasLayers()); 300 367 } 301 368 } 302 369 … … 337 404 public MapFrameAdapter() { 338 405 if ("conflict".equals(mode)) { 339 406 conflictSelectionListener = new ListSelectionListener() { 340 @Override public void valueChanged(ListSelectionEvent e) { 407 @Override 408 public void valueChanged(ListSelectionEvent e) { 341 409 updateEnabledState(); 342 410 } 343 411 }; 344 412 } else if ("problem".equals(mode)) { 345 413 validatorSelectionListener = new TreeSelectionListener() { 346 @Override public void valueChanged(TreeSelectionEvent e) { 414 @Override 415 public void valueChanged(TreeSelectionEvent e) { 347 416 updateEnabledState(); 348 417 } 349 418 }; … … 350 419 } 351 420 } 352 421 353 @Override public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 422 @Override 423 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 354 424 if (conflictSelectionListener != null) { 355 425 if (newFrame != null) { 356 426 newFrame.conflictDialog.addListSelectionListener(conflictSelectionListener); -
src/org/openstreetmap/josm/gui/NavigatableComponent.java
660 660 } 661 661 } 662 662 663 public Stack<ZoomData> getZoomUndoBuffer(){ 664 return zoomUndoBuffer; 665 } 666 667 public Stack<ZoomData> getZoomRedoBuffer(){ 668 return zoomRedoBuffer; 669 } 670 663 671 public boolean hasZoomUndoEntries() { 664 672 return !zoomUndoBuffer.isEmpty(); 665 673 }