| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.gui; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trc; |
|---|
| 6 | |
|---|
| 7 | import java.awt.Component; |
|---|
| 8 | import java.awt.event.ActionEvent; |
|---|
| 9 | import java.util.List; |
|---|
| 10 | |
|---|
| 11 | import javax.swing.Action; |
|---|
| 12 | import javax.swing.JComponent; |
|---|
| 13 | import javax.swing.JMenu; |
|---|
| 14 | import javax.swing.JMenuItem; |
|---|
| 15 | import javax.swing.JPopupMenu; |
|---|
| 16 | import javax.swing.MenuElement; |
|---|
| 17 | |
|---|
| 18 | import org.openstreetmap.josm.Main; |
|---|
| 19 | import org.openstreetmap.josm.actions.AddImageryLayerAction; |
|---|
| 20 | import org.openstreetmap.josm.actions.JosmAction; |
|---|
| 21 | import org.openstreetmap.josm.actions.Map_Rectifier_WMSmenuAction; |
|---|
| 22 | import org.openstreetmap.josm.data.imagery.ImageryInfo; |
|---|
| 23 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo; |
|---|
| 24 | import org.openstreetmap.josm.gui.layer.ImageryLayer; |
|---|
| 25 | import org.openstreetmap.josm.gui.layer.Layer; |
|---|
| 26 | import org.openstreetmap.josm.gui.layer.WMSLayer; |
|---|
| 27 | import org.openstreetmap.josm.tools.ImageProvider; |
|---|
| 28 | |
|---|
| 29 | public class ImageryMenu extends JMenu implements MapView.LayerChangeListener { |
|---|
| 30 | Action offsetAction = new JosmAction( |
|---|
| 31 | tr("Imagery offset"), "mapmode/adjustimg", tr("Adjust imagery offset"), null, false, false) { |
|---|
| 32 | { |
|---|
| 33 | putValue("toolbar", "imagery-offset"); |
|---|
| 34 | Main.toolbar.register(this); |
|---|
| 35 | } |
|---|
| 36 | @Override |
|---|
| 37 | public void actionPerformed(ActionEvent e) { |
|---|
| 38 | List<ImageryLayer> layers = Main.map.mapView.getLayersOfType(ImageryLayer.class); |
|---|
| 39 | if (layers.isEmpty()) { |
|---|
| 40 | setEnabled(false); |
|---|
| 41 | return; |
|---|
| 42 | } |
|---|
| 43 | Component source = null; |
|---|
| 44 | if (e.getSource() instanceof Component) { |
|---|
| 45 | source = (Component)e.getSource(); |
|---|
| 46 | } |
|---|
| 47 | JPopupMenu popup = new JPopupMenu(); |
|---|
| 48 | if (layers.size() == 1) { |
|---|
| 49 | JComponent c = layers.get(0).getOffsetMenuItem(popup); |
|---|
| 50 | if (c instanceof JMenuItem) { |
|---|
| 51 | ((JMenuItem) c).getAction().actionPerformed(e); |
|---|
| 52 | } else { |
|---|
| 53 | if (source == null) return; |
|---|
| 54 | popup.show(source, source.getWidth()/2, source.getHeight()/2); |
|---|
| 55 | } |
|---|
| 56 | return; |
|---|
| 57 | } |
|---|
| 58 | if (source == null) return; |
|---|
| 59 | for (ImageryLayer layer : layers) { |
|---|
| 60 | JMenuItem layerMenu = layer.getOffsetMenuItem(); |
|---|
| 61 | layerMenu.setText(layer.getName()); |
|---|
| 62 | layerMenu.setIcon(layer.getIcon()); |
|---|
| 63 | popup.add(layerMenu); |
|---|
| 64 | } |
|---|
| 65 | popup.show(source, source.getWidth()/2, source.getHeight()/2); |
|---|
| 66 | } |
|---|
| 67 | }; |
|---|
| 68 | JMenuItem singleOffset = new JMenuItem(offsetAction); |
|---|
| 69 | JMenuItem offsetMenuItem = singleOffset; |
|---|
| 70 | Map_Rectifier_WMSmenuAction rectaction = new Map_Rectifier_WMSmenuAction(); |
|---|
| 71 | JosmAction blankmenu = new JosmAction( |
|---|
| 72 | tr("Blank Layer"), /* ICON */"blankmenu", tr("Open a blank WMS layer to load data from a file"), null, false) { |
|---|
| 73 | @Override |
|---|
| 74 | public void actionPerformed(ActionEvent ev) { |
|---|
| 75 | if (!isEnabled()) return; |
|---|
| 76 | Main.main.addLayer(new WMSLayer()); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | @Override |
|---|
| 80 | protected void updateEnabledState() { |
|---|
| 81 | setEnabled(Main.map != null && Main.map.mapView != null && !Main.map.mapView.getAllLayers().isEmpty()); |
|---|
| 82 | } |
|---|
| 83 | }; |
|---|
| 84 | int offsPos; |
|---|
| 85 | |
|---|
| 86 | public ImageryMenu() { |
|---|
| 87 | super(tr("Imagery")); |
|---|
| 88 | MapView.addLayerChangeListener(this); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public void refreshImageryMenu() { |
|---|
| 92 | removeAll(); |
|---|
| 93 | |
|---|
| 94 | // for each configured WMSInfo, add a menu entry. |
|---|
| 95 | for (final ImageryInfo u : ImageryLayerInfo.instance.getLayers()) { |
|---|
| 96 | add(new AddImageryLayerAction(u)); |
|---|
| 97 | } |
|---|
| 98 | addSeparator(); |
|---|
| 99 | add(new JMenuItem(rectaction)); |
|---|
| 100 | |
|---|
| 101 | addSeparator(); |
|---|
| 102 | offsPos = getMenuComponentCount(); |
|---|
| 103 | add(offsetMenuItem); |
|---|
| 104 | addSeparator(); |
|---|
| 105 | add(new JMenuItem(blankmenu)); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | private JMenuItem getNewOffsetMenu(){ |
|---|
| 109 | if (Main.map == null || Main.map.mapView == null) { |
|---|
| 110 | offsetAction.setEnabled(false); |
|---|
| 111 | return singleOffset; |
|---|
| 112 | } |
|---|
| 113 | List<ImageryLayer> layers = Main.map.mapView.getLayersOfType(ImageryLayer.class); |
|---|
| 114 | if (layers.isEmpty()) { |
|---|
| 115 | offsetAction.setEnabled(false); |
|---|
| 116 | return singleOffset; |
|---|
| 117 | } |
|---|
| 118 | offsetAction.setEnabled(true); |
|---|
| 119 | JMenu newMenu = new JMenu(trc("layer","Offset")) { |
|---|
| 120 | // Hack to prevent ToolbarPreference from tracing this menu |
|---|
| 121 | // TODO: Modify ToolbarPreference to not to trace such dynamic submenus? |
|---|
| 122 | @Override |
|---|
| 123 | public MenuElement[] getSubElements() { |
|---|
| 124 | return new MenuElement[0]; |
|---|
| 125 | } |
|---|
| 126 | }; |
|---|
| 127 | newMenu.setIcon(ImageProvider.get("mapmode", "adjustimg")); |
|---|
| 128 | newMenu.setAction(offsetAction); |
|---|
| 129 | if (layers.size() == 1) |
|---|
| 130 | return (JMenuItem)layers.get(0).getOffsetMenuItem(newMenu); |
|---|
| 131 | for (ImageryLayer layer : layers) { |
|---|
| 132 | JMenuItem layerMenu = layer.getOffsetMenuItem(); |
|---|
| 133 | layerMenu.setText(layer.getName()); |
|---|
| 134 | layerMenu.setIcon(layer.getIcon()); |
|---|
| 135 | newMenu.add(layerMenu); |
|---|
| 136 | } |
|---|
| 137 | return newMenu; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | public void refreshOffsetMenu() { |
|---|
| 141 | JMenuItem newItem = getNewOffsetMenu(); |
|---|
| 142 | remove(offsetMenuItem); |
|---|
| 143 | add(newItem, offsPos); |
|---|
| 144 | offsetMenuItem = newItem; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | @Override |
|---|
| 148 | public void activeLayerChange(Layer oldLayer, Layer newLayer) { |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | @Override |
|---|
| 152 | public void layerAdded(Layer newLayer) { |
|---|
| 153 | if (newLayer instanceof ImageryLayer) { |
|---|
| 154 | refreshOffsetMenu(); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | @Override |
|---|
| 159 | public void layerRemoved(Layer oldLayer) { |
|---|
| 160 | if (oldLayer instanceof ImageryLayer) { |
|---|
| 161 | refreshOffsetMenu(); |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | } |
|---|