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