Ignore:
Timestamp:
2016-01-15T14:02:03+01:00 (8 years ago)
Author:
bastiK
Message:

applied #12363 - layer context menus were different at two locations (patch by kolesar)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java

    r9437 r9464  
    2626import java.net.URL;
    2727import java.text.SimpleDateFormat;
     28import java.util.Arrays;
    2829import java.util.ArrayList;
    2930import java.util.Collections;
     
    4849import javax.swing.JPanel;
    4950import javax.swing.JPopupMenu;
     51import javax.swing.JSeparator;
    5052import javax.swing.JTextField;
    5153
     
    8688import org.openstreetmap.josm.io.WMSLayerImporter;
    8789import org.openstreetmap.josm.tools.GBC;
     90import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    8891
    8992/**
     
    126129
    127130    private final AttributionSupport attribution = new AttributionSupport();
     131    private final TileHolder clickedTileHolder = new TileHolder();
    128132
    129133    // needed public access for session exporter
     
    246250    protected void redraw() {
    247251        needRedraw = true;
    248         Main.map.repaint();
     252        if (isVisible()) Main.map.repaint();
    249253    }
    250254
     
    309313
    310314    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"));
    316318        }
    317319
     
    375377    }
    376378
     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
    377395    private class AutoZoomAction extends AbstractAction implements LayerAction {
    378396        AutoZoomAction() {
    379             super(tr("Auto Zoom"));
     397            super(tr("Auto zoom"));
    380398        }
    381399
     
    383401        public void actionPerformed(ActionEvent ae) {
    384402            autoZoom = !autoZoom;
     403            if (autoZoom && getBestZoom() != currentZoomLevel) {
     404                setZoomLevel(getBestZoom());
     405                redraw();
     406            }
    385407        }
    386408
     
    406428        public void actionPerformed(ActionEvent ae) {
    407429            autoLoad = !autoLoad;
     430            if (autoLoad) redraw();
    408431        }
    409432
     
    420443    }
    421444
     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
    422468    private class LoadAllTilesAction extends AbstractAction {
    423469        LoadAllTilesAction() {
    424             super(tr("Load All Tiles"));
     470            super(tr("Load all tiles"));
    425471        }
    426472
     
    434480    private class LoadErroneusTilesAction extends AbstractAction {
    435481        LoadErroneusTilesAction() {
    436             super(tr("Load All Error Tiles"));
     482            super(tr("Load all error tiles"));
    437483        }
    438484
     
    460506        ZoomToBestAction() {
    461507            super(tr("Change resolution"));
     508            setEnabled(!autoZoom && getBestZoom() != currentZoomLevel);
    462509        }
    463510
     
    465512        public void actionPerformed(ActionEvent ae) {
    466513            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();
    467568        }
    468569    }
     
    517618        initTileSource(this.tileSource);
    518619
    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();
    534621        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 
    546622        showErrors = PROP_DEFAULT_SHOWERRORS.get();
    547         JCheckBoxMenuItem showErrorsPopup = new JCheckBoxMenuItem();
    548         showErrorsPopup.setAction(new AbstractAction(tr("Show Errors")) {
    549             @Override
    550             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             @Override
    559             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 commands
    574         tileOptionMenu.add(new JMenuItem(new AbstractAction(
    575                 tr("Increase zoom")) {
    576             @Override
    577             public void actionPerformed(ActionEvent ae) {
    578                 increaseZoomLevel();
    579                 redraw();
    580             }
    581         }));
    582 
    583         tileOptionMenu.add(new JMenuItem(new AbstractAction(
    584                 tr("Decrease zoom")) {
    585             @Override
    586             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             @Override
    595             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             @Override
    605             public void actionPerformed(ActionEvent ae) {
    606                 new PleaseWaitRunnable(tr("Flush Tile Cache")) {
    607                     @Override
    608                     protected void realRun() {
    609                         clearTileCache(getProgressMonitor());
    610                     }
    611 
    612                     @Override
    613                     protected void finish() {
    614                         // empty - flush is instaneus
    615                     }
    616 
    617                     @Override
    618                     protected void cancel() {
    619                         // empty - flush is instaneus
    620                     }
    621                 }.run();
    622             }
    623         }));
    624623
    625624        final MouseAdapter adapter = new MouseAdapter() {
     
    629628                if (e.getButton() == MouseEvent.BUTTON3) {
    630629                    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());
    632631                } else if (e.getButton() == MouseEvent.BUTTON1) {
    633632                    attribution.handleAttribution(e.getPoint(), true);
     
    661660        // start loading.
    662661        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        }
    663678    }
    664679
     
    781796
    782797    protected int getMinZoomLvl() {
    783         return getMinZoomLvl(tileSource);
     798        if (info.getMinZoom() != 0)
     799            return checkMinZoomLvl(info.getMinZoom(), tileSource);
     800        else
     801            return getMinZoomLvl(tileSource);
    784802    }
    785803
     
    836854     */
    837855    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;
    839861    }
    840862
     
    16071629    @Override
    16081630    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() {
    16091640        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()
    16251664        };
    16261665    }
Note: See TracChangeset for help on using the changeset viewer.