1 /*
2  * @(#)BasicTOCCellRenderer.java	1.25 06/10/30
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This code is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 only, as
9  * published by the Free Software Foundation.  Sun designates this
10  * particular file as subject to the "Classpath" exception as provided
11  * by Sun in the LICENSE file that accompanied this code.
12  *
13  * This code is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16  * version 2 for more details (a copy is included in the LICENSE file that
17  * accompanied this code).
18  *
19  * You should have received a copy of the GNU General Public License version
20  * 2 along with this work; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24  * CA 95054 USA or visit www.sun.com if you need additional information or
25  * have any questions.
26  */
27 /*
28  * @(#) BasicTOCCellRenderer.java 1.25 - last change made 10/30/06
29  */
30 
31 package javax.help.plaf.basic;
32 
33 import javax.swing.*;
34 import javax.swing.tree.DefaultTreeCellRenderer;
35 import javax.swing.tree.DefaultMutableTreeNode;
36 import javax.swing.tree.TreeNode;
37 import java.awt.*;
38 import java.net.URL;
39 import java.util.Locale;
40 import javax.help.TOCItem;
41 import javax.help.TOCView;
42 import javax.help.Map;
43 import javax.help.HelpUtilities;
44 import javax.help.Map.ID;
45 
46 /**
47  * Basic cell renderer for TOC UI.
48  *
49  * @author Roger D. Brinkley
50  * @author Eduardo Pelegri-Llopart
51  * @version   1.25     10/30/06
52  */
53 
54 public class BasicTOCCellRenderer extends DefaultTreeCellRenderer
55 {
56     protected Map map;
57     protected TOCView view;
58 
BasicTOCCellRenderer(Map map)59     public BasicTOCCellRenderer(Map map) {
60 	this(map, null);
61     }
62 
BasicTOCCellRenderer(Map map, TOCView view)63     public BasicTOCCellRenderer(Map map, TOCView view) {
64 	super();
65 	this.map = map;
66 	this.view = view;
67     }
68 
69     /**
70       * Configures the renderer based on the components passed in.
71       * Sets the value from messaging value with toString().
72       * The foreground color is set based on the selection and the icon
73       * is set based on on leaf and expanded.
74       */
getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)75     public Component getTreeCellRendererComponent(JTree tree, Object value,
76 						  boolean sel,
77 						  boolean expanded,
78 						  boolean leaf, int row,
79 						  boolean hasFocus)
80     {
81 
82         String stringValue = "";
83 
84         // variable hasFocus was private to DefaultTreeCellRenderer since jdk1.3
85         try {
86             this.hasFocus = hasFocus;
87         } catch (IllegalAccessError e) {
88         }
89 
90         TOCItem item
91 	    = (TOCItem) ((DefaultMutableTreeNode) value).getUserObject();
92 
93 	if (item != null) {
94 	    stringValue = item.getName();
95 	}
96 
97 	setText(stringValue);
98 	if (sel) {
99 	    setForeground(getTextSelectionColor());
100 	} else {
101 	    setForeground(getTextNonSelectionColor());
102 	}
103 
104 	ImageIcon icon = null;
105 	if (item != null) {
106 	    ID id = item.getImageID();
107 	    if (id != null) {
108 		try {
109 		    URL url = map.getURLFromID(id);
110 		    icon = new ImageIcon(url);
111 		} catch (Exception e) {
112 		}
113 	    }
114 	}
115 
116 	// Set the locale of this if there is a lang value
117 	if (item != null) {
118 	    Locale locale = item.getLocale();
119 	    if (locale != null) {
120 		setLocale(locale);
121 	    }
122 	}
123 
124 	// determine which icon to display
125 	if (icon != null) {
126 	    setIcon(icon);
127 	} else if (leaf) {
128 	    setIcon(getLeafIcon());
129 	} else if (expanded) {
130 	    setIcon(getOpenIcon());
131 	} else {
132 	    setIcon(getClosedIcon());
133 	}
134 
135 	selected = sel;
136 
137 	return this;
138     }
139 
getLeafIcon()140     public Icon getLeafIcon() {
141 	Icon icon = null;
142 	if (view != null) {
143 	    ID id = view.getTopicImageID();
144 	    if (id != null) {
145 		try {
146 		    URL url = map.getURLFromID(id);
147 		    icon = new ImageIcon(url);
148 		    return icon;
149 		} catch (Exception e) {
150 		}
151 	    }
152 	}
153 	return super.getLeafIcon();
154     }
155 
getOpenIcon()156     public Icon getOpenIcon() {
157 	Icon icon = null;
158 	if (view != null) {
159 	    ID id = view.getCategoryOpenImageID();
160 	    if (id != null) {
161 		try {
162 		    URL url = map.getURLFromID(id);
163 		    icon = new ImageIcon(url);
164 		    return icon;
165 		} catch (Exception e) {
166 		}
167 	    }
168 	}
169 	return super.getOpenIcon();
170     }
171 
getClosedIcon()172     public Icon getClosedIcon() {
173 	Icon icon = null;
174 	if (view != null) {
175 	    ID id = view.getCategoryClosedImageID();
176 	    if (id != null) {
177 		try {
178 		    URL url = map.getURLFromID(id);
179 		    icon = new ImageIcon(url);
180 		    return icon;
181 		} catch (Exception e) {
182 		}
183 	    }
184 	}
185 	return super.getClosedIcon();
186     }
187 
188 }
189