1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 import javax.swing.tree.DefaultTreeCellRenderer; 23 import javax.swing.tree.TreePath; 24 import javax.swing.JTree; 25 import java.awt.Color; 26 import java.awt.Component; 27 import java.util.Vector; 28 29 30 public class AccessibleTreeCellRenderer 31 extends DefaultTreeCellRenderer 32 { 33 public Color 34 maDefaultColor, 35 maChangedColor; 36 protected Vector 37 maChangedLines; 38 39 40 AccessibleTreeCellRenderer()41 public AccessibleTreeCellRenderer () 42 { 43 maDefaultColor = Color.black; 44 maChangedColor = Color.red; 45 maChangedLines = new Vector (); 46 } 47 getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)48 public Component getTreeCellRendererComponent ( 49 JTree tree, 50 Object value, 51 boolean sel, 52 boolean expanded, 53 boolean leaf, 54 int row, 55 boolean hasFocus) 56 { 57 super.getTreeCellRendererComponent( 58 tree, value, sel, 59 expanded, leaf, row, 60 hasFocus); 61 62 if (maChangedLines.size()<=row || maChangedLines.elementAt (row) == null) 63 setTextNonSelectionColor (maDefaultColor); 64 else 65 setTextNonSelectionColor (maChangedColor); 66 67 return this; 68 } 69 70 /** Tell the cell renderer that no changes shall be displayed anymore. 71 */ clearAllChanges()72 public void clearAllChanges () 73 { 74 maChangedLines.clear(); 75 } 76 77 /** Inform the cell renderer of a new changed line which to paint 78 highlighted when asked to paint it the next time. 79 */ addChangedLine(int nRow)80 public void addChangedLine (int nRow) 81 { 82 if (maChangedLines.size() <= nRow) 83 maChangedLines.setSize (nRow+1); 84 nRow -= 1; // row index is one to large for some reason. 85 maChangedLines.set (nRow, new Boolean (true)); 86 } 87 88 /** Inform the cell renderer of a set of changed line which to paint 89 highlighted when asked to paint them the next time. 90 @param aChangedNodes 91 The set of changed nodes. Each entry is a TreePath. 92 @param aTree 93 The JTree that is used to transform the given TreePath objects 94 into rows. 95 */ addChangedNodes(Vector aChangedNodes, JTree aTree)96 public void addChangedNodes (Vector aChangedNodes, JTree aTree) 97 { 98 for (int i=0; i<aChangedNodes.size(); i++) 99 { 100 TreePath aPath = (TreePath)aChangedNodes.elementAt (i); 101 int nRow = aTree.getRowForPath (aPath); 102 addChangedLine (nRow); 103 } 104 } 105 106 } 107 108