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

Last change on this file since 5508 was 5466, checked in by Don-vip, 12 years ago

see #2519, see #7981 - revert r5458 to allow again several instances of a single imagery provider

  • Property svn:eol-style set to native
File size: 3.1 KB
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 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}
Note: See TracBrowser for help on using the repository browser.