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