1 19 20 package org.netbeans.modules.search; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.Graphics ; 26 import java.awt.Image ; 27 import java.awt.Rectangle ; 28 import java.beans.BeanInfo ; 29 import javax.swing.ImageIcon ; 30 import javax.swing.JCheckBox ; 31 import javax.swing.JComponent ; 32 import javax.swing.JLabel ; 33 import javax.swing.JTree ; 34 import javax.swing.UIManager ; 35 import javax.swing.tree.TreeCellRenderer ; 36 import javax.swing.tree.TreePath ; 37 import org.openide.awt.HtmlRenderer; 38 import org.openide.loaders.DataObject; 39 import org.openide.nodes.Node; 40 import org.openide.util.NbBundle; 41 import org.openide.util.Utilities; 42 43 47 final class NodeRenderer extends JComponent implements TreeCellRenderer { 48 49 50 private static Rectangle checkBounds; 51 52 53 private final HtmlRenderer.Renderer nodeRenderer; 54 55 private final Image rootIconImage; 56 57 private final JCheckBox checkBox; 58 59 60 private Image deletedObjectIconImage; 61 62 private String deletedObjectHtmlSuffix; 63 67 private Component stringDisplayer = new JLabel (" "); 69 72 NodeRenderer(final boolean withCheckBox) { 73 nodeRenderer = HtmlRenderer.createRenderer(); 74 rootIconImage = Utilities.loadImage( 75 "org/netbeans/modules/search/res/find.gif", true); 78 setLayout(null); 79 if (!withCheckBox) { 80 checkBox = null; 81 } else { 82 checkBox = new JCheckBox (); 83 checkBox.setBorderPaintedFlat(true); 84 85 Color c = UIManager.getColor("Tree.textBackground"); if (c == null) { 87 c = Color.WHITE; 89 } 90 checkBox.setBackground(c); 91 92 Dimension dim = checkBox.getPreferredSize(); 93 checkBox.setPreferredSize(new Dimension (dim.width, dim.height - 5)); 94 } 95 } 96 97 99 public Component getTreeCellRendererComponent(JTree tree, 100 Object value, 101 boolean selected, 102 boolean expanded, 103 boolean leaf, 104 int row, 105 boolean hasFocus) { 106 final boolean isRoot = (row == 0); 107 108 String text; 109 boolean isHtml; 110 Image iconImage; 111 boolean checked; 112 113 if (isRoot) { 114 assert value instanceof ResultTreeModel; 115 116 final ResultTreeModel resultTreeModel = (ResultTreeModel) value; 117 text = resultTreeModel.getRootDisplayName(); 118 isHtml = false; 119 iconImage = rootIconImage; 120 checked = (checkBox != null) ? resultTreeModel.isSelected() 121 : false; 122 123 } else if (value.getClass() == MatchingObject.class) { 124 final MatchingObject matchingObj = (MatchingObject) value; 125 final DataObject dataObj = (DataObject) matchingObj.object; 126 final boolean valid = matchingObj.isObjectValid(); 127 if (valid) { 128 final Node node = dataObj.getNodeDelegate(); 129 text = node.getHtmlDisplayName(); 130 isHtml = (text != null); 131 if (!isHtml) { 132 text = node.getDisplayName(); 133 } 134 iconImage = node.getIcon(BeanInfo.ICON_COLOR_16x16); 135 } else { 136 text = dataObj.getName() + getDeletedObjectHtmlSuffix(); 137 isHtml = true; 138 iconImage = getDeletedObjectIconImage(); 139 } 140 checked = (checkBox != null) ? matchingObj.isSelected() 141 : false; 142 143 } else { 144 assert (value instanceof Node); 145 146 final Node node = (Node) value; 147 text = node.getHtmlDisplayName(); 148 isHtml = (text != null); 149 if (!isHtml) { 150 text = node.getDisplayName(); 151 } 152 iconImage = node.getIcon(BeanInfo.ICON_COLOR_16x16); 153 if (checkBox == null) { 154 checked = false; 155 } else { 156 TreePath path = tree.getPathForRow(row); 157 if (path == null) { checked = true; 159 } else { 160 MatchingObject matchingObj 161 = (MatchingObject) path.getPathComponent(1); 162 if (matchingObj.isUniformSelection()) { 163 checked = matchingObj.isSelected(); 164 } else { 165 int parentPathRow 166 = tree.getRowForPath(path.getParentPath()); 167 int index = row - parentPathRow - 1; 168 checked = matchingObj.isSubnodeSelected(index); 169 } 170 } 171 } 172 } 173 174 stringDisplayer = nodeRenderer.getTreeCellRendererComponent( 175 tree, 176 text, 177 selected, 178 expanded, 179 leaf, 180 row, 181 hasFocus); 182 183 nodeRenderer.setHtml(isHtml); 184 nodeRenderer.setIcon(new ImageIcon (iconImage)); 185 186 if (stringDisplayer.getBackground() == null) { 189 stringDisplayer.setBackground(tree.getBackground()); 190 } 191 if (stringDisplayer.getForeground() == null) { 192 stringDisplayer.setForeground(tree.getForeground()); 193 } 194 195 if (checkBox != null) { 196 checkBox.setSelected(checked); 197 return this; 198 } else { 199 return stringDisplayer; 200 } 201 } 202 203 @Override 204 public void paint(Graphics g) { 205 Dimension checkDim = checkBox.getSize(); 206 Dimension labelDim = stringDisplayer.getPreferredSize(); 207 208 int labelY = (checkDim.height >= labelDim.height) 209 ? (checkDim.height - labelDim.height) / 2 210 : 0; 211 checkBox.paint(g); 212 213 220 assert stringDisplayer.getBounds().x == 0 221 && stringDisplayer.getBounds().y == 0; 222 g.translate(checkDim.width, labelY); 223 stringDisplayer.paint(g); 224 g.translate(-checkDim.width, -labelY); 225 } 226 227 @Override 228 public Dimension getPreferredSize() { 229 stringDisplayer.setFont(getFont()); 230 231 Dimension prefSize = new Dimension (stringDisplayer.getPreferredSize()); 232 Dimension checkDim = checkBox.getPreferredSize(); 233 prefSize.width += checkDim.width; 234 prefSize.height = Math.max(prefSize.height, checkDim.height); 235 return prefSize; 236 } 237 238 @Override 239 public void doLayout() { 240 Dimension checkDim = checkBox.getPreferredSize(); 241 Dimension labelDim = stringDisplayer.getPreferredSize(); 242 int checkWidth = checkDim.width; 243 int checkHeight = checkDim.height; 244 int labelWidth = labelDim.width; 245 int labelHeight = labelDim.height; 246 247 int heightDif = labelHeight - checkHeight; 248 int checkY = (heightDif > 2) ? heightDif / 2 - 1 249 : 0; 250 int labelY = (heightDif < 0) ? -heightDif / 2 251 : 0; 252 253 checkBox.setBounds(0, checkY, checkWidth, checkHeight); 254 stringDisplayer.setBounds(checkWidth, labelY, labelWidth, labelHeight); 255 if (checkBounds == null) { 256 checkBounds = checkBox.getBounds(); 257 } 258 } 259 260 private Image getDeletedObjectIconImage() { 261 if (deletedObjectIconImage == null) { 262 deletedObjectIconImage = Utilities.loadImage( 263 "org/netbeans/modules/search/res/invalid.png", true); } 266 return deletedObjectIconImage; 267 } 268 269 private String getDeletedObjectHtmlSuffix() { 270 if (deletedObjectHtmlSuffix == null) { 271 deletedObjectHtmlSuffix 272 = " <font color=\"#ff0000\">" + NbBundle.getMessage(getClass(), 274 "LBL_InvalidFile") + "</font>"; } 277 return deletedObjectHtmlSuffix; 278 } 279 280 static Rectangle getCheckBoxRectangle() { 281 return (Rectangle ) checkBounds.clone(); 282 } 283 } 284 | Popular Tags |