1 30 package browser; 31 32 import java.awt.*; 33 import javax.swing.*; 34 import javax.swing.tree.*; 35 36 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 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 value, 76 boolean selected, 77 boolean expanded, 78 boolean leaf, 79 int row, 80 boolean hasFocus) { 81 82 String 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 99 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 |