001// License: GPL. See LICENSE file for details. 002package org.openstreetmap.josm.gui.layer.geoimage; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.BorderLayout; 007import java.awt.Component; 008import java.awt.Dimension; 009import java.awt.GridBagConstraints; 010import java.awt.GridBagLayout; 011import java.awt.event.ActionEvent; 012import java.awt.event.KeyEvent; 013import java.awt.event.WindowEvent; 014import java.text.DateFormat; 015 016import javax.swing.AbstractAction; 017import javax.swing.Box; 018import javax.swing.ImageIcon; 019import javax.swing.JButton; 020import javax.swing.JComponent; 021import javax.swing.JPanel; 022import javax.swing.JToggleButton; 023 024import org.openstreetmap.josm.Main; 025import org.openstreetmap.josm.gui.MapView; 026import org.openstreetmap.josm.gui.MapView.LayerChangeListener; 027import org.openstreetmap.josm.gui.dialogs.DialogsPanel.Action; 028import org.openstreetmap.josm.gui.dialogs.ToggleDialog; 029import org.openstreetmap.josm.gui.layer.Layer; 030import org.openstreetmap.josm.tools.ImageProvider; 031import org.openstreetmap.josm.tools.Shortcut; 032import org.openstreetmap.josm.tools.date.DateUtils; 033 034public final class ImageViewerDialog extends ToggleDialog implements LayerChangeListener { 035 036 private static final String COMMAND_ZOOM = "zoom"; 037 private static final String COMMAND_CENTERVIEW = "centre"; 038 private static final String COMMAND_NEXT = "next"; 039 private static final String COMMAND_REMOVE = "remove"; 040 private static final String COMMAND_REMOVE_FROM_DISK = "removefromdisk"; 041 private static final String COMMAND_PREVIOUS = "previous"; 042 private static final String COMMAND_COLLAPSE = "collapse"; 043 private static final String COMMAND_FIRST = "first"; 044 private static final String COMMAND_LAST = "last"; 045 private static final String COMMAND_COPY_PATH = "copypath"; 046 047 private ImageDisplay imgDisplay = new ImageDisplay(); 048 private boolean centerView = false; 049 050 // Only one instance of that class is present at one time 051 private static ImageViewerDialog dialog; 052 053 private boolean collapseButtonClicked = false; 054 055 static void newInstance() { 056 dialog = new ImageViewerDialog(); 057 } 058 059 /** 060 * Replies the unique instance of this dialog 061 * @return the unique instance 062 */ 063 public static ImageViewerDialog getInstance() { 064 if (dialog == null) 065 throw new AssertionError("a new instance needs to be created first"); 066 return dialog; 067 } 068 069 private JButton btnNext; 070 private JButton btnPrevious; 071 private JButton btnCollapse; 072 073 private ImageViewerDialog() { 074 super(tr("Geotagged Images"), "geoimage", tr("Display geotagged images"), Shortcut.registerShortcut("tools:geotagged", 075 tr("Tool: {0}", tr("Display geotagged images")), KeyEvent.VK_Y, Shortcut.DIRECT), 200); 076 build(); 077 MapView.addLayerChangeListener(this); 078 } 079 080 protected void build() { 081 JPanel content = new JPanel(); 082 content.setLayout(new BorderLayout()); 083 084 content.add(imgDisplay, BorderLayout.CENTER); 085 086 Dimension buttonDim = new Dimension(26,26); 087 088 ImageAction prevAction = new ImageAction(COMMAND_PREVIOUS, ImageProvider.get("dialogs", "previous"), tr("Previous")); 089 btnPrevious = new JButton(prevAction); 090 btnPrevious.setPreferredSize(buttonDim); 091 Shortcut scPrev = Shortcut.registerShortcut( 092 "geoimage:previous", tr("Geoimage: {0}", tr("Show previous Image")), KeyEvent.VK_PAGE_UP, Shortcut.DIRECT); 093 final String APREVIOUS = "Previous Image"; 094 Main.registerActionShortcut(prevAction, scPrev); 095 btnPrevious.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scPrev.getKeyStroke(), APREVIOUS); 096 btnPrevious.getActionMap().put(APREVIOUS, prevAction); 097 btnPrevious.setEnabled(false); 098 099 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, ImageProvider.get("dialogs", "geoimage/deletefromdisk"), tr("Delete image file from disk")); 110 JButton btnDeleteFromDisk = new JButton(delFromDiskAction); 111 btnDeleteFromDisk.setPreferredSize(buttonDim); 112 Shortcut scDeleteFromDisk = Shortcut.registerShortcut( 113 "geoimage:deletefilefromdisk", tr("Geoimage: {0}", tr("Delete File from disk")), KeyEvent.VK_DELETE, Shortcut.CTRL_SHIFT); 114 final String ADELFROMDISK = "Delete image file from disk"; 115 Main.registerActionShortcut(delFromDiskAction, scDeleteFromDisk); 116 btnDeleteFromDisk.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scDeleteFromDisk.getKeyStroke(), ADELFROMDISK); 117 btnDeleteFromDisk.getActionMap().put(ADELFROMDISK, delFromDiskAction); 118 119 ImageAction copyPathAction = new ImageAction(COMMAND_COPY_PATH, ImageProvider.get("copy"), tr("Copy image path")); 120 JButton btnCopyPath = new JButton(copyPathAction); 121 btnCopyPath.setPreferredSize(buttonDim); 122 Shortcut scCopyPath = Shortcut.registerShortcut( 123 "geoimage:copypath", tr("Geoimage: {0}", tr("Copy image path")), KeyEvent.VK_C, Shortcut.ALT_CTRL_SHIFT); 124 final String ACOPYPATH = "Copy image path"; 125 Main.registerActionShortcut(copyPathAction, scCopyPath); 126 btnCopyPath.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scCopyPath.getKeyStroke(), ACOPYPATH); 127 btnCopyPath.getActionMap().put(ACOPYPATH, copyPathAction); 128 129 ImageAction nextAction = new ImageAction(COMMAND_NEXT, ImageProvider.get("dialogs", "next"), tr("Next")); 130 btnNext = new JButton(nextAction); 131 btnNext.setPreferredSize(buttonDim); 132 Shortcut scNext = Shortcut.registerShortcut( 133 "geoimage:next", tr("Geoimage: {0}", tr("Show next Image")), KeyEvent.VK_PAGE_DOWN, Shortcut.DIRECT); 134 final String ANEXT = "Next Image"; 135 Main.registerActionShortcut(nextAction, scNext); 136 btnNext.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scNext.getKeyStroke(), ANEXT); 137 btnNext.getActionMap().put(ANEXT, nextAction); 138 btnNext.setEnabled(false); 139 140 Main.registerActionShortcut( 141 new ImageAction(COMMAND_FIRST, null, null), 142 Shortcut.registerShortcut( 143 "geoimage:first", tr("Geoimage: {0}", tr("Show first Image")), KeyEvent.VK_HOME, Shortcut.DIRECT) 144 ); 145 Main.registerActionShortcut( 146 new ImageAction(COMMAND_LAST, null, null), 147 Shortcut.registerShortcut( 148 "geoimage:last", tr("Geoimage: {0}", tr("Show last Image")), KeyEvent.VK_END, Shortcut.DIRECT) 149 ); 150 151 JToggleButton tbCentre = new JToggleButton(new ImageAction(COMMAND_CENTERVIEW, ImageProvider.get("dialogs", "centreview"), tr("Center view"))); 152 tbCentre.setPreferredSize(buttonDim); 153 154 JButton btnZoomBestFit = new JButton(new ImageAction(COMMAND_ZOOM, ImageProvider.get("dialogs", "zoom-best-fit"), tr("Zoom best fit and 1:1"))); 155 btnZoomBestFit.setPreferredSize(buttonDim); 156 157 btnCollapse = new JButton(new ImageAction(COMMAND_COLLAPSE, ImageProvider.get("dialogs", "collapse"), tr("Move dialog to the side pane"))); 158 btnCollapse.setPreferredSize(new Dimension(20,20)); 159 btnCollapse.setAlignmentY(Component.TOP_ALIGNMENT); 160 161 JPanel buttons = new JPanel(); 162 buttons.add(btnPrevious); 163 buttons.add(btnNext); 164 buttons.add(Box.createRigidArea(new Dimension(7, 0))); 165 buttons.add(tbCentre); 166 buttons.add(btnZoomBestFit); 167 buttons.add(Box.createRigidArea(new Dimension(7, 0))); 168 buttons.add(btnDelete); 169 buttons.add(btnDeleteFromDisk); 170 buttons.add(Box.createRigidArea(new Dimension(7, 0))); 171 buttons.add(btnCopyPath); 172 173 JPanel bottomPane = new JPanel(); 174 bottomPane.setLayout(new GridBagLayout()); 175 GridBagConstraints gc = new GridBagConstraints(); 176 gc.gridx = 0; 177 gc.gridy = 0; 178 gc.anchor = GridBagConstraints.CENTER; 179 gc.weightx = 1; 180 bottomPane.add(buttons, gc); 181 182 gc.gridx = 1; 183 gc.gridy = 0; 184 gc.anchor = GridBagConstraints.PAGE_END; 185 gc.weightx = 0; 186 bottomPane.add(btnCollapse, gc); 187 188 content.add(bottomPane, BorderLayout.SOUTH); 189 190 createLayout(content, false, null); 191 } 192 193 @Override 194 public void destroy() { 195 MapView.removeLayerChangeListener(this); 196 super.destroy(); 197 } 198 199 class ImageAction extends AbstractAction { 200 private final String action; 201 public ImageAction(String action, ImageIcon icon, String toolTipText) { 202 this.action = action; 203 putValue(SHORT_DESCRIPTION, toolTipText); 204 putValue(SMALL_ICON, icon); 205 } 206 207 @Override 208 public void actionPerformed(ActionEvent e) { 209 if (COMMAND_NEXT.equals(action)) { 210 if (currentLayer != null) { 211 currentLayer.showNextPhoto(); 212 } 213 } else if (COMMAND_PREVIOUS.equals(action)) { 214 if (currentLayer != null) { 215 currentLayer.showPreviousPhoto(); 216 } 217 } else if (COMMAND_FIRST.equals(action) && currentLayer != null) { 218 currentLayer.showFirstPhoto(); 219 } else if (COMMAND_LAST.equals(action) && currentLayer != null) { 220 currentLayer.showLastPhoto(); 221 222 } else if (COMMAND_CENTERVIEW.equals(action)) { 223 centerView = ((JToggleButton) e.getSource()).isSelected(); 224 if (centerView && currentEntry != null && currentEntry.getPos() != null) { 225 Main.map.mapView.zoomTo(currentEntry.getPos()); 226 } 227 228 } else if (COMMAND_ZOOM.equals(action)) { 229 imgDisplay.zoomBestFitOrOne(); 230 231 } else if (COMMAND_REMOVE.equals(action)) { 232 if (currentLayer != null) { 233 currentLayer.removeCurrentPhoto(); 234 } 235 } else if (COMMAND_REMOVE_FROM_DISK.equals(action)) { 236 if (currentLayer != null) { 237 currentLayer.removeCurrentPhotoFromDisk(); 238 } 239 } else if (COMMAND_COPY_PATH.equals(action)) { 240 if (currentLayer != null) { 241 currentLayer.copyCurrentPhotoPath(); 242 } 243 } else if (COMMAND_COLLAPSE.equals(action)) { 244 collapseButtonClicked = true; 245 detachedDialog.getToolkit().getSystemEventQueue().postEvent(new WindowEvent(detachedDialog, WindowEvent.WINDOW_CLOSING)); 246 } 247 } 248 } 249 250 public static void showImage(GeoImageLayer layer, ImageEntry entry) { 251 getInstance().displayImage(layer, entry); 252 if (layer != null) { 253 layer.checkPreviousNextButtons(); 254 } else { 255 setPreviousEnabled(false); 256 setNextEnabled(false); 257 } 258 } 259 260 /** 261 * Enables (or disables) the "Previous" button. 262 * @param value {@code true} to enable the button, {@code false} otherwise 263 */ 264 public static void setPreviousEnabled(boolean value) { 265 getInstance().btnPrevious.setEnabled(value); 266 } 267 268 /** 269 * Enables (or disables) the "Next" button. 270 * @param value {@code true} to enable the button, {@code false} otherwise 271 */ 272 public static void setNextEnabled(boolean value) { 273 getInstance().btnNext.setEnabled(value); 274 } 275 276 private GeoImageLayer currentLayer = null; 277 private ImageEntry currentEntry = null; 278 279 public void displayImage(GeoImageLayer layer, ImageEntry entry) { 280 boolean imageChanged; 281 282 synchronized(this) { 283 // TODO: pop up image dialog but don't load image again 284 285 imageChanged = currentEntry != entry; 286 287 if (centerView && Main.isDisplayingMapView() && entry != null && entry.getPos() != null) { 288 Main.map.mapView.zoomTo(entry.getPos()); 289 } 290 291 currentLayer = layer; 292 currentEntry = entry; 293 } 294 295 if (entry != null) { 296 if (imageChanged) { 297 // Set only if the image is new to preserve zoom and position if the same image is redisplayed 298 // (e.g. to update the OSD). 299 imgDisplay.setImage(entry.getFile(), entry.getExifOrientation()); 300 } 301 setTitle(tr("Geotagged Images") + (entry.getFile() != null ? " - " + entry.getFile().getName() : "")); 302 StringBuilder osd = new StringBuilder(entry.getFile() != null ? entry.getFile().getName() : ""); 303 if (entry.getElevation() != null) { 304 osd.append(tr("\nAltitude: {0} m", entry.getElevation().longValue())); 305 } 306 if (entry.getSpeed() != null) { 307 osd.append(tr("\nSpeed: {0} km/h", Math.round(entry.getSpeed()))); 308 } 309 if (entry.getExifImgDir() != null) { 310 osd.append(tr("\nDirection {0}\u00b0", Math.round(entry.getExifImgDir()))); 311 } 312 DateFormat dtf = DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM); 313 if (entry.hasExifTime()) { 314 osd.append(tr("\nEXIF time: {0}", dtf.format(entry.getExifTime()))); 315 } 316 if (entry.hasGpsTime()) { 317 osd.append(tr("\nGPS time: {0}", dtf.format(entry.getGpsTime()))); 318 } 319 320 imgDisplay.setOsdText(osd.toString()); 321 } else { 322 // if this method is called to reinitialize dialog content with a blank image, 323 // do not actually show the dialog again with a blank image if currently hidden (fix #10672) 324 setTitle(tr("Geotagged Images")); 325 imgDisplay.setImage(null, null); 326 imgDisplay.setOsdText(""); 327 return; 328 } 329 if (! isDialogShowing()) { 330 setIsDocked(false); // always open a detached window when an image is clicked and dialog is closed 331 showDialog(); 332 } else { 333 if (isDocked && isCollapsed) { 334 expand(); 335 dialogsPanel.reconstruct(Action.COLLAPSED_TO_DEFAULT, this); 336 } 337 } 338 } 339 340 /** 341 * When an image is closed, really close it and do not pop 342 * up the side dialog. 343 */ 344 @Override 345 protected boolean dockWhenClosingDetachedDlg() { 346 if (collapseButtonClicked) { 347 collapseButtonClicked = false; 348 return true; 349 } 350 return false; 351 } 352 353 @Override 354 protected void stateChanged() { 355 super.stateChanged(); 356 if (btnCollapse != null) { 357 btnCollapse.setVisible(!isDocked); 358 } 359 } 360 361 /** 362 * Returns whether an image is currently displayed 363 * @return If image is currently displayed 364 */ 365 public boolean hasImage() { 366 return currentEntry != null; 367 } 368 369 /** 370 * Returns the currently displayed image. 371 * @return Currently displayed image or {@code null} 372 * @since 6392 373 */ 374 public static ImageEntry getCurrentImage() { 375 return getInstance().currentEntry; 376 } 377 378 /** 379 * Returns the layer associated with the image. 380 * @return Layer associated with the image 381 * @since 6392 382 */ 383 public static GeoImageLayer getCurrentLayer() { 384 return getInstance().currentLayer; 385 } 386 387 @Override 388 public void activeLayerChange(Layer oldLayer, Layer newLayer) { 389 if (currentLayer == null && newLayer instanceof GeoImageLayer) { 390 ((GeoImageLayer)newLayer).showFirstPhoto(); 391 } 392 } 393 394 @Override 395 public void layerAdded(Layer newLayer) { 396 if (currentLayer == null && newLayer instanceof GeoImageLayer) { 397 ((GeoImageLayer)newLayer).showFirstPhoto(); 398 } 399 } 400 401 @Override 402 public void layerRemoved(Layer oldLayer) { 403 // Clear current image and layer if current layer is deleted 404 if (currentLayer != null && currentLayer.equals(oldLayer)) { 405 showImage(null, null); 406 } 407 // Check buttons state in case of layer merging 408 if (currentLayer != null && oldLayer instanceof GeoImageLayer) { 409 currentLayer.checkPreviousNextButtons(); 410 } 411 } 412}