選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MainFrame.java 20KB

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