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