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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.dialogs.prefs;
  18. import com.dmdirc.addons.ui_swing.components.IconManager;
  19. import com.dmdirc.config.prefs.PreferencesCategory;
  20. import java.awt.Dimension;
  21. import javax.annotation.Nullable;
  22. import javax.swing.BorderFactory;
  23. import javax.swing.Icon;
  24. import javax.swing.JLabel;
  25. import javax.swing.JList;
  26. import javax.swing.UIManager;
  27. import net.miginfocom.layout.PlatformDefaults;
  28. /**
  29. * Category Label.
  30. */
  31. public class CategoryLabel extends JLabel {
  32. /** Serial version UID. */
  33. private static final long serialVersionUID = -1659415238166842265L;
  34. /** Parent list. */
  35. @Nullable
  36. private final JList<? extends PreferencesCategory> parentList;
  37. /**
  38. * Creates a new category label.
  39. *
  40. * @param iconManager Icon manager
  41. * @param parentList Parent list
  42. * @param category Parent category
  43. * @param numCats Number of categories shown
  44. * @param index Index of this label
  45. */
  46. public CategoryLabel(final IconManager iconManager,
  47. @Nullable final JList<? extends PreferencesCategory> parentList,
  48. final PreferencesCategory category, final int numCats,
  49. final int index) {
  50. this.parentList = parentList;
  51. final int padding = (int) (1.5 * PlatformDefaults.getUnitValueX("related").getValue());
  52. setText(category.getTitle());
  53. new IconLoader(iconManager, this, category.getIcon()).execute();
  54. int level = 0;
  55. PreferencesCategory temp = category;
  56. while (temp.getParent() != null) {
  57. temp = temp.getParent();
  58. level++;
  59. }
  60. setPreferredSize(new Dimension(100000, Math.max(16, getFont().getSize())
  61. + padding));
  62. setBorder(BorderFactory.createEmptyBorder(padding / 2, padding + level
  63. * 18, padding / 2, padding));
  64. if (parentList != null) {
  65. setBackground(parentList.getBackground());
  66. setForeground(parentList.getForeground());
  67. }
  68. setOpaque(true);
  69. setToolTipText(null);
  70. if (category.getPath().equals(category.getTitle())) {
  71. boolean hasChildren = false;
  72. for (PreferencesCategory child : category.getSubcats()) {
  73. if (!child.isInline()) {
  74. hasChildren = true;
  75. break;
  76. }
  77. }
  78. hasChildren = hasChildren || index + 1 == numCats;
  79. setBackground(UIManager.getColor("ToolTip.background"));
  80. setForeground(UIManager.getColor("ToolTip.foreground"));
  81. setBorder(BorderFactory.createCompoundBorder(
  82. BorderFactory.createMatteBorder(1, 0, hasChildren ? 1 : 0,
  83. 0, UIManager.getColor("ToolTip.background").darker()
  84. .darker()), getBorder()));
  85. }
  86. }
  87. @Override
  88. public void setIcon(final Icon icon) {
  89. super.setIcon(icon);
  90. if (parentList != null) {
  91. parentList.repaint();
  92. }
  93. }
  94. }