1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.java.swing.plaf.windows; 27 28 import java.awt.*; 29 import java.awt.event.*; 30 31 import java.io.*; 32 import java.util.*; 33 34 import javax.swing.plaf.basic.*; 35 import javax.swing.*; 36 import javax.swing.plaf.*; 37 38 import javax.swing.tree.*; 39 40 import static com.sun.java.swing.plaf.windows.TMSchema.*; 41 import static com.sun.java.swing.plaf.windows.XPStyle.Skin; 42 43 44 /** 45 * A Windows tree. 46 * <p> 47 * <strong>Warning:</strong> 48 * Serialized objects of this class will not be compatible with 49 * future Swing releases. The current serialization support is appropriate 50 * for short term storage or RMI between applications running the same 51 * version of Swing. A future release of Swing will provide support for 52 * long term persistence. 53 * 54 * @author Scott Violet 55 */ 56 public class WindowsTreeUI extends BasicTreeUI { 57 58 public static ComponentUI createUI( JComponent c ) 59 { 60 return new WindowsTreeUI(); 61 } 62 63 64 /** 65 * Ensures that the rows identified by beginRow through endRow are 66 * visible. 67 */ 68 protected void ensureRowsAreVisible(int beginRow, int endRow) { 69 if(tree != null && beginRow >= 0 && endRow < getRowCount(tree)) { 70 Rectangle visRect = tree.getVisibleRect(); 71 if(beginRow == endRow) { 72 Rectangle scrollBounds = getPathBounds(tree, getPathForRow 73 (tree, beginRow)); 74 75 if(scrollBounds != null) { 76 scrollBounds.x = visRect.x; 77 scrollBounds.width = visRect.width; 78 tree.scrollRectToVisible(scrollBounds); 79 } 80 } 81 else { 82 Rectangle beginRect = getPathBounds(tree, getPathForRow 83 (tree, beginRow)); 84 if (beginRect != null) { 85 Rectangle testRect = beginRect; 86 int beginY = beginRect.y; 87 int maxY = beginY + visRect.height; 88 89 for(int counter = beginRow + 1; counter <= endRow; counter++) { 90 testRect = getPathBounds(tree, 91 getPathForRow(tree, counter)); 92 if(testRect != null && (testRect.y + testRect.height) > maxY) { 93 counter = endRow; 94 } 95 } 96 97 if (testRect == null) { 98 return; 99 } 100 101 tree.scrollRectToVisible(new Rectangle(visRect.x, beginY, 1, 102 testRect.y + testRect.height- 103 beginY)); 104 } 105 } 106 } 107 } 108 109 protected static final int HALF_SIZE = 4; 110 protected static final int SIZE = 9; 111 112 /** 113 * Returns the default cell renderer that is used to do the 114 * stamping of each node. 115 */ 116 protected TreeCellRenderer createDefaultCellRenderer() { 117 return new WindowsTreeCellRenderer(); 118 } 119 120 /** 121 * The minus sign button icon 122 * <p> 123 * <strong>Warning:</strong> 124 * Serialized objects of this class will not be compatible with 125 * future Swing releases. The current serialization support is appropriate 126 * for short term storage or RMI between applications running the same 127 * version of Swing. A future release of Swing will provide support for 128 * long term persistence. 129 */ 130 @SuppressWarnings("serial") // Same-version serialization only 131 public static class ExpandedIcon implements Icon, Serializable { 132 133 public static Icon createExpandedIcon() { 134 return new ExpandedIcon(); 135 } 136 137 Skin getSkin(Component c) { 138 XPStyle xp = XPStyle.getXP(); 139 return (xp != null) ? xp.getSkin(c, Part.TVP_GLYPH) : null; 140 } 141 142 public void paintIcon(Component c, Graphics g, int x, int y) { 143 Skin skin = getSkin(c); 144 if (skin != null) { 145 skin.paintSkin(g, x, y, State.OPENED); 146 return; 147 } 148 149 Color backgroundColor = c.getBackground(); 150 151 if(backgroundColor != null) 152 g.setColor(backgroundColor); 153 else 154 g.setColor(Color.white); 155 g.fillRect(x, y, SIZE-1, SIZE-1); 156 g.setColor(Color.gray); 157 g.drawRect(x, y, SIZE-1, SIZE-1); 158 g.setColor(Color.black); 159 g.drawLine(x + 2, y + HALF_SIZE, x + (SIZE - 3), y + HALF_SIZE); 160 } 161 162 public int getIconWidth() { 163 Skin skin = getSkin(null); 164 return (skin != null) ? skin.getWidth() : SIZE; 165 } 166 167 public int getIconHeight() { 168 Skin skin = getSkin(null); 169 return (skin != null) ? skin.getHeight() : SIZE; 170 } 171 } 172 173 /** 174 * The plus sign button icon 175 * <p> 176 * <strong>Warning:</strong> 177 * Serialized objects of this class will not be compatible with 178 * future Swing releases. The current serialization support is appropriate 179 * for short term storage or RMI between applications running the same 180 * version of Swing. A future release of Swing will provide support for 181 * long term persistence. 182 */ 183 @SuppressWarnings("serial") // Superclass is not serializable across versions 184 public static class CollapsedIcon extends ExpandedIcon { 185 public static Icon createCollapsedIcon() { 186 return new CollapsedIcon(); 187 } 188 189 public void paintIcon(Component c, Graphics g, int x, int y) { 190 Skin skin = getSkin(c); 191 if (skin != null) { 192 skin.paintSkin(g, x, y, State.CLOSED); 193 } else { 194 super.paintIcon(c, g, x, y); 195 g.drawLine(x + HALF_SIZE, y + 2, x + HALF_SIZE, y + (SIZE - 3)); 196 } 197 } 198 } 199 200 @SuppressWarnings("serial") // Superclass is not serializable across versions 201 public class WindowsTreeCellRenderer extends DefaultTreeCellRenderer { 202 203 /** 204 * Configures the renderer based on the passed in components. 205 * The value is set from messaging the tree with 206 * <code>convertValueToText</code>, which ultimately invokes 207 * <code>toString</code> on <code>value</code>. 208 * The foreground color is set based on the selection and the icon 209 * is set based on on leaf and expanded. 210 */ 211 public Component getTreeCellRendererComponent(JTree tree, Object value, 212 boolean sel, 213 boolean expanded, 214 boolean leaf, int row, 215 boolean hasFocus) { 216 super.getTreeCellRendererComponent(tree, value, sel, 217 expanded, leaf, row, 218 hasFocus); 219 // Windows displays the open icon when the tree item selected. 220 if (!tree.isEnabled()) { 221 setEnabled(false); 222 if (leaf) { 223 setDisabledIcon(getLeafIcon()); 224 } else if (sel) { 225 setDisabledIcon(getOpenIcon()); 226 } else { 227 setDisabledIcon(getClosedIcon()); 228 } 229 } 230 else { 231 setEnabled(true); 232 if (leaf) { 233 setIcon(getLeafIcon()); 234 } else if (sel) { 235 setIcon(getOpenIcon()); 236 } else { 237 setIcon(getClosedIcon()); 238 } 239 } 240 return this; 241 } 242 243 } 244 245 }