1 | // License: GPL. See LICENSE file for details.
|
---|
2 | // Copyright 2007 by Christian Gallioz (aka khris78)
|
---|
3 | // Parts of code from Geotagged plugin (by Rob Neild)
|
---|
4 | // and the core JOSM source code (by Immanuel Scholz and others)
|
---|
5 |
|
---|
6 | package org.openstreetmap.josm.gui.layer.geoimage;
|
---|
7 |
|
---|
8 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
9 |
|
---|
10 | import java.awt.BorderLayout;
|
---|
11 | import java.awt.Dimension;
|
---|
12 | import java.awt.FlowLayout;
|
---|
13 | import java.awt.event.ActionEvent;
|
---|
14 | import java.awt.event.ActionListener;
|
---|
15 | import java.awt.event.KeyEvent;
|
---|
16 |
|
---|
17 | import javax.swing.JButton;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JToggleButton;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
---|
23 | import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer.ImageEntry;
|
---|
24 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
25 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
26 |
|
---|
27 | public class ImageViewerDialog extends ToggleDialog implements ActionListener {
|
---|
28 |
|
---|
29 | private static final String COMMAND_ZOOM = "zoom";
|
---|
30 | private static final String COMMAND_CENTERVIEW = "centre";
|
---|
31 | private static final String COMMAND_NEXT = "next";
|
---|
32 | private static final String COMMAND_REMOVE = "remove";
|
---|
33 | private static final String COMMAND_PREVIOUS = "previous";
|
---|
34 |
|
---|
35 | private ImageDisplay imgDisplay = new ImageDisplay();
|
---|
36 | private boolean centerView = false;
|
---|
37 |
|
---|
38 | // Only one instance of that class
|
---|
39 | static private ImageViewerDialog INSTANCE = null;
|
---|
40 |
|
---|
41 | public static ImageViewerDialog getInstance() {
|
---|
42 | if (INSTANCE == null) {
|
---|
43 | INSTANCE = new ImageViewerDialog();
|
---|
44 | }
|
---|
45 | return INSTANCE;
|
---|
46 | }
|
---|
47 |
|
---|
48 | private JButton btnNext;
|
---|
49 | private JButton btnPrevious;
|
---|
50 |
|
---|
51 | private ImageViewerDialog() {
|
---|
52 | super(tr("Geotagged Images"), "geoimage", tr("Display geotagged images"), Shortcut.registerShortcut("tools:geotagged", tr("Tool: {0}", tr("Display geotagged images")), KeyEvent.VK_Y, Shortcut.GROUP_EDIT), 200);
|
---|
53 |
|
---|
54 | if (INSTANCE != null) {
|
---|
55 | throw new IllegalStateException("Image viewer dialog should not be instanciated twice !");
|
---|
56 | }
|
---|
57 |
|
---|
58 | INSTANCE = this;
|
---|
59 |
|
---|
60 | JPanel content = new JPanel();
|
---|
61 | content.setLayout(new BorderLayout());
|
---|
62 |
|
---|
63 | content.add(imgDisplay, BorderLayout.CENTER);
|
---|
64 |
|
---|
65 | JPanel buttons = new JPanel();
|
---|
66 | buttons.setLayout(new FlowLayout());
|
---|
67 |
|
---|
68 | JButton button;
|
---|
69 |
|
---|
70 | Dimension buttonDim = new Dimension(26,26);
|
---|
71 | button = new JButton();
|
---|
72 | button.setIcon(ImageProvider.get("dialogs", "previous"));
|
---|
73 | button.setActionCommand(COMMAND_PREVIOUS);
|
---|
74 | button.setToolTipText(tr("Previous"));
|
---|
75 | button.addActionListener(this);
|
---|
76 | button.setPreferredSize(buttonDim);
|
---|
77 | buttons.add(button);
|
---|
78 | btnPrevious = button; //FIX
|
---|
79 |
|
---|
80 | button = new JButton();
|
---|
81 | button.setIcon(ImageProvider.get("dialogs", "delete"));
|
---|
82 | button.setActionCommand(COMMAND_REMOVE);
|
---|
83 | button.setToolTipText(tr("Remove photo from layer"));
|
---|
84 | button.addActionListener(this);
|
---|
85 | button.setPreferredSize(buttonDim);
|
---|
86 | buttons.add(button);
|
---|
87 |
|
---|
88 | button = new JButton();
|
---|
89 | button.setIcon(ImageProvider.get("dialogs", "next"));
|
---|
90 | button.setActionCommand(COMMAND_NEXT);
|
---|
91 | button.setToolTipText(tr("Next"));
|
---|
92 | button.addActionListener(this);
|
---|
93 | button.setPreferredSize(buttonDim);
|
---|
94 | buttons.add(button);
|
---|
95 | btnNext = button;
|
---|
96 |
|
---|
97 | JToggleButton tb = new JToggleButton();
|
---|
98 | tb.setIcon(ImageProvider.get("dialogs", "centreview"));
|
---|
99 | tb.setActionCommand(COMMAND_CENTERVIEW);
|
---|
100 | tb.setToolTipText(tr("Center view"));
|
---|
101 | tb.addActionListener(this);
|
---|
102 | tb.setPreferredSize(buttonDim);
|
---|
103 | buttons.add(tb);
|
---|
104 |
|
---|
105 | button = new JButton();
|
---|
106 | button.setIcon(ImageProvider.get("dialogs", "zoom-best-fit"));
|
---|
107 | button.setActionCommand(COMMAND_ZOOM);
|
---|
108 | button.setToolTipText(tr("Zoom best fit and 1:1"));
|
---|
109 | button.addActionListener(this);
|
---|
110 | button.setPreferredSize(buttonDim);
|
---|
111 | buttons.add(button);
|
---|
112 |
|
---|
113 | content.add(buttons, BorderLayout.SOUTH);
|
---|
114 |
|
---|
115 | add(content, BorderLayout.CENTER);
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void actionPerformed(ActionEvent e) {
|
---|
120 | if (COMMAND_NEXT.equals(e.getActionCommand())) {
|
---|
121 | if (currentLayer != null) {
|
---|
122 | currentLayer.showNextPhoto();
|
---|
123 | }
|
---|
124 | } else if (COMMAND_PREVIOUS.equals(e.getActionCommand())) {
|
---|
125 | if (currentLayer != null) {
|
---|
126 | currentLayer.showPreviousPhoto();
|
---|
127 | }
|
---|
128 |
|
---|
129 | } else if (COMMAND_CENTERVIEW.equals(e.getActionCommand())) {
|
---|
130 | centerView = ((JToggleButton) e.getSource()).isSelected();
|
---|
131 | if (centerView && currentEntry != null && currentEntry.pos != null) {
|
---|
132 | Main.map.mapView.zoomTo(currentEntry.pos);
|
---|
133 | }
|
---|
134 |
|
---|
135 | } else if (COMMAND_ZOOM.equals(e.getActionCommand())) {
|
---|
136 | imgDisplay.zoomBestFitOrOne();
|
---|
137 |
|
---|
138 | } else if (COMMAND_REMOVE.equals(e.getActionCommand())) {
|
---|
139 | if (currentLayer != null) {
|
---|
140 | currentLayer.removeCurrentPhoto();
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|
145 |
|
---|
146 | public static void showImage(GeoImageLayer layer, ImageEntry entry) {
|
---|
147 | getInstance().displayImage(layer, entry);
|
---|
148 | layer.checkPreviousNextButtons();
|
---|
149 | }
|
---|
150 | public static void setPreviousEnabled(Boolean value) {
|
---|
151 | getInstance().btnPrevious.setEnabled(value);
|
---|
152 | }
|
---|
153 | public static void setNextEnabled(Boolean value) {
|
---|
154 | getInstance().btnNext.setEnabled(value);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | private GeoImageLayer currentLayer = null;
|
---|
159 | private ImageEntry currentEntry = null;
|
---|
160 |
|
---|
161 | public void displayImage(GeoImageLayer layer, ImageEntry entry) {
|
---|
162 | synchronized(this) {
|
---|
163 | if (currentLayer == layer && currentEntry == entry) {
|
---|
164 | repaint();
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (centerView && Main.map != null && entry != null && entry.pos != null) {
|
---|
169 | Main.map.mapView.zoomTo(entry.pos);
|
---|
170 | }
|
---|
171 |
|
---|
172 | currentLayer = layer;
|
---|
173 | currentEntry = entry;
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (entry != null) {
|
---|
177 | imgDisplay.setImage(entry.file);
|
---|
178 | StringBuffer osd = new StringBuffer(entry.file != null ? entry.file.getName() : "");
|
---|
179 | if (entry.elevation != null) {
|
---|
180 | osd.append(tr("\nAltitude: {0} m", entry.elevation.longValue()));
|
---|
181 | }
|
---|
182 | if (entry.speed != null) {
|
---|
183 | osd.append(tr("\n{0} km/h", Math.round(entry.speed)));
|
---|
184 | }
|
---|
185 | imgDisplay.setOsdText(osd.toString());
|
---|
186 | } else {
|
---|
187 | imgDisplay.setImage(null);
|
---|
188 | imgDisplay.setOsdText("");
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Returns whether an image is currently displayed
|
---|
194 | * @return If image is currently displayed
|
---|
195 | */
|
---|
196 | public boolean hasImage() {
|
---|
197 | return currentEntry != null;
|
---|
198 | }
|
---|
199 | }
|
---|