Ticket #13815: SetExternalViewerAction.java

File SetExternalViewerAction.java, 2.9 KB (added by Adrian, 8 years ago)

Updated version of source file

Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.geoimage;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.event.ActionEvent;
8import java.io.File;
9import java.util.List;
10
11import javax.swing.AbstractAction;
12import javax.swing.JMenuItem;
13import javax.swing.JFileChooser;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.gui.layer.Layer;
17import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
18import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
19import org.openstreetmap.josm.gui.widgets.FileChooserManager;
20import org.openstreetmap.josm.tools.ImageProvider;
21
22/**
23 * Choose the app to be used as the external image viewer.
24 * @since
25 */
26public class SetExternalViewerAction extends AbstractAction implements LayerAction {
27
28 private final transient GeoImageLayer layer;
29
30 /**
31 * Constructs a new {@code SetExternalViewerAction} action.
32 * @param layer image layer
33 */
34 public SetExternalViewerAction(GeoImageLayer layer) {
35 super(tr("Set external viewer"), ImageProvider.get("dialogs/next"));
36 putValue(SHORT_DESCRIPTION, tr("Choose the application to be used as the external image viewer."));
37 this.layer = layer;
38 }
39
40 /**
41 * This is called after the menu entry was selected.
42 * @param arg0 action event
43 */
44 @Override
45 public void actionPerformed(ActionEvent arg0) {
46 /** Work around a difference in behaviour between the native and non-native file choosers in OS X. */
47 if (Main.isPlatformOsx() && Main.pref.getBoolean("use.native.file.dialog")) {
48 AbstractFileChooser fc = new FileChooserManager(true, "external.viewer.lastdirectory", "").
49 createFileChooser(false, "Choose the external image viewer", null, JFileChooser.DIRECTORIES_ONLY).openFileChooser();
50 if (fc != null) {
51 File sel = fc.getSelectedFile();
52 if (sel != null) {
53 Main.pref.put("external.viewer.path", sel.toString());
54 }
55 }
56 } else {
57 AbstractFileChooser fc = new FileChooserManager(true, "external.viewer.lastdirectory", "").
58 createFileChooser(false, "Choose the external image viewer", null, JFileChooser.FILES_ONLY).openFileChooser();
59 if (fc != null) {
60 File sel = fc.getSelectedFile();
61 if (sel != null) {
62 Main.pref.put("external.viewer.path", sel.toString());
63 }
64 }
65 }
66 }
67
68 /** Create actual menu entry. */
69 @Override
70 public Component createMenuComponent() {
71 JMenuItem item = new JMenuItem(this);
72 return item;
73 }
74
75 /** Check if the current layer is supported. */
76 @Override
77 public boolean supportLayers(List<Layer> layers) {
78 return layers.size() == 1 && layers.get(0) instanceof GeoImageLayer;
79 }
80}