01: package net.xoetrope.builder.editor.helper;
02:
03: import javax.swing.tree.DefaultTreeCellRenderer;
04: import java.awt.Graphics;
05: import java.awt.Dimension;
06: import java.awt.Color;
07: import java.awt.SystemColor;
08:
09: /**
10: * Render a style cell
11: * <p> Copyright (c) Xoetrope Ltd., 2002-2003</p>
12: * <p> $Revision: 1.4 $</p>
13: */
14: public class XStyleCellRenderer extends DefaultTreeCellRenderer {
15: Color styleForegroundColor = Color.red;
16: Color styleBackgroundColor = Color.blue;
17: String fontSize = "10";
18:
19: public XStyleCellRenderer() {
20: setToolTipText("Styles");
21: }
22:
23: /**
24: * Render the cell by drawing the text and then two colour swatches and the font size
25: * @param g
26: */
27: public void paint(Graphics g) {
28: super .paint(g);
29:
30: if (styleBackgroundColor != null) {
31: int width = getWidth();
32: int height = getHeight();
33: g.setColor(SystemColor.control);
34: g.drawRect(width - 32 + 9 - 2 * height, 3, height - 6,
35: height - 6);
36: g.drawRect(width - 32 + 6 - height, 3, height - 6,
37: height - 6);
38:
39: g.setColor(styleBackgroundColor);
40: g.fillRect(width - 32 + 10 - 2 * height, 4, height - 7,
41: height - 7);
42: g.setColor(styleForegroundColor);
43: g.fillRect(width - 32 + 7 - height, 4, height - 7,
44: height - 7);
45: g.drawString(fontSize + " pt", width - 26, height - 5);
46: }
47: }
48:
49: /**
50: * Overrides <code>DefaultTreeCellRenderer.getPreferredSize</code> to
51: * return slightly wider preferred size value.
52: */
53: public Dimension getPreferredSize() {
54: Dimension retDimension = super .getPreferredSize();
55:
56: if (retDimension != null)
57: retDimension = new Dimension(Math.max(retDimension.width
58: + 2 * retDimension.height + 32, 100),
59: retDimension.height);
60: return retDimension;
61: }
62:
63: public void setText(String s) {
64: int delim = s.indexOf('|');
65: if (delim < 0) {
66: super .setText(s);
67: styleBackgroundColor = styleForegroundColor = null;
68: } else {
69: super .setText(s.substring(0, delim));
70: int delim2 = s.indexOf('|', delim + 1);
71: styleBackgroundColor = new Color(Integer.parseInt(s
72: .substring(delim + 1, delim2)));
73: delim = delim2;
74: delim2 = s.indexOf('|', delim + 1);
75: styleForegroundColor = new Color(Integer.parseInt(s
76: .substring(delim + 1, delim2)));
77: fontSize = s.substring(delim2 + 1);
78: }
79: }
80: }
|