1 import javax.swing.tree.DefaultTreeCellRenderer;
2 import javax.swing.tree.TreePath;
3 import javax.swing.JTree;
4 import java.awt.Color;
5 import java.awt.Component;
6 import java.util.Vector;
7 
8 
9 public class AccessibleTreeCellRenderer
10     extends DefaultTreeCellRenderer
11 {
12     public Color
13         maDefaultColor,
14         maChangedColor;
15     protected Vector
16         maChangedLines;
17 
18 
19 
20     public AccessibleTreeCellRenderer ()
21     {
22         maDefaultColor = Color.black;
23         maChangedColor = Color.red;
24         maChangedLines = new Vector ();
25     }
26 
27     public Component getTreeCellRendererComponent (
28         JTree tree,
29         Object value,
30         boolean sel,
31         boolean expanded,
32         boolean leaf,
33         int row,
34         boolean hasFocus)
35     {
36         super.getTreeCellRendererComponent(
37             tree, value, sel,
38             expanded, leaf, row,
39             hasFocus);
40 
41         if (maChangedLines.size()<=row || maChangedLines.elementAt (row) == null)
42             setTextNonSelectionColor (maDefaultColor);
43         else
44             setTextNonSelectionColor (maChangedColor);
45 
46         return this;
47     }
48 
49     /** Tell the cell renderer that no changes shall be displayed anymore.
50     */
51     public void clearAllChanges ()
52     {
53         maChangedLines.clear();
54     }
55 
56     /** Inform the cell renderer of a new changed line which to paint
57         highlighted when asked to paint it the next time.
58     */
59     public void addChangedLine (int nRow)
60     {
61         if (maChangedLines.size() <= nRow)
62             maChangedLines.setSize (nRow+1);
63         nRow -= 1; // row index is one to large for some reason.
64         maChangedLines.set (nRow, new Boolean (true));
65     }
66 
67     /** Inform the cell renderer of a set of changed line which to paint
68         highlighted when asked to paint them the next time.
69         @param aChangedNodes
70             The set of changed nodes.  Each entry is a TreePath.
71         @param aTree
72             The JTree that is used to transform the given TreePath objects
73             into rows.
74     */
75     public void addChangedNodes (Vector aChangedNodes, JTree aTree)
76     {
77         for (int i=0; i<aChangedNodes.size(); i++)
78         {
79             TreePath aPath = (TreePath)aChangedNodes.elementAt (i);
80             int nRow = aTree.getRowForPath (aPath);
81             addChangedLine (nRow);
82         }
83     }
84 
85 }
86 
87