Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CategoryPanel.java 8.0KB

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