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.layer.ImageryLayer;
|
---|
14 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
15 |
|
---|
16 | public class AddImageryLayerAction extends JosmAction implements AdaptableAction {
|
---|
17 |
|
---|
18 | private static final int MAX_ICON_SIZE = 24;
|
---|
19 | private final ImageryInfo info;
|
---|
20 |
|
---|
21 | public AddImageryLayerAction(ImageryInfo info) {
|
---|
22 | super(info.getMenuName(), /* ICON */"imagery_menu", tr("Add imagery layer {0}",info.getName()), null, false, false);
|
---|
23 | putValue("toolbar", "imagery_" + info.getToolbarName());
|
---|
24 | this.info = info;
|
---|
25 | installAdapters();
|
---|
26 |
|
---|
27 | // change toolbar icon from if specified
|
---|
28 | try {
|
---|
29 | if (info.getIcon() != null) {
|
---|
30 | ImageIcon i = new ImageProvider(info.getIcon()).setOptional(true).
|
---|
31 | setMaxHeight(MAX_ICON_SIZE).setMaxWidth(MAX_ICON_SIZE).get();
|
---|
32 | if (i != null) {
|
---|
33 | putValue(Action.SMALL_ICON, i);
|
---|
34 | }
|
---|
35 | }
|
---|
36 | } catch (Exception ex) {
|
---|
37 | throw new RuntimeException(ex.getMessage(), ex);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void actionPerformed(ActionEvent e) {
|
---|
43 | if (!isEnabled()) return;
|
---|
44 | try {
|
---|
45 | Main.main.addLayer(ImageryLayer.create(info));
|
---|
46 | } catch (IllegalArgumentException ex) {
|
---|
47 | if (ex.getMessage() == null || ex.getMessage().isEmpty()) {
|
---|
48 | throw ex;
|
---|
49 | } else {
|
---|
50 | JOptionPane.showMessageDialog(Main.parent,
|
---|
51 | ex.getMessage(), tr("Error"),
|
---|
52 | JOptionPane.ERROR_MESSAGE);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | protected void updateEnabledState() {
|
---|
59 | // never enable blacklisted entries.
|
---|
60 | if (info.isBlacklisted()) {
|
---|
61 | setEnabled(false);
|
---|
62 | } else if (info.getImageryType() == ImageryType.TMS || info.getImageryType() == ImageryType.BING || info.getImageryType() == ImageryType.SCANEX) {
|
---|
63 | setEnabled(true);
|
---|
64 | } else if (Main.map != null && Main.map.mapView != null && !Main.map.mapView.getAllLayers().isEmpty()) {
|
---|
65 | setEnabled(true);
|
---|
66 | } else {
|
---|
67 | setEnabled(false);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|