|
@@ -0,0 +1,158 @@
|
|
1
|
+/*
|
|
2
|
+ *
|
|
3
|
+ * Copyright (c) 2006-2008 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
|
+
|
|
24
|
+package com.dmdirc.addons.ui_swing.dialogs.prefs;
|
|
25
|
+
|
|
26
|
+import com.dmdirc.config.prefs.PreferencesCategory;
|
|
27
|
+import com.dmdirc.logger.ErrorLevel;
|
|
28
|
+import com.dmdirc.logger.Logger;
|
|
29
|
+import com.dmdirc.ui.IconManager;
|
|
30
|
+import java.awt.Color;
|
|
31
|
+import java.awt.Dimension;
|
|
32
|
+import java.util.concurrent.ExecutionException;
|
|
33
|
+import javax.swing.BorderFactory;
|
|
34
|
+import javax.swing.Icon;
|
|
35
|
+import javax.swing.JLabel;
|
|
36
|
+import javax.swing.JList;
|
|
37
|
+import javax.swing.SwingUtilities;
|
|
38
|
+import javax.swing.SwingWorker;
|
|
39
|
+import net.miginfocom.layout.PlatformDefaults;
|
|
40
|
+
|
|
41
|
+/**
|
|
42
|
+ * Category Label.
|
|
43
|
+ */
|
|
44
|
+public class CategoryLabel extends JLabel {
|
|
45
|
+
|
|
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 = -1659415238166842265L;
|
|
52
|
+ /** Panel gap. */
|
|
53
|
+ private final int padding = (int) (1.5 * PlatformDefaults.getUnitValueX(
|
|
54
|
+ "related").getValue());
|
|
55
|
+ private PreferencesCategory category;
|
|
56
|
+ private JList parentList;
|
|
57
|
+ private int index;
|
|
58
|
+
|
|
59
|
+ /**
|
|
60
|
+ *
|
|
61
|
+ * @param parentList
|
|
62
|
+ * @param category
|
|
63
|
+ * @param index
|
|
64
|
+ */
|
|
65
|
+ public CategoryLabel(final JList parentList,
|
|
66
|
+ final PreferencesCategory category, final int index) {
|
|
67
|
+ this.parentList = parentList;
|
|
68
|
+ this.category = category;
|
|
69
|
+ this.index = index;
|
|
70
|
+
|
|
71
|
+ setText(category.getTitle());
|
|
72
|
+ new IconLoader(this, category.getIcon()).execute();
|
|
73
|
+
|
|
74
|
+ int level = 0;
|
|
75
|
+ PreferencesCategory temp = category;
|
|
76
|
+ while (temp.getParent() != null) {
|
|
77
|
+ temp = temp.getParent();
|
|
78
|
+ level++;
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ setPreferredSize(new Dimension(100000, Math.max(16,
|
|
82
|
+ getFont().getSize()) + padding));
|
|
83
|
+ setBorder(BorderFactory.createEmptyBorder(padding / 2, padding + level *
|
|
84
|
+ 18, padding / 2,
|
|
85
|
+ padding));
|
|
86
|
+ setBackground(parentList.getBackground());
|
|
87
|
+ setForeground(parentList.getForeground());
|
|
88
|
+ setOpaque(true);
|
|
89
|
+ setToolTipText(null);
|
|
90
|
+
|
|
91
|
+ if (category.getPath().equals(category.getTitle())) {
|
|
92
|
+ boolean hasChildren = false;
|
|
93
|
+ for (PreferencesCategory child : category.getSubcats()) {
|
|
94
|
+ if (!child.isInline()) {
|
|
95
|
+ hasChildren = true;
|
|
96
|
+ break;
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ hasChildren = hasChildren || index + 1 == parentList.getModel().
|
|
101
|
+ getSize();
|
|
102
|
+
|
|
103
|
+ setBackground(Color.LIGHT_GRAY);
|
|
104
|
+ setBorder(BorderFactory.createCompoundBorder(
|
|
105
|
+ BorderFactory.createMatteBorder(1, 0, hasChildren ? 1 : 0, 0,
|
|
106
|
+ Color.GRAY),
|
|
107
|
+ getBorder()));
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ /**
|
|
114
|
+ * {@inheritDoc}
|
|
115
|
+ *
|
|
116
|
+ * @param icon New icon
|
|
117
|
+ */
|
|
118
|
+ @Override
|
|
119
|
+ public void setIcon(Icon icon) {
|
|
120
|
+ super.setIcon(icon);
|
|
121
|
+
|
|
122
|
+ if (parentList != null) {
|
|
123
|
+ parentList.repaint();
|
|
124
|
+ }
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ private class IconLoader extends SwingWorker<Icon, Void> {
|
|
128
|
+
|
|
129
|
+ private CategoryLabel label;
|
|
130
|
+ private String icon;
|
|
131
|
+
|
|
132
|
+ public IconLoader(final CategoryLabel label, final String icon) {
|
|
133
|
+ this.label = label;
|
|
134
|
+ this.icon = icon;
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ /** {@inheritDoc} */
|
|
138
|
+ @Override
|
|
139
|
+ protected Icon doInBackground() throws Exception {
|
|
140
|
+ return IconManager.getIconManager().getIcon(icon);
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ /** {@inheritDoc} */
|
|
144
|
+ @Override
|
|
145
|
+ protected void done() {
|
|
146
|
+ try {
|
|
147
|
+ label.setIcon(get());
|
|
148
|
+ } catch (InterruptedException ex) {
|
|
149
|
+ Logger.appError(ErrorLevel.LOW,
|
|
150
|
+ "Unable to set icon for prefs cell", ex);
|
|
151
|
+ } catch (ExecutionException ex) {
|
|
152
|
+ Logger.appError(ErrorLevel.LOW, ex.getMessage(), ex);
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ }
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+}
|