| 1 | package wmsplugin;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 | import java.awt.Component;
|
|---|
| 6 | import java.awt.Cursor;
|
|---|
| 7 | import java.awt.GridBagLayout;
|
|---|
| 8 | import java.awt.event.MouseEvent;
|
|---|
| 9 | import java.awt.event.MouseListener;
|
|---|
| 10 | import java.awt.event.MouseMotionListener;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.DefaultComboBoxModel;
|
|---|
| 14 | import javax.swing.DefaultListCellRenderer;
|
|---|
| 15 | import javax.swing.Icon;
|
|---|
| 16 | import javax.swing.JComboBox;
|
|---|
| 17 | import javax.swing.JLabel;
|
|---|
| 18 | import javax.swing.JList;
|
|---|
| 19 | import javax.swing.JOptionPane;
|
|---|
| 20 | import javax.swing.JPanel;
|
|---|
| 21 |
|
|---|
| 22 | import org.openstreetmap.josm.Main;
|
|---|
| 23 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
|---|
| 24 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 25 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 26 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 27 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 28 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 29 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | public class WMSAdjustAction extends MapMode implements MouseListener, MouseMotionListener{
|
|---|
| 33 | //static private final Logger logger = Logger.getLogger(WMSAdjustAction.class.getName());
|
|---|
| 34 |
|
|---|
| 35 | GeorefImage selectedImage;
|
|---|
| 36 | boolean mouseDown;
|
|---|
| 37 | EastNorth prevEastNorth;
|
|---|
| 38 | private WMSLayer adjustingLayer;
|
|---|
| 39 |
|
|---|
| 40 | public WMSAdjustAction(MapFrame mapFrame) {
|
|---|
| 41 | super(tr("Adjust WMS"), "adjustwms",
|
|---|
| 42 | tr("Adjust the position of the selected WMS layer"), mapFrame,
|
|---|
| 43 | ImageProvider.getCursor("normal", "move"));
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | @Override public void enterMode() {
|
|---|
| 49 | super.enterMode();
|
|---|
| 50 | if (!hasWMSLayersToAdjust()) {
|
|---|
| 51 | warnNoWMSLayers();
|
|---|
| 52 | return;
|
|---|
| 53 | }
|
|---|
| 54 | List<WMSLayer> wmsLayers = Main.map.mapView.getLayersOfType(WMSLayer.class);
|
|---|
| 55 | if (wmsLayers.size() == 1) {
|
|---|
| 56 | adjustingLayer = wmsLayers.get(0);
|
|---|
| 57 | } else {
|
|---|
| 58 | adjustingLayer = (WMSLayer)askAdjustLayer(Main.map.mapView.getLayersOfType(WMSLayer.class));
|
|---|
| 59 | }
|
|---|
| 60 | if (adjustingLayer == null)
|
|---|
| 61 | return;
|
|---|
| 62 | if (!adjustingLayer.isVisible()) {
|
|---|
| 63 | adjustingLayer.setVisible(true);
|
|---|
| 64 | }
|
|---|
| 65 | Main.map.mapView.addMouseListener(this);
|
|---|
| 66 | Main.map.mapView.addMouseMotionListener(this);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | @Override public void exitMode() {
|
|---|
| 70 | super.exitMode();
|
|---|
| 71 | Main.map.mapView.removeMouseListener(this);
|
|---|
| 72 | Main.map.mapView.removeMouseMotionListener(this);
|
|---|
| 73 | adjustingLayer = null;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | @Override public void mousePressed(MouseEvent e) {
|
|---|
| 77 | if (e.getButton() != MouseEvent.BUTTON1)
|
|---|
| 78 | return;
|
|---|
| 79 |
|
|---|
| 80 | if (adjustingLayer.isVisible()) {
|
|---|
| 81 | prevEastNorth=Main.map.mapView.getEastNorth(e.getX(),e.getY());
|
|---|
| 82 | selectedImage = adjustingLayer.findImage(prevEastNorth);
|
|---|
| 83 | if(selectedImage!=null) {
|
|---|
| 84 | Main.map.mapView.setCursor
|
|---|
| 85 | (Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | @Override public void mouseDragged(MouseEvent e) {
|
|---|
| 91 | if(selectedImage!=null) {
|
|---|
| 92 | EastNorth eastNorth=
|
|---|
| 93 | Main.map.mapView.getEastNorth(e.getX(),e.getY());
|
|---|
| 94 | adjustingLayer.displace(
|
|---|
| 95 | eastNorth.east()-prevEastNorth.east(),
|
|---|
| 96 | eastNorth.north()-prevEastNorth.north()
|
|---|
| 97 | );
|
|---|
| 98 | prevEastNorth = eastNorth;
|
|---|
| 99 | Main.map.mapView.repaint();
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | @Override public void mouseReleased(MouseEvent e) {
|
|---|
| 104 | Main.map.mapView.repaint();
|
|---|
| 105 | Main.map.mapView.setCursor(Cursor.getDefaultCursor());
|
|---|
| 106 | selectedImage = null;
|
|---|
| 107 | prevEastNorth = null;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | @Override
|
|---|
| 111 | public void mouseEntered(MouseEvent e) {
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | @Override
|
|---|
| 115 | public void mouseExited(MouseEvent e) {
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | @Override
|
|---|
| 119 | public void mouseMoved(MouseEvent e) {
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | @Override public void mouseClicked(MouseEvent e) {
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | // This only makes the buttons look disabled, but since no keyboard shortcut is
|
|---|
| 126 | // provided there aren't any other means to activate this tool
|
|---|
| 127 | @Override public boolean layerIsSupported(Layer l) {
|
|---|
| 128 | return (l instanceof WMSLayer) && l.isVisible();
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * the list cell renderer used to render layer list entries
|
|---|
| 133 | *
|
|---|
| 134 | */
|
|---|
| 135 | static public class LayerListCellRenderer extends DefaultListCellRenderer {
|
|---|
| 136 |
|
|---|
| 137 | protected boolean isActiveLayer(Layer layer) {
|
|---|
| 138 | if (Main.map == null)
|
|---|
| 139 | return false;
|
|---|
| 140 | if (Main.map.mapView == null)
|
|---|
| 141 | return false;
|
|---|
| 142 | return Main.map.mapView.getActiveLayer() == layer;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | @Override
|
|---|
| 146 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
|
|---|
| 147 | boolean cellHasFocus) {
|
|---|
| 148 | Layer layer = (Layer) value;
|
|---|
| 149 | JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected,
|
|---|
| 150 | cellHasFocus);
|
|---|
| 151 | Icon icon = layer.getIcon();
|
|---|
| 152 | label.setIcon(icon);
|
|---|
| 153 | label.setToolTipText(layer.getToolTipText());
|
|---|
| 154 | return label;
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /**
|
|---|
| 159 | * Prompts the user with a list of WMS layers which can be adjusted
|
|---|
| 160 | *
|
|---|
| 161 | * @param adjustableLayers the list of adjustable layers
|
|---|
| 162 | * @return the selected layer; null, if no layer was selected
|
|---|
| 163 | */
|
|---|
| 164 | protected Layer askAdjustLayer(List<? extends Layer> adjustableLayers) {
|
|---|
| 165 | JComboBox layerList = new JComboBox();
|
|---|
| 166 | layerList.setRenderer(new LayerListCellRenderer());
|
|---|
| 167 | layerList.setModel(new DefaultComboBoxModel(adjustableLayers.toArray()));
|
|---|
| 168 | layerList.setSelectedIndex(0);
|
|---|
| 169 |
|
|---|
| 170 | JPanel pnl = new JPanel();
|
|---|
| 171 | pnl.setLayout(new GridBagLayout());
|
|---|
| 172 | pnl.add(new JLabel(tr("Please select the WMS layer to adjust.")), GBC.eol());
|
|---|
| 173 | pnl.add(layerList, GBC.eol());
|
|---|
| 174 |
|
|---|
| 175 | ExtendedDialog diag = new ExtendedDialog(
|
|---|
| 176 | Main.parent,
|
|---|
| 177 | tr("Select WMS layer"),
|
|---|
| 178 | new String[] { tr("Start adjusting"),tr("Cancel") }
|
|---|
| 179 | );
|
|---|
| 180 | diag.setContent(pnl);
|
|---|
| 181 | diag.setButtonIcons(new String[] { "mapmode/adjustwms", "cancel" });
|
|---|
| 182 | diag.showDialog();
|
|---|
| 183 | int decision = diag.getValue();
|
|---|
| 184 | if (decision != 1)
|
|---|
| 185 | return null;
|
|---|
| 186 | Layer adjustLayer = (Layer) layerList.getSelectedItem();
|
|---|
| 187 | return adjustLayer;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /**
|
|---|
| 191 | * Displays a warning message if there are no WMS layers to adjust
|
|---|
| 192 | *
|
|---|
| 193 | */
|
|---|
| 194 | protected void warnNoWMSLayers() {
|
|---|
| 195 | JOptionPane.showMessageDialog(
|
|---|
| 196 | Main.parent,
|
|---|
| 197 | tr("There are currently no WMS layer to adjust."),
|
|---|
| 198 | tr("No layers to adjust"),
|
|---|
| 199 | JOptionPane.WARNING_MESSAGE
|
|---|
| 200 | );
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /**
|
|---|
| 204 | * Replies true if there is at least one WMS layer
|
|---|
| 205 | *
|
|---|
| 206 | * @return true if there is at least one WMS layer
|
|---|
| 207 | */
|
|---|
| 208 | protected boolean hasWMSLayersToAdjust() {
|
|---|
| 209 | if (Main.map == null) return false;
|
|---|
| 210 | if (Main.map.mapView == null) return false;
|
|---|
| 211 | return ! Main.map.mapView.getLayersOfType(WMSLayer.class).isEmpty();
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | @Override
|
|---|
| 215 | protected void updateEnabledState() {
|
|---|
| 216 | setEnabled(hasWMSLayersToAdjust());
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|