1 19 package org.netbeans.modules.refactoring.spi.impl; 20 21 import java.awt.*; 22 import javax.swing.*; 23 import javax.swing.tree.*; 24 import org.openide.awt.HtmlRenderer; 25 import org.openide.util.NbBundle; 26 27 30 public class CheckRenderer extends JPanel implements TreeCellRenderer { 31 32 protected JCheckBox check; 33 protected HtmlRenderer.Renderer renderer = HtmlRenderer.createRenderer(); 34 private static Dimension checkDim; 35 36 static Rectangle checkBounds; 37 38 static { 39 Dimension old = new JCheckBox().getPreferredSize(); 40 checkDim = new Dimension(old.width, old.height - 5); 41 } 42 43 public CheckRenderer(boolean isQuery) { 44 setLayout(null); 45 if (isQuery) { 46 check = null; 47 } else { 48 add(check = new JCheckBox()); 49 Color c = UIManager.getColor("Tree.textBackground"); if (c == null) { 51 c = Color.WHITE; 53 } 54 check.setBackground(c); Dimension dim = check.getPreferredSize(); 56 check.setPreferredSize(checkDim); 57 } 58 } 59 60 61 private Component stringDisplayer = new JLabel(" "); 63 public Component getTreeCellRendererComponent(JTree tree, Object value, 64 boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) { 65 CheckNode node = (CheckNode) value; 66 stringDisplayer = renderer.getTreeCellRendererComponent(tree, 67 getNodeText(node), isSelected, expanded, leaf, row, hasFocus); 68 69 renderer.setIcon (node.getIcon()); 70 stringDisplayer.setEnabled(!node.isDisabled()); 71 setToolTipText("<html>"+node.getToolTip()+"</html>"); 72 73 if (stringDisplayer.getBackground() == null) { 76 stringDisplayer.setBackground (tree.getBackground()); 77 } 78 if (stringDisplayer.getForeground() == null) { 79 stringDisplayer.setForeground (tree.getForeground()); 80 } 81 82 if (check != null) { 83 check.setSelected(node.isSelected()); 84 check.setEnabled(!node.isDisabled()); 85 } 86 return this; 87 } 88 89 public void paintComponent (Graphics g) { 90 Dimension d_check = check == null ? new Dimension(0, 0) : check.getSize(); 91 Dimension d_label = stringDisplayer == null ? new Dimension(0,0) : 92 stringDisplayer.getPreferredSize(); 93 94 int y_check = 0; 95 int y_label = 0; 96 97 if (d_check.height >= d_label.height) { 98 y_label = (d_check.height - d_label.height) / 2; 99 } 100 if (check != null) { 101 check.setBounds (0, 0, d_check.width, d_check.height); 102 check.paint(g); 103 } 104 if (stringDisplayer != null) { 105 int y = y_label-2; 106 stringDisplayer.setBounds (d_check.width, y, 107 d_label.width, getHeight()-1); 108 g.translate (d_check.width, y_label); 109 stringDisplayer.paint(g); 110 g.translate (-d_check.width, -y_label); 111 } 112 } 113 114 private String getNodeText(CheckNode node) { 115 String nodeLabel = node.getLabel() == null ? NbBundle.getMessage(CheckRenderer.class,"LBL_NotAvailable") : node.getLabel(); 116 nodeLabel = "<html>" + nodeLabel; if (node.needsRefresh()) { 118 nodeLabel += " - " + NbBundle.getMessage(RefactoringPanel.class, "LBL_NeedsRefresh"); 119 } 120 nodeLabel += "</html>"; int i = nodeLabel.indexOf("<br>"); 122 if (i!=-1) { 123 return nodeLabel.substring(0,i) +"</html>"; 124 } else { 125 return nodeLabel; 126 } 127 } 128 129 public Dimension getPreferredSize() { 130 if (stringDisplayer != null) { 131 stringDisplayer.setFont(getFont()); 132 } 133 Dimension d_check = check == null ? new Dimension(0, checkDim.height) : 134 check.getPreferredSize(); 135 136 Dimension d_label = stringDisplayer != null ? 137 stringDisplayer.getPreferredSize() : new Dimension(0,0); 138 139 return new Dimension(d_check.width + d_label.width, (d_check.height < d_label.height ? d_label.height : d_check.height)); 140 } 141 142 public void doLayout() { 143 Dimension d_check = check == null ? new Dimension(0, 0) : check.getPreferredSize(); 144 Dimension d_label = stringDisplayer == null ? new Dimension (0,0) : stringDisplayer.getPreferredSize(); 145 int y_check = 0; 146 int y_label = 0; 147 148 if (d_check.height < d_label.height) 149 y_check = (d_label.height - d_check.height) / 2; 150 else 151 y_label = (d_check.height - d_label.height) / 2; 152 153 if (check != null) { 154 check.setLocation(0, y_check); 155 check.setBounds(0, y_check, d_check.width, d_check.height); 156 if (checkBounds == null) 157 checkBounds = check.getBounds(); 158 } 159 } 160 161 public static Rectangle getCheckBoxRectangle() { 162 return (Rectangle) checkBounds.clone(); 163 } 164 } 165 | Popular Tags |