001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.Frame;
008import java.awt.GraphicsDevice;
009import java.awt.GraphicsEnvironment;
010import java.awt.Rectangle;
011import java.awt.Window;
012import java.awt.event.ActionEvent;
013import java.awt.event.KeyEvent;
014import java.util.ArrayList;
015import java.util.List;
016
017import javax.swing.JComponent;
018import javax.swing.JFrame;
019import javax.swing.KeyStroke;
020
021import org.openstreetmap.josm.gui.MainApplication;
022import org.openstreetmap.josm.gui.util.GuiHelper;
023import org.openstreetmap.josm.spi.preferences.Config;
024import org.openstreetmap.josm.tools.PlatformManager;
025import org.openstreetmap.josm.tools.Shortcut;
026import org.openstreetmap.josm.tools.ImageProvider;
027
028/**
029 * This class toggles the full-screen mode.
030 * @since 2533
031 */
032public class FullscreenToggleAction extends ToggleAction {
033    private final transient GraphicsDevice gd;
034    private Rectangle prevBounds;
035
036    /**
037     * Constructs a new {@code FullscreenToggleAction}.
038     */
039    public FullscreenToggleAction() {
040        super(tr("Fullscreen view"),
041              new ImageProvider("fullscreen"),
042              tr("Toggle fullscreen view"),
043              Shortcut.registerShortcut("menu:view:fullscreen", tr("View: {0}", tr("Fullscreen view")), KeyEvent.VK_F11, Shortcut.DIRECT),
044              false /* register */, null, false
045        );
046        setHelpId(ht("/Action/FullscreenView"));
047        setToolbarId("fullscreen");
048        MainApplication.getToolbar().register(this);
049        gd = GraphicsEnvironment.isHeadless() ? null : GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
050        setSelected(Config.getPref().getBoolean("draw.fullscreen", false));
051        notifySelectedState();
052    }
053
054    @Override
055    public void actionPerformed(ActionEvent e) {
056        toggleSelectedState(e);
057        Config.getPref().putBoolean("draw.fullscreen", isSelected());
058        notifySelectedState();
059        safeSetMode();
060    }
061
062    /**
063     * To call if this action must be initially run at JOSM startup.
064     */
065    public void initial() {
066        if (isSelected()) {
067            safeSetMode();
068        }
069    }
070
071    protected void safeSetMode() {
072        try {
073            this.setMode();
074        } catch (Exception exception) {
075            // Something happened. Disable fullscreen.
076            Config.getPref().put("draw.fullscreen", null);
077            throw exception;
078        }
079    }
080
081    protected void setMode() {
082        JFrame frame = MainApplication.getMainFrame();
083
084        List<Window> visibleWindows = new ArrayList<>();
085        visibleWindows.add(frame);
086        for (Window w : Frame.getWindows()) {
087            if (w.isVisible() && w != frame) {
088                visibleWindows.add(w);
089            }
090        }
091
092        boolean selected = isSelected();
093
094        if (frame != null) {
095            frame.dispose();
096            frame.setUndecorated(selected);
097
098            if (selected) {
099                prevBounds = frame.getBounds();
100                frame.setBounds(new Rectangle(GuiHelper.getScreenSize()));
101            }
102        }
103
104        // we cannot use hw-exclusive fullscreen mode in MS-Win, as long
105        // as josm throws out modal dialogs.
106        //
107        // the good thing is: fullscreen works without exclusive mode,
108        // since windows (or java?) draws the undecorated window full-
109        // screen by default (it's a simulated mode, but should be ok)
110        String exclusive = Config.getPref().get("draw.fullscreen.exclusive-mode", "auto");
111        if (("true".equals(exclusive) || ("auto".equals(exclusive) && !PlatformManager.isPlatformWindows())) && gd != null) {
112            gd.setFullScreenWindow(selected ? frame : null);
113        }
114
115        if (!selected && prevBounds != null && frame != null) {
116            frame.setBounds(prevBounds);
117        }
118
119        for (Window wind : visibleWindows) {
120            if (wind != null) {
121                wind.setVisible(true);
122            }
123        }
124
125        // Free F10 key to allow it to be used by plugins, even after full screen (see #7502)
126        if (frame != null) {
127            frame.getJMenuBar().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");
128        }
129    }
130}