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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. *
  3. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package com.dmdirc.addons.ui_swing.dialogs.prefs;
  24. import com.dmdirc.addons.ui_swing.UIUtilities;
  25. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  26. import com.dmdirc.addons.ui_swing.components.TitlePanel;
  27. import com.dmdirc.addons.ui_swing.components.ToolTipPanel;
  28. import com.dmdirc.config.prefs.PreferencesCategory;
  29. import java.awt.Window;
  30. import java.util.Collections;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import javax.swing.BorderFactory;
  34. import javax.swing.JPanel;
  35. import javax.swing.JScrollPane;
  36. import javax.swing.ScrollPaneConstants;
  37. import javax.swing.SwingWorker;
  38. import net.miginfocom.layout.ComponentWrapper;
  39. import net.miginfocom.layout.LayoutCallback;
  40. import net.miginfocom.layout.PlatformDefaults;
  41. import net.miginfocom.swing.MigLayout;
  42. /**
  43. * Panel representing a preferences category.
  44. */
  45. public class CategoryPanel extends JPanel {
  46. /**
  47. * A version number for this class. It should be changed whenever the
  48. * class structure is changed (or anything else that would prevent
  49. * serialized objects being unserialized with the new class).
  50. */
  51. private static final long serialVersionUID = -3268284364607758509L;
  52. /** Panel gap. */
  53. private final int padding = (int) PlatformDefaults.getUnitValueX("related").
  54. getValue();
  55. /** Panel left padding. */
  56. private final int leftPadding = (int) PlatformDefaults.getPanelInsets(1).
  57. getValue();
  58. /** Panel right padding. */
  59. private final int rightPadding = (int) PlatformDefaults.getPanelInsets(3).
  60. getValue();
  61. /** Active preferences category. */
  62. private PreferencesCategory category;
  63. /** Parent window. */
  64. private SwingPreferencesDialog parent;
  65. /** Title label. */
  66. private TitlePanel title;
  67. /** Tooltip display area. */
  68. private ToolTipPanel tooltip;
  69. /** Contents Panel. */
  70. private JScrollPane scrollPane;
  71. /** Loading panel. */
  72. private JPanel loading;
  73. /** Loading panel. */
  74. private JPanel nullCategory;
  75. /** Loading panel. */
  76. private JPanel waitingCategory;
  77. /** Category panel map. */
  78. private Map<PreferencesCategory, JPanel> panels;
  79. /** Category loading swing worker. */
  80. private SwingWorker worker;
  81. /** Waiting. */
  82. private boolean waiting;
  83. /**
  84. * Instantiates a new category panel.
  85. *
  86. * @param parent Parent window
  87. */
  88. public CategoryPanel(final SwingPreferencesDialog parent) {
  89. this(parent, null);
  90. }
  91. /**
  92. * Instantiates a new category panel.
  93. *
  94. * @param parent Parent window
  95. * @param category Initial category
  96. */
  97. public CategoryPanel(final SwingPreferencesDialog parent,
  98. final PreferencesCategory category) {
  99. super(new MigLayout("fillx, wrap, ins 0"));
  100. this.parent = parent;
  101. panels = Collections.synchronizedMap(
  102. new HashMap<PreferencesCategory, JPanel>());
  103. loading = new JPanel(new MigLayout("fillx"));
  104. loading.add(new TextLabel("Loading..."));
  105. nullCategory = new JPanel(new MigLayout("fillx"));
  106. nullCategory.add(new TextLabel("Please select a category."));
  107. waitingCategory = new JPanel(new MigLayout("fillx"));
  108. waitingCategory.add(new TextLabel("Please wait, loading..."));
  109. scrollPane = new JScrollPane(loading);
  110. scrollPane.setHorizontalScrollBarPolicy(
  111. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  112. scrollPane.getVerticalScrollBar().setUnitIncrement(15);
  113. title = new TitlePanel(BorderFactory.createEtchedBorder(),
  114. "Preferences");
  115. tooltip = new ToolTipPanel("Hover over a setting to see a " +
  116. "description, if available.");
  117. add(title, "pushx, growx, h 45!");
  118. add(scrollPane, "grow, push");
  119. add(tooltip, "pushx, growx, h 65!");
  120. panels.put(null, loading);
  121. setCategory(category);
  122. ((MigLayout) getLayout()).addLayoutCallback(new LayoutCallback() {
  123. /** {@inheritDoc} */
  124. @Override
  125. public void correctBounds(final ComponentWrapper cw) {
  126. if (cw.getComponent() == scrollPane) {
  127. parent.setPanelHeight((int) (scrollPane.getViewport().
  128. getExtentSize().height * 0.95));
  129. }
  130. }
  131. });
  132. }
  133. /**
  134. * Returns this categrory panel's parent window.
  135. *
  136. * @return Parent window
  137. */
  138. protected Window getParentWindow() {
  139. return parent;
  140. }
  141. /**
  142. * Returns the tooltip panel for this category panel.
  143. *
  144. * @return Tooltip panel
  145. */
  146. protected ToolTipPanel getToolTipPanel() {
  147. return tooltip;
  148. }
  149. /**
  150. * Informs the category panel a category has been loaded.
  151. *
  152. * @param loader Category loader
  153. * @param category Loaded category
  154. */
  155. protected void categoryLoaded(final PrefsCategoryLoader loader,
  156. final PreferencesCategory category) {
  157. panels.put(category, loader.getPanel());
  158. categoryLoaded(category);
  159. }
  160. private void categoryLoaded(final PreferencesCategory category) {
  161. if (this.category == category) {
  162. UIUtilities.invokeAndWait(new Runnable() {
  163. /** {@inheritDoc} */
  164. @Override
  165. public void run() {
  166. scrollPane.setViewportView(panels.get(category));
  167. if (category == null) {
  168. title.setText("Preferences");
  169. } else {
  170. title.setText(category.getPath());
  171. }
  172. }
  173. });
  174. }
  175. }
  176. /**
  177. * Sets the new active category for this panel and relays out.
  178. *
  179. * @param category New Category
  180. */
  181. public void setCategory(final PreferencesCategory category) {
  182. this.category = category;
  183. if (!panels.containsKey(category)) {
  184. UIUtilities.invokeAndWait(new Runnable() {
  185. @Override
  186. public void run() {
  187. scrollPane.setViewportView(loading);
  188. }
  189. });
  190. worker = new PrefsCategoryLoader(this, category);
  191. worker.execute();
  192. } else {
  193. categoryLoaded(category);
  194. }
  195. }
  196. /**
  197. * Sets this panel to a waiting to load state.
  198. *
  199. * @param b
  200. */
  201. public void setWaiting(boolean b) {
  202. waiting = b;
  203. scrollPane.setViewportView(waitingCategory);
  204. }
  205. }