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 | @Override public boolean layerIsSupported(Layer l) {
|
---|
126 | return hasWMSLayersToAdjust();
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * the list cell renderer used to render layer list entries
|
---|
131 | *
|
---|
132 | */
|
---|
133 | static public class LayerListCellRenderer extends DefaultListCellRenderer {
|
---|
134 |
|
---|
135 | protected boolean isActiveLayer(Layer layer) {
|
---|
136 | if (Main.map == null)
|
---|
137 | return false;
|
---|
138 | if (Main.map.mapView == null)
|
---|
139 | return false;
|
---|
140 | return Main.map.mapView.getActiveLayer() == layer;
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
|
---|
145 | boolean cellHasFocus) {
|
---|
146 | Layer layer = (Layer) value;
|
---|
147 | JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected,
|
---|
148 | cellHasFocus);
|
---|
149 | Icon icon = layer.getIcon();
|
---|
150 | label.setIcon(icon);
|
---|
151 | label.setToolTipText(layer.getToolTipText());
|
---|
152 | return label;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Prompts the user with a list of WMS layers which can be adjusted
|
---|
158 | *
|
---|
159 | * @param adjustableLayers the list of adjustable layers
|
---|
160 | * @return the selected layer; null, if no layer was selected
|
---|
161 | */
|
---|
162 | protected Layer askAdjustLayer(List<? extends Layer> adjustableLayers) {
|
---|
163 | JComboBox layerList = new JComboBox();
|
---|
164 | layerList.setRenderer(new LayerListCellRenderer());
|
---|
165 | layerList.setModel(new DefaultComboBoxModel(adjustableLayers.toArray()));
|
---|
166 | layerList.setSelectedIndex(0);
|
---|
167 |
|
---|
168 | JPanel pnl = new JPanel();
|
---|
169 | pnl.setLayout(new GridBagLayout());
|
---|
170 | pnl.add(new JLabel(tr("Please select the WMS layer to adjust.")), GBC.eol());
|
---|
171 | pnl.add(layerList, GBC.eol());
|
---|
172 |
|
---|
173 | ExtendedDialog diag = new ExtendedDialog(
|
---|
174 | Main.parent,
|
---|
175 | tr("Select WMS layer"),
|
---|
176 | new String[] { tr("Start adjusting"),tr("Cancel") }
|
---|
177 | );
|
---|
178 | diag.setContent(pnl);
|
---|
179 | diag.setButtonIcons(new String[] { "mapmode/adjustwms", "cancel" });
|
---|
180 | diag.showDialog();
|
---|
181 | int decision = diag.getValue();
|
---|
182 | if (decision != 1)
|
---|
183 | return null;
|
---|
184 | Layer adjustLayer = (Layer) layerList.getSelectedItem();
|
---|
185 | return adjustLayer;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Displays a warning message if there are no WMS layers to adjust
|
---|
190 | *
|
---|
191 | */
|
---|
192 | protected void warnNoWMSLayers() {
|
---|
193 | JOptionPane.showMessageDialog(
|
---|
194 | Main.parent,
|
---|
195 | tr("There are currently no WMS layer to adjust."),
|
---|
196 | tr("No layers to adjust"),
|
---|
197 | JOptionPane.WARNING_MESSAGE
|
---|
198 | );
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Replies true if there is at least one WMS layer
|
---|
203 | *
|
---|
204 | * @return true if there is at least one WMS layer
|
---|
205 | */
|
---|
206 | protected boolean hasWMSLayersToAdjust() {
|
---|
207 | if (Main.map == null) return false;
|
---|
208 | if (Main.map.mapView == null) return false;
|
---|
209 | return ! Main.map.mapView.getLayersOfType(WMSLayer.class).isEmpty();
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override
|
---|
213 | protected void updateEnabledState() {
|
---|
214 | setEnabled(hasWMSLayersToAdjust());
|
---|
215 | }
|
---|
216 | }
|
---|