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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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.Dialog;
  48. import java.awt.Dimension;
  49. import java.awt.event.WindowEvent;
  50. import java.awt.event.WindowListener;
  51. import java.util.Optional;
  52. import javax.inject.Provider;
  53. import javax.swing.ImageIcon;
  54. import javax.swing.JFrame;
  55. import javax.swing.JPanel;
  56. import javax.swing.JSplitPane;
  57. import javax.swing.SwingUtilities;
  58. import javax.swing.WindowConstants;
  59. import net.engio.mbassy.listener.Handler;
  60. import net.miginfocom.swing.MigLayout;
  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 MigLayout(
  253. "fill, ins rel, wrap 1, hidemode 2"));
  254. layoutComponents();
  255. setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  256. pack();
  257. }
  258. /**
  259. * Sets the menu bar that this frame will use.
  260. *
  261. * <p>
  262. * Must be called prior to {@link #initComponents()}.
  263. *
  264. * @param menuBar The menu bar to use.
  265. */
  266. public void setMenuBar(final MenuBar menuBar) {
  267. checkOnEDT();
  268. apple.setMenuBar(menuBar);
  269. setJMenuBar(menuBar);
  270. }
  271. /**
  272. * Sets the window manager that this frame will use.
  273. *
  274. * <p>
  275. * Must be called prior to {@link #initComponents()}.
  276. *
  277. * @param windowManager The window manager to use.
  278. */
  279. public void setWindowManager(final CtrlTabWindowManager windowManager) {
  280. this.frameManager = windowManager;
  281. }
  282. /**
  283. * Sets the status bar that will be used.
  284. *
  285. * <p>
  286. * Must be called prior to {@link #initComponents()}.
  287. *
  288. * @param statusBar The status bar to be used.
  289. */
  290. public void setStatusBar(final SwingStatusBar statusBar) {
  291. this.statusBar = statusBar;
  292. }
  293. /**
  294. * Lays out the this component.
  295. */
  296. private void layoutComponents() {
  297. getContentPane().add(mainSplitPane, "grow, push");
  298. getContentPane().add(statusBar, "wmax 100%-2*rel, "
  299. + "wmin 100%-2*rel, south, gap rel rel 0 rel");
  300. }
  301. /**
  302. * Initialises the split pane.
  303. *
  304. * @return Returns the initialised split pane
  305. */
  306. private SplitPane initSplitPane() {
  307. final SplitPane splitPane = new SplitPane(globalConfig, SplitPane.Orientation.HORIZONTAL);
  308. position = FramemanagerPosition.getPosition(
  309. globalConfig.getOption("ui", "framemanagerPosition"));
  310. if (position == FramemanagerPosition.UNKNOWN) {
  311. position = FramemanagerPosition.LEFT;
  312. }
  313. if (!mainFrameManager.canPositionVertically() && (position
  314. == FramemanagerPosition.LEFT || position
  315. == FramemanagerPosition.RIGHT)) {
  316. position = FramemanagerPosition.BOTTOM;
  317. }
  318. if (!mainFrameManager.canPositionHorizontally() && (position
  319. == FramemanagerPosition.TOP || position
  320. == FramemanagerPosition.BOTTOM)) {
  321. position = FramemanagerPosition.LEFT;
  322. }
  323. switch (position) {
  324. case TOP:
  325. splitPane.setTopComponent(frameManagerPanel);
  326. splitPane.setBottomComponent(framePanel);
  327. splitPane.setResizeWeight(0.0);
  328. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  329. frameManagerPanel.setPreferredSize(new Dimension(
  330. Integer.MAX_VALUE, globalConfig.getOptionInt("ui", "frameManagerSize")));
  331. break;
  332. case LEFT:
  333. splitPane.setLeftComponent(frameManagerPanel);
  334. splitPane.setRightComponent(framePanel);
  335. splitPane.setResizeWeight(0.0);
  336. splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
  337. frameManagerPanel.setPreferredSize(new Dimension(
  338. globalConfig.getOptionInt("ui", "frameManagerSize"), Integer.MAX_VALUE));
  339. break;
  340. case BOTTOM:
  341. splitPane.setTopComponent(framePanel);
  342. splitPane.setBottomComponent(frameManagerPanel);
  343. splitPane.setResizeWeight(1.0);
  344. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  345. frameManagerPanel.setPreferredSize(new Dimension(
  346. Integer.MAX_VALUE, globalConfig.getOptionInt("ui", "frameManagerSize")));
  347. break;
  348. case RIGHT:
  349. splitPane.setLeftComponent(framePanel);
  350. splitPane.setRightComponent(frameManagerPanel);
  351. splitPane.setResizeWeight(1.0);
  352. splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
  353. frameManagerPanel.setPreferredSize(new Dimension(
  354. globalConfig.getOptionInt("ui", "frameManagerSize"), Integer.MAX_VALUE));
  355. break;
  356. default:
  357. break;
  358. }
  359. return splitPane;
  360. }
  361. /**
  362. * Exits with an "OK" status code.
  363. */
  364. public void quit() {
  365. quit(0);
  366. }
  367. /**
  368. * Exit code call to quit.
  369. *
  370. * @param exitCode Exit code
  371. */
  372. public void quit(final int exitCode) {
  373. if (exitCode == 0 && globalConfig.getOptionBool("ui", "confirmQuit")) {
  374. new StandardQuestionDialog(this, Dialog.ModalityType.APPLICATION_MODAL,
  375. "Quit confirm", "You are about to quit DMDirc, are you sure?",
  376. () -> { doQuit(0); return true;}).display();
  377. return;
  378. }
  379. doQuit(exitCode);
  380. }
  381. /**
  382. * Exit code call to quit.
  383. *
  384. * @param exitCode Exit code
  385. */
  386. public void doQuit(final int exitCode) {
  387. this.exitCode = exitCode;
  388. quitting = true;
  389. quitWorker.get().execute();
  390. }
  391. @Override
  392. public void configChanged(final String domain, final String key) {
  393. if ("ui".equals(domain)) {
  394. switch (key) {
  395. case "framemanager":
  396. case "framemanagerPosition":
  397. UIUtilities.invokeAndWait(() -> {
  398. setVisible(false);
  399. getContentPane().remove(mainSplitPane);
  400. initFrameManagers();
  401. getContentPane().removeAll();
  402. layoutComponents();
  403. setVisible(true);
  404. });
  405. break;
  406. default:
  407. showVersion = globalConfig.getOptionBool("ui", "showversion");
  408. break;
  409. }
  410. } else {
  411. imageIcon = new ImageIcon(iconManager.getImage("icon"));
  412. UIUtilities.invokeLater(() -> setIconImage(imageIcon.getImage()));
  413. }
  414. }
  415. @Handler(invocation = EdtHandlerInvocation.class)
  416. public void setActiveFrame(final SwingActiveWindowChangeRequestEvent event) {
  417. checkOnEDT();
  418. focusOrder.offerAndMove(activeFrame);
  419. framePanel.setVisible(false);
  420. framePanel.removeAll();
  421. activeFrame = event.getWindow();
  422. if (activeFrame.isPresent()) {
  423. framePanel.add(activeFrame.get().getDisplayFrame(), "grow");
  424. setTitle(activeFrame.get().getContainer().getTitle());
  425. activeFrame.get().getContainer().getUnreadStatusManager().clearStatus();
  426. } else {
  427. framePanel.add(new JPanel(), "grow");
  428. setTitle(null);
  429. }
  430. framePanel.setVisible(true);
  431. if (activeFrame.isPresent()) {
  432. final TextFrame textFrame = activeFrame.get();
  433. textFrame.requestFocus();
  434. textFrame.requestFocusInWindow();
  435. textFrame.activateFrame();
  436. }
  437. swingEventBus.publish(new SwingWindowSelectedEvent(activeFrame));
  438. }
  439. @Handler(invocation = EdtHandlerInvocation.class)
  440. public void doWindowAdded(final SwingWindowAddedEvent event) {
  441. checkOnEDT();
  442. if (!activeFrame.isPresent()) {
  443. setActiveFrame(new SwingActiveWindowChangeRequestEvent(
  444. Optional.of(event.getChildWindow())));
  445. }
  446. }
  447. @Handler(invocation = EdtHandlerInvocation.class)
  448. public void doWindowDeleted(final SwingWindowDeletedEvent event) {
  449. checkOnEDT();
  450. final Optional<TextFrame> window = Optional.of(event.getChildWindow());
  451. focusOrder.remove(window);
  452. if (activeFrame.equals(window)) {
  453. activeFrame = Optional.empty();
  454. framePanel.setVisible(false);
  455. framePanel.removeAll();
  456. framePanel.setVisible(true);
  457. if (focusOrder.peek() == null) {
  458. SwingUtilities.invokeLater(frameManager::scrollUp);
  459. } else {
  460. setActiveFrame(new SwingActiveWindowChangeRequestEvent(focusOrder.peek()));
  461. }
  462. }
  463. }
  464. @Handler(invocation = EdtHandlerInvocation.class)
  465. public void titleChanged(final FrameTitleChangedEvent event) {
  466. checkOnEDT();
  467. activeFrame.map(TextFrame::getContainer)
  468. .filter(isEqual(event.getContainer()))
  469. .ifPresent(c -> setTitle(event.getTitle()));
  470. }
  471. @Handler(invocation = EdtHandlerInvocation.class)
  472. public void unreadStatusChanged(final UnreadStatusChangedEvent event) {
  473. checkOnEDT();
  474. activeFrame.map(TextFrame::getContainer)
  475. .filter(isEqual(event.getSource()))
  476. .ifPresent(c -> event.getManager().clearStatus());
  477. }
  478. @Override
  479. public void dispose() {
  480. if (!quitting) {
  481. removeWindowListener(this);
  482. }
  483. globalConfig.removeListener(this);
  484. super.dispose();
  485. }
  486. }