1. /*
  2. * @(#)WindowsTreeUI.java 1.23 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.windows;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.io.*;
  11. import java.util.*;
  12. import javax.swing.plaf.basic.*;
  13. import javax.swing.*;
  14. import javax.swing.plaf.*;
  15. import javax.swing.tree.*;
  16. /**
  17. * A Windows tree.
  18. * <p>
  19. * <strong>Warning:</strong>
  20. * Serialized objects of this class will not be compatible with
  21. * future Swing releases. The current serialization support is appropriate
  22. * for short term storage or RMI between applications running the same
  23. * version of Swing. A future release of Swing will provide support for
  24. * long term persistence.
  25. *
  26. * @version 1.23 12/19/03
  27. * @author Scott Violet
  28. */
  29. public class WindowsTreeUI extends BasicTreeUI {
  30. public static ComponentUI createUI( JComponent c )
  31. {
  32. return new WindowsTreeUI();
  33. }
  34. /**
  35. * Ensures that the rows identified by beginRow through endRow are
  36. * visible.
  37. */
  38. protected void ensureRowsAreVisible(int beginRow, int endRow) {
  39. if(tree != null && beginRow >= 0 && endRow < getRowCount(tree)) {
  40. Rectangle visRect = tree.getVisibleRect();
  41. if(beginRow == endRow) {
  42. Rectangle scrollBounds = getPathBounds(tree, getPathForRow
  43. (tree, beginRow));
  44. if(scrollBounds != null) {
  45. scrollBounds.x = visRect.x;
  46. scrollBounds.width = visRect.width;
  47. tree.scrollRectToVisible(scrollBounds);
  48. }
  49. }
  50. else {
  51. Rectangle beginRect = getPathBounds(tree, getPathForRow
  52. (tree, beginRow));
  53. Rectangle testRect = beginRect;
  54. int beginY = beginRect.y;
  55. int maxY = beginY + visRect.height;
  56. for(int counter = beginRow + 1; counter <= endRow; counter++) {
  57. testRect = getPathBounds(tree,
  58. getPathForRow(tree, counter));
  59. if((testRect.y + testRect.height) > maxY)
  60. counter = endRow;
  61. }
  62. tree.scrollRectToVisible(new Rectangle(visRect.x, beginY, 1,
  63. testRect.y + testRect.height-
  64. beginY));
  65. }
  66. }
  67. }
  68. static protected final int HALF_SIZE = 4;
  69. static protected final int SIZE = 9;
  70. /**
  71. * Returns the default cell renderer that is used to do the
  72. * stamping of each node.
  73. */
  74. protected TreeCellRenderer createDefaultCellRenderer() {
  75. return new WindowsTreeCellRenderer();
  76. }
  77. /**
  78. * The minus sign button icon
  79. * <p>
  80. * <strong>Warning:</strong>
  81. * Serialized objects of this class will not be compatible with
  82. * future Swing releases. The current serialization support is appropriate
  83. * for short term storage or RMI between applications running the same
  84. * version of Swing. A future release of Swing will provide support for
  85. * long term persistence.
  86. */
  87. public static class ExpandedIcon implements Icon, Serializable {
  88. XPStyle xp = XPStyle.getXP();
  89. XPStyle.Skin skin = (xp != null) ? xp.getSkin("treeview.glyph") : null;
  90. static public Icon createExpandedIcon() {
  91. return new ExpandedIcon();
  92. }
  93. public void paintIcon(Component c, Graphics g, int x, int y) {
  94. if (skin != null) {
  95. skin.paintSkin(g, x, y, 1);
  96. return;
  97. }
  98. Color backgroundColor = c.getBackground();
  99. if(backgroundColor != null)
  100. g.setColor(backgroundColor);
  101. else
  102. g.setColor(Color.white);
  103. g.fillRect(x, y, SIZE-1, SIZE-1);
  104. g.setColor(Color.gray);
  105. g.drawRect(x, y, SIZE-1, SIZE-1);
  106. g.setColor(Color.black);
  107. g.drawLine(x + 2, y + HALF_SIZE, x + (SIZE - 3), y + HALF_SIZE);
  108. }
  109. public int getIconWidth() { return (skin != null) ? skin.getWidth() : SIZE; }
  110. public int getIconHeight() { return (skin != null) ? skin.getHeight() : SIZE; }
  111. }
  112. /**
  113. * The plus sign button icon
  114. * <p>
  115. * <strong>Warning:</strong>
  116. * Serialized objects of this class will not be compatible with
  117. * future Swing releases. The current serialization support is appropriate
  118. * for short term storage or RMI between applications running the same
  119. * version of Swing. A future release of Swing will provide support for
  120. * long term persistence.
  121. */
  122. public static class CollapsedIcon extends ExpandedIcon {
  123. static public Icon createCollapsedIcon() {
  124. return new CollapsedIcon();
  125. }
  126. public void paintIcon(Component c, Graphics g, int x, int y) {
  127. if (skin != null) {
  128. skin.paintSkin(g, x, y, 0);
  129. } else {
  130. super.paintIcon(c, g, x, y);
  131. g.drawLine(x + HALF_SIZE, y + 2, x + HALF_SIZE, y + (SIZE - 3));
  132. }
  133. }
  134. }
  135. public class WindowsTreeCellRenderer extends DefaultTreeCellRenderer {
  136. /**
  137. * Configures the renderer based on the passed in components.
  138. * The value is set from messaging the tree with
  139. * <code>convertValueToText</code>, which ultimately invokes
  140. * <code>toString</code> on <code>value</code>.
  141. * The foreground color is set based on the selection and the icon
  142. * is set based on on leaf and expanded.
  143. */
  144. public Component getTreeCellRendererComponent(JTree tree, Object value,
  145. boolean sel,
  146. boolean expanded,
  147. boolean leaf, int row,
  148. boolean hasFocus) {
  149. super.getTreeCellRendererComponent(tree, value, sel,
  150. expanded, leaf, row,
  151. hasFocus);
  152. // Windows displays the open icon when the tree item selected.
  153. if (!tree.isEnabled()) {
  154. setEnabled(false);
  155. if (leaf) {
  156. setDisabledIcon(getLeafIcon());
  157. } else if (sel) {
  158. setDisabledIcon(getOpenIcon());
  159. } else {
  160. setDisabledIcon(getClosedIcon());
  161. }
  162. }
  163. else {
  164. setEnabled(true);
  165. if (leaf) {
  166. setIcon(getLeafIcon());
  167. } else if (sel) {
  168. setIcon(getOpenIcon());
  169. } else {
  170. setIcon(getClosedIcon());
  171. }
  172. }
  173. return this;
  174. }
  175. }
  176. }