1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.pointinfo;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Cursor;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.InputEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 | import java.awt.event.MouseListener;
|
---|
13 |
|
---|
14 | import javax.swing.ImageIcon;
|
---|
15 | import javax.swing.JEditorPane;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JScrollPane;
|
---|
18 | import javax.swing.event.HyperlinkEvent;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
21 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
23 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
24 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
25 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
26 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
27 | import org.openstreetmap.josm.tools.Logging;
|
---|
28 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
30 | import org.xml.sax.SAXException;
|
---|
31 |
|
---|
32 | class PointInfoAction extends MapMode implements MouseListener {
|
---|
33 |
|
---|
34 | private static final long serialVersionUID = 1L;
|
---|
35 |
|
---|
36 | protected boolean cancel;
|
---|
37 | protected AbstractPointInfoModule module;
|
---|
38 |
|
---|
39 | private String htmlText = "";
|
---|
40 | private String coordinatesText = "";
|
---|
41 |
|
---|
42 | PointInfoAction() {
|
---|
43 | super(tr("Point info"), "pointinfo", tr("Point info."),
|
---|
44 | Shortcut.registerShortcut("tools:pointInfo", tr("More tools: {0}", tr("Point info")), KeyEvent.VK_X, Shortcut.CTRL_SHIFT),
|
---|
45 | getCursor());
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public void enterMode() {
|
---|
50 | if (!isEnabled()) {
|
---|
51 | return;
|
---|
52 | }
|
---|
53 | super.enterMode();
|
---|
54 | MainApplication.getMap().mapView.setCursor(getCursor());
|
---|
55 | MainApplication.getMap().mapView.addMouseListener(this);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public void exitMode() {
|
---|
60 | super.exitMode();
|
---|
61 | MainApplication.getMap().mapView.removeMouseListener(this);
|
---|
62 | }
|
---|
63 |
|
---|
64 | private static Cursor getCursor() {
|
---|
65 | return ImageProvider.getCursor("crosshair", "pointinfo");
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected void infoAsync(Point clickPoint) {
|
---|
69 | cancel = false;
|
---|
70 | /**
|
---|
71 | * Positional data
|
---|
72 | */
|
---|
73 | final LatLon pos = MainApplication.getMap().mapView.getLatLon(clickPoint.x, clickPoint.y);
|
---|
74 |
|
---|
75 | try {
|
---|
76 | module = PointInfoPlugin.getModule(pos);
|
---|
77 | PleaseWaitRunnable infoTask = new PleaseWaitRunnable(tr("Connecting server")) {
|
---|
78 | @Override
|
---|
79 | protected void realRun() throws SAXException {
|
---|
80 | infoSync(pos, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, true));
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | protected void finish() {
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | protected void afterFinish() {
|
---|
89 | if (htmlText.length() > 0) {
|
---|
90 | // Show result
|
---|
91 | JEditorPane msgLabel = new JEditorPane("text/html", htmlText);
|
---|
92 | msgLabel.setEditable(false);
|
---|
93 | msgLabel.setOpaque(false);
|
---|
94 | msgLabel.addHyperlinkListener(hle -> {
|
---|
95 | if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
|
---|
96 | Logging.info(hle.getURL().toString());
|
---|
97 | if (hle.getURL() == null || hle.getURL().toString().isEmpty()) {
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | if (!hle.getURL().toString().startsWith("http")) {
|
---|
101 | module.performAction(hle.getURL().toString());
|
---|
102 | } else {
|
---|
103 | String ret = OpenBrowser.displayUrl(hle.getURL().toString());
|
---|
104 | if (ret != null) {
|
---|
105 | PointInfoUtils.showNotification(ret, "error");
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | });
|
---|
110 | JScrollPane scrollPane = new JScrollPane(msgLabel);
|
---|
111 | Object[] objects = {scrollPane};
|
---|
112 | final ImageIcon icon = ImageProvider.get("dialogs/pointinfo", ImageProvider.ImageSizes.SETTINGS_TAB);
|
---|
113 | JOptionPane.showMessageDialog(
|
---|
114 | null, objects, tr("PointInfo") + " " + coordinatesText, JOptionPane.PLAIN_MESSAGE, icon);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | protected void cancel() {
|
---|
120 | PointInfoAction.this.cancel();
|
---|
121 | }
|
---|
122 | };
|
---|
123 | new Thread(infoTask).start();
|
---|
124 | } catch (Exception e) {
|
---|
125 | Logging.error(e);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void infoSync(LatLon pos, ProgressMonitor progressMonitor) {
|
---|
130 |
|
---|
131 | progressMonitor.beginTask(null, 3);
|
---|
132 | try {
|
---|
133 | module.prepareData(pos);
|
---|
134 | htmlText = module.getHtml();
|
---|
135 | coordinatesText = PointInfoUtils.formatCoordinates(pos.lat(), pos.lon());
|
---|
136 |
|
---|
137 | } finally {
|
---|
138 | progressMonitor.finishTask();
|
---|
139 | }
|
---|
140 | progressMonitor.invalidate();
|
---|
141 | if (htmlText.length() == 0) {
|
---|
142 | GuiHelper.runInEDTAndWait(
|
---|
143 | () -> PointInfoUtils.showNotification(tr("Data not available.")+ "\n(" + pos.toDisplayString() + ")", "warning"));
|
---|
144 | return;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | public void cancel() {
|
---|
149 | cancel = true;
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public void mouseClicked(MouseEvent e) {
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Override
|
---|
157 | public void mouseEntered(MouseEvent e) {
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | public void mouseExited(MouseEvent e) {
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | public void mousePressed(MouseEvent e) {
|
---|
166 | if (!MainApplication.getMap().mapView.isActiveLayerDrawable()) {
|
---|
167 | return;
|
---|
168 | }
|
---|
169 | requestFocusInMapView();
|
---|
170 | updateKeyModifiers(e);
|
---|
171 | if (e.getButton() == MouseEvent.BUTTON1) {
|
---|
172 | infoAsync(e.getPoint());
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | protected void updateKeyModifiers(MouseEvent e) {
|
---|
178 | ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
---|
179 | alt = (e.getModifiers() & (ActionEvent.ALT_MASK | InputEvent.ALT_GRAPH_MASK)) != 0;
|
---|
180 | shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Override
|
---|
184 | public void mouseReleased(MouseEvent e) {
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|