| 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 | @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 | } |
|---|