You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainFrame.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing;
  18. import com.dmdirc.addons.ui_swing.components.IconManager;
  19. import com.dmdirc.addons.ui_swing.components.SplitPane;
  20. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  21. import com.dmdirc.addons.ui_swing.components.menubar.MenuBar;
  22. import com.dmdirc.addons.ui_swing.components.statusbar.SwingStatusBar;
  23. import com.dmdirc.addons.ui_swing.dialogs.StandardQuestionDialog;
  24. import com.dmdirc.addons.ui_swing.events.ClientMinimisedEvent;
  25. import com.dmdirc.addons.ui_swing.events.ClientUnminimisedEvent;
  26. import com.dmdirc.addons.ui_swing.events.SwingActiveWindowChangeRequestEvent;
  27. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  28. import com.dmdirc.addons.ui_swing.events.SwingWindowAddedEvent;
  29. import com.dmdirc.addons.ui_swing.events.SwingWindowDeletedEvent;
  30. import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
  31. import com.dmdirc.addons.ui_swing.framemanager.FrameManager;
  32. import com.dmdirc.addons.ui_swing.framemanager.FramemanagerPosition;
  33. import com.dmdirc.addons.ui_swing.framemanager.ctrltab.CtrlTabWindowManager;
  34. import com.dmdirc.events.FrameTitleChangedEvent;
  35. import com.dmdirc.events.UnreadStatusChangedEvent;
  36. import com.dmdirc.events.eventbus.EventBus;
  37. import com.dmdirc.interfaces.LifecycleController;
  38. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  39. import com.dmdirc.interfaces.config.ConfigChangeListener;
  40. import com.dmdirc.ui.CoreUIUtils;
  41. import com.dmdirc.util.collections.QueuedLinkedHashSet;
  42. import java.awt.BorderLayout;
  43. import java.awt.Dialog;
  44. import java.awt.Dimension;
  45. import java.awt.event.WindowEvent;
  46. import java.awt.event.WindowListener;
  47. import java.util.Optional;
  48. import javax.inject.Provider;
  49. import javax.swing.BorderFactory;
  50. import javax.swing.ImageIcon;
  51. import javax.swing.JFrame;
  52. import javax.swing.JPanel;
  53. import javax.swing.JSplitPane;
  54. import javax.swing.SwingUtilities;
  55. import javax.swing.WindowConstants;
  56. import net.miginfocom.layout.PlatformDefaults;
  57. import net.miginfocom.swing.MigLayout;
  58. import net.engio.mbassy.listener.Handler;
  59. import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
  60. import static java.util.function.Predicate.isEqual;
  61. /**
  62. * The main application frame.
  63. */
  64. public class MainFrame extends JFrame implements WindowListener, ConfigChangeListener {
  65. /** A version number for this class. */
  66. private static final long serialVersionUID = 9;
  67. /** Focus queue. */
  68. private final QueuedLinkedHashSet<Optional<TextFrame>> focusOrder;
  69. /** Apple instance. */
  70. private final Apple apple;
  71. /** Controller to use to end the program. */
  72. private final LifecycleController lifecycleController;
  73. /** The global config to read settings from. */
  74. private final AggregateConfigProvider globalConfig;
  75. /** The icon manager to use to get icons. */
  76. private final IconManager iconManager;
  77. /** The quit worker to use when quitting the app. */
  78. private final Provider<QuitWorker> quitWorker;
  79. /** Client Version. */
  80. private final String version;
  81. /** Frame manager used for ctrl tab frame switching. */
  82. private CtrlTabWindowManager frameManager;
  83. /** Provider of frame managers. */
  84. private final Provider<FrameManager> frameManagerProvider;
  85. /** The bus to despatch events on. */
  86. private final EventBus eventBus;
  87. /** Swing event bus to post events to. */
  88. private final SwingEventBus swingEventBus;
  89. /** The main application icon. */
  90. private ImageIcon imageIcon;
  91. /** The frame manager that's being used. */
  92. private FrameManager mainFrameManager;
  93. /** Active frame. */
  94. private Optional<TextFrame> activeFrame;
  95. /** Panel holding frame. */
  96. private JPanel framePanel;
  97. /** Main panel. */
  98. private JPanel frameManagerPanel;
  99. /** Frame manager position. */
  100. private FramemanagerPosition position;
  101. /** Show version? */
  102. private boolean showVersion;
  103. /** Exit code. */
  104. private int exitCode;
  105. /** Status bar. */
  106. private SwingStatusBar statusBar;
  107. /** Main split pane. */
  108. private SplitPane mainSplitPane;
  109. /** Are we quitting or closing? */
  110. private boolean quitting;
  111. /** Have we initialised our settings and listeners? */
  112. private boolean initDone;
  113. /**
  114. * Creates new form MainFrame.
  115. *
  116. * @param apple Apple instance
  117. * @param lifecycleController Controller to use to end the application.
  118. * @param globalConfig The config to read settings from.
  119. * @param quitWorker The quit worker to use when quitting the app.
  120. * @param iconManager The icon manager to use to get icons.
  121. * @param frameManagerProvider Provider to use to retrieve frame managers.
  122. * @param eventBus The event bus to post events to.
  123. * @param swingEventBus The event bus to post swing events to.
  124. */
  125. public MainFrame(
  126. final Apple apple,
  127. final LifecycleController lifecycleController,
  128. final AggregateConfigProvider globalConfig,
  129. final Provider<QuitWorker> quitWorker,
  130. final IconManager iconManager,
  131. final Provider<FrameManager> frameManagerProvider,
  132. final EventBus eventBus,
  133. final SwingEventBus swingEventBus) {
  134. checkOnEDT();
  135. this.apple = apple;
  136. this.lifecycleController = lifecycleController;
  137. this.globalConfig = globalConfig;
  138. this.quitWorker = quitWorker;
  139. this.iconManager = iconManager;
  140. this.frameManagerProvider = frameManagerProvider;
  141. this.eventBus = eventBus;
  142. this.swingEventBus = swingEventBus;
  143. activeFrame = Optional.empty();
  144. version = globalConfig.getOption("version", "version");
  145. focusOrder = new QueuedLinkedHashSet<>();
  146. }
  147. @Override
  148. public void setVisible(final boolean visible) {
  149. checkOnEDT();
  150. if (!initDone) {
  151. swingEventBus.subscribe(this);
  152. imageIcon = new ImageIcon(iconManager.getImage("icon"));
  153. setIconImage(imageIcon.getImage());
  154. CoreUIUtils.centreWindow(this);
  155. addWindowListener(this);
  156. showVersion = globalConfig.getOptionBool("ui", "showversion");
  157. globalConfig.addChangeListener("ui", "showversion", this);
  158. globalConfig.addChangeListener("ui", "framemanager", this);
  159. globalConfig.addChangeListener("ui", "framemanagerPosition", this);
  160. globalConfig.addChangeListener("icon", "icon", this);
  161. addWindowFocusListener(new EventTriggeringFocusListener(eventBus));
  162. setTitle(getTitlePrefix());
  163. initDone = true;
  164. }
  165. super.setVisible(visible);
  166. }
  167. /**
  168. * Returns the size of the frame manager.
  169. *
  170. * @return Frame manager size.
  171. */
  172. public int getFrameManagerSize() {
  173. if (position == FramemanagerPosition.LEFT || position == FramemanagerPosition.RIGHT) {
  174. return frameManagerPanel.getWidth();
  175. } else {
  176. return frameManagerPanel.getHeight();
  177. }
  178. }
  179. @Override
  180. public MenuBar getJMenuBar() {
  181. return (MenuBar) super.getJMenuBar();
  182. }
  183. @Override
  184. public void setTitle(final String title) {
  185. checkOnEDT();
  186. if (title == null || !activeFrame.isPresent()) {
  187. MainFrame.super.setTitle(getTitlePrefix());
  188. } else {
  189. MainFrame.super.setTitle(getTitlePrefix() + " - " + title);
  190. }
  191. }
  192. /**
  193. * Gets the string which should be prefixed to this frame's title.
  194. *
  195. * @return This frame's title prefix
  196. */
  197. private String getTitlePrefix() {
  198. return "DMDirc" + (showVersion ? ' ' + version : "");
  199. }
  200. @Override
  201. public void windowOpened(final WindowEvent windowEvent) {
  202. //ignore
  203. }
  204. @Override
  205. public void windowClosing(final WindowEvent windowEvent) {
  206. quit(exitCode);
  207. }
  208. @Override
  209. public void windowClosed(final WindowEvent windowEvent) {
  210. UIUtilities.invokeOffEDTNoLogging(() -> lifecycleController.quit(exitCode));
  211. }
  212. @Override
  213. public void windowIconified(final WindowEvent windowEvent) {
  214. eventBus.publishAsync(new ClientMinimisedEvent());
  215. }
  216. @Override
  217. public void windowDeiconified(final WindowEvent windowEvent) {
  218. eventBus.publishAsync(new ClientUnminimisedEvent());
  219. }
  220. @Override
  221. public void windowActivated(final WindowEvent windowEvent) {
  222. //ignore
  223. }
  224. @Override
  225. public void windowDeactivated(final WindowEvent windowEvent) {
  226. //ignore
  227. }
  228. /** Initialiases the frame managers. */
  229. private void initFrameManagers() {
  230. checkOnEDT();
  231. frameManagerPanel.removeAll();
  232. if (mainFrameManager != null) {
  233. swingEventBus.unsubscribe(mainFrameManager);
  234. }
  235. mainFrameManager = frameManagerProvider.get();
  236. mainFrameManager.setParent(frameManagerPanel);
  237. swingEventBus.subscribe(mainFrameManager);
  238. }
  239. /**
  240. * Initialises the components for this frame.
  241. */
  242. public void initComponents() {
  243. checkOnEDT();
  244. frameManagerPanel = new JPanel();
  245. activeFrame = Optional.empty();
  246. framePanel = new JPanel(new MigLayout("fill, ins 0"));
  247. initFrameManagers();
  248. mainSplitPane = initSplitPane();
  249. setPreferredSize(new Dimension(800, 600));
  250. getContentPane().setLayout(new BorderLayout());
  251. layoutComponents();
  252. setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  253. pack();
  254. }
  255. /**
  256. * Sets the menu bar that this frame will use.
  257. *
  258. * <p>
  259. * Must be called prior to {@link #initComponents()}.
  260. *
  261. * @param menuBar The menu bar to use.
  262. */
  263. public void setMenuBar(final MenuBar menuBar) {
  264. checkOnEDT();
  265. apple.setMenuBar(menuBar);
  266. setJMenuBar(menuBar);
  267. }
  268. /**
  269. * Sets the window manager that this frame will use.
  270. *
  271. * <p>
  272. * Must be called prior to {@link #initComponents()}.
  273. *
  274. * @param windowManager The window manager to use.
  275. */
  276. public void setWindowManager(final CtrlTabWindowManager windowManager) {
  277. this.frameManager = windowManager;
  278. }
  279. /**
  280. * Sets the status bar that will be used.
  281. *
  282. * <p>
  283. * Must be called prior to {@link #initComponents()}.
  284. *
  285. * @param statusBar The status bar to be used.
  286. */
  287. public void setStatusBar(final SwingStatusBar statusBar) {
  288. this.statusBar = statusBar;
  289. }
  290. /**
  291. * Lays out the this component.
  292. */
  293. private void layoutComponents() {
  294. int gap = (int) PlatformDefaults.getPanelInsets(0).getValue();
  295. mainSplitPane.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
  296. statusBar.setBorder(BorderFactory.createEmptyBorder(0, gap, gap, gap));
  297. getContentPane().add(mainSplitPane, BorderLayout.CENTER);
  298. getContentPane().add(statusBar, BorderLayout.PAGE_END);
  299. }
  300. /**
  301. * Initialises the split pane.
  302. *
  303. * @return Returns the initialised split pane
  304. */
  305. private SplitPane initSplitPane() {
  306. final SplitPane splitPane = new SplitPane(globalConfig, SplitPane.Orientation.HORIZONTAL);
  307. position = FramemanagerPosition.getPosition(
  308. globalConfig.getOption("ui", "framemanagerPosition"));
  309. if (position == FramemanagerPosition.UNKNOWN) {
  310. position = FramemanagerPosition.LEFT;
  311. }
  312. if (!mainFrameManager.canPositionVertically() && (position
  313. == FramemanagerPosition.LEFT || position
  314. == FramemanagerPosition.RIGHT)) {
  315. position = FramemanagerPosition.BOTTOM;
  316. }
  317. if (!mainFrameManager.canPositionHorizontally() && (position
  318. == FramemanagerPosition.TOP || position
  319. == FramemanagerPosition.BOTTOM)) {
  320. position = FramemanagerPosition.LEFT;
  321. }
  322. switch (position) {
  323. case TOP:
  324. splitPane.setTopComponent(frameManagerPanel);
  325. splitPane.setBottomComponent(framePanel);
  326. splitPane.setResizeWeight(0.0);
  327. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  328. frameManagerPanel.setPreferredSize(new Dimension(
  329. Integer.MAX_VALUE, globalConfig.getOptionInt("ui", "frameManagerSize")));
  330. break;
  331. case LEFT:
  332. splitPane.setLeftComponent(frameManagerPanel);
  333. splitPane.setRightComponent(framePanel);
  334. splitPane.setResizeWeight(0.0);
  335. splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
  336. frameManagerPanel.setPreferredSize(new Dimension(
  337. globalConfig.getOptionInt("ui", "frameManagerSize"), Integer.MAX_VALUE));
  338. break;
  339. case BOTTOM:
  340. splitPane.setTopComponent(framePanel);
  341. splitPane.setBottomComponent(frameManagerPanel);
  342. splitPane.setResizeWeight(1.0);
  343. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  344. frameManagerPanel.setPreferredSize(new Dimension(
  345. Integer.MAX_VALUE, globalConfig.getOptionInt("ui", "frameManagerSize")));
  346. break;
  347. case RIGHT:
  348. splitPane.setLeftComponent(framePanel);
  349. splitPane.setRightComponent(frameManagerPanel);
  350. splitPane.setResizeWeight(1.0);
  351. splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
  352. frameManagerPanel.setPreferredSize(new Dimension(
  353. globalConfig.getOptionInt("ui", "frameManagerSize"), Integer.MAX_VALUE));
  354. break;
  355. default:
  356. break;
  357. }
  358. return splitPane;
  359. }
  360. /**
  361. * Exits with an "OK" status code.
  362. */
  363. public void quit() {
  364. quit(0);
  365. }
  366. /**
  367. * Exit code call to quit.
  368. *
  369. * @param exitCode Exit code
  370. */
  371. public void quit(final int exitCode) {
  372. if (exitCode == 0 && globalConfig.getOptionBool("ui", "confirmQuit")) {
  373. new StandardQuestionDialog(this, Dialog.ModalityType.APPLICATION_MODAL,
  374. "Quit confirm", "You are about to quit DMDirc, are you sure?",
  375. () -> { doQuit(0); return true;}).display();
  376. return;
  377. }
  378. doQuit(exitCode);
  379. }
  380. /**
  381. * Exit code call to quit.
  382. *
  383. * @param exitCode Exit code
  384. */
  385. public void doQuit(final int exitCode) {
  386. this.exitCode = exitCode;
  387. quitting = true;
  388. quitWorker.get().execute();
  389. }
  390. @Override
  391. public void configChanged(final String domain, final String key) {
  392. if ("ui".equals(domain)) {
  393. switch (key) {
  394. case "framemanager":
  395. case "framemanagerPosition":
  396. UIUtilities.invokeAndWait(() -> {
  397. setVisible(false);
  398. getContentPane().remove(mainSplitPane);
  399. initFrameManagers();
  400. getContentPane().removeAll();
  401. layoutComponents();
  402. setVisible(true);
  403. });
  404. break;
  405. default:
  406. showVersion = globalConfig.getOptionBool("ui", "showversion");
  407. break;
  408. }
  409. } else {
  410. imageIcon = new ImageIcon(iconManager.getImage("icon"));
  411. UIUtilities.invokeLater(() -> setIconImage(imageIcon.getImage()));
  412. }
  413. }
  414. @Handler(invocation = EdtHandlerInvocation.class)
  415. public void setActiveFrame(final SwingActiveWindowChangeRequestEvent event) {
  416. checkOnEDT();
  417. focusOrder.offerAndMove(activeFrame);
  418. framePanel.setVisible(false);
  419. framePanel.removeAll();
  420. activeFrame = event.getWindow();
  421. if (activeFrame.isPresent()) {
  422. framePanel.add(activeFrame.get().getDisplayFrame(), "grow");
  423. setTitle(activeFrame.get().getContainer().getTitle());
  424. activeFrame.get().getContainer().getUnreadStatusManager().clearStatus();
  425. } else {
  426. framePanel.add(new JPanel(), "grow");
  427. setTitle(null);
  428. }
  429. framePanel.setVisible(true);
  430. if (activeFrame.isPresent()) {
  431. final TextFrame textFrame = activeFrame.get();
  432. textFrame.requestFocus();
  433. textFrame.requestFocusInWindow();
  434. textFrame.activateFrame();
  435. }
  436. swingEventBus.publish(new SwingWindowSelectedEvent(activeFrame));
  437. }
  438. @Handler(invocation = EdtHandlerInvocation.class)
  439. public void doWindowAdded(final SwingWindowAddedEvent event) {
  440. checkOnEDT();
  441. if (!activeFrame.isPresent()) {
  442. setActiveFrame(new SwingActiveWindowChangeRequestEvent(
  443. Optional.of(event.getChildWindow())));
  444. }
  445. }
  446. @Handler(invocation = EdtHandlerInvocation.class)
  447. public void doWindowDeleted(final SwingWindowDeletedEvent event) {
  448. checkOnEDT();
  449. final Optional<TextFrame> window = Optional.of(event.getChildWindow());
  450. focusOrder.remove(window);
  451. if (activeFrame.equals(window)) {
  452. activeFrame = Optional.empty();
  453. framePanel.setVisible(false);
  454. framePanel.removeAll();
  455. framePanel.setVisible(true);
  456. if (focusOrder.peek() == null) {
  457. SwingUtilities.invokeLater(frameManager::scrollUp);
  458. } else {
  459. setActiveFrame(new SwingActiveWindowChangeRequestEvent(focusOrder.peek()));
  460. }
  461. }
  462. }
  463. @Handler(invocation = EdtHandlerInvocation.class)
  464. public void titleChanged(final FrameTitleChangedEvent event) {
  465. checkOnEDT();
  466. activeFrame.map(TextFrame::getContainer)
  467. .filter(isEqual(event.getContainer()))
  468. .ifPresent(c -> setTitle(event.getTitle()));
  469. }
  470. @Handler(invocation = EdtHandlerInvocation.class)
  471. public void unreadStatusChanged(final UnreadStatusChangedEvent event) {
  472. checkOnEDT();
  473. activeFrame.map(TextFrame::getContainer)
  474. .filter(isEqual(event.getSource()))
  475. .ifPresent(c -> event.getManager().clearStatus());
  476. }
  477. @Override
  478. public void dispose() {
  479. if (!quitting) {
  480. removeWindowListener(this);
  481. }
  482. globalConfig.removeListener(this);
  483. super.dispose();
  484. }
  485. }