1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 /*
15 * This file has been modified by Chris Pheby in accordance with Section 4.2
16 * of the Apache Software License
17 */
18 package org.jadira.dependencynavigator.gui;
19
20 import java.awt.Color;
21 import java.awt.Component;
22 import java.awt.Dimension;
23 import java.awt.Font;
24 import java.awt.FontMetrics;
25
26 import javax.swing.BorderFactory;
27 import javax.swing.JTree;
28 import javax.swing.tree.DefaultTreeCellRenderer;
29
30 import org.jadira.dependencynavigator.model.Artifact;
31
32 public class DependencyTreeRenderer extends DefaultTreeCellRenderer {
33
34 private static final long serialVersionUID = -4604073586040441112L;
35
36 @Override
37 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
38 DefaultTreeCellRenderer r = (DefaultTreeCellRenderer) super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
39 Artifact artifact = (Artifact) value;
40 r.setForeground(Color.BLACK);
41 r.setBackground(Color.WHITE);
42 r.setOpaque(true);
43 if (selected) {
44 r.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
45 } else {
46 r.setBorder(null);
47 }
48 if (Artifact.SELECTED_NONE == artifact.getSelected()) {
49 r.setFont(r.getFont().deriveFont(Font.PLAIN));
50 } else {
51 r.setFont(r.getFont().deriveFont(Font.BOLD));
52 if (Artifact.SELECTED_PRINCIPLE == artifact.getSelected()) {
53 r.setBackground(Color.YELLOW);
54 }
55 }
56 return r;
57 }
58
59 /**
60 * Add this override to recalculate the width of this JLabel. The super class default behaviour miscalculates the
61 * width, and so the '...' can appear. Instead, we 'simulate' the FontMetrics' stringWidth() method, by using
62 * charWidth(), plus some initialization and padding
63 */
64 @Override
65 public Dimension getPreferredSize() {
66 Dimension dim = super.getPreferredSize();
67 FontMetrics fm = getFontMetrics(getFont());
68 char[] chars = getText().toCharArray();
69 /*
70 * Initialize the width value to take into account any icons and gaps. Don't try to get the Icon's width
71 * programatically from within this method, as an infinite loop will result. I've just assumed a width of 16
72 * here, but you could (should?) save the actual value in getTreeCellRendererComponent() and retrieve it here,
73 * if you wanted.
74 */
75 int w = getIconTextGap() + 16;
76 for (int i = 0; i < chars.length; i++) {
77 w += fm.charWidth(chars[i]);
78 }
79 w += getText().length();
80 dim.width = w;
81 return dim;
82 }
83 }