source: josm/trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageViewerDialog.java@ 9543

Last change on this file since 9543 was 9543, checked in by Don-vip, 8 years ago

refactoring - global simplification of use of setLayout method - simply pass layout to JPanel constructor

  • Property svn:eol-style set to native
File size: 17.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.geoimage;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Dimension;
9import java.awt.GridBagConstraints;
10import java.awt.GridBagLayout;
11import java.awt.event.ActionEvent;
12import java.awt.event.KeyEvent;
13import java.awt.event.WindowEvent;
14import java.text.DateFormat;
15
16import javax.swing.AbstractAction;
17import javax.swing.Box;
18import javax.swing.ImageIcon;
19import javax.swing.JButton;
20import javax.swing.JComponent;
21import javax.swing.JPanel;
22import javax.swing.JToggleButton;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.gui.MapView;
26import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
27import org.openstreetmap.josm.gui.dialogs.DialogsPanel.Action;
28import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
29import org.openstreetmap.josm.gui.layer.Layer;
30import org.openstreetmap.josm.tools.ImageProvider;
31import org.openstreetmap.josm.tools.Shortcut;
32import org.openstreetmap.josm.tools.date.DateUtils;
33
34public final class ImageViewerDialog extends ToggleDialog implements LayerChangeListener {
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 private static final String COMMAND_FIRST = "first";
44 private static final String COMMAND_LAST = "last";
45 private static final String COMMAND_COPY_PATH = "copypath";
46
47 private final ImageDisplay imgDisplay = new ImageDisplay();
48 private boolean centerView;
49
50 // Only one instance of that class is present at one time
51 private static volatile ImageViewerDialog dialog;
52
53 private boolean collapseButtonClicked;
54
55 static void newInstance() {
56 dialog = new ImageViewerDialog();
57 }
58
59 /**
60 * Replies the unique instance of this dialog
61 * @return the unique instance
62 */
63 public static ImageViewerDialog getInstance() {
64 if (dialog == null)
65 throw new AssertionError("a new instance needs to be created first");
66 return dialog;
67 }
68
69 private JButton btnNext;
70 private JButton btnPrevious;
71 private JButton btnCollapse;
72 private JToggleButton tbCentre;
73
74 private ImageViewerDialog() {
75 super(tr("Geotagged Images"), "geoimage", tr("Display geotagged images"), Shortcut.registerShortcut("tools:geotagged",
76 tr("Tool: {0}", tr("Display geotagged images")), KeyEvent.VK_Y, Shortcut.DIRECT), 200);
77 build();
78 MapView.addLayerChangeListener(this);
79 }
80
81 protected void build() {
82 JPanel content = new JPanel(new BorderLayout());
83
84 content.add(imgDisplay, BorderLayout.CENTER);
85
86 Dimension buttonDim = new Dimension(26, 26);
87
88 ImageAction prevAction = new ImageAction(COMMAND_PREVIOUS, ImageProvider.get("dialogs", "previous"), tr("Previous"));
89 btnPrevious = new JButton(prevAction);
90 btnPrevious.setPreferredSize(buttonDim);
91 Shortcut scPrev = Shortcut.registerShortcut(
92 "geoimage:previous", tr("Geoimage: {0}", tr("Show previous Image")), KeyEvent.VK_PAGE_UP, Shortcut.DIRECT);
93 final String APREVIOUS = "Previous Image";
94 Main.registerActionShortcut(prevAction, scPrev);
95 btnPrevious.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scPrev.getKeyStroke(), APREVIOUS);
96 btnPrevious.getActionMap().put(APREVIOUS, prevAction);
97 btnPrevious.setEnabled(false);
98
99 final String DELETE_TEXT = tr("Remove photo from layer");
100 ImageAction delAction = new ImageAction(COMMAND_REMOVE, ImageProvider.get("dialogs", "delete"), DELETE_TEXT);
101 JButton btnDelete = new JButton(delAction);
102 btnDelete.setPreferredSize(buttonDim);
103 Shortcut scDelete = Shortcut.registerShortcut(
104 "geoimage:deleteimagefromlayer", tr("Geoimage: {0}", tr("Remove photo from layer")), KeyEvent.VK_DELETE, Shortcut.SHIFT);
105 Main.registerActionShortcut(delAction, scDelete);
106 btnDelete.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDelete.getKeyStroke(), DELETE_TEXT);
107 btnDelete.getActionMap().put(DELETE_TEXT, delAction);
108
109 ImageAction delFromDiskAction = new ImageAction(COMMAND_REMOVE_FROM_DISK,
110 ImageProvider.get("dialogs", "geoimage/deletefromdisk"), tr("Delete image file from disk"));
111 JButton btnDeleteFromDisk = new JButton(delFromDiskAction);
112 btnDeleteFromDisk.setPreferredSize(buttonDim);
113 Shortcut scDeleteFromDisk = Shortcut.registerShortcut(
114 "geoimage:deletefilefromdisk", tr("Geoimage: {0}", tr("Delete File from disk")), KeyEvent.VK_DELETE, Shortcut.CTRL_SHIFT);
115 final String ADELFROMDISK = "Delete image file from disk";
116 Main.registerActionShortcut(delFromDiskAction, scDeleteFromDisk);
117 btnDeleteFromDisk.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDeleteFromDisk.getKeyStroke(), ADELFROMDISK);
118 btnDeleteFromDisk.getActionMap().put(ADELFROMDISK, delFromDiskAction);
119
120 ImageAction copyPathAction = new ImageAction(COMMAND_COPY_PATH, ImageProvider.get("copy"), tr("Copy image path"));
121 JButton btnCopyPath = new JButton(copyPathAction);
122 btnCopyPath.setPreferredSize(buttonDim);
123 Shortcut scCopyPath = Shortcut.registerShortcut(
124 "geoimage:copypath", tr("Geoimage: {0}", tr("Copy image path")), KeyEvent.VK_C, Shortcut.ALT_CTRL_SHIFT);
125 final String ACOPYPATH = "Copy image path";
126 Main.registerActionShortcut(copyPathAction, scCopyPath);
127 btnCopyPath.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scCopyPath.getKeyStroke(), ACOPYPATH);
128 btnCopyPath.getActionMap().put(ACOPYPATH, copyPathAction);
129
130 ImageAction nextAction = new ImageAction(COMMAND_NEXT, ImageProvider.get("dialogs", "next"), tr("Next"));
131 btnNext = new JButton(nextAction);
132 btnNext.setPreferredSize(buttonDim);
133 Shortcut scNext = Shortcut.registerShortcut(
134 "geoimage:next", tr("Geoimage: {0}", tr("Show next Image")), KeyEvent.VK_PAGE_DOWN, Shortcut.DIRECT);
135 final String ANEXT = "Next Image";
136 Main.registerActionShortcut(nextAction, scNext);
137 btnNext.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scNext.getKeyStroke(), ANEXT);
138 btnNext.getActionMap().put(ANEXT, nextAction);
139 btnNext.setEnabled(false);
140
141 Main.registerActionShortcut(
142 new ImageAction(COMMAND_FIRST, null, null),
143 Shortcut.registerShortcut(
144 "geoimage:first", tr("Geoimage: {0}", tr("Show first Image")), KeyEvent.VK_HOME, Shortcut.DIRECT)
145 );
146 Main.registerActionShortcut(
147 new ImageAction(COMMAND_LAST, null, null),
148 Shortcut.registerShortcut(
149 "geoimage:last", tr("Geoimage: {0}", tr("Show last Image")), KeyEvent.VK_END, Shortcut.DIRECT)
150 );
151
152 tbCentre = new JToggleButton(new ImageAction(COMMAND_CENTERVIEW,
153 ImageProvider.get("dialogs", "centreview"), tr("Center view")));
154 tbCentre.setPreferredSize(buttonDim);
155
156 JButton btnZoomBestFit = new JButton(new ImageAction(COMMAND_ZOOM,
157 ImageProvider.get("dialogs", "zoom-best-fit"), tr("Zoom best fit and 1:1")));
158 btnZoomBestFit.setPreferredSize(buttonDim);
159
160 btnCollapse = new JButton(new ImageAction(COMMAND_COLLAPSE,
161 ImageProvider.get("dialogs", "collapse"), tr("Move dialog to the side pane")));
162 btnCollapse.setPreferredSize(new Dimension(20, 20));
163 btnCollapse.setAlignmentY(Component.TOP_ALIGNMENT);
164
165 JPanel buttons = new JPanel();
166 buttons.add(btnPrevious);
167 buttons.add(btnNext);
168 buttons.add(Box.createRigidArea(new Dimension(7, 0)));
169 buttons.add(tbCentre);
170 buttons.add(btnZoomBestFit);
171 buttons.add(Box.createRigidArea(new Dimension(7, 0)));
172 buttons.add(btnDelete);
173 buttons.add(btnDeleteFromDisk);
174 buttons.add(Box.createRigidArea(new Dimension(7, 0)));
175 buttons.add(btnCopyPath);
176
177 JPanel bottomPane = new JPanel(new GridBagLayout());
178 GridBagConstraints gc = new GridBagConstraints();
179 gc.gridx = 0;
180 gc.gridy = 0;
181 gc.anchor = GridBagConstraints.CENTER;
182 gc.weightx = 1;
183 bottomPane.add(buttons, gc);
184
185 gc.gridx = 1;
186 gc.gridy = 0;
187 gc.anchor = GridBagConstraints.PAGE_END;
188 gc.weightx = 0;
189 bottomPane.add(btnCollapse, gc);
190
191 content.add(bottomPane, BorderLayout.SOUTH);
192
193 createLayout(content, false, null);
194 }
195
196 @Override
197 public void destroy() {
198 MapView.removeLayerChangeListener(this);
199 super.destroy();
200 }
201
202 class ImageAction extends AbstractAction {
203 private final String action;
204
205 ImageAction(String action, ImageIcon icon, String toolTipText) {
206 this.action = action;
207 putValue(SHORT_DESCRIPTION, toolTipText);
208 putValue(SMALL_ICON, icon);
209 }
210
211 @Override
212 public void actionPerformed(ActionEvent e) {
213 if (COMMAND_NEXT.equals(action)) {
214 if (currentLayer != null) {
215 currentLayer.showNextPhoto();
216 }
217 } else if (COMMAND_PREVIOUS.equals(action)) {
218 if (currentLayer != null) {
219 currentLayer.showPreviousPhoto();
220 }
221 } else if (COMMAND_FIRST.equals(action) && currentLayer != null) {
222 currentLayer.showFirstPhoto();
223 } else if (COMMAND_LAST.equals(action) && currentLayer != null) {
224 currentLayer.showLastPhoto();
225 } else if (COMMAND_CENTERVIEW.equals(action)) {
226 final JToggleButton button = (JToggleButton) e.getSource();
227 centerView = button.isEnabled() && button.isSelected();
228 if (centerView && currentEntry != null && currentEntry.getPos() != null) {
229 Main.map.mapView.zoomTo(currentEntry.getPos());
230 }
231 } else if (COMMAND_ZOOM.equals(action)) {
232 imgDisplay.zoomBestFitOrOne();
233 } else if (COMMAND_REMOVE.equals(action)) {
234 if (currentLayer != null) {
235 currentLayer.removeCurrentPhoto();
236 }
237 } else if (COMMAND_REMOVE_FROM_DISK.equals(action)) {
238 if (currentLayer != null) {
239 currentLayer.removeCurrentPhotoFromDisk();
240 }
241 } else if (COMMAND_COPY_PATH.equals(action)) {
242 if (currentLayer != null) {
243 currentLayer.copyCurrentPhotoPath();
244 }
245 } else if (COMMAND_COLLAPSE.equals(action)) {
246 collapseButtonClicked = true;
247 detachedDialog.getToolkit().getSystemEventQueue().postEvent(new WindowEvent(detachedDialog, WindowEvent.WINDOW_CLOSING));
248 }
249 }
250 }
251
252 public static void showImage(GeoImageLayer layer, ImageEntry entry) {
253 getInstance().displayImage(layer, entry);
254 if (layer != null) {
255 layer.checkPreviousNextButtons();
256 } else {
257 setPreviousEnabled(false);
258 setNextEnabled(false);
259 }
260 }
261
262 /**
263 * Enables (or disables) the "Previous" button.
264 * @param value {@code true} to enable the button, {@code false} otherwise
265 */
266 public static void setPreviousEnabled(boolean value) {
267 getInstance().btnPrevious.setEnabled(value);
268 }
269
270 /**
271 * Enables (or disables) the "Next" button.
272 * @param value {@code true} to enable the button, {@code false} otherwise
273 */
274 public static void setNextEnabled(boolean value) {
275 getInstance().btnNext.setEnabled(value);
276 }
277
278 /**
279 * Enables (or disables) the "Center view" button.
280 * @param value {@code true} to enable the button, {@code false} otherwise
281 * @return the old enabled value. Can be used to restore the original enable state
282 */
283 public static synchronized boolean setCentreEnabled(boolean value) {
284 final ImageViewerDialog instance = getInstance();
285 final boolean wasEnabled = instance.tbCentre.isEnabled();
286 instance.tbCentre.setEnabled(value);
287 instance.tbCentre.getAction().actionPerformed(new ActionEvent(instance.tbCentre, 0, null));
288 return wasEnabled;
289 }
290
291 private transient GeoImageLayer currentLayer;
292 private transient ImageEntry currentEntry;
293
294 public void displayImage(GeoImageLayer layer, ImageEntry entry) {
295 boolean imageChanged;
296
297 synchronized (this) {
298 // TODO: pop up image dialog but don't load image again
299
300 imageChanged = currentEntry != entry;
301
302 if (centerView && Main.isDisplayingMapView() && entry != null && entry.getPos() != null) {
303 Main.map.mapView.zoomTo(entry.getPos());
304 }
305
306 currentLayer = layer;
307 currentEntry = entry;
308 }
309
310 if (entry != null) {
311 if (imageChanged) {
312 // Set only if the image is new to preserve zoom and position if the same image is redisplayed
313 // (e.g. to update the OSD).
314 imgDisplay.setImage(entry.getFile(), entry.getExifOrientation());
315 }
316 setTitle(tr("Geotagged Images") + (entry.getFile() != null ? " - " + entry.getFile().getName() : ""));
317 StringBuilder osd = new StringBuilder(entry.getFile() != null ? entry.getFile().getName() : "");
318 if (entry.getElevation() != null) {
319 osd.append(tr("\nAltitude: {0} m", Math.round(entry.getElevation())));
320 }
321 if (entry.getSpeed() != null) {
322 osd.append(tr("\nSpeed: {0} km/h", Math.round(entry.getSpeed())));
323 }
324 if (entry.getExifImgDir() != null) {
325 osd.append(tr("\nDirection {0}\u00b0", Math.round(entry.getExifImgDir())));
326 }
327 DateFormat dtf = DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM);
328 if (entry.hasExifTime()) {
329 osd.append(tr("\nEXIF time: {0}", dtf.format(entry.getExifTime())));
330 }
331 if (entry.hasGpsTime()) {
332 osd.append(tr("\nGPS time: {0}", dtf.format(entry.getGpsTime())));
333 }
334
335 imgDisplay.setOsdText(osd.toString());
336 } else {
337 // if this method is called to reinitialize dialog content with a blank image,
338 // do not actually show the dialog again with a blank image if currently hidden (fix #10672)
339 setTitle(tr("Geotagged Images"));
340 imgDisplay.setImage(null, null);
341 imgDisplay.setOsdText("");
342 return;
343 }
344 if (!isDialogShowing()) {
345 setIsDocked(false); // always open a detached window when an image is clicked and dialog is closed
346 showDialog();
347 } else {
348 if (isDocked && isCollapsed) {
349 expand();
350 dialogsPanel.reconstruct(Action.COLLAPSED_TO_DEFAULT, this);
351 }
352 }
353 }
354
355 /**
356 * When an image is closed, really close it and do not pop
357 * up the side dialog.
358 */
359 @Override
360 protected boolean dockWhenClosingDetachedDlg() {
361 if (collapseButtonClicked) {
362 collapseButtonClicked = false;
363 return true;
364 }
365 return false;
366 }
367
368 @Override
369 protected void stateChanged() {
370 super.stateChanged();
371 if (btnCollapse != null) {
372 btnCollapse.setVisible(!isDocked);
373 }
374 }
375
376 /**
377 * Returns whether an image is currently displayed
378 * @return If image is currently displayed
379 */
380 public boolean hasImage() {
381 return currentEntry != null;
382 }
383
384 /**
385 * Returns the currently displayed image.
386 * @return Currently displayed image or {@code null}
387 * @since 6392
388 */
389 public static ImageEntry getCurrentImage() {
390 return getInstance().currentEntry;
391 }
392
393 /**
394 * Returns the layer associated with the image.
395 * @return Layer associated with the image
396 * @since 6392
397 */
398 public static GeoImageLayer getCurrentLayer() {
399 return getInstance().currentLayer;
400 }
401
402 /**
403 * Returns whether the center view is currently active.
404 * @return {@code true} if the center view is active, {@code false} otherwise
405 * @since 9416
406 */
407 public static boolean isCenterView() {
408 return getInstance().centerView;
409 }
410
411 @Override
412 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
413 if (currentLayer == null && newLayer instanceof GeoImageLayer) {
414 ((GeoImageLayer) newLayer).showFirstPhoto();
415 }
416 }
417
418 @Override
419 public void layerAdded(Layer newLayer) {
420 if (currentLayer == null && newLayer instanceof GeoImageLayer) {
421 ((GeoImageLayer) newLayer).showFirstPhoto();
422 }
423 }
424
425 @Override
426 public void layerRemoved(Layer oldLayer) {
427 // Clear current image and layer if current layer is deleted
428 if (currentLayer != null && currentLayer.equals(oldLayer)) {
429 showImage(null, null);
430 }
431 // Check buttons state in case of layer merging
432 if (currentLayer != null && oldLayer instanceof GeoImageLayer) {
433 currentLayer.checkPreviousNextButtons();
434 }
435 }
436}
Note: See TracBrowser for help on using the repository browser.