[2566] | 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;
|
---|
[2602] | 11 | import java.awt.Component;
|
---|
[2566] | 12 | import java.awt.Dimension;
|
---|
[2602] | 13 | import java.awt.GridBagConstraints;
|
---|
| 14 | import java.awt.GridBagLayout;
|
---|
[2566] | 15 | import java.awt.event.ActionEvent;
|
---|
| 16 | import java.awt.event.ActionListener;
|
---|
| 17 | import java.awt.event.KeyEvent;
|
---|
[2602] | 18 | import java.awt.event.WindowEvent;
|
---|
[2566] | 19 |
|
---|
[2602] | 20 | import javax.swing.Box;
|
---|
[2601] | 21 | import javax.swing.AbstractAction;
|
---|
| 22 | import javax.swing.ImageIcon;
|
---|
[2566] | 23 | import javax.swing.JButton;
|
---|
[2601] | 24 | import javax.swing.JComponent;
|
---|
[2566] | 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;
|
---|
[2577] | 30 | import org.openstreetmap.josm.gui.dialogs.DialogsPanel.Action;
|
---|
[2566] | 31 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
| 32 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 33 |
|
---|
[2601] | 34 | public class ImageViewerDialog extends ToggleDialog {
|
---|
[2566] | 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";
|
---|
[2627] | 40 | private static final String COMMAND_REMOVE_FROM_DISK = "removefromdisk";
|
---|
[2566] | 41 | private static final String COMMAND_PREVIOUS = "previous";
|
---|
[2602] | 42 | private static final String COMMAND_COLLAPSE = "collapse";
|
---|
[2566] | 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 |
|
---|
[2602] | 50 | private boolean collapseButtonClicked = false;
|
---|
| 51 |
|
---|
[2566] | 52 | public static ImageViewerDialog getInstance() {
|
---|
| 53 | if (INSTANCE == null) {
|
---|
| 54 | INSTANCE = new ImageViewerDialog();
|
---|
| 55 | }
|
---|
| 56 | return INSTANCE;
|
---|
| 57 | }
|
---|
[2577] | 58 |
|
---|
[2566] | 59 | private JButton btnNext;
|
---|
| 60 | private JButton btnPrevious;
|
---|
[2602] | 61 | private JButton btnCollapse;
|
---|
[2566] | 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 |
|
---|
[2601] | 66 | if (INSTANCE != null) {
|
---|
| 67 | throw new IllegalStateException("Image viewer dialog should not be instanciated twice !");
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[2577] | 70 | /* Don't show a detached dialog right from the start. */
|
---|
| 71 | if (isShowing && !isDocked) {
|
---|
| 72 | setIsShowing(false);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[2566] | 75 | INSTANCE = this;
|
---|
[2577] | 76 |
|
---|
[2566] | 77 | JPanel content = new JPanel();
|
---|
| 78 | content.setLayout(new BorderLayout());
|
---|
| 79 |
|
---|
| 80 | content.add(imgDisplay, BorderLayout.CENTER);
|
---|
| 81 |
|
---|
[2602] | 82 | Dimension buttonDim = new Dimension(26,26);
|
---|
[2566] | 83 |
|
---|
[2601] | 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);
|
---|
[2566] | 94 |
|
---|
[2627] | 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);
|
---|
[2601] | 98 | btnDelete.setPreferredSize(buttonDim);
|
---|
[2627] | 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);
|
---|
[2602] | 105 |
|
---|
[2627] | 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 |
|
---|
[2601] | 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);
|
---|
[2566] | 127 |
|
---|
[2601] | 128 | JToggleButton tbCentre = new JToggleButton(new ImageAction(COMMAND_CENTERVIEW, ImageProvider.get("dialogs", "centreview"), tr("Center view")));
|
---|
| 129 | tbCentre.setPreferredSize(buttonDim);
|
---|
[2602] | 130 |
|
---|
[2601] | 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);
|
---|
[2602] | 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);
|
---|
[2601] | 143 | buttons.add(btnZoomBestFit);
|
---|
[2602] | 144 | buttons.add(Box.createRigidArea(new Dimension(14, 0)));
|
---|
| 145 | buttons.add(btnDelete);
|
---|
[2627] | 146 | buttons.add(btnDeleteFromDisk);
|
---|
[2566] | 147 |
|
---|
[2602] | 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);
|
---|
[2566] | 156 |
|
---|
[2602] | 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 |
|
---|
[2566] | 165 | add(content, BorderLayout.CENTER);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[2601] | 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 | }
|
---|
[2566] | 175 |
|
---|
[2601] | 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 | }
|
---|
[2566] | 185 |
|
---|
[2601] | 186 | } else if (COMMAND_CENTERVIEW.equals(action)) {
|
---|
| 187 | centerView = ((JToggleButton) e.getSource()).isSelected();
|
---|
[2662] | 188 | if (centerView && currentEntry != null && currentEntry.getPos() != null) {
|
---|
| 189 | Main.map.mapView.zoomTo(currentEntry.getPos());
|
---|
[2601] | 190 | }
|
---|
[2566] | 191 |
|
---|
[2601] | 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 | }
|
---|
[2627] | 199 | } else if (COMMAND_REMOVE_FROM_DISK.equals(action)) {
|
---|
| 200 | if (currentLayer != null) {
|
---|
| 201 | currentLayer.removeCurrentPhotoFromDisk();
|
---|
| 202 | }
|
---|
[2602] | 203 | } else if (COMMAND_COLLAPSE.equals(action)) {
|
---|
| 204 | collapseButtonClicked = true;
|
---|
| 205 | detachedDialog.getToolkit().getSystemEventQueue().postEvent(new WindowEvent(detachedDialog, WindowEvent.WINDOW_CLOSING));
|
---|
[2566] | 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 |
|
---|
| 222 | private GeoImageLayer currentLayer = null;
|
---|
| 223 | private ImageEntry currentEntry = null;
|
---|
| 224 |
|
---|
| 225 | public void displayImage(GeoImageLayer layer, ImageEntry entry) {
|
---|
| 226 | synchronized(this) {
|
---|
[2617] | 227 | // if (currentLayer == layer && currentEntry == entry) {
|
---|
| 228 | // repaint();
|
---|
| 229 | // return;
|
---|
| 230 | // } TODO: pop up image dialog but don't load image again
|
---|
[2566] | 231 |
|
---|
[2662] | 232 | if (centerView && Main.map != null && entry != null && entry.getPos() != null) {
|
---|
| 233 | Main.map.mapView.zoomTo(entry.getPos());
|
---|
[2566] | 234 | }
|
---|
| 235 |
|
---|
| 236 | currentLayer = layer;
|
---|
| 237 | currentEntry = entry;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | if (entry != null) {
|
---|
| 241 | imgDisplay.setImage(entry.file);
|
---|
[2602] | 242 | titleBar.setTitle("Geotagged Images" + (entry.file != null ? " - " + entry.file.getName() : ""));
|
---|
[2566] | 243 | StringBuffer osd = new StringBuffer(entry.file != null ? entry.file.getName() : "");
|
---|
[2662] | 244 | if (entry.getElevation() != null) {
|
---|
| 245 | osd.append(tr("\nAltitude: {0} m", entry.getElevation().longValue()));
|
---|
[2566] | 246 | }
|
---|
[2662] | 247 | if (entry.getSpeed() != null) {
|
---|
| 248 | osd.append(tr("\n{0} km/h", Math.round(entry.getSpeed())));
|
---|
[2566] | 249 | }
|
---|
| 250 | imgDisplay.setOsdText(osd.toString());
|
---|
| 251 | } else {
|
---|
| 252 | imgDisplay.setImage(null);
|
---|
| 253 | imgDisplay.setOsdText("");
|
---|
| 254 | }
|
---|
[2577] | 255 | if (! isDialogShowing()) {
|
---|
| 256 | setIsDocked(false); // always open a detached window when an image is clicked and dialog is closed
|
---|
| 257 | showDialog();
|
---|
| 258 | } else {
|
---|
| 259 | if (isDocked && isCollapsed) {
|
---|
| 260 | expand();
|
---|
| 261 | dialogsPanel.reconstruct(Action.COLLAPSED_TO_DEFAULT, this);
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[2566] | 265 | }
|
---|
[2577] | 266 |
|
---|
[2566] | 267 | /**
|
---|
[2577] | 268 | * When pressing the Toggle button always show the docked dialog.
|
---|
| 269 | */
|
---|
| 270 | @Override
|
---|
| 271 | protected void toggleButtonHook() {
|
---|
| 272 | if (! isShowing) {
|
---|
| 273 | setIsDocked(true);
|
---|
| 274 | setIsCollapsed(false);
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /**
|
---|
| 279 | * When an image is closed, really close it and do not pop
|
---|
| 280 | * up the side dialog.
|
---|
| 281 | */
|
---|
| 282 | @Override
|
---|
| 283 | protected boolean dockWhenClosingDetachedDlg() {
|
---|
[2602] | 284 | if (collapseButtonClicked) {
|
---|
| 285 | collapseButtonClicked = false;
|
---|
| 286 | return true;
|
---|
| 287 | }
|
---|
[2577] | 288 | return false;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
[2602] | 291 | @Override
|
---|
| 292 | protected void stateChanged() {
|
---|
| 293 | super.stateChanged();
|
---|
| 294 | if (btnCollapse != null) {
|
---|
| 295 | btnCollapse.setVisible(!isDocked);
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[2577] | 299 | /**
|
---|
[2566] | 300 | * Returns whether an image is currently displayed
|
---|
| 301 | * @return If image is currently displayed
|
---|
| 302 | */
|
---|
| 303 | public boolean hasImage() {
|
---|
| 304 | return currentEntry != null;
|
---|
| 305 | }
|
---|
| 306 | }
|
---|