123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- /*
- * Copyright (c) 2006-2017 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
- * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
- * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing;
-
- import com.dmdirc.addons.ui_swing.components.IconManager;
- import com.dmdirc.addons.ui_swing.components.SplitPane;
- import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
- import com.dmdirc.addons.ui_swing.components.menubar.MenuBar;
- import com.dmdirc.addons.ui_swing.components.statusbar.SwingStatusBar;
- import com.dmdirc.addons.ui_swing.dialogs.StandardQuestionDialog;
- import com.dmdirc.addons.ui_swing.events.ClientMinimisedEvent;
- import com.dmdirc.addons.ui_swing.events.ClientUnminimisedEvent;
- import com.dmdirc.addons.ui_swing.events.SwingActiveWindowChangeRequestEvent;
- import com.dmdirc.addons.ui_swing.events.SwingEventBus;
- import com.dmdirc.addons.ui_swing.events.SwingWindowAddedEvent;
- import com.dmdirc.addons.ui_swing.events.SwingWindowDeletedEvent;
- import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
- import com.dmdirc.addons.ui_swing.framemanager.FrameManager;
- import com.dmdirc.addons.ui_swing.framemanager.FramemanagerPosition;
- import com.dmdirc.addons.ui_swing.framemanager.ctrltab.CtrlTabWindowManager;
- import com.dmdirc.events.FrameTitleChangedEvent;
- import com.dmdirc.events.UnreadStatusChangedEvent;
- import com.dmdirc.events.eventbus.EventBus;
- import com.dmdirc.interfaces.LifecycleController;
- import com.dmdirc.interfaces.config.AggregateConfigProvider;
- import com.dmdirc.interfaces.config.ConfigChangeListener;
- import com.dmdirc.ui.CoreUIUtils;
- import com.dmdirc.util.collections.QueuedLinkedHashSet;
-
- import java.awt.BorderLayout;
- import java.awt.Dialog;
- import java.awt.Dimension;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import java.util.Optional;
-
- import javax.inject.Provider;
-
- import javax.swing.BorderFactory;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JSplitPane;
- import javax.swing.SwingUtilities;
- import javax.swing.WindowConstants;
-
- import net.miginfocom.layout.PlatformDefaults;
- import net.miginfocom.swing.MigLayout;
-
- import net.engio.mbassy.listener.Handler;
-
- import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
- import static java.util.function.Predicate.isEqual;
-
- /**
- * The main application frame.
- */
- public class MainFrame extends JFrame implements WindowListener, ConfigChangeListener {
-
- /** A version number for this class. */
- private static final long serialVersionUID = 9;
- /** Focus queue. */
- private final QueuedLinkedHashSet<Optional<TextFrame>> focusOrder;
- /** Apple instance. */
- private final Apple apple;
- /** Controller to use to end the program. */
- private final LifecycleController lifecycleController;
- /** The global config to read settings from. */
- private final AggregateConfigProvider globalConfig;
- /** The icon manager to use to get icons. */
- private final IconManager iconManager;
- /** The quit worker to use when quitting the app. */
- private final Provider<QuitWorker> quitWorker;
- /** Client Version. */
- private final String version;
- /** Frame manager used for ctrl tab frame switching. */
- private CtrlTabWindowManager frameManager;
- /** Provider of frame managers. */
- private final Provider<FrameManager> frameManagerProvider;
- /** The bus to despatch events on. */
- private final EventBus eventBus;
- /** Swing event bus to post events to. */
- private final SwingEventBus swingEventBus;
- /** The main application icon. */
- private ImageIcon imageIcon;
- /** The frame manager that's being used. */
- private FrameManager mainFrameManager;
- /** Active frame. */
- private Optional<TextFrame> activeFrame;
- /** Panel holding frame. */
- private JPanel framePanel;
- /** Main panel. */
- private JPanel frameManagerPanel;
- /** Frame manager position. */
- private FramemanagerPosition position;
- /** Show version? */
- private boolean showVersion;
- /** Exit code. */
- private int exitCode;
- /** Status bar. */
- private SwingStatusBar statusBar;
- /** Main split pane. */
- private SplitPane mainSplitPane;
- /** Are we quitting or closing? */
- private boolean quitting;
- /** Have we initialised our settings and listeners? */
- private boolean initDone;
-
- /**
- * Creates new form MainFrame.
- *
- * @param apple Apple instance
- * @param lifecycleController Controller to use to end the application.
- * @param globalConfig The config to read settings from.
- * @param quitWorker The quit worker to use when quitting the app.
- * @param iconManager The icon manager to use to get icons.
- * @param frameManagerProvider Provider to use to retrieve frame managers.
- * @param eventBus The event bus to post events to.
- * @param swingEventBus The event bus to post swing events to.
- */
- public MainFrame(
- final Apple apple,
- final LifecycleController lifecycleController,
- final AggregateConfigProvider globalConfig,
- final Provider<QuitWorker> quitWorker,
- final IconManager iconManager,
- final Provider<FrameManager> frameManagerProvider,
- final EventBus eventBus,
- final SwingEventBus swingEventBus) {
- checkOnEDT();
- this.apple = apple;
- this.lifecycleController = lifecycleController;
- this.globalConfig = globalConfig;
- this.quitWorker = quitWorker;
- this.iconManager = iconManager;
- this.frameManagerProvider = frameManagerProvider;
- this.eventBus = eventBus;
- this.swingEventBus = swingEventBus;
- activeFrame = Optional.empty();
- version = globalConfig.getOption("version", "version");
- focusOrder = new QueuedLinkedHashSet<>();
- }
-
- @Override
- public void setVisible(final boolean visible) {
- checkOnEDT();
- if (!initDone) {
- swingEventBus.subscribe(this);
- imageIcon = new ImageIcon(iconManager.getImage("icon"));
- setIconImage(imageIcon.getImage());
-
- CoreUIUtils.centreWindow(this);
-
- addWindowListener(this);
-
- showVersion = globalConfig.getOptionBool("ui", "showversion");
- globalConfig.addChangeListener("ui", "showversion", this);
- globalConfig.addChangeListener("ui", "framemanager", this);
- globalConfig.addChangeListener("ui", "framemanagerPosition", this);
- globalConfig.addChangeListener("icon", "icon", this);
-
- addWindowFocusListener(new EventTriggeringFocusListener(eventBus));
-
- setTitle(getTitlePrefix());
- initDone = true;
- }
- super.setVisible(visible);
- }
-
- /**
- * Returns the size of the frame manager.
- *
- * @return Frame manager size.
- */
- public int getFrameManagerSize() {
- if (position == FramemanagerPosition.LEFT || position == FramemanagerPosition.RIGHT) {
- return frameManagerPanel.getWidth();
- } else {
- return frameManagerPanel.getHeight();
- }
- }
-
- @Override
- public MenuBar getJMenuBar() {
- return (MenuBar) super.getJMenuBar();
- }
-
- @Override
- public void setTitle(final String title) {
- checkOnEDT();
- if (title == null || !activeFrame.isPresent()) {
- MainFrame.super.setTitle(getTitlePrefix());
- } else {
- MainFrame.super.setTitle(getTitlePrefix() + " - " + title);
- }
- }
-
- /**
- * Gets the string which should be prefixed to this frame's title.
- *
- * @return This frame's title prefix
- */
- private String getTitlePrefix() {
- return "DMDirc" + (showVersion ? ' ' + version : "");
- }
-
- @Override
- public void windowOpened(final WindowEvent windowEvent) {
- //ignore
- }
-
- @Override
- public void windowClosing(final WindowEvent windowEvent) {
- quit(exitCode);
- }
-
- @Override
- public void windowClosed(final WindowEvent windowEvent) {
- UIUtilities.invokeOffEDTNoLogging(() -> lifecycleController.quit(exitCode));
- }
-
- @Override
- public void windowIconified(final WindowEvent windowEvent) {
- eventBus.publishAsync(new ClientMinimisedEvent());
- }
-
- @Override
- public void windowDeiconified(final WindowEvent windowEvent) {
- eventBus.publishAsync(new ClientUnminimisedEvent());
- }
-
- @Override
- public void windowActivated(final WindowEvent windowEvent) {
- //ignore
- }
-
- @Override
- public void windowDeactivated(final WindowEvent windowEvent) {
- //ignore
- }
-
- /** Initialiases the frame managers. */
- private void initFrameManagers() {
- checkOnEDT();
- frameManagerPanel.removeAll();
- if (mainFrameManager != null) {
- swingEventBus.unsubscribe(mainFrameManager);
- }
- mainFrameManager = frameManagerProvider.get();
- mainFrameManager.setParent(frameManagerPanel);
- swingEventBus.subscribe(mainFrameManager);
- }
-
- /**
- * Initialises the components for this frame.
- */
- public void initComponents() {
- checkOnEDT();
- frameManagerPanel = new JPanel();
- activeFrame = Optional.empty();
- framePanel = new JPanel(new MigLayout("fill, ins 0"));
- initFrameManagers();
- mainSplitPane = initSplitPane();
-
- setPreferredSize(new Dimension(800, 600));
-
- getContentPane().setLayout(new BorderLayout());
- layoutComponents();
-
- setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
-
- pack();
- }
-
- /**
- * Sets the menu bar that this frame will use.
- *
- * <p>
- * Must be called prior to {@link #initComponents()}.
- *
- * @param menuBar The menu bar to use.
- */
- public void setMenuBar(final MenuBar menuBar) {
- checkOnEDT();
- apple.setMenuBar(menuBar);
- setJMenuBar(menuBar);
- }
-
- /**
- * Sets the window manager that this frame will use.
- *
- * <p>
- * Must be called prior to {@link #initComponents()}.
- *
- * @param windowManager The window manager to use.
- */
- public void setWindowManager(final CtrlTabWindowManager windowManager) {
- this.frameManager = windowManager;
- }
-
- /**
- * Sets the status bar that will be used.
- *
- * <p>
- * Must be called prior to {@link #initComponents()}.
- *
- * @param statusBar The status bar to be used.
- */
- public void setStatusBar(final SwingStatusBar statusBar) {
- this.statusBar = statusBar;
- }
-
- /**
- * Lays out the this component.
- */
- private void layoutComponents() {
- int gap = (int) PlatformDefaults.getPanelInsets(0).getValue();
- mainSplitPane.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
- statusBar.setBorder(BorderFactory.createEmptyBorder(0, gap, gap, gap));
- getContentPane().add(mainSplitPane, BorderLayout.CENTER);
- getContentPane().add(statusBar, BorderLayout.PAGE_END);
- }
-
- /**
- * Initialises the split pane.
- *
- * @return Returns the initialised split pane
- */
- private SplitPane initSplitPane() {
- final SplitPane splitPane = new SplitPane(globalConfig, SplitPane.Orientation.HORIZONTAL);
- position = FramemanagerPosition.getPosition(
- globalConfig.getOption("ui", "framemanagerPosition"));
-
- if (position == FramemanagerPosition.UNKNOWN) {
- position = FramemanagerPosition.LEFT;
- }
-
- if (!mainFrameManager.canPositionVertically() && (position
- == FramemanagerPosition.LEFT || position
- == FramemanagerPosition.RIGHT)) {
- position = FramemanagerPosition.BOTTOM;
- }
- if (!mainFrameManager.canPositionHorizontally() && (position
- == FramemanagerPosition.TOP || position
- == FramemanagerPosition.BOTTOM)) {
- position = FramemanagerPosition.LEFT;
- }
-
- switch (position) {
- case TOP:
- splitPane.setTopComponent(frameManagerPanel);
- splitPane.setBottomComponent(framePanel);
- splitPane.setResizeWeight(0.0);
- splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
- frameManagerPanel.setPreferredSize(new Dimension(
- Integer.MAX_VALUE, globalConfig.getOptionInt("ui", "frameManagerSize")));
- break;
- case LEFT:
- splitPane.setLeftComponent(frameManagerPanel);
- splitPane.setRightComponent(framePanel);
- splitPane.setResizeWeight(0.0);
- splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
- frameManagerPanel.setPreferredSize(new Dimension(
- globalConfig.getOptionInt("ui", "frameManagerSize"), Integer.MAX_VALUE));
- break;
- case BOTTOM:
- splitPane.setTopComponent(framePanel);
- splitPane.setBottomComponent(frameManagerPanel);
- splitPane.setResizeWeight(1.0);
- splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
- frameManagerPanel.setPreferredSize(new Dimension(
- Integer.MAX_VALUE, globalConfig.getOptionInt("ui", "frameManagerSize")));
- break;
- case RIGHT:
- splitPane.setLeftComponent(framePanel);
- splitPane.setRightComponent(frameManagerPanel);
- splitPane.setResizeWeight(1.0);
- splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
- frameManagerPanel.setPreferredSize(new Dimension(
- globalConfig.getOptionInt("ui", "frameManagerSize"), Integer.MAX_VALUE));
- break;
- default:
- break;
- }
-
- return splitPane;
- }
-
- /**
- * Exits with an "OK" status code.
- */
- public void quit() {
- quit(0);
- }
-
- /**
- * Exit code call to quit.
- *
- * @param exitCode Exit code
- */
- public void quit(final int exitCode) {
- if (exitCode == 0 && globalConfig.getOptionBool("ui", "confirmQuit")) {
- new StandardQuestionDialog(this, Dialog.ModalityType.APPLICATION_MODAL,
- "Quit confirm", "You are about to quit DMDirc, are you sure?",
- () -> { doQuit(0); return true;}).display();
- return;
- }
- doQuit(exitCode);
- }
-
- /**
- * Exit code call to quit.
- *
- * @param exitCode Exit code
- */
- public void doQuit(final int exitCode) {
- this.exitCode = exitCode;
- quitting = true;
-
- quitWorker.get().execute();
- }
-
- @Override
- public void configChanged(final String domain, final String key) {
- if ("ui".equals(domain)) {
- switch (key) {
- case "framemanager":
- case "framemanagerPosition":
- UIUtilities.invokeAndWait(() -> {
- setVisible(false);
- getContentPane().remove(mainSplitPane);
- initFrameManagers();
- getContentPane().removeAll();
- layoutComponents();
- setVisible(true);
- });
- break;
- default:
- showVersion = globalConfig.getOptionBool("ui", "showversion");
- break;
- }
- } else {
- imageIcon = new ImageIcon(iconManager.getImage("icon"));
- UIUtilities.invokeLater(() -> setIconImage(imageIcon.getImage()));
- }
- }
-
- @Handler(invocation = EdtHandlerInvocation.class)
- public void setActiveFrame(final SwingActiveWindowChangeRequestEvent event) {
- checkOnEDT();
- focusOrder.offerAndMove(activeFrame);
- framePanel.setVisible(false);
- framePanel.removeAll();
-
- activeFrame = event.getWindow();
-
- if (activeFrame.isPresent()) {
- framePanel.add(activeFrame.get().getDisplayFrame(), "grow");
- setTitle(activeFrame.get().getContainer().getTitle());
- activeFrame.get().getContainer().getUnreadStatusManager().clearStatus();
- } else {
- framePanel.add(new JPanel(), "grow");
- setTitle(null);
- }
-
- framePanel.setVisible(true);
-
- if (activeFrame.isPresent()) {
- final TextFrame textFrame = activeFrame.get();
- textFrame.requestFocus();
- textFrame.requestFocusInWindow();
- textFrame.activateFrame();
- }
-
- swingEventBus.publish(new SwingWindowSelectedEvent(activeFrame));
- }
-
- @Handler(invocation = EdtHandlerInvocation.class)
- public void doWindowAdded(final SwingWindowAddedEvent event) {
- checkOnEDT();
- if (!activeFrame.isPresent()) {
- setActiveFrame(new SwingActiveWindowChangeRequestEvent(
- Optional.of(event.getChildWindow())));
- }
- }
-
- @Handler(invocation = EdtHandlerInvocation.class)
- public void doWindowDeleted(final SwingWindowDeletedEvent event) {
- checkOnEDT();
- final Optional<TextFrame> window = Optional.of(event.getChildWindow());
- focusOrder.remove(window);
- if (activeFrame.equals(window)) {
- activeFrame = Optional.empty();
- framePanel.setVisible(false);
- framePanel.removeAll();
- framePanel.setVisible(true);
- if (focusOrder.peek() == null) {
- SwingUtilities.invokeLater(frameManager::scrollUp);
- } else {
- setActiveFrame(new SwingActiveWindowChangeRequestEvent(focusOrder.peek()));
- }
- }
- }
-
- @Handler(invocation = EdtHandlerInvocation.class)
- public void titleChanged(final FrameTitleChangedEvent event) {
- checkOnEDT();
- activeFrame.map(TextFrame::getContainer)
- .filter(isEqual(event.getContainer()))
- .ifPresent(c -> setTitle(event.getTitle()));
- }
-
- @Handler(invocation = EdtHandlerInvocation.class)
- public void unreadStatusChanged(final UnreadStatusChangedEvent event) {
- checkOnEDT();
- activeFrame.map(TextFrame::getContainer)
- .filter(isEqual(event.getSource()))
- .ifPresent(c -> event.getManager().clearStatus());
- }
-
- @Override
- public void dispose() {
- if (!quitting) {
- removeWindowListener(this);
- }
-
- globalConfig.removeListener(this);
- super.dispose();
- }
-
- }
|