1 7 8 package javax.swing.tree; 9 10 import javax.swing.*; 11 import javax.swing.plaf.ColorUIResource ; 12 import javax.swing.plaf.FontUIResource ; 13 import javax.swing.plaf.basic.BasicGraphicsUtils ; 14 import java.awt.*; 15 import java.awt.event.*; 16 import java.beans.*; 17 import java.io.*; 18 import java.util.*; 19 20 60 public class DefaultTreeCellRenderer extends JLabel implements TreeCellRenderer 61 { 62 63 private JTree tree; 64 65 66 protected boolean selected; 67 68 protected boolean hasFocus; 69 70 private boolean drawsFocusBorderAroundIcon; 71 72 private boolean drawDashedFocusIndicator; 73 74 78 private Color treeBGColor; 79 83 private Color focusBGColor; 84 85 87 transient protected Icon closedIcon; 88 89 90 transient protected Icon leafIcon; 91 92 93 transient protected Icon openIcon; 94 95 97 protected Color textSelectionColor; 98 99 100 protected Color textNonSelectionColor; 101 102 103 protected Color backgroundSelectionColor; 104 105 106 protected Color backgroundNonSelectionColor; 107 108 109 protected Color borderSelectionColor; 110 111 116 public DefaultTreeCellRenderer() { 117 setHorizontalAlignment(JLabel.LEFT); 118 119 setLeafIcon(UIManager.getIcon("Tree.leafIcon")); 120 setClosedIcon(UIManager.getIcon("Tree.closedIcon")); 121 setOpenIcon(UIManager.getIcon("Tree.openIcon")); 122 123 setTextSelectionColor(UIManager.getColor("Tree.selectionForeground")); 124 setTextNonSelectionColor(UIManager.getColor("Tree.textForeground")); 125 setBackgroundSelectionColor(UIManager.getColor("Tree.selectionBackground")); 126 setBackgroundNonSelectionColor(UIManager.getColor("Tree.textBackground")); 127 setBorderSelectionColor(UIManager.getColor("Tree.selectionBorderColor")); 128 Object value = UIManager.get("Tree.drawsFocusBorderAroundIcon"); 129 drawsFocusBorderAroundIcon = (value != null && ((Boolean )value). 130 booleanValue()); 131 value = UIManager.get("Tree.drawDashedFocusIndicator"); 132 drawDashedFocusIndicator = (value != null && ((Boolean )value). 133 booleanValue()); 134 } 135 136 137 141 public Icon getDefaultOpenIcon() { 142 return UIManager.getIcon("Tree.openIcon"); 143 } 144 145 149 public Icon getDefaultClosedIcon() { 150 return UIManager.getIcon("Tree.closedIcon"); 151 } 152 153 157 public Icon getDefaultLeafIcon() { 158 return UIManager.getIcon("Tree.leafIcon"); 159 } 160 161 164 public void setOpenIcon(Icon newIcon) { 165 openIcon = newIcon; 166 } 167 168 171 public Icon getOpenIcon() { 172 return openIcon; 173 } 174 175 178 public void setClosedIcon(Icon newIcon) { 179 closedIcon = newIcon; 180 } 181 182 186 public Icon getClosedIcon() { 187 return closedIcon; 188 } 189 190 193 public void setLeafIcon(Icon newIcon) { 194 leafIcon = newIcon; 195 } 196 197 200 public Icon getLeafIcon() { 201 return leafIcon; 202 } 203 204 207 public void setTextSelectionColor(Color newColor) { 208 textSelectionColor = newColor; 209 } 210 211 214 public Color getTextSelectionColor() { 215 return textSelectionColor; 216 } 217 218 221 public void setTextNonSelectionColor(Color newColor) { 222 textNonSelectionColor = newColor; 223 } 224 225 228 public Color getTextNonSelectionColor() { 229 return textNonSelectionColor; 230 } 231 232 235 public void setBackgroundSelectionColor(Color newColor) { 236 backgroundSelectionColor = newColor; 237 } 238 239 240 243 public Color getBackgroundSelectionColor() { 244 return backgroundSelectionColor; 245 } 246 247 250 public void setBackgroundNonSelectionColor(Color newColor) { 251 backgroundNonSelectionColor = newColor; 252 } 253 254 257 public Color getBackgroundNonSelectionColor() { 258 return backgroundNonSelectionColor; 259 } 260 261 264 public void setBorderSelectionColor(Color newColor) { 265 borderSelectionColor = newColor; 266 } 267 268 271 public Color getBorderSelectionColor() { 272 return borderSelectionColor; 273 } 274 275 282 public void setFont(Font font) { 283 if(font instanceof FontUIResource ) 284 font = null; 285 super.setFont(font); 286 } 287 288 293 public Font getFont() { 294 Font font = super.getFont(); 295 296 if (font == null && tree != null) { 297 font = tree.getFont(); 300 } 301 return font; 302 } 303 304 312 public void setBackground(Color color) { 313 if(color instanceof ColorUIResource ) 314 color = null; 315 super.setBackground(color); 316 } 317 318 326 public Component getTreeCellRendererComponent(JTree tree, Object value, 327 boolean sel, 328 boolean expanded, 329 boolean leaf, int row, 330 boolean hasFocus) { 331 String stringValue = tree.convertValueToText(value, sel, 332 expanded, leaf, row, hasFocus); 333 334 this.tree = tree; 335 this.hasFocus = hasFocus; 336 setText(stringValue); 337 if(sel) 338 setForeground(getTextSelectionColor()); 339 else 340 setForeground(getTextNonSelectionColor()); 341 if (!tree.isEnabled()) { 343 setEnabled(false); 344 if (leaf) { 345 setDisabledIcon(getLeafIcon()); 346 } else if (expanded) { 347 setDisabledIcon(getOpenIcon()); 348 } else { 349 setDisabledIcon(getClosedIcon()); 350 } 351 } 352 else { 353 setEnabled(true); 354 if (leaf) { 355 setIcon(getLeafIcon()); 356 } else if (expanded) { 357 setIcon(getOpenIcon()); 358 } else { 359 setIcon(getClosedIcon()); 360 } 361 } 362 setComponentOrientation(tree.getComponentOrientation()); 363 364 selected = sel; 365 366 return this; 367 } 368 369 372 public void paint(Graphics g) { 373 Color bColor; 374 375 if(selected) { 376 bColor = getBackgroundSelectionColor(); 377 } else { 378 bColor = getBackgroundNonSelectionColor(); 379 if(bColor == null) 380 bColor = getBackground(); 381 } 382 int imageOffset = -1; 383 if(bColor != null) { 384 Icon currentI = getIcon(); 385 386 imageOffset = getLabelStart(); 387 g.setColor(bColor); 388 if(getComponentOrientation().isLeftToRight()) { 389 g.fillRect(imageOffset, 0, getWidth() - imageOffset, 390 getHeight()); 391 } else { 392 g.fillRect(0, 0, getWidth() - imageOffset, 393 getHeight()); 394 } 395 } 396 397 if (hasFocus) { 398 if (drawsFocusBorderAroundIcon) { 399 imageOffset = 0; 400 } 401 else if (imageOffset == -1) { 402 imageOffset = getLabelStart(); 403 } 404 if(getComponentOrientation().isLeftToRight()) { 405 paintFocus(g, imageOffset, 0, getWidth() - imageOffset, 406 getHeight()); 407 } else { 408 paintFocus(g, 0, 0, getWidth() - imageOffset, getHeight()); 409 } 410 } 411 super.paint(g); 412 } 413 414 private void paintFocus(Graphics g, int x, int y, int w, int h) { 415 Color bsColor = getBorderSelectionColor(); 416 417 if (bsColor != null && (selected || !drawDashedFocusIndicator)) { 418 g.setColor(bsColor); 419 g.drawRect(x, y, w - 1, h - 1); 420 } 421 if (drawDashedFocusIndicator) { 422 Color color; 423 if (selected) { 424 color = getBackgroundSelectionColor(); 425 } else { 426 color = getBackgroundNonSelectionColor(); 427 if(color == null) { 428 color = getBackground(); 429 } 430 } 431 432 if (treeBGColor != color) { 433 treeBGColor = color; 434 focusBGColor = new Color(~color.getRGB()); 435 } 436 g.setColor(focusBGColor); 437 BasicGraphicsUtils.drawDashedRect(g, x, y, w, h); 438 } 439 } 440 441 private int getLabelStart() { 442 Icon currentI = getIcon(); 443 if(currentI != null && getText() != null) { 444 return currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1); 445 } 446 return 0; 447 } 448 449 453 public Dimension getPreferredSize() { 454 Dimension retDimension = super.getPreferredSize(); 455 456 if(retDimension != null) 457 retDimension = new Dimension(retDimension.width + 3, 458 retDimension.height); 459 return retDimension; 460 } 461 462 467 public void validate() {} 468 469 476 public void invalidate() {} 477 478 483 public void revalidate() {} 484 485 490 public void repaint(long tm, int x, int y, int width, int height) {} 491 492 497 public void repaint(Rectangle r) {} 498 499 506 public void repaint() {} 507 508 513 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 514 if (propertyName=="text") 516 super.firePropertyChange(propertyName, oldValue, newValue); 517 } 518 519 524 public void firePropertyChange(String propertyName, byte oldValue, byte newValue) {} 525 526 531 public void firePropertyChange(String propertyName, char oldValue, char newValue) {} 532 533 538 public void firePropertyChange(String propertyName, short oldValue, short newValue) {} 539 540 545 public void firePropertyChange(String propertyName, int oldValue, int newValue) {} 546 547 552 public void firePropertyChange(String propertyName, long oldValue, long newValue) {} 553 554 559 public void firePropertyChange(String propertyName, float oldValue, float newValue) {} 560 561 566 public void firePropertyChange(String propertyName, double oldValue, double newValue) {} 567 568 573 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {} 574 575 } 576 | Popular Tags |