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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.dialogs.prefs;
  18. import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
  19. import com.dmdirc.addons.ui_swing.UIUtilities;
  20. import com.dmdirc.addons.ui_swing.components.IconManager;
  21. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  22. import com.dmdirc.addons.ui_swing.components.TitlePanel;
  23. import com.dmdirc.addons.ui_swing.components.ToolTipPanel;
  24. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  25. import com.dmdirc.config.prefs.PreferencesCategory;
  26. import java.util.Arrays;
  27. import java.util.Collections;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import javax.inject.Inject;
  31. import javax.swing.BorderFactory;
  32. import javax.swing.JPanel;
  33. import javax.swing.JScrollPane;
  34. import javax.swing.ScrollPaneConstants;
  35. import javax.swing.SwingUtilities;
  36. import net.miginfocom.swing.MigLayout;
  37. /**
  38. * Panel representing a preferences category.
  39. */
  40. public class CategoryPanel extends JPanel {
  41. /** Serial version UID. */
  42. private static final long serialVersionUID = -3268284364607758509L;
  43. /** Active preferences category. */
  44. private PreferencesCategory category;
  45. /** Title label. */
  46. private final TitlePanel title;
  47. /** Tooltip display area. */
  48. private final ToolTipPanel tooltip;
  49. /** Contents Panel. */
  50. private final JScrollPane scrollPane;
  51. /** Loading panel. */
  52. private final JPanel loading;
  53. /** Loading panel. */
  54. private final JPanel nullCategory;
  55. /** Loading panel. */
  56. private final JPanel waitingCategory;
  57. /** Category panel map. */
  58. private final Map<PreferencesCategory, JPanel> panels;
  59. /** Category loading swing worker. */
  60. private LoggingSwingWorker<JPanel, Object> worker;
  61. /** Prefs component factory. */
  62. private final PrefsComponentFactory factory;
  63. /**
  64. * Instantiates a new category panel.
  65. *
  66. * @param factory Prefs component factory instance
  67. * @param iconManager Icon manager
  68. */
  69. @Inject
  70. public CategoryPanel(
  71. final PrefsComponentFactory factory,
  72. final IconManager iconManager) {
  73. this(factory, iconManager, null);
  74. }
  75. /**
  76. * Instantiates a new category panel.
  77. *
  78. * @param factory Prefs component factory instance
  79. * @param iconManager Icon manager
  80. * @param category Initial category
  81. */
  82. public CategoryPanel(
  83. final PrefsComponentFactory factory,
  84. final IconManager iconManager,
  85. final PreferencesCategory category) {
  86. super(new MigLayout("fillx, wrap, ins 0"));
  87. this.factory = factory;
  88. panels = Collections.synchronizedMap(new HashMap<>());
  89. loading = new JPanel(new MigLayout("fillx"));
  90. loading.add(new TextLabel("Loading..."));
  91. nullCategory = new JPanel(new MigLayout("fillx"));
  92. nullCategory.add(new TextLabel("Please select a category."));
  93. waitingCategory = new JPanel(new MigLayout("fillx"));
  94. waitingCategory.add(new TextLabel("Please wait, loading..."));
  95. scrollPane = new JScrollPane(loading);
  96. scrollPane.setHorizontalScrollBarPolicy(
  97. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  98. scrollPane.getVerticalScrollBar().setUnitIncrement(15);
  99. title = new TitlePanel(BorderFactory.createEtchedBorder(),
  100. "Preferences");
  101. tooltip = new ToolTipPanel(iconManager,
  102. "Hover over a setting to see a description, if available.");
  103. add(title, "pushx, growx, h 45!");
  104. add(scrollPane, "grow, push");
  105. add(tooltip, "pushx, growx, h 70!");
  106. panels.put(null, loading);
  107. setCategory(category);
  108. }
  109. /**
  110. * Returns the tooltip panel for this category panel.
  111. *
  112. * @return Tooltip panel
  113. */
  114. protected ToolTipPanel getToolTipPanel() {
  115. return tooltip;
  116. }
  117. /**
  118. * Informs the category panel a category has been loaded.
  119. *
  120. * @param loader Category loader
  121. * @param category Loaded category
  122. */
  123. protected void categoryLoaded(final PrefsCategoryLoader loader,
  124. final PreferencesCategory category) {
  125. panels.put(category, loader.getPanel());
  126. categoryLoaded(category);
  127. }
  128. private void categoryLoaded(final PreferencesCategory category) {
  129. if (this.category == category) {
  130. UIUtilities.invokeAndWait(() -> {
  131. final JPanel panel = panels.get(category);
  132. scrollPane.setViewportView(panel);
  133. //Hack around mig bug
  134. panel.invalidate();
  135. panel.validate();
  136. Arrays.stream(panel.getComponents()).filter(c -> c instanceof JPanel)
  137. .forEach(c -> {
  138. c.invalidate();
  139. c.validate();
  140. });
  141. //And for good measure, hack the crap out of it some more :(
  142. SwingUtilities.invokeLater(() -> {
  143. panel.invalidate();
  144. panel.validate();
  145. Arrays.stream(panel.getComponents()).filter(c -> c instanceof JPanel)
  146. .forEach(c -> {
  147. c.invalidate();
  148. c.validate();
  149. });
  150. });
  151. if (category == null) {
  152. title.setText("Preferences");
  153. } else {
  154. title.setText(category.getPath());
  155. }
  156. });
  157. }
  158. }
  159. /**
  160. * Sets the new active category for this panel and relays out.
  161. *
  162. * @param category New Category
  163. */
  164. public void setCategory(final PreferencesCategory category) {
  165. this.category = category;
  166. if (category == null) {
  167. tooltip.setWarning(null);
  168. } else {
  169. tooltip.setWarning(category.getWarning());
  170. }
  171. if (panels.containsKey(category)) {
  172. categoryLoaded(category);
  173. } else {
  174. UIUtilities.invokeAndWait(() -> scrollPane.setViewportView(loading));
  175. worker = new PrefsCategoryLoader(factory, this, category);
  176. worker.execute();
  177. }
  178. }
  179. /**
  180. * Sets this panel to a waiting to load state.
  181. *
  182. * @param b Loading state
  183. */
  184. public void setWaiting(final boolean b) {
  185. UIUtilities.invokeLater(() -> scrollPane.setViewportView(waitingCategory));
  186. }
  187. /**
  188. * Displays an error panel to the end user.
  189. *
  190. * @param message Message to display
  191. */
  192. public void setError(final String message) {
  193. UIUtilities.invokeLater(() -> {
  194. final JPanel panel = new JPanel(new MigLayout("fillx"));
  195. panel.add(new TextLabel("An error has occurred loading the "
  196. + "preferences dialog, an error has been raised: "),
  197. "wrap");
  198. panel.add(new TextLabel(message));
  199. scrollPane.setViewportView(panel);
  200. });
  201. }
  202. }