Changeset 9464 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2016-01-15T14:02:03+01:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java
r9437 r9464 26 26 import java.net.URL; 27 27 import java.text.SimpleDateFormat; 28 import java.util.Arrays; 28 29 import java.util.ArrayList; 29 30 import java.util.Collections; … … 48 49 import javax.swing.JPanel; 49 50 import javax.swing.JPopupMenu; 51 import javax.swing.JSeparator; 50 52 import javax.swing.JTextField; 51 53 … … 86 88 import org.openstreetmap.josm.io.WMSLayerImporter; 87 89 import org.openstreetmap.josm.tools.GBC; 90 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; 88 91 89 92 /** … … 126 129 127 130 private final AttributionSupport attribution = new AttributionSupport(); 131 private final TileHolder clickedTileHolder = new TileHolder(); 128 132 129 133 // needed public access for session exporter … … 246 250 protected void redraw() { 247 251 needRedraw = true; 248 Main.map.repaint();252 if (isVisible()) Main.map.repaint(); 249 253 } 250 254 … … 309 313 310 314 private final class ShowTileInfoAction extends AbstractAction { 311 private final transient TileHolder clickedTileHolder; 312 313 private ShowTileInfoAction(TileHolder clickedTileHolder) { 314 super(tr("Show Tile Info")); 315 this.clickedTileHolder = clickedTileHolder; 315 316 private ShowTileInfoAction() { 317 super(tr("Show tile info")); 316 318 } 317 319 … … 375 377 } 376 378 379 private class LoadTileAction extends AbstractAction { 380 381 private LoadTileAction() { 382 super(tr("Load tile")); 383 } 384 385 @Override 386 public void actionPerformed(ActionEvent ae) { 387 Tile clickedTile = clickedTileHolder.getTile(); 388 if (clickedTile != null) { 389 loadTile(clickedTile, true); 390 redraw(); 391 } 392 } 393 } 394 377 395 private class AutoZoomAction extends AbstractAction implements LayerAction { 378 396 AutoZoomAction() { 379 super(tr("Auto Zoom"));397 super(tr("Auto zoom")); 380 398 } 381 399 … … 383 401 public void actionPerformed(ActionEvent ae) { 384 402 autoZoom = !autoZoom; 403 if (autoZoom && getBestZoom() != currentZoomLevel) { 404 setZoomLevel(getBestZoom()); 405 redraw(); 406 } 385 407 } 386 408 … … 406 428 public void actionPerformed(ActionEvent ae) { 407 429 autoLoad = !autoLoad; 430 if (autoLoad) redraw(); 408 431 } 409 432 … … 420 443 } 421 444 445 private class ShowErrorsAction extends AbstractAction implements LayerAction { 446 ShowErrorsAction() { 447 super(tr("Show errors")); 448 } 449 450 @Override 451 public void actionPerformed(ActionEvent ae) { 452 showErrors = !showErrors; 453 redraw(); 454 } 455 456 public Component createMenuComponent() { 457 JCheckBoxMenuItem item = new JCheckBoxMenuItem(this); 458 item.setSelected(showErrors); 459 return item; 460 } 461 462 @Override 463 public boolean supportLayers(List<Layer> layers) { 464 return actionSupportLayers(layers); 465 } 466 } 467 422 468 private class LoadAllTilesAction extends AbstractAction { 423 469 LoadAllTilesAction() { 424 super(tr("Load All Tiles"));470 super(tr("Load all tiles")); 425 471 } 426 472 … … 434 480 private class LoadErroneusTilesAction extends AbstractAction { 435 481 LoadErroneusTilesAction() { 436 super(tr("Load All Error Tiles"));482 super(tr("Load all error tiles")); 437 483 } 438 484 … … 460 506 ZoomToBestAction() { 461 507 super(tr("Change resolution")); 508 setEnabled(!autoZoom && getBestZoom() != currentZoomLevel); 462 509 } 463 510 … … 465 512 public void actionPerformed(ActionEvent ae) { 466 513 setZoomLevel(getBestZoom()); 514 redraw(); 515 } 516 } 517 518 private class IncreaseZoomAction extends AbstractAction { 519 IncreaseZoomAction() { 520 super(tr("Increase zoom")); 521 setEnabled(!autoZoom && zoomIncreaseAllowed()); 522 } 523 524 @Override 525 public void actionPerformed(ActionEvent ae) { 526 increaseZoomLevel(); 527 redraw(); 528 } 529 } 530 531 private class DecreaseZoomAction extends AbstractAction { 532 DecreaseZoomAction() { 533 super(tr("Decrease zoom")); 534 setEnabled(!autoZoom && zoomDecreaseAllowed()); 535 } 536 537 @Override 538 public void actionPerformed(ActionEvent ae) { 539 decreaseZoomLevel(); 540 redraw(); 541 } 542 } 543 544 private class FlushTileCacheAction extends AbstractAction { 545 FlushTileCacheAction() { 546 super(tr("Flush tile cache")); 547 setEnabled(tileLoader instanceof CachedTileLoader); 548 } 549 550 @Override 551 public void actionPerformed(ActionEvent ae) { 552 new PleaseWaitRunnable(tr("Flush tile cache")) { 553 @Override 554 protected void realRun() { 555 clearTileCache(getProgressMonitor()); 556 } 557 558 @Override 559 protected void finish() { 560 // empty - flush is instaneus 561 } 562 563 @Override 564 protected void cancel() { 565 // empty - flush is instaneus 566 } 567 }.run(); 467 568 } 468 569 } … … 517 618 initTileSource(this.tileSource); 518 619 519 // keep them final here, so we avoid namespace clutter in the class 520 final JPopupMenu tileOptionMenu = new JPopupMenu(); 521 final TileHolder clickedTileHolder = new TileHolder(); 522 Field autoZoomField; 523 Field autoLoadField; 524 Field showErrorsField; 525 try { 526 autoZoomField = AbstractTileSourceLayer.class.getField("autoZoom"); 527 autoLoadField = AbstractTileSourceLayer.class.getDeclaredField("autoLoad"); 528 showErrorsField = AbstractTileSourceLayer.class.getDeclaredField("showErrors"); 529 } catch (NoSuchFieldException | SecurityException e) { 530 // shoud not happen 531 throw new RuntimeException(e); 532 } 533 620 autoLoad = PROP_DEFAULT_AUTOLOAD.get(); 534 621 autoZoom = PROP_DEFAULT_AUTOZOOM.get(); 535 JCheckBoxMenuItem autoZoomPopup = new JCheckBoxMenuItem();536 autoZoomPopup.setModel(new BooleanButtonModel(autoZoomField));537 autoZoomPopup.setAction(new AutoZoomAction());538 tileOptionMenu.add(autoZoomPopup);539 540 autoLoad = PROP_DEFAULT_AUTOLOAD.get();541 JCheckBoxMenuItem autoLoadPopup = new JCheckBoxMenuItem();542 autoLoadPopup.setAction(new AutoLoadTilesAction());543 autoLoadPopup.setModel(new BooleanButtonModel(autoLoadField));544 tileOptionMenu.add(autoLoadPopup);545 546 622 showErrors = PROP_DEFAULT_SHOWERRORS.get(); 547 JCheckBoxMenuItem showErrorsPopup = new JCheckBoxMenuItem();548 showErrorsPopup.setAction(new AbstractAction(tr("Show Errors")) {549 @Override550 public void actionPerformed(ActionEvent ae) {551 showErrors = !showErrors;552 }553 });554 showErrorsPopup.setModel(new BooleanButtonModel(showErrorsField));555 tileOptionMenu.add(showErrorsPopup);556 557 tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Load Tile")) {558 @Override559 public void actionPerformed(ActionEvent ae) {560 Tile clickedTile = clickedTileHolder.getTile();561 if (clickedTile != null) {562 loadTile(clickedTile, true);563 redraw();564 }565 }566 }));567 568 tileOptionMenu.add(new JMenuItem(new ShowTileInfoAction(clickedTileHolder)));569 570 tileOptionMenu.add(new JMenuItem(new LoadAllTilesAction()));571 tileOptionMenu.add(new JMenuItem(new LoadErroneusTilesAction()));572 573 // increase and decrease commands574 tileOptionMenu.add(new JMenuItem(new AbstractAction(575 tr("Increase zoom")) {576 @Override577 public void actionPerformed(ActionEvent ae) {578 increaseZoomLevel();579 redraw();580 }581 }));582 583 tileOptionMenu.add(new JMenuItem(new AbstractAction(584 tr("Decrease zoom")) {585 @Override586 public void actionPerformed(ActionEvent ae) {587 decreaseZoomLevel();588 redraw();589 }590 }));591 592 tileOptionMenu.add(new JMenuItem(new AbstractAction(593 tr("Snap to tile size")) {594 @Override595 public void actionPerformed(ActionEvent ae) {596 double newFactor = Math.sqrt(getScaleFactor(currentZoomLevel));597 Main.map.mapView.zoomToFactor(newFactor);598 redraw();599 }600 }));601 602 tileOptionMenu.add(new JMenuItem(new AbstractAction(603 tr("Flush Tile Cache")) {604 @Override605 public void actionPerformed(ActionEvent ae) {606 new PleaseWaitRunnable(tr("Flush Tile Cache")) {607 @Override608 protected void realRun() {609 clearTileCache(getProgressMonitor());610 }611 612 @Override613 protected void finish() {614 // empty - flush is instaneus615 }616 617 @Override618 protected void cancel() {619 // empty - flush is instaneus620 }621 }.run();622 }623 }));624 623 625 624 final MouseAdapter adapter = new MouseAdapter() { … … 629 628 if (e.getButton() == MouseEvent.BUTTON3) { 630 629 clickedTileHolder.setTile(getTileForPixelpos(e.getX(), e.getY())); 631 tileOptionMenu.show(e.getComponent(), e.getX(), e.getY());630 new TileSourceLayerPopup().show(e.getComponent(), e.getX(), e.getY()); 632 631 } else if (e.getButton() == MouseEvent.BUTTON1) { 633 632 attribution.handleAttribution(e.getPoint(), true); … … 661 660 // start loading. 662 661 Main.map.repaint(500); 662 } 663 664 public class TileSourceLayerPopup extends JPopupMenu { 665 public TileSourceLayerPopup() { 666 667 for (Action a : getCommonEntries()) { 668 if (a instanceof LayerAction) { 669 add(((LayerAction) a).createMenuComponent()); 670 } else { 671 add(new JMenuItem(a)); 672 } 673 } 674 add(new JSeparator()); 675 add(new JMenuItem(new LoadTileAction())); 676 add(new JMenuItem(new ShowTileInfoAction())); 677 } 663 678 } 664 679 … … 781 796 782 797 protected int getMinZoomLvl() { 783 return getMinZoomLvl(tileSource); 798 if (info.getMinZoom() != 0) 799 return checkMinZoomLvl(info.getMinZoom(), tileSource); 800 else 801 return getMinZoomLvl(tileSource); 784 802 } 785 803 … … 836 854 */ 837 855 public boolean zoomDecreaseAllowed() { 838 return currentZoomLevel > this.getMinZoomLvl(); 856 boolean zda = currentZoomLevel > this.getMinZoomLvl(); 857 if (Main.isDebugEnabled()) { 858 Main.debug("zoomDecreaseAllowed(): " + zda + ' ' + currentZoomLevel + " vs. " + this.getMinZoomLvl()); 859 } 860 return zda; 839 861 } 840 862 … … 1607 1629 @Override 1608 1630 public Action[] getMenuEntries() { 1631 ArrayList<Action> actions = new ArrayList<Action>(); 1632 actions.addAll(Arrays.asList(getLayerListEntries())); 1633 actions.addAll(Arrays.asList(getCommonEntries())); 1634 actions.add(SeparatorLayerAction.INSTANCE); 1635 actions.add(new LayerListPopup.InfoAction(this)); 1636 return actions.toArray(new Action[actions.size()]); 1637 } 1638 1639 public Action[] getLayerListEntries() { 1609 1640 return new Action[] { 1610 LayerListDialog.getInstance().createActivateLayerAction(this), 1611 LayerListDialog.getInstance().createShowHideLayerAction(), 1612 LayerListDialog.getInstance().createDeleteLayerAction(), 1613 SeparatorLayerAction.INSTANCE, 1614 // color, 1615 new OffsetAction(), 1616 new RenameLayerAction(this.getAssociatedFile(), this), 1617 SeparatorLayerAction.INSTANCE, 1618 new AutoLoadTilesAction(), 1619 new AutoZoomAction(), 1620 new ZoomToBestAction(), 1621 new ZoomToNativeLevelAction(), 1622 new LoadErroneusTilesAction(), 1623 new LoadAllTilesAction(), 1624 new LayerListPopup.InfoAction(this) 1641 LayerListDialog.getInstance().createActivateLayerAction(this), 1642 LayerListDialog.getInstance().createShowHideLayerAction(), 1643 LayerListDialog.getInstance().createDeleteLayerAction(), 1644 SeparatorLayerAction.INSTANCE, 1645 // color, 1646 new OffsetAction(), 1647 new RenameLayerAction(this.getAssociatedFile(), this), 1648 SeparatorLayerAction.INSTANCE 1649 }; 1650 } 1651 1652 public Action[] getCommonEntries() { 1653 return new Action[] { 1654 new AutoLoadTilesAction(), 1655 new AutoZoomAction(), 1656 new ShowErrorsAction(), 1657 new IncreaseZoomAction(), 1658 new DecreaseZoomAction(), 1659 new ZoomToBestAction(), 1660 new ZoomToNativeLevelAction(), 1661 new FlushTileCacheAction(), 1662 new LoadErroneusTilesAction(), 1663 new LoadAllTilesAction() 1625 1664 }; 1626 1665 }
Note:
See TracChangeset
for help on using the changeset viewer.