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.

CategoryLabel.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.components.IconManager;
  24. import com.dmdirc.config.prefs.PreferencesCategory;
  25. import java.awt.Dimension;
  26. import javax.annotation.Nullable;
  27. import javax.swing.BorderFactory;
  28. import javax.swing.Icon;
  29. import javax.swing.JLabel;
  30. import javax.swing.JList;
  31. import javax.swing.UIManager;
  32. import net.miginfocom.layout.PlatformDefaults;
  33. /**
  34. * Category Label.
  35. */
  36. public class CategoryLabel extends JLabel {
  37. /** Serial version UID. */
  38. private static final long serialVersionUID = -1659415238166842265L;
  39. /** Parent list. */
  40. @Nullable
  41. private final JList<? extends PreferencesCategory> parentList;
  42. /**
  43. * Creates a new category label.
  44. *
  45. * @param iconManager Icon manager
  46. * @param parentList Parent list
  47. * @param category Parent category
  48. * @param numCats Number of categories shown
  49. * @param index Index of this label
  50. */
  51. public CategoryLabel(final IconManager iconManager,
  52. @Nullable final JList<? extends PreferencesCategory> parentList,
  53. final PreferencesCategory category, final int numCats,
  54. final int index) {
  55. this.parentList = parentList;
  56. final int padding = (int) (1.5 * PlatformDefaults.getUnitValueX("related").getValue());
  57. setText(category.getTitle());
  58. new IconLoader(iconManager, this, category.getIcon()).execute();
  59. int level = 0;
  60. PreferencesCategory temp = category;
  61. while (temp.getParent() != null) {
  62. temp = temp.getParent();
  63. level++;
  64. }
  65. setPreferredSize(new Dimension(100000, Math.max(16, getFont().getSize())
  66. + padding));
  67. setBorder(BorderFactory.createEmptyBorder(padding / 2, padding + level
  68. * 18, padding / 2, padding));
  69. if (parentList != null) {
  70. setBackground(parentList.getBackground());
  71. setForeground(parentList.getForeground());
  72. }
  73. setOpaque(true);
  74. setToolTipText(null);
  75. if (category.getPath().equals(category.getTitle())) {
  76. boolean hasChildren = false;
  77. for (PreferencesCategory child : category.getSubcats()) {
  78. if (!child.isInline()) {
  79. hasChildren = true;
  80. break;
  81. }
  82. }
  83. hasChildren = hasChildren || index + 1 == numCats;
  84. setBackground(UIManager.getColor("ToolTip.background"));
  85. setForeground(UIManager.getColor("ToolTip.foreground"));
  86. setBorder(BorderFactory.createCompoundBorder(
  87. BorderFactory.createMatteBorder(1, 0, hasChildren ? 1 : 0,
  88. 0, UIManager.getColor("ToolTip.background").darker()
  89. .darker()), getBorder()));
  90. }
  91. }
  92. @Override
  93. public void setIcon(final Icon icon) {
  94. super.setIcon(icon);
  95. if (parentList != null) {
  96. parentList.repaint();
  97. }
  98. }
  99. }