1 /*
2 * $Id: OverviewNodeRenderer.java,v 1.6 2007/07/04 19:42:51 edankert Exp $
3 *
4 * The contents of this file are subject to the Mozilla Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the License.
12 *
13 * The Original Code is XML Hammer code. (org.xmlhammer.*)
14 *
15 * The Initial Developer of the Original Code is Edwin Dankert. Portions created
16 * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer.
17 * All Rights Reserved.
18 *
19 * Contributor(s): Edwin Dankert <edankert@gmail.com>
20 */
21 package org.xmlhammer.gui.overview;
22
23 import java.awt.Component;
24
25 import javax.swing.ImageIcon;
26 import javax.swing.JLabel;
27 import javax.swing.JTree;
28 import javax.swing.tree.DefaultTreeCellRenderer;
29
30 import org.bounce.QIcon;
31 import org.bounce.image.ImageLoader;
32
33 /***
34 * The cell renderer for a XmlElementNode.
35 *
36 * @version $Revision: 1.6 $, $Date: 2007/07/04 19:42:51 $
37 * @author Edwin Dankert <edankert@gmail.com>
38 */
39 public class OverviewNodeRenderer extends DefaultTreeCellRenderer {
40 private static final long serialVersionUID = 1330322329212308384L;
41 private static final ImageIcon ERROR_ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/ovr16/error_ovr.gif");
42
43 private OverviewNode result = null;
44
45 /***
46 * Configures the renderer based on the passed in components.
47 * The value is set from messaging the tree with
48 * <code>convertValueToText</code>, which ultimately invokes
49 * <code>toString</code> on <code>value</code>.
50 * The foreground color is set based on the selection and the icon
51 * is set based on on leaf and expanded.
52 */
53 public Component getTreeCellRendererComponent( JTree tree, Object value,
54 boolean selected, boolean expanded, boolean leaf,
55 int row, boolean focus) {
56 JLabel label = (JLabel)super.getTreeCellRendererComponent( tree, value, selected, expanded, leaf, row, focus);
57 label.setIconTextGap( 5);
58
59 if ( value instanceof OverviewNode) {
60 result = (OverviewNode)value;
61
62 label.setText(result.getNodeName());
63 // label.setToolTipText( result.getDescription());
64 if (isError(result)) {
65 label.setIcon(new QIcon(result.getNodeIcon(), ERROR_ICON, QIcon.SOUTH_WEST));
66 } else {
67 label.setIcon(result.getNodeIcon());
68 }
69 } else {
70 label.setText(value.toString());
71 }
72
73 // if ( selected) {
74 // setForeground( UIManager.getColor("Tree.selectionForeground"));
75 // setBackground( UIManager.getColor("Tree.selectionBackground"));
76 // } else {
77 // setForeground( tree.getForeground());
78 // setBackground( tree.getBackground());
79 // }
80 //
81 // setComponentOrientation( tree.getComponentOrientation());
82 // setEnabled( tree.isEnabled());
83 // setFont( tree.getFont());
84
85 return label;
86 }
87
88 public static boolean isError(OverviewNode node) {
89 if (node.getError() == null) {
90 for (OverviewNode child : node.getChildNodes()) {
91 if (isError(child)) {
92 return true;
93 }
94 }
95
96 return false;
97 }
98
99 return true;
100 }
101
102 // public Icon getIcon() {
103 // return result != null ? result.getIcon() : null;
104 // }
105 //
106 // public String getName() {
107 // return result != null ? result.getName() : null;
108 // }
109 //
110 // public String getValue() {
111 // return result != null ? result.getValue() : null;
112 // }
113 //
114 // public boolean isSelected() {
115 // return selected;
116 // }
117 }