File Source: TreeViewTreeCellRenderer.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewTreeCellRenderer__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
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
23 package com.dmdirc.addons.ui_swing.framemanager.tree;
24
25 import com.dmdirc.interfaces.ConfigChangeListener;
26 import com.dmdirc.config.ConfigManager;
27 import com.dmdirc.config.IdentityManager;
28
29 import java.awt.Color;
30 import java.awt.Component;
31 import java.awt.Font;
32
33 import javax.swing.JLabel;
34 import javax.swing.JTree;
35 import javax.swing.tree.TreeCellRenderer;
36
37 /**
38 * Displays a node in a tree according to its type.
39 */
40 public class TreeViewTreeCellRenderer implements TreeCellRenderer,
41 ConfigChangeListener {
42
43 /**
44 * A version number for this class. It should be changed whenever the class
45 * structure is changed (or anything else that would prevent serialized
46 * objects being unserialized with the new class).
47 */
48 private static final long serialVersionUID = 3;
49 /** Parent frame manager. */
50 private final TreeFrameManager manager;
51 /** Config manager. */
52 private final ConfigManager config;
53 /** Rollover colours. */
54 private Color rolloverColour;
55 /** Active bold. */
56 private boolean activeBold;
57 /** Active background. */
58 private Color activeBackground;
59 /** Active foreground. */
60 private Color activeForeground;
61
62 /**
63 * Creates a new instance of TreeViewTreeCellRenderer.
64 *
65 * @param manager Parent TreeFrameManager
66 */
/*
P/P * Method: void com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewTreeCellRenderer(TreeFrameManager)
*
* Preconditions:
* manager != null
* manager.tree != null
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* this.config.listeners != null
* this.config.sources != null
*
* Postconditions:
* com/dmdirc/config/IdentityManager.globalconfig == One-of{old com/dmdirc/config/IdentityManager.globalconfig, &new ConfigManager(getGlobalConfig#1)}
* com/dmdirc/config/IdentityManager.globalconfig != null
* this.config == com/dmdirc/config/IdentityManager.globalconfig
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(this.activeBackground)
* init'ed(this.activeBold)
* init'ed(this.activeForeground)
* this.manager == manager
* this.manager != null
* init'ed(this.rolloverColour)
* ...
*/
67 public TreeViewTreeCellRenderer(final TreeFrameManager manager) {
68 this.manager = manager;
69
70 config = IdentityManager.getGlobalConfig();
71
72 setColours();
73
74 config.addChangeListener("ui", this);
75 config.addChangeListener("treeview", this);
76 }
77
78 /**
79 * Configures the renderer based on the passed parameters.
80 *
81 * @param tree JTree for this renderer.
82 * @param value node to be renderered.
83 * @param sel whether the node is selected.
84 * @param expanded whether the node is expanded.
85 * @param leaf whether the node is a leaf.
86 * @param row the node's row.
87 * @param hasFocus whether the node has focus.
88 *
89 * @return RendererComponent for this node.
90 */
91 @Override
92 public final Component getTreeCellRendererComponent(final JTree tree,
93 final Object value, final boolean sel, final boolean expanded,
94 final boolean leaf, final int row, final boolean hasFocus) {
95
/*
P/P * Method: Component getTreeCellRendererComponent(JTree, Object, bool, bool, bool, int, bool)
*
* Preconditions:
* (soft) init'ed(this.activeBackground)
* (soft) init'ed(this.activeBold)
* (soft) init'ed(this.activeForeground)
* (soft) init'ed(this.rolloverColour)
* (soft) tree != null
* (soft) init'ed(value.label)
* (soft) init'ed(value.label.notificationColour)
* (soft) init'ed(value.label.rollover)
* (soft) init'ed(value.label.selected)
*
* Presumptions:
* com.dmdirc.addons.ui_swing.framemanager.tree.NodeLabel:getFont(...)@118 != null
* com.dmdirc.addons.ui_swing.framemanager.tree.NodeLabel:getFont(...)@120 != null
* com.dmdirc.addons.ui_swing.framemanager.tree.NodeLabel:getFont(...)@125 != null
*
* Postconditions:
* return_value == One-of{&new JLabel(getTreeCellRendererComponent#1), &new JLabel(getTreeCellRendererComponent#2), value.label}
* return_value != null
* new JLabel(getTreeCellRendererComponent#1) num objects <= 1
* new JLabel(getTreeCellRendererComponent#2) num objects <= 1
*
* Test Vectors:
* this.activeBold: {0}, {1}
* value: Inverse{null}, Addr_Set{null}
* value.label: Inverse{null}, Addr_Set{null}
* value.label.notificationColour: Addr_Set{null}, Inverse{null}
* value.label.rollover: {0}, {1}
* value.label.selected: {0}, {1}
*/
96 if (value == null) {
97 return new JLabel("Node == null");
98 }
99 final NodeLabel label = ((TreeViewNode) value).getLabel();
100 if (label == null) {
101 return new JLabel("Label == null");
102 }
103
104 label.setBackground(tree.getBackground());
105 label.setForeground(tree.getForeground());
106
107 if (label.isRollover()) {
108 label.setBackground(rolloverColour);
109 }
110
111 final Color colour = label.getNotificationColour();
112 if (colour != null) {
113 label.setForeground(colour);
114 }
115
116 if (label.isSelected()) {
117 if (activeBold) {
118 label.setFont(label.getFont().deriveFont(Font.BOLD));
119 } else {
120 label.setFont(label.getFont().deriveFont(Font.PLAIN));
121 }
122 label.setBackground(activeBackground);
123 label.setForeground(activeForeground);
124 } else {
125 label.setFont(label.getFont().deriveFont(Font.PLAIN));
126 }
127
128 return label;
129 }
130
131 /** Sets the colours for the renderer. */
132 private void setColours() {
/*
P/P * Method: void setColours()
*
* Preconditions:
* this.config != null
* this.config.sources != null
* this.manager != null
* this.manager.tree != null
*
* Postconditions:
* init'ed(this.activeBackground)
* init'ed(this.activeBold)
* init'ed(this.activeForeground)
* init'ed(this.rolloverColour)
*/
133 rolloverColour = config.getOptionColour(
134 "ui", "treeviewRolloverColour",
135 "treeview", "backgroundcolour",
136 "ui", "backgroundcolour");
137 activeBackground = config.getOptionColour(
138 "ui", "treeviewActiveBackground",
139 "treeview", "backgroundcolour",
140 "ui", "backgroundcolour");
141 activeForeground = config.getOptionColour(
142 "ui", "treeviewActiveForeground",
143 "treeview", "foregroundcolour",
144 "ui", "foregroundcolour");
145 activeBold = config.getOptionBool("ui", "treeviewActiveBold");
146
147 manager.getTree().repaint();
148 }
149
150 /** {@inheritDoc} */
151 @Override
152 public void configChanged(final String domain, final String key) {
/*
P/P * Method: void configChanged(String, String)
*
* Preconditions:
* (soft) this.config != null
* (soft) this.config.sources != null
* (soft) this.manager != null
* (soft) this.manager.tree != null
*
* Postconditions:
* possibly_updated(this.activeBackground)
* possibly_updated(this.activeBold)
* possibly_updated(this.activeForeground)
* possibly_updated(this.rolloverColour)
*
* Test Vectors:
* java.lang.String:equals(...)@153: {1}, {0}
* java.lang.String:equals(...)@153: {0}, {1}
*/
153 if (("ui".equals(domain) || "treeview".equals(domain)) &&
154 ("treeviewRolloverColour".equals(key) ||
155 "treeviewActiveBackground".equals(key) ||
156 "treeviewActiveForeground".equals(key) ||
157 "treeviewActiveBold".equals(key) ||
158 "backgroundcolour".equals(key) ||
159 "foregroundcolour".equals(key))) {
160 setColours();
161 }
162 }
163 }
SofCheck Inspector Build Version : 2.17854
| TreeViewTreeCellRenderer.java |
2009-Jun-25 01:54:24 |
| TreeViewTreeCellRenderer.class |
2009-Sep-02 17:04:16 |