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.

CategoryPanel.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.dialogs.prefs;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
  26. import com.dmdirc.addons.ui_swing.UIUtilities;
  27. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  28. import com.dmdirc.addons.ui_swing.components.TitlePanel;
  29. import com.dmdirc.addons.ui_swing.components.ToolTipPanel;
  30. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  31. import com.dmdirc.config.prefs.PreferencesCategory;
  32. import com.dmdirc.ui.IconManager;
  33. import java.util.Arrays;
  34. import java.util.Collections;
  35. import java.util.HashMap;
  36. import java.util.Map;
  37. import javax.inject.Inject;
  38. import javax.swing.BorderFactory;
  39. import javax.swing.JPanel;
  40. import javax.swing.JScrollPane;
  41. import javax.swing.ScrollPaneConstants;
  42. import javax.swing.SwingUtilities;
  43. import net.miginfocom.swing.MigLayout;
  44. /**
  45. * Panel representing a preferences category.
  46. */
  47. public class CategoryPanel extends JPanel {
  48. /** Serial version UID. */
  49. private static final long serialVersionUID = -3268284364607758509L;
  50. /** Active preferences category. */
  51. private PreferencesCategory category;
  52. /** Title label. */
  53. private final TitlePanel title;
  54. /** Tooltip display area. */
  55. private final ToolTipPanel tooltip;
  56. /** Contents Panel. */
  57. private final JScrollPane scrollPane;
  58. /** Loading panel. */
  59. private final JPanel loading;
  60. /** Loading panel. */
  61. private final JPanel nullCategory;
  62. /** Loading panel. */
  63. private final JPanel waitingCategory;
  64. /** Category panel map. */
  65. private final Map<PreferencesCategory, JPanel> panels;
  66. /** Category loading swing worker. */
  67. private LoggingSwingWorker<JPanel, Object> worker;
  68. /** Prefs component factory. */
  69. private final PrefsComponentFactory factory;
  70. /** The event bus to post errors to. */
  71. private final DMDircMBassador eventBus;
  72. /**
  73. * Instantiates a new category panel.
  74. *
  75. * @param eventBus The event bus to post errors to
  76. * @param factory Prefs component factory instance
  77. * @param iconManager Icon manager
  78. */
  79. @Inject
  80. public CategoryPanel(
  81. final DMDircMBassador eventBus,
  82. final PrefsComponentFactory factory,
  83. @GlobalConfig final IconManager iconManager) {
  84. this(eventBus, factory, iconManager, null);
  85. }
  86. /**
  87. * Instantiates a new category panel.
  88. *
  89. * @param eventBus The event bus to post errors to
  90. * @param factory Prefs component factory instance
  91. * @param iconManager Icon manager
  92. * @param category Initial category
  93. */
  94. public CategoryPanel(
  95. final DMDircMBassador eventBus, final PrefsComponentFactory factory,
  96. final IconManager iconManager,
  97. final PreferencesCategory category) {
  98. super(new MigLayout("fillx, wrap, ins 0"));
  99. this.factory = factory;
  100. this.eventBus = eventBus;
  101. panels = Collections.synchronizedMap(new HashMap<PreferencesCategory, JPanel>());
  102. loading = new JPanel(new MigLayout("fillx"));
  103. loading.add(new TextLabel("Loading..."));
  104. nullCategory = new JPanel(new MigLayout("fillx"));
  105. nullCategory.add(new TextLabel("Please select a category."));
  106. waitingCategory = new JPanel(new MigLayout("fillx"));
  107. waitingCategory.add(new TextLabel("Please wait, loading..."));
  108. scrollPane = new JScrollPane(loading);
  109. scrollPane.setHorizontalScrollBarPolicy(
  110. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  111. scrollPane.getVerticalScrollBar().setUnitIncrement(15);
  112. title = new TitlePanel(BorderFactory.createEtchedBorder(),
  113. "Preferences");
  114. tooltip = new ToolTipPanel(iconManager,
  115. "Hover over a setting to see a description, if available.");
  116. add(title, "pushx, growx, h 45!");
  117. add(scrollPane, "grow, push");
  118. add(tooltip, "pushx, growx, h 70!");
  119. panels.put(null, loading);
  120. setCategory(category);
  121. }
  122. /**
  123. * Returns the tooltip panel for this category panel.
  124. *
  125. * @return Tooltip panel
  126. */
  127. protected ToolTipPanel getToolTipPanel() {
  128. return tooltip;
  129. }
  130. /**
  131. * Informs the category panel a category has been loaded.
  132. *
  133. * @param loader Category loader
  134. * @param category Loaded category
  135. */
  136. protected void categoryLoaded(final PrefsCategoryLoader loader,
  137. final PreferencesCategory category) {
  138. panels.put(category, loader.getPanel());
  139. categoryLoaded(category);
  140. }
  141. private void categoryLoaded(final PreferencesCategory category) {
  142. if (this.category == category) {
  143. UIUtilities.invokeAndWait(() -> {
  144. final JPanel panel = panels.get(category);
  145. scrollPane.setViewportView(panel);
  146. //Hack around mig bug
  147. panel.invalidate();
  148. panel.validate();
  149. Arrays.stream(panel.getComponents()).filter(c -> c instanceof JPanel)
  150. .forEach(c -> {
  151. c.invalidate();
  152. c.validate();
  153. });
  154. //And for good measure, hack the crap out of it some more :(
  155. SwingUtilities.invokeLater(() -> {
  156. panel.invalidate();
  157. panel.validate();
  158. Arrays.stream(panel.getComponents()).filter(c -> c instanceof JPanel)
  159. .forEach(c -> {
  160. c.invalidate();
  161. c.validate();
  162. });
  163. });
  164. if (category == null) {
  165. title.setText("Preferences");
  166. } else {
  167. title.setText(category.getPath());
  168. }
  169. });
  170. }
  171. }
  172. /**
  173. * Sets the new active category for this panel and relays out.
  174. *
  175. * @param category New Category
  176. */
  177. public void setCategory(final PreferencesCategory category) {
  178. this.category = category;
  179. if (category == null) {
  180. tooltip.setWarning(null);
  181. } else {
  182. tooltip.setWarning(category.getWarning());
  183. }
  184. if (panels.containsKey(category)) {
  185. categoryLoaded(category);
  186. } else {
  187. UIUtilities.invokeAndWait(() -> scrollPane.setViewportView(loading));
  188. worker = new PrefsCategoryLoader(factory, eventBus, this, category);
  189. worker.execute();
  190. }
  191. }
  192. /**
  193. * Sets this panel to a waiting to load state.
  194. *
  195. * @param b Loading state
  196. */
  197. public void setWaiting(final boolean b) {
  198. UIUtilities.invokeLater(() -> scrollPane.setViewportView(waitingCategory));
  199. }
  200. /**
  201. * Displays an error panel to the end user.
  202. *
  203. * @param message Message to display
  204. */
  205. public void setError(final String message) {
  206. UIUtilities.invokeLater(() -> {
  207. final JPanel panel = new JPanel(new MigLayout("fillx"));
  208. panel.add(new TextLabel("An error has occurred loading the "
  209. + "preferences dialog, an error has been raised: "),
  210. "wrap");
  211. panel.add(new TextLabel(message));
  212. scrollPane.setViewportView(panel);
  213. });
  214. }
  215. }