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.Component;
|
---|
12 | import java.awt.Dimension;
|
---|
13 | import java.awt.GridBagConstraints;
|
---|
14 | import java.awt.GridBagLayout;
|
---|
15 | import java.awt.event.ActionEvent;
|
---|
16 | import java.awt.event.KeyEvent;
|
---|
17 | import java.awt.event.WindowEvent;
|
---|
18 |
|
---|
19 | import javax.swing.Box;
|
---|
20 | import javax.swing.AbstractAction;
|
---|
21 | import javax.swing.ImageIcon;
|
---|
22 | import javax.swing.JButton;
|
---|
23 | import javax.swing.JComponent;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 | import javax.swing.JToggleButton;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
---|
29 | import org.openstreetmap.josm.gui.dialogs.DialogsPanel.Action;
|
---|
30 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
31 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
32 |
|
---|
33 | public class ImageViewerDialog extends ToggleDialog {
|
---|
34 |
|
---|
35 | private static final String COMMAND_ZOOM = "zoom";
|
---|
36 | private static final String COMMAND_CENTERVIEW = "centre";
|
---|
37 | private static final String COMMAND_NEXT = "next";
|
---|
38 | private static final String COMMAND_REMOVE = "remove";
|
---|
39 | private static final String COMMAND_REMOVE_FROM_DISK = "removefromdisk";
|
---|
40 | private static final String COMMAND_PREVIOUS = "previous";
|
---|
41 | private static final String COMMAND_COLLAPSE = "collapse";
|
---|
42 |
|
---|
43 | private ImageDisplay imgDisplay = new ImageDisplay();
|
---|
44 | private boolean centerView = false;
|
---|
45 |
|
---|
46 | // Only one instance of that class is present at one time
|
---|
47 | private static ImageViewerDialog dialog;
|
---|
48 |
|
---|
49 | private boolean collapseButtonClicked = false;
|
---|
50 |
|
---|
51 | static void newInstance() {
|
---|
52 | dialog = new ImageViewerDialog();
|
---|
53 | }
|
---|
54 |
|
---|
55 | public static ImageViewerDialog getInstance() {
|
---|
56 | if (dialog == null) {
|
---|
57 | throw new AssertionError(); // a new instance needs to be created first
|
---|
58 | }
|
---|
59 | return dialog;
|
---|
60 | }
|
---|
61 |
|
---|
62 | private JButton btnNext;
|
---|
63 | private JButton btnPrevious;
|
---|
64 | private JButton btnCollapse;
|
---|
65 |
|
---|
66 | private ImageViewerDialog() {
|
---|
67 | 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);
|
---|
68 |
|
---|
69 | /* Don't show a detached dialog right from the start. */
|
---|
70 | if (isShowing && !isDocked) {
|
---|
71 | setIsShowing(false);
|
---|
72 | }
|
---|
73 |
|
---|
74 | JPanel content = new JPanel();
|
---|
75 | content.setLayout(new BorderLayout());
|
---|
76 |
|
---|
77 | content.add(imgDisplay, BorderLayout.CENTER);
|
---|
78 |
|
---|
79 | Dimension buttonDim = new Dimension(26,26);
|
---|
80 |
|
---|
81 | ImageAction prevAction = new ImageAction(COMMAND_PREVIOUS, ImageProvider.get("dialogs", "previous"), tr("Previous"));
|
---|
82 | btnPrevious = new JButton(prevAction);
|
---|
83 | btnPrevious.setPreferredSize(buttonDim);
|
---|
84 | Shortcut scPrev = Shortcut.registerShortcut(
|
---|
85 | "geoimage:previous", tr("Geoimage: {0}", tr("Show previous Image")), KeyEvent.VK_PAGE_UP, Shortcut.GROUP_DIRECT);
|
---|
86 | final String APREVIOUS = "Previous Image";
|
---|
87 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scPrev.getKeyStroke(), APREVIOUS);
|
---|
88 | Main.contentPane.getActionMap().put(APREVIOUS, prevAction);
|
---|
89 | btnPrevious.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scPrev.getKeyStroke(), APREVIOUS);
|
---|
90 | btnPrevious.getActionMap().put(APREVIOUS, prevAction);
|
---|
91 |
|
---|
92 | final String DELETE_TEXT = tr("Remove photo from layer");
|
---|
93 | ImageAction delAction = new ImageAction(COMMAND_REMOVE, ImageProvider.get("dialogs", "delete"), DELETE_TEXT);
|
---|
94 | JButton btnDelete = new JButton(delAction);
|
---|
95 | btnDelete.setPreferredSize(buttonDim);
|
---|
96 | Shortcut scDelete = Shortcut.registerShortcut(
|
---|
97 | "geoimage:deleteimagefromlayer", tr("Geoimage: {0}", DELETE_TEXT), KeyEvent.VK_DELETE, Shortcut.GROUP_DIRECT, Shortcut.SHIFT_DEFAULT);
|
---|
98 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDelete.getKeyStroke(), DELETE_TEXT);
|
---|
99 | Main.contentPane.getActionMap().put(DELETE_TEXT, delAction);
|
---|
100 | btnDelete.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDelete.getKeyStroke(), DELETE_TEXT);
|
---|
101 | btnDelete.getActionMap().put(DELETE_TEXT, delAction);
|
---|
102 |
|
---|
103 | ImageAction delFromDiskAction = new ImageAction(COMMAND_REMOVE_FROM_DISK, ImageProvider.get("dialogs", "geoimage/deletefromdisk"), tr("Delete image file from disk"));
|
---|
104 | JButton btnDeleteFromDisk = new JButton(delFromDiskAction);
|
---|
105 | btnDeleteFromDisk.setPreferredSize(buttonDim);
|
---|
106 | Shortcut scDeleteFromDisk = Shortcut.registerShortcut(
|
---|
107 | "geoimage:deletefilefromdisk", tr("Geoimage: {0}", tr("Delete File from disk")), KeyEvent.VK_DELETE, Shortcut.GROUP_DIRECT, Shortcut.GROUP_MENU + Shortcut.SHIFT_DEFAULT);
|
---|
108 | final String ADELFROMDISK = "Delete image file from disk";
|
---|
109 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDeleteFromDisk.getKeyStroke(), ADELFROMDISK);
|
---|
110 | Main.contentPane.getActionMap().put(ADELFROMDISK, delFromDiskAction);
|
---|
111 | btnDeleteFromDisk.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDeleteFromDisk.getKeyStroke(), ADELFROMDISK);
|
---|
112 | btnDeleteFromDisk.getActionMap().put(ADELFROMDISK, delFromDiskAction);
|
---|
113 |
|
---|
114 | ImageAction nextAction = new ImageAction(COMMAND_NEXT, ImageProvider.get("dialogs", "next"), tr("Next"));
|
---|
115 | btnNext = new JButton(nextAction);
|
---|
116 | btnNext.setPreferredSize(buttonDim);
|
---|
117 | Shortcut scNext = Shortcut.registerShortcut(
|
---|
118 | "geoimage:next", tr("Geoimage: {0}", tr("Show next Image")), KeyEvent.VK_PAGE_DOWN, Shortcut.GROUP_DIRECT);
|
---|
119 | final String ANEXT = "Next Image";
|
---|
120 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scNext.getKeyStroke(), ANEXT);
|
---|
121 | Main.contentPane.getActionMap().put(ANEXT, nextAction);
|
---|
122 | btnNext.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scNext.getKeyStroke(), ANEXT);
|
---|
123 | btnNext.getActionMap().put(ANEXT, nextAction);
|
---|
124 |
|
---|
125 | JToggleButton tbCentre = new JToggleButton(new ImageAction(COMMAND_CENTERVIEW, ImageProvider.get("dialogs", "centreview"), tr("Center view")));
|
---|
126 | tbCentre.setPreferredSize(buttonDim);
|
---|
127 |
|
---|
128 | JButton btnZoomBestFit = new JButton(new ImageAction(COMMAND_ZOOM, ImageProvider.get("dialogs", "zoom-best-fit"), tr("Zoom best fit and 1:1")));
|
---|
129 | btnZoomBestFit.setPreferredSize(buttonDim);
|
---|
130 |
|
---|
131 | btnCollapse = new JButton(new ImageAction(COMMAND_COLLAPSE, ImageProvider.get("dialogs", "collapse"), tr("Move dialog to the side pane")));
|
---|
132 | btnCollapse.setPreferredSize(new Dimension(20,20));
|
---|
133 | btnCollapse.setAlignmentY(Component.TOP_ALIGNMENT);
|
---|
134 |
|
---|
135 | JPanel buttons = new JPanel();
|
---|
136 | buttons.add(btnPrevious);
|
---|
137 | buttons.add(btnNext);
|
---|
138 | buttons.add(Box.createRigidArea(new Dimension(14, 0)));
|
---|
139 | buttons.add(tbCentre);
|
---|
140 | buttons.add(btnZoomBestFit);
|
---|
141 | buttons.add(Box.createRigidArea(new Dimension(14, 0)));
|
---|
142 | buttons.add(btnDelete);
|
---|
143 | buttons.add(btnDeleteFromDisk);
|
---|
144 |
|
---|
145 | JPanel bottomPane = new JPanel();
|
---|
146 | bottomPane.setLayout(new GridBagLayout());
|
---|
147 | GridBagConstraints gc = new GridBagConstraints();
|
---|
148 | gc.gridx = 0;
|
---|
149 | gc.gridy = 0;
|
---|
150 | gc.anchor = GridBagConstraints.CENTER;
|
---|
151 | gc.weightx = 1;
|
---|
152 | bottomPane.add(buttons, gc);
|
---|
153 |
|
---|
154 | gc.gridx = 1;
|
---|
155 | gc.gridy = 0;
|
---|
156 | gc.anchor = GridBagConstraints.PAGE_END;
|
---|
157 | gc.weightx = 0;
|
---|
158 | bottomPane.add(btnCollapse, gc);
|
---|
159 |
|
---|
160 | content.add(bottomPane, BorderLayout.SOUTH);
|
---|
161 |
|
---|
162 | add(content, BorderLayout.CENTER);
|
---|
163 | }
|
---|
164 |
|
---|
165 | class ImageAction extends AbstractAction {
|
---|
166 | private final String action;
|
---|
167 | public ImageAction(String action, ImageIcon icon, String toolTipText) {
|
---|
168 | this.action = action;
|
---|
169 | putValue(SHORT_DESCRIPTION, toolTipText);
|
---|
170 | putValue(SMALL_ICON, icon);
|
---|
171 | }
|
---|
172 |
|
---|
173 | public void actionPerformed(ActionEvent e) {
|
---|
174 | if (COMMAND_NEXT.equals(action)) {
|
---|
175 | if (currentLayer != null) {
|
---|
176 | currentLayer.showNextPhoto();
|
---|
177 | }
|
---|
178 | } else if (COMMAND_PREVIOUS.equals(action)) {
|
---|
179 | if (currentLayer != null) {
|
---|
180 | currentLayer.showPreviousPhoto();
|
---|
181 | }
|
---|
182 |
|
---|
183 | } else if (COMMAND_CENTERVIEW.equals(action)) {
|
---|
184 | centerView = ((JToggleButton) e.getSource()).isSelected();
|
---|
185 | if (centerView && currentEntry != null && currentEntry.getPos() != null) {
|
---|
186 | Main.map.mapView.zoomTo(currentEntry.getPos());
|
---|
187 | }
|
---|
188 |
|
---|
189 | } else if (COMMAND_ZOOM.equals(action)) {
|
---|
190 | imgDisplay.zoomBestFitOrOne();
|
---|
191 |
|
---|
192 | } else if (COMMAND_REMOVE.equals(action)) {
|
---|
193 | if (currentLayer != null) {
|
---|
194 | currentLayer.removeCurrentPhoto();
|
---|
195 | }
|
---|
196 | } else if (COMMAND_REMOVE_FROM_DISK.equals(action)) {
|
---|
197 | if (currentLayer != null) {
|
---|
198 | currentLayer.removeCurrentPhotoFromDisk();
|
---|
199 | }
|
---|
200 | } else if (COMMAND_COLLAPSE.equals(action)) {
|
---|
201 | collapseButtonClicked = true;
|
---|
202 | detachedDialog.getToolkit().getSystemEventQueue().postEvent(new WindowEvent(detachedDialog, WindowEvent.WINDOW_CLOSING));
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | public static void showImage(GeoImageLayer layer, ImageEntry entry) {
|
---|
208 | getInstance().displayImage(layer, entry);
|
---|
209 | layer.checkPreviousNextButtons();
|
---|
210 | }
|
---|
211 | public static void setPreviousEnabled(Boolean value) {
|
---|
212 | getInstance().btnPrevious.setEnabled(value);
|
---|
213 | }
|
---|
214 | public static void setNextEnabled(Boolean value) {
|
---|
215 | getInstance().btnNext.setEnabled(value);
|
---|
216 | }
|
---|
217 |
|
---|
218 | private GeoImageLayer currentLayer = null;
|
---|
219 | private ImageEntry currentEntry = null;
|
---|
220 |
|
---|
221 | public void displayImage(GeoImageLayer layer, ImageEntry entry) {
|
---|
222 | synchronized(this) {
|
---|
223 | // if (currentLayer == layer && currentEntry == entry) {
|
---|
224 | // repaint();
|
---|
225 | // return;
|
---|
226 | // } TODO: pop up image dialog but don't load image again
|
---|
227 |
|
---|
228 | if (centerView && Main.map != null && entry != null && entry.getPos() != null) {
|
---|
229 | Main.map.mapView.zoomTo(entry.getPos());
|
---|
230 | }
|
---|
231 |
|
---|
232 | currentLayer = layer;
|
---|
233 | currentEntry = entry;
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (entry != null) {
|
---|
237 | imgDisplay.setImage(entry.getFile());
|
---|
238 | setTitle("Geotagged Images" + (entry.getFile() != null ? " - " + entry.getFile().getName() : ""));
|
---|
239 | StringBuffer osd = new StringBuffer(entry.getFile() != null ? entry.getFile().getName() : "");
|
---|
240 | if (entry.getElevation() != null) {
|
---|
241 | osd.append(tr("\nAltitude: {0} m", entry.getElevation().longValue()));
|
---|
242 | }
|
---|
243 | if (entry.getSpeed() != null) {
|
---|
244 | osd.append(tr("\n{0} km/h", Math.round(entry.getSpeed())));
|
---|
245 | }
|
---|
246 | //if (entry.getPos() != null) {
|
---|
247 | // osd.append(tr("\nlat: {0}, lon: {1}", Double.toString(entry.getPos().lat()), Double.toString(entry.getPos().lon())));
|
---|
248 | //}
|
---|
249 | //osd.append(tr("\nfile mtime: {0}", Long.toString(entry.getFile().lastModified())));
|
---|
250 | //osd.append(tr("\nImage exif time: {0}", Long.toString(entry.getExifTime().getTime())));
|
---|
251 | //if (entry.getGpsTime() != null) {
|
---|
252 | // osd.append(tr("\nImage gps time: {0}", Long.toString(entry.getGpsTime().getTime())));
|
---|
253 | //}
|
---|
254 |
|
---|
255 | imgDisplay.setOsdText(osd.toString());
|
---|
256 | } else {
|
---|
257 | imgDisplay.setImage(null);
|
---|
258 | imgDisplay.setOsdText("");
|
---|
259 | }
|
---|
260 | if (! isDialogShowing()) {
|
---|
261 | setIsDocked(false); // always open a detached window when an image is clicked and dialog is closed
|
---|
262 | showDialog();
|
---|
263 | } else {
|
---|
264 | if (isDocked && isCollapsed) {
|
---|
265 | expand();
|
---|
266 | dialogsPanel.reconstruct(Action.COLLAPSED_TO_DEFAULT, this);
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * When pressing the Toggle button always show the docked dialog.
|
---|
274 | */
|
---|
275 | @Override
|
---|
276 | protected void toggleButtonHook() {
|
---|
277 | if (! isShowing) {
|
---|
278 | setIsDocked(true);
|
---|
279 | setIsCollapsed(false);
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * When an image is closed, really close it and do not pop
|
---|
285 | * up the side dialog.
|
---|
286 | */
|
---|
287 | @Override
|
---|
288 | protected boolean dockWhenClosingDetachedDlg() {
|
---|
289 | if (collapseButtonClicked) {
|
---|
290 | collapseButtonClicked = false;
|
---|
291 | return true;
|
---|
292 | }
|
---|
293 | return false;
|
---|
294 | }
|
---|
295 |
|
---|
296 | @Override
|
---|
297 | protected void stateChanged() {
|
---|
298 | super.stateChanged();
|
---|
299 | if (btnCollapse != null) {
|
---|
300 | btnCollapse.setVisible(!isDocked);
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Returns whether an image is currently displayed
|
---|
306 | * @return If image is currently displayed
|
---|
307 | */
|
---|
308 | public boolean hasImage() {
|
---|
309 | return currentEntry != null;
|
---|
310 | }
|
---|
311 | }
|
---|