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