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

Last change on this file since 2617 was 2617, checked in by bastiK, 14 years ago

geoimage: improved thumbnails (closes #4101)

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
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
6package org.openstreetmap.josm.gui.layer.geoimage;
7
8import static org.openstreetmap.josm.tools.I18n.tr;
9
10import java.awt.BorderLayout;
11import java.awt.Component;
12import java.awt.Dimension;
13import java.awt.GridBagConstraints;
14import java.awt.GridBagLayout;
15import java.awt.event.ActionEvent;
16import java.awt.event.ActionListener;
17import java.awt.event.KeyEvent;
18import java.awt.event.WindowEvent;
19
20import javax.swing.Box;
21import javax.swing.AbstractAction;
22import javax.swing.ImageIcon;
23import javax.swing.JButton;
24import javax.swing.JComponent;
25import javax.swing.JPanel;
26import javax.swing.JToggleButton;
27
28import org.openstreetmap.josm.Main;
29import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
30import org.openstreetmap.josm.gui.dialogs.DialogsPanel.Action;
31import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer.ImageEntry;
32import org.openstreetmap.josm.tools.ImageProvider;
33import org.openstreetmap.josm.tools.Shortcut;
34
35public class ImageViewerDialog extends ToggleDialog {
36
37 private static final String COMMAND_ZOOM = "zoom";
38 private static final String COMMAND_CENTERVIEW = "centre";
39 private static final String COMMAND_NEXT = "next";
40 private static final String COMMAND_REMOVE = "remove";
41 private static final String COMMAND_PREVIOUS = "previous";
42 private static final String COMMAND_COLLAPSE = "collapse";
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
50 private boolean collapseButtonClicked = false;
51
52 public static ImageViewerDialog getInstance() {
53 if (INSTANCE == null) {
54 INSTANCE = new ImageViewerDialog();
55 }
56 return INSTANCE;
57 }
58
59 private JButton btnNext;
60 private JButton btnPrevious;
61 private JButton btnCollapse;
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
66 if (INSTANCE != null) {
67 throw new IllegalStateException("Image viewer dialog should not be instanciated twice !");
68 }
69
70 /* Don't show a detached dialog right from the start. */
71 if (isShowing && !isDocked) {
72 setIsShowing(false);
73 }
74
75 INSTANCE = this;
76
77 JPanel content = new JPanel();
78 content.setLayout(new BorderLayout());
79
80 content.add(imgDisplay, BorderLayout.CENTER);
81
82 Dimension buttonDim = new Dimension(26,26);
83
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);
94
95 JButton btnDelete = new JButton(new ImageAction(COMMAND_REMOVE, ImageProvider.get("dialogs", "delete"), tr("Remove photo from layer")));
96 btnDelete.setPreferredSize(buttonDim);
97
98 ImageAction nextAction = new ImageAction(COMMAND_NEXT, ImageProvider.get("dialogs", "next"), tr("Next"));
99 btnNext = new JButton(nextAction);
100 btnNext.setPreferredSize(buttonDim);
101 Shortcut scNext = Shortcut.registerShortcut(
102 "geoimage:next", tr("Geoimage: {0}", tr("Show next Image")), KeyEvent.VK_PAGE_DOWN, Shortcut.GROUP_DIRECT);
103 final String ANEXT = "Next Image";
104 Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scNext.getKeyStroke(), ANEXT);
105 Main.contentPane.getActionMap().put(ANEXT, nextAction);
106 btnNext.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scNext.getKeyStroke(), ANEXT);
107 btnNext.getActionMap().put(ANEXT, nextAction);
108
109 JToggleButton tbCentre = new JToggleButton(new ImageAction(COMMAND_CENTERVIEW, ImageProvider.get("dialogs", "centreview"), tr("Center view")));
110 tbCentre.setPreferredSize(buttonDim);
111
112 JButton btnZoomBestFit = new JButton(new ImageAction(COMMAND_ZOOM, ImageProvider.get("dialogs", "zoom-best-fit"), tr("Zoom best fit and 1:1")));
113 btnZoomBestFit.setPreferredSize(buttonDim);
114
115 btnCollapse = new JButton(new ImageAction(COMMAND_COLLAPSE, ImageProvider.get("dialogs", "collapse"), tr("Move dialog to the side pane")));
116 btnCollapse.setPreferredSize(new Dimension(20,20));
117 btnCollapse.setAlignmentY(Component.TOP_ALIGNMENT);
118
119 JPanel buttons = new JPanel();
120 buttons.add(btnPrevious);
121 buttons.add(btnNext);
122 buttons.add(Box.createRigidArea(new Dimension(14, 0)));
123 buttons.add(tbCentre);
124 buttons.add(btnZoomBestFit);
125 buttons.add(Box.createRigidArea(new Dimension(14, 0)));
126 buttons.add(btnDelete);
127
128 JPanel bottomPane = new JPanel();
129 bottomPane.setLayout(new GridBagLayout());
130 GridBagConstraints gc = new GridBagConstraints();
131 gc.gridx = 0;
132 gc.gridy = 0;
133 gc.anchor = GridBagConstraints.CENTER;
134 gc.weightx = 1;
135 bottomPane.add(buttons, gc);
136
137 gc.gridx = 1;
138 gc.gridy = 0;
139 gc.anchor = GridBagConstraints.PAGE_END;
140 gc.weightx = 0;
141 bottomPane.add(btnCollapse, gc);
142
143 content.add(bottomPane, BorderLayout.SOUTH);
144
145 add(content, BorderLayout.CENTER);
146 }
147
148 class ImageAction extends AbstractAction {
149 private final String action;
150 public ImageAction(String action, ImageIcon icon, String toolTipText) {
151 this.action = action;
152 putValue(SHORT_DESCRIPTION, toolTipText);
153 putValue(SMALL_ICON, icon);
154 }
155
156 public void actionPerformed(ActionEvent e) {
157 if (COMMAND_NEXT.equals(action)) {
158 if (currentLayer != null) {
159 currentLayer.showNextPhoto();
160 }
161 } else if (COMMAND_PREVIOUS.equals(action)) {
162 if (currentLayer != null) {
163 currentLayer.showPreviousPhoto();
164 }
165
166 } else if (COMMAND_CENTERVIEW.equals(action)) {
167 centerView = ((JToggleButton) e.getSource()).isSelected();
168 if (centerView && currentEntry != null && currentEntry.pos != null) {
169 Main.map.mapView.zoomTo(currentEntry.pos);
170 }
171
172 } else if (COMMAND_ZOOM.equals(action)) {
173 imgDisplay.zoomBestFitOrOne();
174
175 } else if (COMMAND_REMOVE.equals(action)) {
176 if (currentLayer != null) {
177 currentLayer.removeCurrentPhoto();
178 }
179 } else if (COMMAND_COLLAPSE.equals(action)) {
180 collapseButtonClicked = true;
181 detachedDialog.getToolkit().getSystemEventQueue().postEvent(new WindowEvent(detachedDialog, WindowEvent.WINDOW_CLOSING));
182 }
183 }
184 }
185
186 public static void showImage(GeoImageLayer layer, ImageEntry entry) {
187 getInstance().displayImage(layer, entry);
188 layer.checkPreviousNextButtons();
189 }
190 public static void setPreviousEnabled(Boolean value) {
191 getInstance().btnPrevious.setEnabled(value);
192 }
193 public static void setNextEnabled(Boolean value) {
194 getInstance().btnNext.setEnabled(value);
195 }
196
197
198 private GeoImageLayer currentLayer = null;
199 private ImageEntry currentEntry = null;
200
201 public void displayImage(GeoImageLayer layer, ImageEntry entry) {
202 synchronized(this) {
203// if (currentLayer == layer && currentEntry == entry) {
204// repaint();
205// return;
206// } TODO: pop up image dialog but don't load image again
207
208 if (centerView && Main.map != null && entry != null && entry.pos != null) {
209 Main.map.mapView.zoomTo(entry.pos);
210 }
211
212 currentLayer = layer;
213 currentEntry = entry;
214 }
215
216 if (entry != null) {
217 imgDisplay.setImage(entry.file);
218 titleBar.setTitle("Geotagged Images" + (entry.file != null ? " - " + entry.file.getName() : ""));
219 StringBuffer osd = new StringBuffer(entry.file != null ? entry.file.getName() : "");
220 if (entry.elevation != null) {
221 osd.append(tr("\nAltitude: {0} m", entry.elevation.longValue()));
222 }
223 if (entry.speed != null) {
224 osd.append(tr("\n{0} km/h", Math.round(entry.speed)));
225 }
226 imgDisplay.setOsdText(osd.toString());
227 } else {
228 imgDisplay.setImage(null);
229 imgDisplay.setOsdText("");
230 }
231 if (! isDialogShowing()) {
232 setIsDocked(false); // always open a detached window when an image is clicked and dialog is closed
233 showDialog();
234 } else {
235 if (isDocked && isCollapsed) {
236 expand();
237 dialogsPanel.reconstruct(Action.COLLAPSED_TO_DEFAULT, this);
238 }
239 }
240
241 }
242
243 /**
244 * When pressing the Toggle button always show the docked dialog.
245 */
246 @Override
247 protected void toggleButtonHook() {
248 if (! isShowing) {
249 setIsDocked(true);
250 setIsCollapsed(false);
251 }
252 }
253
254 /**
255 * When an image is closed, really close it and do not pop
256 * up the side dialog.
257 */
258 @Override
259 protected boolean dockWhenClosingDetachedDlg() {
260 if (collapseButtonClicked) {
261 collapseButtonClicked = false;
262 return true;
263 }
264 return false;
265 }
266
267 @Override
268 protected void stateChanged() {
269 super.stateChanged();
270 if (btnCollapse != null) {
271 btnCollapse.setVisible(!isDocked);
272 }
273 }
274
275 /**
276 * Returns whether an image is currently displayed
277 * @return If image is currently displayed
278 */
279 public boolean hasImage() {
280 return currentEntry != null;
281 }
282}
Note: See TracBrowser for help on using the repository browser.