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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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.SplitPane;
  25. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  26. import com.dmdirc.addons.ui_swing.components.menubar.MenuBar;
  27. import com.dmdirc.addons.ui_swing.components.statusbar.SwingStatusBar;
  28. import com.dmdirc.addons.ui_swing.dialogs.StandardQuestionDialog;
  29. import com.dmdirc.addons.ui_swing.events.SwingActiveWindowChangeRequestEvent;
  30. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  31. import com.dmdirc.addons.ui_swing.events.SwingWindowAddedEvent;
  32. import com.dmdirc.addons.ui_swing.events.SwingWindowDeletedEvent;
  33. import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
  34. import com.dmdirc.addons.ui_swing.framemanager.FrameManager;
  35. import com.dmdirc.addons.ui_swing.framemanager.FramemanagerPosition;
  36. import com.dmdirc.addons.ui_swing.framemanager.ctrltab.CtrlTabWindowManager;
  37. import com.dmdirc.events.ClientMinimisedEvent;
  38. import com.dmdirc.events.ClientUnminimisedEvent;
  39. import com.dmdirc.events.FrameTitleChangedEvent;
  40. import com.dmdirc.events.UnreadStatusChangedEvent;
  41. import com.dmdirc.interfaces.LifecycleController;
  42. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  43. import com.dmdirc.interfaces.config.ConfigChangeListener;
  44. import com.dmdirc.interfaces.ui.Window;
  45. import com.dmdirc.ui.CoreUIUtils;
  46. import com.dmdirc.ui.IconManager;
  47. import com.dmdirc.util.collections.QueuedLinkedHashSet;
  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.inject.Provider;
  54. import javax.swing.ImageIcon;
  55. import javax.swing.JFrame;
  56. import javax.swing.JPanel;
  57. import javax.swing.JSplitPane;
  58. import javax.swing.SwingUtilities;
  59. import javax.swing.WindowConstants;
  60. import net.miginfocom.swing.MigLayout;
  61. import net.engio.mbassy.listener.Handler;
  62. import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
  63. import static java.util.function.Predicate.isEqual;
  64. /**
  65. * The main application frame.
  66. */
  67. public class MainFrame extends JFrame implements WindowListener, ConfigChangeListener {
  68. /** A version number for this class. */
  69. private static final long serialVersionUID = 9;
  70. /** Focus queue. */
  71. private final QueuedLinkedHashSet<Optional<TextFrame>> focusOrder;
  72. /** Apple instance. */
  73. private final Apple apple;
  74. /** Controller to use to end the program. */
  75. private final LifecycleController lifecycleController;
  76. /** The global config to read settings from. */
  77. private final AggregateConfigProvider globalConfig;
  78. /** The icon manager to use to get icons. */
  79. private final IconManager iconManager;
  80. /** The quit worker to use when quitting the app. */
  81. private final Provider<QuitWorker> quitWorker;
  82. /** Client Version. */
  83. private final String version;
  84. /** Frame manager used for ctrl tab frame switching. */
  85. private CtrlTabWindowManager frameManager;
  86. /** Provider of frame managers. */
  87. private final Provider<FrameManager> frameManagerProvider;
  88. /** The bus to despatch events on. */
  89. private final DMDircMBassador eventBus;
  90. /** Swing event bus to post events to. */
  91. private final SwingEventBus swingEventBus;
  92. /** The main application icon. */
  93. private ImageIcon imageIcon;
  94. /** The frame manager that's being used. */
  95. private FrameManager mainFrameManager;
  96. /** Active frame. */
  97. private Optional<TextFrame> activeFrame;
  98. /** Panel holding frame. */
  99. private JPanel framePanel;
  100. /** Main panel. */
  101. private JPanel frameManagerPanel;
  102. /** Frame manager position. */
  103. private FramemanagerPosition position;
  104. /** Show version? */
  105. private boolean showVersion;
  106. /** Exit code. */
  107. private int exitCode;
  108. /** Status bar. */
  109. private SwingStatusBar statusBar;
  110. /** Main split pane. */
  111. private SplitPane mainSplitPane;
  112. /** Are we quitting or closing? */
  113. private boolean quitting;
  114. /** Have we initialised our settings and listeners? */
  115. private boolean initDone;
  116. /**
  117. * Creates new form MainFrame.
  118. *
  119. * @param apple Apple instance
  120. * @param lifecycleController Controller to use to end the application.
  121. * @param globalConfig The config to read settings from.
  122. * @param quitWorker The quit worker to use when quitting the app.
  123. * @param iconManager The icon manager to use to get icons.
  124. * @param frameManagerProvider Provider to use to retrieve frame managers.
  125. * @param eventBus The event bus to post events to.
  126. * @param swingEventBus The event bus to post swing events to.
  127. */
  128. public MainFrame(
  129. final Apple apple,
  130. final LifecycleController lifecycleController,
  131. final AggregateConfigProvider globalConfig,
  132. final Provider<QuitWorker> quitWorker,
  133. final IconManager iconManager,
  134. final Provider<FrameManager> frameManagerProvider,
  135. final DMDircMBassador eventBus,
  136. final SwingEventBus swingEventBus) {
  137. checkOnEDT();
  138. this.apple = apple;
  139. this.lifecycleController = lifecycleController;
  140. this.globalConfig = globalConfig;
  141. this.quitWorker = quitWorker;
  142. this.iconManager = iconManager;
  143. this.frameManagerProvider = frameManagerProvider;
  144. this.eventBus = eventBus;
  145. this.swingEventBus = swingEventBus;
  146. version = globalConfig.getOption("version", "version");
  147. focusOrder = new QueuedLinkedHashSet<>();
  148. }
  149. @Override
  150. public void setVisible(final boolean visible) {
  151. if (!initDone) {
  152. swingEventBus.subscribe(this);
  153. imageIcon = new ImageIcon(iconManager.getImage("icon"));
  154. setIconImage(imageIcon.getImage());
  155. CoreUIUtils.centreWindow(this);
  156. addWindowListener(this);
  157. showVersion = globalConfig.getOptionBool("ui", "showversion");
  158. globalConfig.addChangeListener("ui", "showversion", this);
  159. globalConfig.addChangeListener("ui", "framemanager", this);
  160. globalConfig.addChangeListener("ui", "framemanagerPosition", this);
  161. globalConfig.addChangeListener("icon", "icon", this);
  162. addWindowFocusListener(new EventTriggeringFocusListener(eventBus));
  163. setTitle(getTitlePrefix());
  164. initDone = true;
  165. }
  166. super.setVisible(visible);
  167. }
  168. /**
  169. * Returns the size of the frame manager.
  170. *
  171. * @return Frame manager size.
  172. */
  173. public int getFrameManagerSize() {
  174. if (position == FramemanagerPosition.LEFT || position == FramemanagerPosition.RIGHT) {
  175. return frameManagerPanel.getWidth();
  176. } else {
  177. return frameManagerPanel.getHeight();
  178. }
  179. }
  180. @Override
  181. public MenuBar getJMenuBar() {
  182. return (MenuBar) super.getJMenuBar();
  183. }
  184. @Override
  185. public void setTitle(final String title) {
  186. UIUtilities.invokeLater(() -> {
  187. if (title == null || activeFrame == null) {
  188. MainFrame.super.setTitle(getTitlePrefix());
  189. } else {
  190. MainFrame.super.setTitle(getTitlePrefix() + " - " + title);
  191. }
  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. new Thread(() -> lifecycleController.quit(exitCode), "Quit thread").start();
  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. UIUtilities.invokeAndWait(() -> {
  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. /**
  243. * Initialises the components for this frame.
  244. */
  245. public void initComponents() {
  246. UIUtilities.invokeAndWait(() -> {
  247. frameManagerPanel = new JPanel();
  248. activeFrame = null;
  249. framePanel = new JPanel(new MigLayout("fill, ins 0"));
  250. initFrameManagers();
  251. mainSplitPane = initSplitPane();
  252. setPreferredSize(new Dimension(800, 600));
  253. getContentPane().setLayout(new MigLayout(
  254. "fill, ins rel, wrap 1, hidemode 2"));
  255. layoutComponents();
  256. setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  257. pack();
  258. });
  259. }
  260. /**
  261. * Sets the menu bar that this frame will use.
  262. *
  263. * <p>
  264. * Must be called prior to {@link #initComponents()}.
  265. *
  266. * @param menuBar The menu bar to use.
  267. */
  268. public void setMenuBar(final MenuBar menuBar) {
  269. UIUtilities.invokeAndWait(() -> {
  270. apple.setMenuBar(menuBar);
  271. setJMenuBar(menuBar);
  272. });
  273. }
  274. /**
  275. * Sets the window manager that this frame will use.
  276. *
  277. * <p>
  278. * Must be called prior to {@link #initComponents()}.
  279. *
  280. * @param windowManager The window manager to use.
  281. */
  282. public void setWindowManager(final CtrlTabWindowManager windowManager) {
  283. this.frameManager = windowManager;
  284. }
  285. /**
  286. * Sets the status bar that will be used.
  287. *
  288. * <p>
  289. * Must be called prior to {@link #initComponents()}.
  290. *
  291. * @param statusBar The status bar to be used.
  292. */
  293. public void setStatusBar(final SwingStatusBar statusBar) {
  294. this.statusBar = statusBar;
  295. }
  296. /**
  297. * Lays out the this component.
  298. */
  299. private void layoutComponents() {
  300. getContentPane().add(mainSplitPane, "grow, push");
  301. getContentPane().add(statusBar, "wmax 100%-2*rel, "
  302. + "wmin 100%-2*rel, south, gap rel rel 0 rel");
  303. }
  304. /**
  305. * Initialises the split pane.
  306. *
  307. * @return Returns the initialised split pane
  308. */
  309. private SplitPane initSplitPane() {
  310. final SplitPane splitPane = new SplitPane(globalConfig, SplitPane.Orientation.HORIZONTAL);
  311. position = FramemanagerPosition.getPosition(
  312. globalConfig.getOption("ui", "framemanagerPosition"));
  313. if (position == FramemanagerPosition.UNKNOWN) {
  314. position = FramemanagerPosition.LEFT;
  315. }
  316. if (!mainFrameManager.canPositionVertically() && (position
  317. == FramemanagerPosition.LEFT || position
  318. == FramemanagerPosition.RIGHT)) {
  319. position = FramemanagerPosition.BOTTOM;
  320. }
  321. if (!mainFrameManager.canPositionHorizontally() && (position
  322. == FramemanagerPosition.TOP || position
  323. == FramemanagerPosition.BOTTOM)) {
  324. position = FramemanagerPosition.LEFT;
  325. }
  326. switch (position) {
  327. case TOP:
  328. splitPane.setTopComponent(frameManagerPanel);
  329. splitPane.setBottomComponent(framePanel);
  330. splitPane.setResizeWeight(0.0);
  331. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  332. frameManagerPanel.setPreferredSize(new Dimension(
  333. Integer.MAX_VALUE, globalConfig.getOptionInt("ui", "frameManagerSize")));
  334. break;
  335. case LEFT:
  336. splitPane.setLeftComponent(frameManagerPanel);
  337. splitPane.setRightComponent(framePanel);
  338. splitPane.setResizeWeight(0.0);
  339. splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
  340. frameManagerPanel.setPreferredSize(new Dimension(
  341. globalConfig.getOptionInt("ui", "frameManagerSize"), Integer.MAX_VALUE));
  342. break;
  343. case BOTTOM:
  344. splitPane.setTopComponent(framePanel);
  345. splitPane.setBottomComponent(frameManagerPanel);
  346. splitPane.setResizeWeight(1.0);
  347. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  348. frameManagerPanel.setPreferredSize(new Dimension(
  349. Integer.MAX_VALUE, globalConfig.getOptionInt("ui", "frameManagerSize")));
  350. break;
  351. case RIGHT:
  352. splitPane.setLeftComponent(framePanel);
  353. splitPane.setRightComponent(frameManagerPanel);
  354. splitPane.setResizeWeight(1.0);
  355. splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
  356. frameManagerPanel.setPreferredSize(new Dimension(
  357. globalConfig.getOptionInt("ui", "frameManagerSize"), Integer.MAX_VALUE));
  358. break;
  359. default:
  360. break;
  361. }
  362. return splitPane;
  363. }
  364. /**
  365. * Exits with an "OK" status code.
  366. */
  367. public void quit() {
  368. quit(0);
  369. }
  370. /**
  371. * Exit code call to quit.
  372. *
  373. * @param exitCode Exit code
  374. */
  375. public void quit(final int exitCode) {
  376. if (exitCode == 0 && globalConfig.getOptionBool("ui", "confirmQuit")) {
  377. new StandardQuestionDialog(this, Dialog.ModalityType.APPLICATION_MODAL,
  378. "Quit confirm", "You are about to quit DMDirc, are you sure?",
  379. () -> { doQuit(0); return true;}).display();
  380. return;
  381. }
  382. doQuit(exitCode);
  383. }
  384. /**
  385. * Exit code call to quit.
  386. *
  387. * @param exitCode Exit code
  388. */
  389. public void doQuit(final int exitCode) {
  390. this.exitCode = exitCode;
  391. quitting = true;
  392. quitWorker.get().execute();
  393. }
  394. @Override
  395. public void configChanged(final String domain, final String key) {
  396. if ("ui".equals(domain)) {
  397. switch (key) {
  398. case "framemanager":
  399. case "framemanagerPosition":
  400. UIUtilities.invokeAndWait(() -> {
  401. setVisible(false);
  402. getContentPane().remove(mainSplitPane);
  403. initFrameManagers();
  404. getContentPane().removeAll();
  405. layoutComponents();
  406. setVisible(true);
  407. });
  408. break;
  409. default:
  410. showVersion = globalConfig.getOptionBool("ui", "showversion");
  411. break;
  412. }
  413. } else {
  414. imageIcon = new ImageIcon(iconManager.getImage("icon"));
  415. UIUtilities.invokeLater(() -> setIconImage(imageIcon.getImage()));
  416. }
  417. }
  418. @Handler(invocation = EdtHandlerInvocation.class)
  419. public void setActiveFrame(final SwingActiveWindowChangeRequestEvent event) {
  420. focusOrder.offerAndMove(activeFrame);
  421. framePanel.setVisible(false);
  422. framePanel.removeAll();
  423. activeFrame = event.getWindow();
  424. if (activeFrame.isPresent()) {
  425. framePanel.add(activeFrame.get().getDisplayFrame(), "grow");
  426. setTitle(activeFrame.get().getContainer().getTitle());
  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
  441. public void doWindowAdded(final SwingWindowAddedEvent event) {
  442. if (!activeFrame.isPresent()) {
  443. setActiveFrame(new SwingActiveWindowChangeRequestEvent(
  444. Optional.of(event.getChildWindow())));
  445. }
  446. }
  447. @Handler
  448. public void doWindowDeleted(final SwingWindowDeletedEvent event) {
  449. final Optional<TextFrame> window = Optional.of(event.getChildWindow());
  450. focusOrder.remove(window);
  451. if (activeFrame.equals(window)) {
  452. activeFrame = null;
  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
  464. public void titleChanged(final FrameTitleChangedEvent event) {
  465. activeFrame.map(Window::getContainer)
  466. .filter(isEqual(event.getContainer()))
  467. .ifPresent(c -> setTitle(event.getTitle()));
  468. }
  469. @Handler
  470. public void unreadStatusChanged(final UnreadStatusChangedEvent event) {
  471. activeFrame.map(Window::getContainer)
  472. .filter(isEqual(event.getSource()))
  473. .ifPresent(c -> event.getManager().clearStatus());
  474. }
  475. @Override
  476. public void dispose() {
  477. if (!quitting) {
  478. removeWindowListener(this);
  479. }
  480. globalConfig.removeListener(this);
  481. super.dispose();
  482. }
  483. }