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.

SwingController.java 34KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.Channel;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.Query;
  26. import com.dmdirc.Server;
  27. import com.dmdirc.WritableFrameContainer;
  28. import com.dmdirc.addons.ui_swing.components.frames.ChannelFrame;
  29. import com.dmdirc.addons.ui_swing.components.frames.CustomFrame;
  30. import com.dmdirc.addons.ui_swing.components.frames.CustomInputFrame;
  31. import com.dmdirc.addons.ui_swing.components.frames.QueryFrame;
  32. import com.dmdirc.addons.ui_swing.components.frames.ServerFrame;
  33. import com.dmdirc.addons.ui_swing.components.addonpanel.AddonPanel;
  34. import com.dmdirc.addons.ui_swing.components.addonpanel.PluginPanel;
  35. import com.dmdirc.addons.ui_swing.components.addonpanel.ThemePanel;
  36. import com.dmdirc.addons.ui_swing.components.statusbar.FeedbackNag;
  37. import com.dmdirc.addons.ui_swing.components.statusbar.SwingStatusBar;
  38. import com.dmdirc.addons.ui_swing.dialogs.DialogKeyListener;
  39. import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
  40. import com.dmdirc.addons.ui_swing.dialogs.StandardMessageDialog;
  41. import com.dmdirc.addons.ui_swing.dialogs.channelsetting.ChannelSettingsDialog;
  42. import com.dmdirc.addons.ui_swing.dialogs.error.ErrorListDialog;
  43. import com.dmdirc.addons.ui_swing.dialogs.prefs.SwingPreferencesDialog;
  44. import com.dmdirc.addons.ui_swing.dialogs.prefs.URLConfigPanel;
  45. import com.dmdirc.addons.ui_swing.dialogs.prefs.UpdateConfigPanel;
  46. import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
  47. import com.dmdirc.addons.ui_swing.dialogs.sslcertificate.SSLCertificateDialog;
  48. import com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog;
  49. import com.dmdirc.addons.ui_swing.dialogs.url.URLDialog;
  50. import com.dmdirc.addons.ui_swing.wizard.WizardListener;
  51. import com.dmdirc.addons.ui_swing.wizard.firstrun.SwingFirstRunWizard;
  52. import com.dmdirc.config.Identity;
  53. import com.dmdirc.config.IdentityManager;
  54. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  55. import com.dmdirc.config.prefs.PreferencesCategory;
  56. import com.dmdirc.config.prefs.PreferencesInterface;
  57. import com.dmdirc.config.prefs.PreferencesDialogModel;
  58. import com.dmdirc.config.prefs.PreferencesSetting;
  59. import com.dmdirc.config.prefs.PreferencesType;
  60. import com.dmdirc.util.validators.NotEmptyValidator;
  61. import com.dmdirc.util.validators.NumericalValidator;
  62. import com.dmdirc.logger.ErrorLevel;
  63. import com.dmdirc.logger.Logger;
  64. import com.dmdirc.plugins.Plugin;
  65. import com.dmdirc.ui.WindowManager;
  66. import com.dmdirc.ui.core.components.StatusBarManager;
  67. import com.dmdirc.ui.core.dialogs.sslcertificate.SSLCertificateDialogModel;
  68. import com.dmdirc.ui.core.util.URLHandler;
  69. import com.dmdirc.ui.interfaces.ChannelWindow;
  70. import com.dmdirc.ui.interfaces.FrameListener;
  71. import com.dmdirc.ui.interfaces.InputWindow;
  72. import com.dmdirc.ui.interfaces.QueryWindow;
  73. import com.dmdirc.ui.interfaces.ServerWindow;
  74. import com.dmdirc.ui.interfaces.StatusBar;
  75. import com.dmdirc.ui.interfaces.UIController;
  76. import com.dmdirc.ui.interfaces.Window;
  77. import com.dmdirc.updater.Update;
  78. import com.dmdirc.util.ReturnableThread;
  79. import java.awt.Font;
  80. import java.awt.GraphicsEnvironment;
  81. import java.awt.KeyboardFocusManager;
  82. import java.awt.Toolkit;
  83. import java.awt.Dialog.ModalityType;
  84. import java.net.URI;
  85. import java.util.ArrayList;
  86. import java.util.HashMap;
  87. import java.util.List;
  88. import java.util.Map;
  89. import java.util.concurrent.Semaphore;
  90. import java.util.concurrent.atomic.AtomicBoolean;
  91. import java.util.logging.Level;
  92. import javax.swing.JMenuItem;
  93. import javax.swing.SwingUtilities;
  94. import javax.swing.UIManager;
  95. import javax.swing.UnsupportedLookAndFeelException;
  96. import javax.swing.UIManager.LookAndFeelInfo;
  97. import net.miginfocom.layout.PlatformDefaults;
  98. /**
  99. * Controls the main swing UI.
  100. */
  101. public class SwingController extends Plugin implements UIController {
  102. /**
  103. * A version number for this class. It should be changed whenever the class
  104. * structure is changed (or anything else that would prevent serialized
  105. * objects being unserialized with the new class).
  106. */
  107. private static final long serialVersionUID = 1;
  108. /** Logger to use. */
  109. private static final java.util.logging.Logger LOGGER =
  110. java.util.logging.Logger.getLogger(SwingController.class.getName());
  111. /** Window factory. */
  112. private final SwingWindowFactory windowFactory =
  113. new SwingWindowFactory(this);
  114. /** Waiting on mainframe creation. */
  115. private final AtomicBoolean mainFrameCreated = new AtomicBoolean(false);
  116. /** URL Handler to use. */
  117. private final URLHandler urlHandler = new URLHandler(this);
  118. /** Singleton instance of MainFrame. */
  119. private MainFrame me;
  120. /** Status bar. */
  121. private SwingStatusBar statusBar;
  122. /** Top level window list. */
  123. private final List<java.awt.Window> windows;
  124. /** Error dialog. */
  125. private ErrorListDialog errorDialog;
  126. /** Instantiates a new SwingController. */
  127. public SwingController() {
  128. super();
  129. setAntiAlias();
  130. windows = new ArrayList<java.awt.Window>();
  131. }
  132. /**
  133. * Make swing not use Anti Aliasing if the user doesn't want it.
  134. */
  135. public final void setAntiAlias() {
  136. // For this to work it *HAS* to be before anything else UI related.
  137. final boolean aaSetting = IdentityManager.getGlobalConfig().
  138. getOptionBool("ui", "antialias");
  139. System.setProperty("awt.useSystemAAFontSettings",
  140. Boolean.toString(aaSetting));
  141. System.setProperty("swing.aatext", Boolean.toString(aaSetting));
  142. }
  143. /**
  144. * Does the main frame exist?
  145. *
  146. * @return true iif mainframe exists
  147. */
  148. protected boolean hasMainFrame() {
  149. return mainFrameCreated.get();
  150. }
  151. /** {@inheritDoc} */
  152. @Override
  153. public MainFrame getMainWindow() {
  154. return getMainFrame();
  155. }
  156. /**
  157. * Returns a URL Handler which may be used when working with the Swing UI.
  158. *
  159. * @return A URL handler for use with the swing UI
  160. * @since 0.6.5
  161. */
  162. public URLHandler getURLHandler() {
  163. return urlHandler;
  164. }
  165. /**
  166. * Returns the window factory used by this controller.
  167. *
  168. * @return This controller's window factory
  169. * @since 0.6.4
  170. */
  171. public SwingWindowFactory getWindowFactory() {
  172. return windowFactory;
  173. }
  174. /**
  175. * Retrieves the main window used by this UI.
  176. *
  177. * @return This UI's main window
  178. */
  179. public MainFrame getMainFrame() {
  180. return me;
  181. }
  182. /**
  183. * {@inheritDoc}
  184. *
  185. * @deprecated Should not be used externally - use the
  186. * {@link com.dmdirc.ui.core.components.StatusBarManager} instead.
  187. */
  188. @Override
  189. @Deprecated
  190. public StatusBar getStatusBar() {
  191. return getSwingStatusBar();
  192. }
  193. /**
  194. * Retrieves the Swing Status Bar used by this UI.
  195. *
  196. * @return This UI's status bar
  197. */
  198. public SwingStatusBar getSwingStatusBar() {
  199. return statusBar;
  200. }
  201. /**
  202. * {@inheritDoc}
  203. *
  204. * @deprecated Controllers should listen for window events using a
  205. * {@link FrameListener} and create windows as needed.
  206. */
  207. @Override
  208. @Deprecated
  209. public ChannelWindow getChannel(final Channel channel) {
  210. return UIUtilities.invokeAndWait(new ReturnableThread<ChannelFrame>() {
  211. /** {@inheritDoc} */
  212. @Override
  213. public void run() {
  214. setObject(new ChannelFrame(SwingController.this, channel));
  215. }
  216. });
  217. }
  218. /**
  219. * {@inheritDoc}
  220. *
  221. * @deprecated Controllers should listen for window events using a
  222. * {@link FrameListener} and create windows as needed.
  223. */
  224. @Override
  225. @Deprecated
  226. public ServerWindow getServer(final Server server) {
  227. return UIUtilities.invokeAndWait(new ReturnableThread<ServerFrame>() {
  228. /** {@inheritDoc} */
  229. @Override
  230. public void run() {
  231. setObject(new ServerFrame(SwingController.this, server));
  232. }
  233. });
  234. }
  235. /**
  236. * {@inheritDoc}
  237. *
  238. * @deprecated Controllers should listen for window events using a
  239. * {@link FrameListener} and create windows as needed.
  240. */
  241. @Override
  242. @Deprecated
  243. public QueryWindow getQuery(final Query query) {
  244. return UIUtilities.invokeAndWait(new ReturnableThread<QueryFrame>() {
  245. /** {@inheritDoc} */
  246. @Override
  247. public void run() {
  248. setObject(new QueryFrame(SwingController.this, query));
  249. }
  250. });
  251. }
  252. /**
  253. * {@inheritDoc}
  254. *
  255. * @deprecated Controllers should listen for window events using a
  256. * {@link FrameListener} and create windows as needed.
  257. */
  258. @Override
  259. @Deprecated
  260. public Window getWindow(final FrameContainer<?> owner) {
  261. return UIUtilities.invokeAndWait(new ReturnableThread<CustomFrame>() {
  262. /** {@inheritDoc} */
  263. @Override
  264. public void run() {
  265. setObject(new CustomFrame(SwingController.this, owner));
  266. }
  267. });
  268. }
  269. /**
  270. * {@inheritDoc}
  271. *
  272. * @deprecated Controllers should listen for window events using a
  273. * {@link FrameListener} and create windows as needed.
  274. */
  275. @Override
  276. @Deprecated
  277. public InputWindow getInputWindow(final WritableFrameContainer<?> owner) {
  278. LOGGER.finest("getInputWindow()");
  279. return UIUtilities.invokeAndWait(
  280. new ReturnableThread<CustomInputFrame>() {
  281. /** {@inheritDoc} */
  282. @Override
  283. public void run() {
  284. LOGGER.finest("getInputWindow(): run");
  285. setObject(new CustomInputFrame(SwingController.this, owner));
  286. LOGGER.log(Level.FINEST, "getInputWindow(): object set: {1}",
  287. getObject());
  288. }
  289. });
  290. }
  291. /** {@inheritDoc} */
  292. @Override
  293. public SwingUpdaterDialog getUpdaterDialog(final List<Update> updates) {
  294. return UIUtilities.invokeAndWait(
  295. new ReturnableThread<SwingUpdaterDialog>() {
  296. /** {@inheritDoc} */
  297. @Override
  298. public void run() {
  299. setObject(SwingUpdaterDialog.getSwingUpdaterDialog(updates,
  300. me));
  301. }
  302. });
  303. }
  304. /** {@inheritDoc} */
  305. @Override
  306. public void showFirstRunWizard() {
  307. final Semaphore semaphore = new Semaphore(0);
  308. UIUtilities.invokeLater(new Runnable() {
  309. /** {@inheritDoc} */
  310. @Override
  311. public void run() {
  312. final WizardListener listener = new WizardListener() {
  313. /** {@inheritDoc} */
  314. @Override
  315. public void wizardFinished() {
  316. semaphore.release();
  317. }
  318. /** {@inheritDoc} */
  319. @Override
  320. public void wizardCancelled() {
  321. semaphore.release();
  322. }
  323. };
  324. final SwingFirstRunWizard wizard = new SwingFirstRunWizard(me,
  325. SwingController.this);
  326. wizard.getWizardDialog().addWizardListener(listener);
  327. wizard.display();
  328. }
  329. });
  330. semaphore.acquireUninterruptibly();
  331. }
  332. /**
  333. * {@inheritDoc}
  334. *
  335. * @deprecated Migration wizard is no longer used or needed
  336. */
  337. @Override
  338. @Deprecated
  339. public void showMigrationWizard() {
  340. // Do nothing
  341. }
  342. /** {@inheritDoc} */
  343. @Override
  344. public void showChannelSettingsDialog(final Channel channel) {
  345. UIUtilities.invokeLater(new Runnable() {
  346. /** {@inheritDoc} */
  347. @Override
  348. public void run() {
  349. ChannelSettingsDialog.showChannelSettingsDialog(channel, me,
  350. (InputWindow) getWindowFactory().getSwingWindow(
  351. channel));
  352. }
  353. });
  354. }
  355. /** {@inheritDoc} */
  356. @Override
  357. public void showServerSettingsDialog(final Server server) {
  358. UIUtilities.invokeLater(new Runnable() {
  359. /** {@inheritDoc} */
  360. @Override
  361. public void run() {
  362. ServerSettingsDialog.showServerSettingsDialog(server, me);
  363. }
  364. });
  365. }
  366. /**
  367. * Updates the look and feel to the current config setting.
  368. */
  369. public void updateLookAndFeel() {
  370. try {
  371. UIManager.setLookAndFeel(UIUtilities.getLookAndFeel(IdentityManager.
  372. getGlobalConfig().getOption("ui", "lookandfeel")));
  373. final int state = UIUtilities.invokeAndWait(
  374. new ReturnableThread<Integer>() {
  375. /** {@inheritDoc} */
  376. @Override
  377. public void run() {
  378. setObject(me.getExtendedState());
  379. }
  380. });
  381. UIUtilities.invokeLater(new Runnable() {
  382. /** {@inheritDoc} */
  383. @Override
  384. public void run() {
  385. SwingUtilities.updateComponentTreeUI(errorDialog);
  386. }
  387. });
  388. for (final java.awt.Window window : getTopLevelWindows()) {
  389. UIUtilities.invokeLater(new Runnable() {
  390. /** {@inheritDoc} */
  391. @Override
  392. public void run() {
  393. SwingUtilities.updateComponentTreeUI(window);
  394. if (window != me) {
  395. window.pack();
  396. }
  397. }
  398. });
  399. }
  400. UIUtilities.invokeLater(new Runnable() {
  401. /** {@inheritDoc} */
  402. @Override
  403. public void run() {
  404. me.setExtendedState(state);
  405. }
  406. });
  407. } catch (ClassNotFoundException ex) {
  408. Logger.userError(ErrorLevel.LOW,
  409. "Unable to change Look and Feel: " + ex.getMessage());
  410. } catch (InstantiationException ex) {
  411. Logger.userError(ErrorLevel.LOW,
  412. "Unable to change Look and Feel: " + ex.getMessage());
  413. } catch (IllegalAccessException ex) {
  414. Logger.userError(ErrorLevel.LOW,
  415. "Unable to change Look and Feel: " + ex.getMessage());
  416. } catch (UnsupportedLookAndFeelException ex) {
  417. Logger.userError(ErrorLevel.LOW,
  418. "Unable to change Look and Feel: " + ex.getMessage());
  419. }
  420. }
  421. /** {@inheritDoc} */
  422. @Override
  423. public void initUISettings() {
  424. // This will do nothing on non OS X Systems
  425. if (Apple.isApple()) {
  426. final Apple apple = Apple.getApple();
  427. apple.setUISettings();
  428. apple.setListener();
  429. }
  430. final Font defaultFont = new Font(Font.DIALOG, Font.TRUETYPE_FONT, 12);
  431. if (UIManager.getFont("TextField.font") == null) {
  432. UIManager.put("TextField.font", defaultFont);
  433. }
  434. if (UIManager.getFont("TextPane.font") == null) {
  435. UIManager.put("TextPane.font", defaultFont);
  436. }
  437. try {
  438. UIUtilities.initUISettings();
  439. UIManager.setLookAndFeel(UIUtilities.getLookAndFeel(IdentityManager.
  440. getGlobalConfig().getOption("ui", "lookandfeel")));
  441. } catch (UnsupportedOperationException ex) {
  442. Logger.userError(ErrorLevel.LOW, "Unable to set UI Settings");
  443. } catch (UnsupportedLookAndFeelException ex) {
  444. Logger.userError(ErrorLevel.LOW, "Unable to set UI Settings");
  445. } catch (IllegalAccessException ex) {
  446. Logger.userError(ErrorLevel.LOW, "Unable to set UI Settings");
  447. } catch (InstantiationException ex) {
  448. Logger.userError(ErrorLevel.LOW, "Unable to set UI Settings");
  449. } catch (ClassNotFoundException ex) {
  450. Logger.userError(ErrorLevel.LOW, "Unable to set UI Settings");
  451. }
  452. if ("Metal".equals(UIManager.getLookAndFeel().getName())
  453. || Apple.isAppleUI()) {
  454. PlatformDefaults.setPlatform(PlatformDefaults.WINDOWS_XP);
  455. }
  456. }
  457. /**
  458. * {@inheritDoc}
  459. *
  460. * @deprecated
  461. */
  462. @Override
  463. @Deprecated
  464. public Window getActiveWindow() {
  465. return me.getActiveFrame();
  466. }
  467. /**
  468. * {@inheritDoc}
  469. *
  470. * @deprecated
  471. */
  472. @Override
  473. @Deprecated
  474. public Server getActiveServer() {
  475. if (!mainFrameCreated.get()) {
  476. return null;
  477. }
  478. if (getActiveWindow() == null) {
  479. return null;
  480. } else {
  481. return getActiveWindow().getContainer().getServer();
  482. }
  483. }
  484. /** {@inheritDoc} */
  485. @Override
  486. public void showURLDialog(final URI url) {
  487. UIUtilities.invokeLater(new Runnable() {
  488. /** {@inheritDoc} */
  489. @Override
  490. public void run() {
  491. URLDialog.showURLDialog(url, me, urlHandler);
  492. }
  493. });
  494. }
  495. /** {@inheritDoc} */
  496. @Override
  497. public void showSSLCertificateDialog(
  498. final SSLCertificateDialogModel model) {
  499. UIUtilities.invokeLater(new Runnable() {
  500. /** {@inheritDoc} */
  501. @Override
  502. public void run() {
  503. new SSLCertificateDialog(me, model).display();
  504. }
  505. });
  506. }
  507. /** {@inheritDoc} */
  508. @Override
  509. public void showFeedbackNag() {
  510. UIUtilities.invokeLater(new Runnable() {
  511. /** {@inheritDoc} */
  512. @Override
  513. public void run() {
  514. new FeedbackNag(SwingController.this);
  515. }
  516. });
  517. }
  518. /** {@inheritDoc} */
  519. @Override
  520. public void showMessageDialog(final String title, final String message) {
  521. UIUtilities.invokeLater(new Runnable() {
  522. /** {@inheritDoc} */
  523. @Override
  524. public void run() {
  525. new StandardMessageDialog(me, ModalityType.MODELESS, title,
  526. message).display();
  527. }
  528. });
  529. }
  530. /**
  531. * Shows the error dialog.
  532. */
  533. public void showErrorDialog() {
  534. errorDialog.display();
  535. }
  536. /** {@inheritDoc} */
  537. @Override
  538. public String getUserInput(final String prompt) {
  539. final StandardInputDialog dialog = new StandardInputDialog(me,
  540. ModalityType.MODELESS, "Input required", prompt,
  541. new NotEmptyValidator()) {
  542. /**
  543. * A version number for this class. It should be changed whenever
  544. * the class structure is changed (or anything else that would
  545. * prevent serialized objects being unserialized with the new
  546. * class).
  547. */
  548. private static final long serialVersionUID = 1;
  549. /** {@inheritDoc} */
  550. @Override
  551. public boolean save() {
  552. return true;
  553. }
  554. /** {@inheritDoc} */
  555. @Override
  556. public void cancelled() {
  557. //Do nothing.
  558. }
  559. };
  560. dialog.displayBlocking();
  561. return dialog.getText();
  562. }
  563. /** {@inheritDoc} */
  564. @Override
  565. public PreferencesInterface getPluginPrefsPanel() {
  566. return UIUtilities.invokeAndWait(new ReturnableThread<AddonPanel>() {
  567. /** {@inheritDoc} */
  568. @Override
  569. public void run() {
  570. setObject(new PluginPanel(me, SwingController.this));
  571. }
  572. });
  573. }
  574. /** {@inheritDoc} */
  575. @Override
  576. public PreferencesInterface getUpdatesPrefsPanel() {
  577. return UIUtilities.invokeAndWait(
  578. new ReturnableThread<PreferencesInterface>() {
  579. /** {@inheritDoc} */
  580. @Override
  581. public void run() {
  582. setObject(new UpdateConfigPanel(SwingController.this));
  583. }
  584. });
  585. }
  586. /** {@inheritDoc} */
  587. @Override
  588. public PreferencesInterface getUrlHandlersPrefsPanel() {
  589. return UIUtilities.invokeAndWait(
  590. new ReturnableThread<PreferencesInterface>() {
  591. /** {@inheritDoc} */
  592. @Override
  593. public void run() {
  594. setObject(new URLConfigPanel(me, urlHandler));
  595. }
  596. });
  597. }
  598. /** {@inheritDoc} */
  599. @Override
  600. public PreferencesInterface getThemesPrefsPanel() {
  601. return UIUtilities.invokeAndWait(
  602. new ReturnableThread<PreferencesInterface>() {
  603. /** {@inheritDoc} */
  604. @Override
  605. public void run() {
  606. setObject(new ThemePanel(me, SwingController.this));
  607. }
  608. });
  609. }
  610. /**
  611. * Returns the current look and feel.
  612. *
  613. * @return Current look and feel
  614. */
  615. public static String getLookAndFeel() {
  616. return UIManager.getLookAndFeel().getName();
  617. }
  618. /** {@inheritDoc} */
  619. @Override
  620. public void onLoad() {
  621. if (GraphicsEnvironment.isHeadless()) {
  622. throw new IllegalStateException(
  623. "Swing UI can't be run in a headless environment");
  624. }
  625. Toolkit.getDefaultToolkit().getSystemEventQueue().
  626. push(new DMDircEventQueue(this));
  627. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  628. addKeyEventDispatcher(new DialogKeyListener());
  629. UIUtilities.invokeAndWait(new Runnable() {
  630. /** {@inheritDoc} */
  631. @Override
  632. public void run() {
  633. initUISettings();
  634. me = new MainFrame(SwingController.this);
  635. me.setVisible(true);
  636. mainFrameCreated.set(true);
  637. statusBar = me.getStatusBar();
  638. errorDialog = new ErrorListDialog(me);
  639. StatusBarManager.getStatusBarManager().registerStatusBar(
  640. statusBar);
  641. }
  642. });
  643. if (!mainFrameCreated.get()) {
  644. throw new IllegalStateException(
  645. "Main frame not created. Unable to continue.");
  646. }
  647. WindowManager.addFrameListener(windowFactory);
  648. }
  649. /** {@inheritDoc} */
  650. @Override
  651. public void onUnload() {
  652. // Do nothing
  653. }
  654. /** {@inheritDoc} */
  655. @Override
  656. public void domainUpdated() {
  657. final Identity defaults = IdentityManager.getAddonIdentity();
  658. defaults.setOption("ui", "textPaneFontName",
  659. UIManager.getFont("TextPane.font").getFamily());
  660. defaults.setOption("ui", "textPaneFontSize",
  661. UIManager.getFont("TextPane.font").getSize());
  662. }
  663. /**
  664. * Returns the preferences dialog instance creating if required.
  665. *
  666. * @return Swing prefs dialog
  667. */
  668. public SwingPreferencesDialog getPrefsDialog() {
  669. return SwingPreferencesDialog.getSwingPreferencesDialog(this);
  670. }
  671. /** {@inheritDoc} */
  672. @Override
  673. public void showConfig(final PreferencesDialogModel manager) {
  674. manager.getCategory("GUI").addSubCategory(createGeneralCategory());
  675. }
  676. /**
  677. * Creates the "Advanced" category.
  678. *
  679. * @return Newly created preferences category
  680. */
  681. private PreferencesCategory createGeneralCategory() {
  682. final PreferencesCategory general = new PluginPreferencesCategory(
  683. getPluginInfo(), "Swing UI", "These config options apply "
  684. + "only to the swing UI.", "category-gui");
  685. final Map<String, String> lafs = new HashMap<String, String>();
  686. final Map<String, String> framemanagers = new HashMap<String, String>();
  687. final Map<String, String> fmpositions = new HashMap<String, String>();
  688. framemanagers.put(
  689. "com.dmdirc.addons.ui_swing.framemanager.tree.TreeFrameManager",
  690. "Treeview");
  691. framemanagers.put(
  692. "com.dmdirc.addons.ui_swing.framemanager.buttonbar.ButtonBar",
  693. "Button bar");
  694. fmpositions.put("top", "Top");
  695. fmpositions.put("bottom", "Bottom");
  696. fmpositions.put("left", "Left");
  697. fmpositions.put("right", "Right");
  698. final LookAndFeelInfo[] plaf = UIManager.getInstalledLookAndFeels();
  699. lafs.put("Native", "Native");
  700. for (LookAndFeelInfo laf : plaf) {
  701. lafs.put(laf.getName(), laf.getName());
  702. }
  703. general.addSetting(new PreferencesSetting("ui", "lookandfeel",
  704. "Look and feel", "The Java look and feel to use", lafs));
  705. general.addSetting(new PreferencesSetting("ui", "framemanager",
  706. "Window manager", "Which window manager should be used?",
  707. framemanagers));
  708. general.addSetting(new PreferencesSetting("ui", "framemanagerPosition",
  709. "Window manager position", "Where should the window "
  710. + "manager be positioned?", fmpositions));
  711. general.addSetting(new PreferencesSetting(PreferencesType.FONT,
  712. "ui", "textPaneFontName", "Textpane font",
  713. "Font for the textpane"));
  714. general.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
  715. "ui", "textPaneFontSize", "Textpane font size",
  716. "Font size for the textpane"));
  717. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  718. "ui", "sortrootwindows", "Sort root windows",
  719. "Sort child windows in the frame managers?"));
  720. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  721. "ui", "sortchildwindows", "Sort child windows",
  722. "Sort root windows in the frame managers?"));
  723. general.addSubCategory(createNicklistCategory());
  724. general.addSubCategory(createTreeViewCategory());
  725. general.addSubCategory(createAdvancedCategory());
  726. return general;
  727. }
  728. /**
  729. * Creates the "Advanced" category.
  730. *
  731. * @return Newly created preferences category
  732. */
  733. private PreferencesCategory createAdvancedCategory() {
  734. final PreferencesCategory advanced = new PluginPreferencesCategory(
  735. getPluginInfo(), "Advanced", "");
  736. advanced.addSetting(new PreferencesSetting(
  737. PreferencesType.OPTIONALINTEGER,
  738. new NumericalValidator(10, -1), "ui", "frameBufferSize",
  739. "Window buffer size", "The maximum number of lines in a window"
  740. + " buffer"));
  741. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  742. getDomain(), "mdiBarVisibility", "MDI Bar Visibility",
  743. "Controls the visibility of the MDI bar"));
  744. advanced.addSetting(
  745. new PreferencesSetting(PreferencesType.BOOLEAN, "ui",
  746. "useOneTouchExpandable", "Use one touch expandable split "
  747. + "panes?", "Use one touch expandable arrows for "
  748. + "collapsing/expanding the split panes"));
  749. advanced.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
  750. getDomain(), "windowMenuItems", "Window menu item count",
  751. "Number of items to show in the window menu"));
  752. advanced.addSetting(
  753. new PreferencesSetting(PreferencesType.INTEGER, getDomain(),
  754. "windowMenuScrollInterval", "Window menu scroll interval",
  755. "Number of milliseconds to pause when autoscrolling in the "
  756. + "window menu"));
  757. advanced.addSetting(
  758. new PreferencesSetting(PreferencesType.BOOLEAN, getDomain(),
  759. "showtopicbar", "Show topic bar",
  760. "Shows a graphical topic bar in channels."));
  761. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  762. getDomain(),
  763. "shownicklist", "Show nicklist?",
  764. "Do you want the nicklist visible"));
  765. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  766. getDomain(), "showfulltopic", "Show full topic in topic bar?",
  767. "Do you want to show the full topic in the topic bar or just"
  768. + "first line?"));
  769. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  770. getDomain(), "hideEmptyTopicBar", "Hide empty topic bar?",
  771. "Do you want to hide the topic bar when there is no topic"));
  772. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  773. getDomain(), "textpanelinenotification",
  774. "New line notification", "Do you want to be notified about new "
  775. + "lines whilst scrolled up?"));
  776. return advanced;
  777. }
  778. /**
  779. * Creates the "Treeview" category.
  780. *
  781. * @return Newly created preferences category
  782. */
  783. private PreferencesCategory createTreeViewCategory() {
  784. final PreferencesCategory treeview = new PluginPreferencesCategory(
  785. getPluginInfo(), "Treeview", "", "treeview");
  786. treeview.addSetting(new PreferencesSetting(
  787. PreferencesType.OPTIONALCOLOUR,
  788. "treeview", "backgroundcolour", "Treeview background colour",
  789. "Background colour to use for the treeview"));
  790. treeview.addSetting(new PreferencesSetting(
  791. PreferencesType.OPTIONALCOLOUR,
  792. "treeview", "foregroundcolour", "Treeview foreground colour",
  793. "Foreground colour to use for the treeview"));
  794. treeview.addSetting(new PreferencesSetting(
  795. PreferencesType.OPTIONALCOLOUR,
  796. "ui", "treeviewRolloverColour", "Treeview rollover colour",
  797. "Background colour to use when the mouse cursor is over a "
  798. + "node"));
  799. treeview.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  800. "ui", "treeviewActiveBold", "Active node bold",
  801. "Make the active node bold?"));
  802. treeview.addSetting(new PreferencesSetting(
  803. PreferencesType.OPTIONALCOLOUR,
  804. "ui", "treeviewActiveBackground", "Active node background",
  805. "Background colour to use for active treeview node"));
  806. treeview.addSetting(new PreferencesSetting(
  807. PreferencesType.OPTIONALCOLOUR,
  808. "ui", "treeviewActiveForeground", "Active node foreground",
  809. "Foreground colour to use for active treeview node"));
  810. treeview.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  811. getDomain(), "showtreeexpands", "Show expand/collapse handles",
  812. "Do you want to show tree view collapse/expand handles"));
  813. return treeview;
  814. }
  815. /**
  816. * Creates the "Nicklist" category.
  817. *
  818. * @return Newly created preferences category
  819. */
  820. private PreferencesCategory createNicklistCategory() {
  821. final PreferencesCategory nicklist = new PluginPreferencesCategory(
  822. getPluginInfo(), "Nicklist", "", "nicklist");
  823. nicklist.addSetting(new PreferencesSetting(
  824. PreferencesType.OPTIONALCOLOUR,
  825. "ui", "nicklistbackgroundcolour", "Nicklist background colour",
  826. "Background colour to use for the nicklist"));
  827. nicklist.addSetting(new PreferencesSetting(
  828. PreferencesType.OPTIONALCOLOUR,
  829. "ui", "nicklistforegroundcolour", "Nicklist foreground colour",
  830. "Foreground colour to use for the nicklist"));
  831. nicklist.addSetting(new PreferencesSetting(
  832. PreferencesType.OPTIONALCOLOUR,
  833. "ui", "nickListAltBackgroundColour",
  834. "Alternate background colour",
  835. "Background colour to use for every other nicklist entry"));
  836. nicklist.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  837. "nicklist", "sortByMode", "Sort nicklist by user mode",
  838. "Sort nicknames by the modes that they have?"));
  839. nicklist.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  840. "nicklist", "sortByCase", "Sort nicklist by case",
  841. "Sort nicknames in a case-sensitive manner?"));
  842. return nicklist;
  843. }
  844. /**
  845. * Adds a top level window to the window list.
  846. *
  847. * @param source New window
  848. */
  849. protected void addTopLevelWindow(final java.awt.Window source) {
  850. synchronized (windows) {
  851. windows.add(source);
  852. }
  853. }
  854. /**
  855. * Deletes a top level window to the window list.
  856. *
  857. * @param source Old window
  858. */
  859. protected void delTopLevelWindow(final java.awt.Window source) {
  860. synchronized (windows) {
  861. windows.remove(source);
  862. }
  863. }
  864. /**
  865. * Returns a list of top level windows.
  866. *
  867. * @return Top level window list
  868. */
  869. public List<java.awt.Window> getTopLevelWindows() {
  870. synchronized (windows) {
  871. return windows;
  872. }
  873. }
  874. /**
  875. * Returns an instance of SwingController. This method is exported for use
  876. * in other plugins.
  877. *
  878. * @return A reference to this SwingController.
  879. */
  880. public UIController getController() {
  881. return this;
  882. }
  883. /**
  884. * Adds the specified menu item to the named parent menu, creating the
  885. * parent menu if required.
  886. *
  887. * @param parentMenu Parent menu name
  888. * @param menuItem Menu item to add
  889. */
  890. public void addMenuItem(final String parentMenu, final JMenuItem menuItem) {
  891. me.getJMenuBar().addMenuItem(parentMenu, menuItem);
  892. }
  893. }