Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MainFrame.java 19KB

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