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 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 this.addPropertyChangeListener( 34 new PropertyChangeListener() 35 { 36 public void propertyChange(PropertyChangeEvent e) 37 { 38 { 41 { 45 setup(); 46 } 47 } 49 53 } 54 }); 55 56 } 58 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 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 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 try 112 { 113 n = node.getMailFolder().getNumberOfNewMails(); 114 } catch(Exception 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) { 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 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 this.setComponentOrientation(tree.getComponentOrientation()); 158 161 return this; 162 } 163 164 165 166 } | Popular Tags |