source: josm/trunk/src/org/openstreetmap/josm/actions/FullscreenToggleAction.java@ 16505

Last change on this file since 16505 was 16505, checked in by GerdP, 4 years ago

see #19296: Actions should avoid to install listeners which are not needed
tbc

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Frame;
8import java.awt.GraphicsDevice;
9import java.awt.GraphicsEnvironment;
10import java.awt.Rectangle;
11import java.awt.Window;
12import java.awt.event.ActionEvent;
13import java.awt.event.KeyEvent;
14import java.util.ArrayList;
15import java.util.List;
16
17import javax.swing.JComponent;
18import javax.swing.JFrame;
19import javax.swing.KeyStroke;
20
21import org.openstreetmap.josm.gui.MainApplication;
22import org.openstreetmap.josm.gui.util.GuiHelper;
23import org.openstreetmap.josm.spi.preferences.Config;
24import org.openstreetmap.josm.tools.PlatformManager;
25import org.openstreetmap.josm.tools.Shortcut;
26
27/**
28 * This class toggles the full-screen mode.
29 * @since 2533
30 */
31public class FullscreenToggleAction extends ToggleAction {
32 private final transient GraphicsDevice gd;
33 private Rectangle prevBounds;
34
35 /**
36 * Constructs a new {@code FullscreenToggleAction}.
37 */
38 public FullscreenToggleAction() {
39 super(tr("Fullscreen view"),
40 null, /* no icon */
41 tr("Toggle fullscreen view"),
42 Shortcut.registerShortcut("menu:view:fullscreen", tr("Toggle fullscreen view"), KeyEvent.VK_F11, Shortcut.DIRECT),
43 false /* register */
44 );
45 setHelpId(ht("/Action/FullscreenView"));
46 setToolbarId("fullscreen");
47 MainApplication.getToolbar().register(this);
48 gd = GraphicsEnvironment.isHeadless() ? null : GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
49 setSelected(Config.getPref().getBoolean("draw.fullscreen", false));
50 notifySelectedState();
51 }
52
53 @Override
54 protected void installAdapters() {
55 // not needed
56 }
57
58 @Override
59 public void actionPerformed(ActionEvent e) {
60 toggleSelectedState(e);
61 Config.getPref().putBoolean("draw.fullscreen", isSelected());
62 notifySelectedState();
63 setMode();
64 }
65
66 /**
67 * To call if this action must be initially run at JOSM startup.
68 */
69 public void initial() {
70 if (isSelected()) {
71 setMode();
72 }
73 }
74
75 protected void setMode() {
76 JFrame frame = MainApplication.getMainFrame();
77
78 List<Window> visibleWindows = new ArrayList<>();
79 visibleWindows.add(frame);
80 for (Window w : Frame.getWindows()) {
81 if (w.isVisible() && w != frame) {
82 visibleWindows.add(w);
83 }
84 }
85
86 boolean selected = isSelected();
87
88 if (frame != null) {
89 frame.dispose();
90 frame.setUndecorated(selected);
91
92 if (selected) {
93 prevBounds = frame.getBounds();
94 frame.setBounds(new Rectangle(GuiHelper.getScreenSize()));
95 }
96 }
97
98 // we cannot use hw-exclusive fullscreen mode in MS-Win, as long
99 // as josm throws out modal dialogs.
100 //
101 // the good thing is: fullscreen works without exclusive mode,
102 // since windows (or java?) draws the undecorated window full-
103 // screen by default (it's a simulated mode, but should be ok)
104 String exclusive = Config.getPref().get("draw.fullscreen.exclusive-mode", "auto");
105 if (("true".equals(exclusive) || ("auto".equals(exclusive) && !PlatformManager.isPlatformWindows())) && gd != null) {
106 gd.setFullScreenWindow(selected ? frame : null);
107 }
108
109 if (!selected && prevBounds != null && frame != null) {
110 frame.setBounds(prevBounds);
111 }
112
113 for (Window wind : visibleWindows) {
114 if (wind != null) {
115 wind.setVisible(true);
116 }
117 }
118
119 // Free F10 key to allow it to be used by plugins, even after full screen (see #7502)
120 if (frame != null) {
121 frame.getJMenuBar().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");
122 }
123 }
124}
Note: See TracBrowser for help on using the repository browser.