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