1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import javax.swing.Action;
|
---|
8 | import javax.swing.ImageIcon;
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
12 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
13 | import org.openstreetmap.josm.gui.actionsupport.AlignImageryPanel;
|
---|
14 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
15 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
16 |
|
---|
17 | public 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 | protected boolean isLayerAlreadyPresent() {
|
---|
60 | if (Main.isDisplayingMapView()) {
|
---|
61 | for (ImageryLayer layer : Main.map.mapView.getLayersOfType(ImageryLayer.class)) {
|
---|
62 | if (info.equals(layer.getInfo())) {
|
---|
63 | return true;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | return false;
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | protected void updateEnabledState() {
|
---|
72 | // never enable blacklisted entries. Do not add same imagery layer twice (fix #2519)
|
---|
73 | if (info.isBlacklisted() /*|| isLayerAlreadyPresent()*/) { // FIXME check disabled to allow several instances with different settings (see #7981)
|
---|
74 | setEnabled(false);
|
---|
75 | } else if (info.getImageryType() == ImageryType.TMS || info.getImageryType() == ImageryType.BING || info.getImageryType() == ImageryType.SCANEX) {
|
---|
76 | setEnabled(true);
|
---|
77 | } else if (Main.isDisplayingMapView() && !Main.map.mapView.getAllLayers().isEmpty()) {
|
---|
78 | setEnabled(true);
|
---|
79 | } else {
|
---|
80 | setEnabled(false);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|