1 7 8 package com.sun.java.swing.plaf.windows; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 13 import java.io.*; 14 import java.util.*; 15 16 import javax.swing.plaf.basic.*; 17 import javax.swing.*; 18 import javax.swing.plaf.*; 19 20 import javax.swing.tree.*; 21 22 import com.sun.java.swing.plaf.windows.TMSchema.*; 23 import com.sun.java.swing.plaf.windows.XPStyle.Skin; 24 25 26 39 public class WindowsTreeUI extends BasicTreeUI { 40 41 public static ComponentUI createUI( JComponent c ) 42 { 43 return new WindowsTreeUI(); 44 } 45 46 47 51 protected void ensureRowsAreVisible(int beginRow, int endRow) { 52 if(tree != null && beginRow >= 0 && endRow < getRowCount(tree)) { 53 Rectangle visRect = tree.getVisibleRect(); 54 if(beginRow == endRow) { 55 Rectangle scrollBounds = getPathBounds(tree, getPathForRow 56 (tree, beginRow)); 57 58 if(scrollBounds != null) { 59 scrollBounds.x = visRect.x; 60 scrollBounds.width = visRect.width; 61 tree.scrollRectToVisible(scrollBounds); 62 } 63 } 64 else { 65 Rectangle beginRect = getPathBounds(tree, getPathForRow 66 (tree, beginRow)); 67 Rectangle testRect = beginRect; 68 int beginY = beginRect.y; 69 int maxY = beginY + visRect.height; 70 71 for(int counter = beginRow + 1; counter <= endRow; counter++) { 72 testRect = getPathBounds(tree, 73 getPathForRow(tree, counter)); 74 if((testRect.y + testRect.height) > maxY) 75 counter = endRow; 76 } 77 tree.scrollRectToVisible(new Rectangle(visRect.x, beginY, 1, 78 testRect.y + testRect.height- 79 beginY)); 80 } 81 } 82 } 83 84 static protected final int HALF_SIZE = 4; 85 static protected final int SIZE = 9; 86 87 91 protected TreeCellRenderer createDefaultCellRenderer() { 92 return new WindowsTreeCellRenderer(); 93 } 94 95 105 public static class ExpandedIcon implements Icon, Serializable { 106 107 static public Icon createExpandedIcon() { 108 return new ExpandedIcon(); 109 } 110 111 Skin getSkin(Component c) { 112 XPStyle xp = XPStyle.getXP(); 113 return (xp != null) ? xp.getSkin(c, Part.TVP_GLYPH) : null; 114 } 115 116 public void paintIcon(Component c, Graphics g, int x, int y) { 117 Skin skin = getSkin(c); 118 if (skin != null) { 119 skin.paintSkin(g, x, y, State.OPENED); 120 return; 121 } 122 123 Color backgroundColor = c.getBackground(); 124 125 if(backgroundColor != null) 126 g.setColor(backgroundColor); 127 else 128 g.setColor(Color.white); 129 g.fillRect(x, y, SIZE-1, SIZE-1); 130 g.setColor(Color.gray); 131 g.drawRect(x, y, SIZE-1, SIZE-1); 132 g.setColor(Color.black); 133 g.drawLine(x + 2, y + HALF_SIZE, x + (SIZE - 3), y + HALF_SIZE); 134 } 135 136 public int getIconWidth() { 137 Skin skin = getSkin(null); 138 return (skin != null) ? skin.getWidth() : SIZE; 139 } 140 141 public int getIconHeight() { 142 Skin skin = getSkin(null); 143 return (skin != null) ? skin.getHeight() : SIZE; 144 } 145 } 146 147 157 public static class CollapsedIcon extends ExpandedIcon { 158 static public Icon createCollapsedIcon() { 159 return new CollapsedIcon(); 160 } 161 162 public void paintIcon(Component c, Graphics g, int x, int y) { 163 Skin skin = getSkin(c); 164 if (skin != null) { 165 skin.paintSkin(g, x, y, State.CLOSED); 166 } else { 167 super.paintIcon(c, g, x, y); 168 g.drawLine(x + HALF_SIZE, y + 2, x + HALF_SIZE, y + (SIZE - 3)); 169 } 170 } 171 } 172 173 public class WindowsTreeCellRenderer extends DefaultTreeCellRenderer { 174 175 183 public Component getTreeCellRendererComponent(JTree tree, Object value, 184 boolean sel, 185 boolean expanded, 186 boolean leaf, int row, 187 boolean hasFocus) { 188 super.getTreeCellRendererComponent(tree, value, sel, 189 expanded, leaf, row, 190 hasFocus); 191 if (!tree.isEnabled()) { 193 setEnabled(false); 194 if (leaf) { 195 setDisabledIcon(getLeafIcon()); 196 } else if (sel) { 197 setDisabledIcon(getOpenIcon()); 198 } else { 199 setDisabledIcon(getClosedIcon()); 200 } 201 } 202 else { 203 setEnabled(true); 204 if (leaf) { 205 setIcon(getLeafIcon()); 206 } else if (sel) { 207 setIcon(getOpenIcon()); 208 } else { 209 setIcon(getClosedIcon()); 210 } 211 } 212 return this; 213 } 214 215 } 216 217 } 218 | Popular Tags |