1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.markerlayer;
|
---|
3 |
|
---|
4 | import java.awt.BorderLayout;
|
---|
5 | import java.awt.Cursor;
|
---|
6 | import java.awt.GraphicsEnvironment;
|
---|
7 | import java.awt.Image;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.net.URL;
|
---|
10 | import java.util.Collections;
|
---|
11 |
|
---|
12 | import javax.swing.Icon;
|
---|
13 | import javax.swing.ImageIcon;
|
---|
14 | import javax.swing.JDialog;
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JScrollPane;
|
---|
19 | import javax.swing.JToggleButton;
|
---|
20 | import javax.swing.JViewport;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
24 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
25 | import org.openstreetmap.josm.data.gpx.GpxLink;
|
---|
26 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
27 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Marker representing an image. Uses a special icon, and when clicked,
|
---|
31 | * displays an image view dialog. Re-uses some code from GeoImageLayer.
|
---|
32 | *
|
---|
33 | * @author Frederik Ramm
|
---|
34 | *
|
---|
35 | */
|
---|
36 | public class ImageMarker extends ButtonMarker {
|
---|
37 |
|
---|
38 | public URL imageUrl;
|
---|
39 |
|
---|
40 | public ImageMarker(LatLon ll, URL imageUrl, MarkerLayer parentLayer, double time, double offset) {
|
---|
41 | super(ll, /* ICON(markers/) */ "photo", parentLayer, time, offset);
|
---|
42 | this.imageUrl = imageUrl;
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override public void actionPerformed(ActionEvent ev) {
|
---|
46 | final JPanel p = new JPanel(new BorderLayout());
|
---|
47 | final JScrollPane scroll = new JScrollPane(new JLabel(loadScaledImage(imageUrl, 580)));
|
---|
48 | final JViewport vp = scroll.getViewport();
|
---|
49 | p.add(scroll, BorderLayout.CENTER);
|
---|
50 |
|
---|
51 | final JToggleButton scale = new JToggleButton(ImageProvider.get("misc", "rectangle"));
|
---|
52 |
|
---|
53 | JPanel p2 = new JPanel();
|
---|
54 | p2.add(scale);
|
---|
55 | p.add(p2, BorderLayout.SOUTH);
|
---|
56 | scale.addActionListener(ev1 -> {
|
---|
57 | p.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
|
---|
58 | if (scale.getModel().isSelected()) {
|
---|
59 | ((JLabel) vp.getView()).setIcon(loadScaledImage(imageUrl, Math.max(vp.getWidth(), vp.getHeight())));
|
---|
60 | } else {
|
---|
61 | ((JLabel) vp.getView()).setIcon(new ImageIcon(imageUrl));
|
---|
62 | }
|
---|
63 | p.setCursor(Cursor.getDefaultCursor());
|
---|
64 | });
|
---|
65 | scale.setSelected(true);
|
---|
66 | JOptionPane pane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE);
|
---|
67 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
68 | JDialog dlg = pane.createDialog(Main.parent, imageUrl.toString());
|
---|
69 | dlg.setModal(false);
|
---|
70 | dlg.toFront();
|
---|
71 | dlg.setVisible(true);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private static Icon loadScaledImage(URL u, int maxSize) {
|
---|
76 | Image img = new ImageIcon(u).getImage();
|
---|
77 | int w = img.getWidth(null);
|
---|
78 | int h = img.getHeight(null);
|
---|
79 | if (w > h) {
|
---|
80 | h = (int) Math.round(maxSize*((double) h/w));
|
---|
81 | w = maxSize;
|
---|
82 | } else {
|
---|
83 | w = (int) Math.round(maxSize*((double) w/h));
|
---|
84 | h = maxSize;
|
---|
85 | }
|
---|
86 | return new ImageIcon(img.getScaledInstance(w, h, Image.SCALE_SMOOTH));
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public WayPoint convertToWayPoint() {
|
---|
91 | WayPoint wpt = super.convertToWayPoint();
|
---|
92 | GpxLink link = new GpxLink(imageUrl.toString());
|
---|
93 | link.type = "image";
|
---|
94 | wpt.put(GpxConstants.META_LINKS, Collections.singleton(link));
|
---|
95 | return wpt;
|
---|
96 | }
|
---|
97 | }
|
---|