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