123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- /*
- * Copyright (c) 2006-2017 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
- * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
- * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.dialogs.prefs;
-
- import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
- import com.dmdirc.addons.ui_swing.UIUtilities;
- import com.dmdirc.addons.ui_swing.components.IconManager;
- import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
- import com.dmdirc.addons.ui_swing.components.TitlePanel;
- import com.dmdirc.addons.ui_swing.components.ToolTipPanel;
- import com.dmdirc.addons.ui_swing.components.text.TextLabel;
- import com.dmdirc.config.prefs.PreferencesCategory;
-
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.inject.Inject;
- import javax.swing.BorderFactory;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.ScrollPaneConstants;
- import javax.swing.SwingUtilities;
-
- import net.miginfocom.swing.MigLayout;
-
- /**
- * Panel representing a preferences category.
- */
- public class CategoryPanel extends JPanel {
-
- /** Serial version UID. */
- private static final long serialVersionUID = -3268284364607758509L;
- /** Active preferences category. */
- private PreferencesCategory category;
- /** Title label. */
- private final TitlePanel title;
- /** Tooltip display area. */
- private final ToolTipPanel tooltip;
- /** Contents Panel. */
- private final JScrollPane scrollPane;
- /** Loading panel. */
- private final JPanel loading;
- /** Loading panel. */
- private final JPanel nullCategory;
- /** Loading panel. */
- private final JPanel waitingCategory;
- /** Category panel map. */
- private final Map<PreferencesCategory, JPanel> panels;
- /** Category loading swing worker. */
- private LoggingSwingWorker<JPanel, Object> worker;
- /** Prefs component factory. */
- private final PrefsComponentFactory factory;
-
- /**
- * Instantiates a new category panel.
- *
- * @param factory Prefs component factory instance
- * @param iconManager Icon manager
- */
- @Inject
- public CategoryPanel(
- final PrefsComponentFactory factory,
- final IconManager iconManager) {
- this(factory, iconManager, null);
- }
-
- /**
- * Instantiates a new category panel.
- *
- * @param factory Prefs component factory instance
- * @param iconManager Icon manager
- * @param category Initial category
- */
- public CategoryPanel(
- final PrefsComponentFactory factory,
- final IconManager iconManager,
- final PreferencesCategory category) {
- super(new MigLayout("fillx, wrap, ins 0"));
- this.factory = factory;
-
- panels = Collections.synchronizedMap(new HashMap<>());
-
- loading = new JPanel(new MigLayout("fillx"));
- loading.add(new TextLabel("Loading..."));
-
- nullCategory = new JPanel(new MigLayout("fillx"));
- nullCategory.add(new TextLabel("Please select a category."));
-
- waitingCategory = new JPanel(new MigLayout("fillx"));
- waitingCategory.add(new TextLabel("Please wait, loading..."));
-
- scrollPane = new JScrollPane(loading);
- scrollPane.setHorizontalScrollBarPolicy(
- ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
- scrollPane.getVerticalScrollBar().setUnitIncrement(15);
-
- title = new TitlePanel(BorderFactory.createEtchedBorder(),
- "Preferences");
- tooltip = new ToolTipPanel(iconManager,
- "Hover over a setting to see a description, if available.");
-
- add(title, "pushx, growx, h 45!");
- add(scrollPane, "grow, push");
- add(tooltip, "pushx, growx, h 70!");
-
- panels.put(null, loading);
- setCategory(category);
- }
-
- /**
- * Returns the tooltip panel for this category panel.
- *
- * @return Tooltip panel
- */
- protected ToolTipPanel getToolTipPanel() {
- return tooltip;
- }
-
- /**
- * Informs the category panel a category has been loaded.
- *
- * @param loader Category loader
- * @param category Loaded category
- */
- protected void categoryLoaded(final PrefsCategoryLoader loader,
- final PreferencesCategory category) {
- panels.put(category, loader.getPanel());
- categoryLoaded(category);
- }
-
- private void categoryLoaded(final PreferencesCategory category) {
- if (this.category == category) {
- UIUtilities.invokeAndWait(() -> {
- final JPanel panel = panels.get(category);
- scrollPane.setViewportView(panel);
- //Hack around mig bug
- panel.invalidate();
- panel.validate();
- Arrays.stream(panel.getComponents()).filter(c -> c instanceof JPanel)
- .forEach(c -> {
- c.invalidate();
- c.validate();
- });
- //And for good measure, hack the crap out of it some more :(
- SwingUtilities.invokeLater(() -> {
- panel.invalidate();
- panel.validate();
- Arrays.stream(panel.getComponents()).filter(c -> c instanceof JPanel)
- .forEach(c -> {
- c.invalidate();
- c.validate();
- });
- });
- if (category == null) {
- title.setText("Preferences");
- } else {
- title.setText(category.getPath());
- }
- });
- }
- }
-
- /**
- * Sets the new active category for this panel and relays out.
- *
- * @param category New Category
- */
- public void setCategory(final PreferencesCategory category) {
- this.category = category;
-
- if (category == null) {
- tooltip.setWarning(null);
- } else {
- tooltip.setWarning(category.getWarning());
- }
-
- if (panels.containsKey(category)) {
- categoryLoaded(category);
- } else {
- UIUtilities.invokeAndWait(() -> scrollPane.setViewportView(loading));
-
- worker = new PrefsCategoryLoader(factory, this, category);
- worker.execute();
- }
- }
-
- /**
- * Sets this panel to a waiting to load state.
- *
- * @param b Loading state
- */
- public void setWaiting(final boolean b) {
- UIUtilities.invokeLater(() -> scrollPane.setViewportView(waitingCategory));
- }
-
- /**
- * Displays an error panel to the end user.
- *
- * @param message Message to display
- */
- public void setError(final String message) {
- UIUtilities.invokeLater(() -> {
- final JPanel panel = new JPanel(new MigLayout("fillx"));
- panel.add(new TextLabel("An error has occurred loading the "
- + "preferences dialog, an error has been raised: "),
- "wrap");
- panel.add(new TextLabel(message));
- scrollPane.setViewportView(panel);
- });
- }
-
- }
|