source: josm/trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java @ 5241

Revision 5091, 2.7 KB checked in by akks, 2 months ago (diff)

Warning against misaligned imagery for new users - see #7450.
Added addTopPanel function in MapFrame for other possible enhancements

  • Property svn:eol-style set to native
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import javax.swing.Action;
8import javax.swing.ImageIcon;
9import javax.swing.JOptionPane;
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.imagery.ImageryInfo;
12import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
13import org.openstreetmap.josm.gui.actionsupport.AlignImageryPanel;
14import org.openstreetmap.josm.gui.layer.ImageryLayer;
15import org.openstreetmap.josm.tools.ImageProvider;
16
17public class AddImageryLayerAction extends JosmAction implements AdaptableAction {
18
19    private static final int MAX_ICON_SIZE = 24;
20    private final ImageryInfo info;
21
22    public AddImageryLayerAction(ImageryInfo info) {
23        super(info.getMenuName(), /* ICON */"imagery_menu", tr("Add imagery layer {0}",info.getName()), null, false, false);
24        putValue("toolbar", "imagery_" + info.getToolbarName());
25        this.info = info;
26        installAdapters();
27
28        // change toolbar icon from if specified
29        try {
30            if (info.getIcon() != null) {
31                ImageIcon i = new ImageProvider(info.getIcon()).setOptional(true).
32                        setMaxHeight(MAX_ICON_SIZE).setMaxWidth(MAX_ICON_SIZE).get();
33                if (i != null) {
34                    putValue(Action.SMALL_ICON, i);
35                }
36            }
37        } catch (Exception ex) {
38            throw new RuntimeException(ex.getMessage(), ex);
39        }
40    }
41
42    @Override
43    public void actionPerformed(ActionEvent e) {
44        if (!isEnabled()) return;
45        try {
46            Main.main.addLayer(ImageryLayer.create(info));
47            AlignImageryPanel.addNagPanelIfNeeded();
48        } catch (IllegalArgumentException ex) {
49            if (ex.getMessage() == null || ex.getMessage().isEmpty()) {
50                throw ex;
51            } else {
52                JOptionPane.showMessageDialog(Main.parent,
53                        ex.getMessage(), tr("Error"),
54                        JOptionPane.ERROR_MESSAGE);
55            }
56        }
57    }
58
59    @Override
60    protected void updateEnabledState() {
61        // never enable blacklisted entries.
62        if (info.isBlacklisted()) {
63            setEnabled(false);
64        } else if (info.getImageryType() == ImageryType.TMS || info.getImageryType() == ImageryType.BING || info.getImageryType() == ImageryType.SCANEX) {
65            setEnabled(true);
66        } else if (Main.map != null && Main.map.mapView != null && !Main.map.mapView.getAllLayers().isEmpty()) {
67            setEnabled(true);
68        } else {
69            setEnabled(false);
70        }
71    }
72}
Note: See TracBrowser for help on using the repository browser.