KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > folders > FolderViewTreeRenderer


1 package SnowMailClient.view.folders;
2
3 import SnowMailClient.model.folders.*;
4
5 import javax.swing.*;
6 import javax.swing.border.*;
7 import javax.swing.tree.*;
8 import java.awt.*;
9 import java.beans.*;
10
11 /**
12    ??? class DefaultTreeCellRenderer has some problems with non opaque
13    backgrounds
14    ### can be optimized for performance... see DefaultTreeCellRenderer
15 */

16 public class FolderViewTreeRenderer extends JLabel implements TreeCellRenderer
17 {
18   Border validDropTargetBorder = null;
19   Border invalidDropTargetBorder = null;
20   Border emptyBorder = null;
21   int dropTargetRow = -1;
22   boolean isValid = true;
23   Font normalFont, boldFont;
24
25
26   public FolderViewTreeRenderer()
27   {
28     super();
29
30     setup();
31
32     // recall after all themes change
33
this.addPropertyChangeListener(
34      new PropertyChangeListener()
35      {
36         public void propertyChange(PropertyChangeEvent e)
37         {
38            // String name = e.getPropertyName();
39
//if (name.equals("lookAndFeel"))
40
{
41               // SwingUtilities.invokeLater( new Runnable()
42
// {
43
// public void run()
44
{
45                      setup();
46                    }
47 // });
48
}
49         /* else
50             {
51               System.out.println(">"+name);
52             }*/

53         }
54      });
55
56   } // Constructor
57

58   /** must be called after each ui change
59   */

60   private void setup()
61   {
62     normalFont = UIManager.getFont("Tree.font");
63     int fontSize = normalFont.getSize();
64     boldFont = new Font(normalFont.getFontName(), Font.BOLD, fontSize);
65
66
67     emptyBorder = new EmptyBorder(fontSize/4, fontSize/2, fontSize/4, fontSize/2);
68     Border emptyBorderM1 = new EmptyBorder(fontSize/4-1, fontSize/2-1, fontSize/4-1, fontSize/2-1);
69
70     validDropTargetBorder = BorderFactory.createCompoundBorder(
71            BorderFactory.createLineBorder(Color.green, 1), emptyBorderM1);
72
73     invalidDropTargetBorder = BorderFactory.createCompoundBorder(
74            BorderFactory.createLineBorder(Color.red, 1), emptyBorderM1);
75
76   }
77
78
79     /**
80      * Notification from the <code>UIManager</code> that the look and feel
81      * [L&F] has changed.
82      * Replaces the current UI object with the latest version from the
83      * <code>UIManager</code>.
84      *
85      * @see JComponent#updateUI
86      */

87     public void updateUI()
88     {
89         super.updateUI();
90         setup();
91     }
92
93
94   public void setDropTarget(int dropTargetRow, boolean isValid)
95   {
96     this.dropTargetRow = dropTargetRow;
97     this.isValid = isValid;
98   }
99
100   public Component getTreeCellRendererComponent(
101      JTree tree, Object JavaDoc value,
102      boolean selected, boolean expanded,
103      boolean leaf, int row, boolean hasFocus)
104   {
105     FolderTreeNode node = (FolderTreeNode) value;
106
107     int n = 0;
108     if(node.isSystemFolder())
109     {
110       // ONLY READ SYSTEM FOLDERS, for memory reasons...
111
try
112       {
113         n = node.getMailFolder().getNumberOfNewMails();
114       } catch(Exception JavaDoc e) {}
115     }
116
117     if(n>0)
118     {
119       this.setFont(boldFont);
120     }
121     else
122     {
123       this.setFont(normalFont);
124     }
125
126     if(row == dropTargetRow)
127     {
128        if(isValid) //node.getParent()!=null)
129
{
130          this.setBorder(this.validDropTargetBorder);
131        }
132        else
133        {
134          this.setBorder(this.invalidDropTargetBorder);
135        }
136     }
137     else
138     {
139        this.setBorder(emptyBorder);
140     }
141
142     // background
143
if(selected)
144     {
145        setBackground(UIManager.getColor("Tree.selectionBackground"));
146        this.setOpaque(true);
147     }
148     else
149     {
150        setBackground(UIManager.getColor("Tree.textBackground"));
151        this.setOpaque(false);
152     }
153
154     this.setText(node.getFolderName()+(n>0?" ("+n+")":""));
155
156     // UGLY BUT NEcessary ??
157
this.setComponentOrientation(tree.getComponentOrientation());
158 // setComponentOrientation(tree.getComponentOrientation());
159
// this.setPreferredSize( this.getPreferredSize() );
160

161     return this;
162   }
163
164
165
166 } // FolderViewTreeRenderer
Popular Tags