source: osm/applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSAdjustAction.java@ 18670

Last change on this file since 18670 was 18670, checked in by guggis, 15 years ago

'Updated build.xml'

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