KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > browser > StyleBrowserCellRenderer


1 /*
2  * StyleBrowserCellRenderer.java
3  *
4  * Steady State CSS2 Parser
5  *
6  * Copyright (C) 1999, 2002 Steady State Software Ltd. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * To contact the authors of the library, write to Steady State Software Ltd.,
23  * 49 Littleworth, Wing, Buckinghamshire, LU7 0JX, England
24  *
25  * http://www.steadystate.com/css/
26  * mailto:css@steadystate.co.uk
27  *
28  * $Id: StyleBrowserCellRenderer.java,v 1.1.1.1 2003/12/28 21:22:36 davidsch Exp $
29  */

30 package browser;
31
32 import java.awt.*;
33 import javax.swing.*;
34 import javax.swing.tree.*;
35
36 /**
37  * @author David Schweinsberg
38  * @version $Release$
39  */

40 public class StyleBrowserCellRenderer extends JLabel implements TreeCellRenderer {
41
42   private static ImageIcon _pageIcon;
43   private static ImageIcon _charsetIcon;
44   private static ImageIcon _unknownIcon;
45   private static ImageIcon _styleIcon;
46   private static ImageIcon _importIcon;
47   private static ImageIcon _fontFaceIcon;
48   private static ImageIcon _styleSheetIcon;
49   private static ImageIcon _listIcon;
50   private static ImageIcon _styleDeclarationIcon;
51   private static ImageIcon _mediaIcon;
52
53   static {
54     try {
55       _pageIcon = new ImageIcon("images/PageRule.jpg");
56       _charsetIcon = new ImageIcon("images/CharsetRule.jpg");
57       _unknownIcon = new ImageIcon("images/UnknownRule.jpg");
58       _styleIcon = new ImageIcon("images/StyleRule.jpg");
59       _importIcon = new ImageIcon("images/ImportRule.jpg");
60       _fontFaceIcon = new ImageIcon("images/FontFaceRule.jpg");
61       _styleSheetIcon = new ImageIcon("images/StyleSheet.jpg");
62       _listIcon = new ImageIcon("images/List.jpg");
63       _styleDeclarationIcon = new ImageIcon("images/StyleDeclaration.jpg");
64       _mediaIcon = new ImageIcon("images/MediaRule.jpg");
65     }
66     catch(Exception JavaDoc e) {
67       System.out.println("Couldn't load images: " + e);
68     }
69   }
70
71   private boolean _selected;
72
73   public Component getTreeCellRendererComponent(
74     JTree tree,
75     Object JavaDoc value,
76     boolean selected,
77     boolean expanded,
78     boolean leaf,
79     int row,
80     boolean hasFocus) {
81
82     String JavaDoc str = tree.convertValueToText(
83       value,
84       selected,
85       expanded,
86       leaf,
87       row,
88       hasFocus);
89
90     setText(str);
91     setToolTipText(str);
92
93     if (leaf)
94       setIcon(null);
95     else {
96 // StyleData sd =
97
// (StyleData)((DefaultMutableTreeNode)value).getUserObject();
98

99       // This is hardly efficient, but it'll do for now
100
if (str.equals("CSSPageRule"))
101         setIcon(_pageIcon);
102       else if (str.equals("CSSCharsetRule"))
103         setIcon(_charsetIcon);
104       else if (str.equals("CSSUnknownRule"))
105         setIcon(_unknownIcon);
106       else if (str.equals("CSSStyleRule"))
107         setIcon(_styleIcon);
108       else if (str.equals("CSSImportRule"))
109         setIcon (_importIcon);
110       else if (str.equals("CSSFontFaceRule"))
111         setIcon(_fontFaceIcon);
112       else if (str.equals("CSSStyleSheet"))
113         setIcon(_styleSheetIcon);
114       else if (str.equals("MediaList") || str.equals("CSSRuleList"))
115         setIcon(_listIcon);
116       else if (str.equals("CSSStyleDeclaration"))
117         setIcon(_styleDeclarationIcon);
118       else if (str.equals("CSSMediaRule"))
119         setIcon(_mediaIcon);
120       else
121         setIcon(null);
122     }
123
124     setForeground(selected ? SystemColor.textHighlightText : SystemColor.textText);
125
126     _selected = selected;
127
128     return this;
129   }
130
131   public void paint(Graphics g) {
132     if (_selected)
133       g.setColor(SystemColor.textHighlight);
134     else if (getParent() != null)
135       g.setColor(getParent().getBackground());
136     else
137       g.setColor(getBackground());
138
139     Icon icon = getIcon();
140     int offset = 0;
141
142     if (icon != null && getText() != null)
143       offset = icon.getIconWidth() + getIconTextGap();
144
145     g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
146
147     super.paint(g);
148   }
149 }
150
Popular Tags