Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CategoryPanel.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 java.util.concurrent.atomic.AtomicBoolean;
  34. import javax.swing.BorderFactory;
  35. import javax.swing.JPanel;
  36. import javax.swing.JScrollPane;
  37. import javax.swing.ScrollPaneConstants;
  38. import javax.swing.SwingWorker;
  39. import net.miginfocom.layout.ComponentWrapper;
  40. import net.miginfocom.layout.LayoutCallback;
  41. import net.miginfocom.layout.PlatformDefaults;
  42. import net.miginfocom.swing.MigLayout;
  43. /**
  44. * Panel representing a preferences category.
  45. */
  46. public class CategoryPanel extends JPanel {
  47. /**
  48. * A version number for this class. It should be changed whenever the
  49. * class structure is changed (or anything else that would prevent
  50. * serialized objects being unserialized with the new class).
  51. */
  52. private static final long serialVersionUID = -3268284364607758509L;
  53. /** Panel gap. */
  54. private final int padding = (int) PlatformDefaults.getUnitValueX("related").
  55. getValue();
  56. /** Panel left padding. */
  57. private final int leftPadding = (int) PlatformDefaults.getPanelInsets(1).
  58. getValue();
  59. /** Panel right padding. */
  60. private final int rightPadding = (int) PlatformDefaults.getPanelInsets(3).
  61. getValue();
  62. /** Active preferences category. */
  63. private PreferencesCategory category;
  64. /** Parent window. */
  65. private SwingPreferencesDialog parent;
  66. /** Title label. */
  67. private TitlePanel title;
  68. /** Tooltip display area. */
  69. private ToolTipPanel tooltip;
  70. /** Contents Panel. */
  71. private JScrollPane scrollPane;
  72. /** Loading panel. */
  73. private JPanel loading;
  74. /** Loading panel. */
  75. private JPanel nullCategory;
  76. /** Loading panel. */
  77. private JPanel waitingCategory;
  78. /** Category panel map. */
  79. private Map<PreferencesCategory, JPanel> panels;
  80. /** Category loading swing worker. */
  81. private SwingWorker worker;
  82. /** Waiting. */
  83. private boolean waiting;
  84. /**
  85. * Instantiates a new category panel.
  86. *
  87. * @param parent Parent window
  88. */
  89. public CategoryPanel(final SwingPreferencesDialog parent) {
  90. this(parent, null);
  91. }
  92. /**
  93. * Instantiates a new category panel.
  94. *
  95. * @param parent Parent window
  96. * @param category Initial category
  97. */
  98. public CategoryPanel(final SwingPreferencesDialog parent,
  99. final PreferencesCategory category) {
  100. super(new MigLayout("fillx, wrap, ins 0"));
  101. this.parent = parent;
  102. panels = Collections.synchronizedMap(
  103. new HashMap<PreferencesCategory, JPanel>());
  104. loading = new JPanel(new MigLayout("fillx"));
  105. loading.add(new TextLabel("Loading..."));
  106. nullCategory = new JPanel(new MigLayout("fillx"));
  107. nullCategory.add(new TextLabel("Please select a category."));
  108. waitingCategory = new JPanel(new MigLayout("fillx"));
  109. waitingCategory.add(new TextLabel("Please wait, loading..."));
  110. scrollPane = new JScrollPane(loading);
  111. scrollPane.setHorizontalScrollBarPolicy(
  112. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  113. scrollPane.getVerticalScrollBar().setUnitIncrement(15);
  114. title = new TitlePanel(BorderFactory.createEtchedBorder(),
  115. "Preferences");
  116. tooltip = new ToolTipPanel("Hover over a setting to see a " +
  117. "description, if available.");
  118. add(title, "pushx, growx, h 45!");
  119. add(scrollPane, "grow, push");
  120. add(tooltip, "pushx, growx, h 65!");
  121. panels.put(null, loading);
  122. setCategory(category);
  123. ((MigLayout) getLayout()).addLayoutCallback(new LayoutCallback() {
  124. /** {@inheritDoc} */
  125. @Override
  126. public void correctBounds(final ComponentWrapper cw) {
  127. if (cw.getComponent() == scrollPane) {
  128. parent.setPanelHeight((int) (scrollPane.getViewport().
  129. getExtentSize().height * 0.95));
  130. }
  131. }
  132. });
  133. }
  134. /**
  135. * Returns this categrory panel's parent window.
  136. *
  137. * @return Parent window
  138. */
  139. protected Window getParentWindow() {
  140. return parent;
  141. }
  142. /**
  143. * Returns the tooltip panel for this category panel.
  144. *
  145. * @return Tooltip panel
  146. */
  147. protected ToolTipPanel getToolTipPanel() {
  148. return tooltip;
  149. }
  150. /**
  151. * Informs the category panel a category has been loaded.
  152. *
  153. * @param loader Category loader
  154. * @param category Loaded category
  155. */
  156. protected void categoryLoaded(final PrefsCategoryLoader loader,
  157. final PreferencesCategory category) {
  158. panels.put(category, loader.getPanel());
  159. categoryLoaded(category);
  160. }
  161. private void categoryLoaded(final PreferencesCategory category) {
  162. if (this.category == category) {
  163. UIUtilities.invokeAndWait(new Runnable() {
  164. /** {@inheritDoc} */
  165. @Override
  166. public void run() {
  167. scrollPane.setViewportView(panels.get(category));
  168. if (category == null) {
  169. title.setText("Preferences");
  170. } else {
  171. title.setText(category.getPath());
  172. }
  173. }
  174. });
  175. }
  176. }
  177. /**
  178. * Sets the new active category for this panel and relays out.
  179. *
  180. * @param category New Category
  181. */
  182. public void setCategory(final PreferencesCategory category) {
  183. this.category = category;
  184. if (!panels.containsKey(category)) {
  185. UIUtilities.invokeAndWait(new Runnable() {
  186. @Override
  187. public void run() {
  188. scrollPane.setViewportView(loading);
  189. }
  190. });
  191. worker = new PrefsCategoryLoader(this, category);
  192. worker.execute();
  193. } else {
  194. categoryLoaded(category);
  195. }
  196. }
  197. /**
  198. * Sets this panel to a waiting to load state.
  199. *
  200. * @param b
  201. */
  202. public void setWaiting(boolean b) {
  203. waiting = b;
  204. scrollPane.setViewportView(waitingCategory);
  205. }
  206. }