1 19 package org.netbeans.modules.palette.ui; 20 21 import java.awt.*; 22 import javax.swing.*; 23 import javax.swing.tree.*; 24 import org.netbeans.modules.palette.DefaultSettings; 25 import org.netbeans.modules.palette.Settings; 26 import org.openide.explorer.view.NodeRenderer; 27 import org.openide.explorer.view.Visualizer; 28 import org.openide.nodes.Node; 29 30 33 class CheckRenderer extends JPanel implements TreeCellRenderer { 34 35 protected JCheckBox check; 36 private NodeRenderer nodeRenderer; 37 private DefaultSettings settings; 38 private static Dimension checkDim; 39 40 static Rectangle checkBounds; 41 42 static { 43 Dimension old = new JCheckBox().getPreferredSize(); 44 checkDim = new Dimension(old.width, old.height - 5); 45 } 46 47 public CheckRenderer( DefaultSettings settings ) { 48 this.nodeRenderer = new NodeRenderer(); 49 this.settings = settings; 50 setLayout(null); 51 add(check = new JCheckBox()); 52 Color c = UIManager.getColor("Tree.textBackground"); if (c == null) { 54 c = Color.WHITE; 56 } 57 check.setBackground(c); Dimension dim = check.getPreferredSize(); 59 check.setPreferredSize(checkDim); 60 } 61 62 63 private Component stringDisplayer = new JLabel(" "); 65 public Component getTreeCellRendererComponent(JTree tree, Object value, 66 boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) { 67 stringDisplayer = nodeRenderer.getTreeCellRendererComponent(tree, 68 value, isSelected, expanded, leaf, row, hasFocus); 69 70 TreePath path = tree.getPathForRow( row ); 71 if( null != path && 1 == path.getPathCount() ) { 72 return stringDisplayer; 74 } 75 76 if( stringDisplayer instanceof JComponent ) { 77 setToolTipText( ((JComponent)stringDisplayer).getToolTipText() ); 78 } 79 80 if (stringDisplayer.getBackground() == null) { 83 stringDisplayer.setBackground (tree.getBackground()); 84 } 85 if (stringDisplayer.getForeground() == null) { 86 stringDisplayer.setForeground (tree.getForeground()); 87 } 88 89 if( check != null ) { 90 Node node; 91 if( value instanceof Node ) { 92 node = (Node)value; 93 } else { 94 node = Visualizer.findNode( value ); 95 } 96 check.setSelected( null == node || settings.isNodeVisible( node ) ); check.setEnabled( true ); } 99 return this; 100 } 101 102 public void paintComponent (Graphics g) { 103 Dimension d_check = check == null ? new Dimension(0, 0) : check.getSize(); 104 Dimension d_label = stringDisplayer == null ? new Dimension(0,0) : 105 stringDisplayer.getPreferredSize(); 106 107 int y_check = 0; 108 int y_label = 0; 109 110 if (d_check.height >= d_label.height) { 111 y_label = (d_check.height - d_label.height) / 2; 112 } 113 if (check != null) { 114 check.setBounds (0, 0, d_check.width, d_check.height); 115 check.paint(g); 116 } 117 if (stringDisplayer != null) { 118 int y = y_label-2; 119 stringDisplayer.setBounds (d_check.width, y, 120 d_label.width, getHeight()-1); 121 g.translate (d_check.width, y_label); 122 stringDisplayer.paint(g); 123 g.translate (-d_check.width, -y_label); 124 } 125 } 126 127 public Dimension getPreferredSize() { 128 if (stringDisplayer != null) { 129 stringDisplayer.setFont(getFont()); 130 } 131 Dimension d_check = check == null ? new Dimension(0, checkDim.height) : 132 check.getPreferredSize(); 133 134 Dimension d_label = stringDisplayer != null ? 135 stringDisplayer.getPreferredSize() : new Dimension(0,0); 136 137 return new Dimension(d_check.width + d_label.width, (d_check.height < d_label.height ? d_label.height : d_check.height)); 138 } 139 140 public void doLayout() { 141 Dimension d_check = check == null ? new Dimension(0, 0) : check.getPreferredSize(); 142 Dimension d_label = stringDisplayer == null ? new Dimension (0,0) : stringDisplayer.getPreferredSize(); 143 int y_check = 0; 144 int y_label = 0; 145 146 if (d_check.height < d_label.height) 147 y_check = (d_label.height - d_check.height) / 2; 148 else 149 y_label = (d_check.height - d_label.height) / 2; 150 151 if (check != null) { 152 check.setLocation(0, y_check); 153 check.setBounds(0, y_check, d_check.width, d_check.height); 154 if (checkBounds == null) 155 checkBounds = check.getBounds(); 156 } 157 } 158 159 public static Rectangle getCheckBoxRectangle() { 160 return (Rectangle) checkBounds.clone(); 161 } 162 } 163 | Popular Tags |