1. /*
  2. * @(#)WindowsTreeUI.java 1.21 03/01/23
  3. *
  4. * Copyright 2003 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.21 01/23/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. protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds,
  35. Insets insets, Rectangle bounds,
  36. TreePath path, int row,
  37. boolean isExpanded,
  38. boolean hasBeenExpanded, boolean
  39. isLeaf) {
  40. if (XPStyle.getXP() == null) {
  41. super.paintHorizontalPartOfLeg(g, clipBounds, insets, bounds, path,
  42. row, isExpanded, hasBeenExpanded, isLeaf);
  43. }
  44. }
  45. protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds,
  46. Insets insets, TreePath path) {
  47. if (XPStyle.getXP() == null) {
  48. super.paintVerticalPartOfLeg(g, clipBounds, insets, path);
  49. }
  50. }
  51. /**
  52. * Ensures that the rows identified by beginRow through endRow are
  53. * visible.
  54. */
  55. protected void ensureRowsAreVisible(int beginRow, int endRow) {
  56. if(tree != null && beginRow >= 0 && endRow < getRowCount(tree)) {
  57. Rectangle visRect = tree.getVisibleRect();
  58. if(beginRow == endRow) {
  59. Rectangle scrollBounds = getPathBounds(tree, getPathForRow
  60. (tree, beginRow));
  61. if(scrollBounds != null) {
  62. scrollBounds.x = visRect.x;
  63. scrollBounds.width = visRect.width;
  64. tree.scrollRectToVisible(scrollBounds);
  65. }
  66. }
  67. else {
  68. Rectangle beginRect = getPathBounds(tree, getPathForRow
  69. (tree, beginRow));
  70. Rectangle testRect = beginRect;
  71. int beginY = beginRect.y;
  72. int maxY = beginY + visRect.height;
  73. for(int counter = beginRow + 1; counter <= endRow; counter++) {
  74. testRect = getPathBounds(tree,
  75. getPathForRow(tree, counter));
  76. if((testRect.y + testRect.height) > maxY)
  77. counter = endRow;
  78. }
  79. tree.scrollRectToVisible(new Rectangle(visRect.x, beginY, 1,
  80. testRect.y + testRect.height-
  81. beginY));
  82. }
  83. }
  84. }
  85. static protected final int HALF_SIZE = 4;
  86. static protected final int SIZE = 9;
  87. /**
  88. * Returns the default cell renderer that is used to do the
  89. * stamping of each node.
  90. */
  91. protected TreeCellRenderer createDefaultCellRenderer() {
  92. return new WindowsTreeCellRenderer();
  93. }
  94. /**
  95. * The minus sign button icon
  96. * <p>
  97. * <strong>Warning:</strong>
  98. * Serialized objects of this class will not be compatible with
  99. * future Swing releases. The current serialization support is appropriate
  100. * for short term storage or RMI between applications running the same
  101. * version of Swing. A future release of Swing will provide support for
  102. * long term persistence.
  103. */
  104. public static class ExpandedIcon implements Icon, Serializable {
  105. XPStyle xp = XPStyle.getXP();
  106. XPStyle.Skin skin = (xp != null) ? xp.getSkin("treeview.glyph") : null;
  107. static public Icon createExpandedIcon() {
  108. return new ExpandedIcon();
  109. }
  110. public void paintIcon(Component c, Graphics g, int x, int y) {
  111. if (skin != null) {
  112. skin.paintSkin(g, x, y, 1);
  113. return;
  114. }
  115. Color backgroundColor = c.getBackground();
  116. if(backgroundColor != null)
  117. g.setColor(backgroundColor);
  118. else
  119. g.setColor(Color.white);
  120. g.fillRect(x, y, SIZE-1, SIZE-1);
  121. g.setColor(Color.gray);
  122. g.drawRect(x, y, SIZE-1, SIZE-1);
  123. g.setColor(Color.black);
  124. g.drawLine(x + 2, y + HALF_SIZE, x + (SIZE - 3), y + HALF_SIZE);
  125. }
  126. public int getIconWidth() { return (skin != null) ? skin.getWidth() : SIZE; }
  127. public int getIconHeight() { return (skin != null) ? skin.getHeight() : SIZE; }
  128. }
  129. /**
  130. * The plus sign button icon
  131. * <p>
  132. * <strong>Warning:</strong>
  133. * Serialized objects of this class will not be compatible with
  134. * future Swing releases. The current serialization support is appropriate
  135. * for short term storage or RMI between applications running the same
  136. * version of Swing. A future release of Swing will provide support for
  137. * long term persistence.
  138. */
  139. public static class CollapsedIcon extends ExpandedIcon {
  140. static public Icon createCollapsedIcon() {
  141. return new CollapsedIcon();
  142. }
  143. public void paintIcon(Component c, Graphics g, int x, int y) {
  144. if (skin != null) {
  145. skin.paintSkin(g, x, y, 0);
  146. } else {
  147. super.paintIcon(c, g, x, y);
  148. g.drawLine(x + HALF_SIZE, y + 2, x + HALF_SIZE, y + (SIZE - 3));
  149. }
  150. }
  151. }
  152. public class WindowsTreeCellRenderer extends DefaultTreeCellRenderer {
  153. /**
  154. * Configures the renderer based on the passed in components.
  155. * The value is set from messaging the tree with
  156. * <code>convertValueToText</code>, which ultimately invokes
  157. * <code>toString</code> on <code>value</code>.
  158. * The foreground color is set based on the selection and the icon
  159. * is set based on on leaf and expanded.
  160. */
  161. public Component getTreeCellRendererComponent(JTree tree, Object value,
  162. boolean sel,
  163. boolean expanded,
  164. boolean leaf, int row,
  165. boolean hasFocus) {
  166. super.getTreeCellRendererComponent(tree, value, sel,
  167. expanded, leaf, row,
  168. hasFocus);
  169. // Windows displays the open icon when the tree item selected.
  170. if (!tree.isEnabled()) {
  171. setEnabled(false);
  172. if (leaf) {
  173. setDisabledIcon(getLeafIcon());
  174. } else if (sel) {
  175. setDisabledIcon(getOpenIcon());
  176. } else {
  177. setDisabledIcon(getClosedIcon());
  178. }
  179. }
  180. else {
  181. setEnabled(true);
  182. if (leaf) {
  183. setIcon(getLeafIcon());
  184. } else if (sel) {
  185. setIcon(getOpenIcon());
  186. } else {
  187. setIcon(getClosedIcon());
  188. }
  189. }
  190. return this;
  191. }
  192. }
  193. }