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

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

Adjusted toggle dialog behavior for geoimage:

  • When click on a thumbnail, show the detached dialog unless the side dialog is already open.
  • When closing the detached dialog do not open it in the side dialog again, just close.
  • When the toggle button is clicked, always open the side dialog.
  • Property svn:eol-style set to native
File size: 7.7 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.Dimension;
12import java.awt.FlowLayout;
13import java.awt.event.ActionEvent;
14import java.awt.event.ActionListener;
15import java.awt.event.KeyEvent;
16
17import javax.swing.JButton;
18import javax.swing.JPanel;
19import javax.swing.JToggleButton;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
23import org.openstreetmap.josm.gui.dialogs.DialogsPanel.Action;
24import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer.ImageEntry;
25import org.openstreetmap.josm.tools.ImageProvider;
26import org.openstreetmap.josm.tools.Shortcut;
27
28public class ImageViewerDialog extends ToggleDialog implements ActionListener {
29
30 private static final String COMMAND_ZOOM = "zoom";
31 private static final String COMMAND_CENTERVIEW = "centre";
32 private static final String COMMAND_NEXT = "next";
33 private static final String COMMAND_REMOVE = "remove";
34 private static final String COMMAND_PREVIOUS = "previous";
35
36 private ImageDisplay imgDisplay = new ImageDisplay();
37 private boolean centerView = false;
38
39 // Only one instance of that class
40 static private ImageViewerDialog INSTANCE = null;
41
42 public static ImageViewerDialog getInstance() {
43 if (INSTANCE == null) {
44 INSTANCE = new ImageViewerDialog();
45 }
46 return INSTANCE;
47 }
48
49 private JButton btnNext;
50 private JButton btnPrevious;
51
52 private ImageViewerDialog() {
53 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);
54
55 /* Don't show a detached dialog right from the start. */
56 if (isShowing && !isDocked) {
57 setIsShowing(false);
58 }
59
60 if (INSTANCE != null) {
61 throw new IllegalStateException("Image viewer dialog should not be instanciated twice !");
62 }
63
64 INSTANCE = this;
65
66 JPanel content = new JPanel();
67 content.setLayout(new BorderLayout());
68
69 content.add(imgDisplay, BorderLayout.CENTER);
70
71 JPanel buttons = new JPanel();
72 buttons.setLayout(new FlowLayout());
73
74 JButton button;
75
76 Dimension buttonDim = new Dimension(26,26);
77 button = new JButton();
78 button.setIcon(ImageProvider.get("dialogs", "previous"));
79 button.setActionCommand(COMMAND_PREVIOUS);
80 button.setToolTipText(tr("Previous"));
81 button.addActionListener(this);
82 button.setPreferredSize(buttonDim);
83 buttons.add(button);
84 btnPrevious = button; //FIX
85
86 button = new JButton();
87 button.setIcon(ImageProvider.get("dialogs", "delete"));
88 button.setActionCommand(COMMAND_REMOVE);
89 button.setToolTipText(tr("Remove photo from layer"));
90 button.addActionListener(this);
91 button.setPreferredSize(buttonDim);
92 buttons.add(button);
93
94 button = new JButton();
95 button.setIcon(ImageProvider.get("dialogs", "next"));
96 button.setActionCommand(COMMAND_NEXT);
97 button.setToolTipText(tr("Next"));
98 button.addActionListener(this);
99 button.setPreferredSize(buttonDim);
100 buttons.add(button);
101 btnNext = button;
102
103 JToggleButton tb = new JToggleButton();
104 tb.setIcon(ImageProvider.get("dialogs", "centreview"));
105 tb.setActionCommand(COMMAND_CENTERVIEW);
106 tb.setToolTipText(tr("Center view"));
107 tb.addActionListener(this);
108 tb.setPreferredSize(buttonDim);
109 buttons.add(tb);
110
111 button = new JButton();
112 button.setIcon(ImageProvider.get("dialogs", "zoom-best-fit"));
113 button.setActionCommand(COMMAND_ZOOM);
114 button.setToolTipText(tr("Zoom best fit and 1:1"));
115 button.addActionListener(this);
116 button.setPreferredSize(buttonDim);
117 buttons.add(button);
118
119 content.add(buttons, BorderLayout.SOUTH);
120
121 add(content, BorderLayout.CENTER);
122
123 }
124
125 public void actionPerformed(ActionEvent e) {
126 if (COMMAND_NEXT.equals(e.getActionCommand())) {
127 if (currentLayer != null) {
128 currentLayer.showNextPhoto();
129 }
130 } else if (COMMAND_PREVIOUS.equals(e.getActionCommand())) {
131 if (currentLayer != null) {
132 currentLayer.showPreviousPhoto();
133 }
134
135 } else if (COMMAND_CENTERVIEW.equals(e.getActionCommand())) {
136 centerView = ((JToggleButton) e.getSource()).isSelected();
137 if (centerView && currentEntry != null && currentEntry.pos != null) {
138 Main.map.mapView.zoomTo(currentEntry.pos);
139 }
140
141 } else if (COMMAND_ZOOM.equals(e.getActionCommand())) {
142 imgDisplay.zoomBestFitOrOne();
143
144 } else if (COMMAND_REMOVE.equals(e.getActionCommand())) {
145 if (currentLayer != null) {
146 currentLayer.removeCurrentPhoto();
147 }
148 }
149
150 }
151
152 public static void showImage(GeoImageLayer layer, ImageEntry entry) {
153 getInstance().displayImage(layer, entry);
154 layer.checkPreviousNextButtons();
155 }
156 public static void setPreviousEnabled(Boolean value) {
157 getInstance().btnPrevious.setEnabled(value);
158 }
159 public static void setNextEnabled(Boolean value) {
160 getInstance().btnNext.setEnabled(value);
161 }
162
163
164 private GeoImageLayer currentLayer = null;
165 private ImageEntry currentEntry = null;
166
167 public void displayImage(GeoImageLayer layer, ImageEntry entry) {
168 synchronized(this) {
169 if (currentLayer == layer && currentEntry == entry) {
170 repaint();
171 return;
172 }
173
174 if (centerView && Main.map != null && entry != null && entry.pos != null) {
175 Main.map.mapView.zoomTo(entry.pos);
176 }
177
178 currentLayer = layer;
179 currentEntry = entry;
180 }
181
182 if (entry != null) {
183 imgDisplay.setImage(entry.file);
184 StringBuffer osd = new StringBuffer(entry.file != null ? entry.file.getName() : "");
185 if (entry.elevation != null) {
186 osd.append(tr("\nAltitude: {0} m", entry.elevation.longValue()));
187 }
188 if (entry.speed != null) {
189 osd.append(tr("\n{0} km/h", Math.round(entry.speed)));
190 }
191 imgDisplay.setOsdText(osd.toString());
192 } else {
193 imgDisplay.setImage(null);
194 imgDisplay.setOsdText("");
195 }
196 if (! isDialogShowing()) {
197 setIsDocked(false); // always open a detached window when an image is clicked and dialog is closed
198 showDialog();
199 } else {
200 if (isDocked && isCollapsed) {
201 expand();
202 dialogsPanel.reconstruct(Action.COLLAPSED_TO_DEFAULT, this);
203 }
204 }
205
206 }
207
208 /**
209 * When pressing the Toggle button always show the docked dialog.
210 */
211 @Override
212 protected void toggleButtonHook() {
213 if (! isShowing) {
214 setIsDocked(true);
215 setIsCollapsed(false);
216 }
217 }
218
219 /**
220 * When an image is closed, really close it and do not pop
221 * up the side dialog.
222 */
223 @Override
224 protected boolean dockWhenClosingDetachedDlg() {
225 return false;
226 }
227
228 /**
229 * Returns whether an image is currently displayed
230 * @return If image is currently displayed
231 */
232 public boolean hasImage() {
233 return currentEntry != null;
234 }
235}
Note: See TracBrowser for help on using the repository browser.