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     static protected final int HALF_SIZE = 4;
 110     static protected 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     public static class ExpandedIcon implements Icon, Serializable {
 131 
 132         static public Icon createExpandedIcon() {
 133             return new ExpandedIcon();
 134         }
 135 
 136         Skin getSkin(Component c) {
 137             XPStyle xp = XPStyle.getXP();
 138             return (xp != null) ? xp.getSkin(c, Part.TVP_GLYPH) : null;
 139         }
 140 
 141         public void paintIcon(Component c, Graphics g, int x, int y) {
 142             Skin skin = getSkin(c);
 143             if (skin != null) {
 144                 skin.paintSkin(g, x, y, State.OPENED);
 145                 return;
 146             }
 147 
 148             Color     backgroundColor = c.getBackground();
 149 
 150             if(backgroundColor != null)
 151                 g.setColor(backgroundColor);
 152             else
 153                 g.setColor(Color.white);
 154             g.fillRect(x, y, SIZE-1, SIZE-1);
 155             g.setColor(Color.gray);
 156             g.drawRect(x, y, SIZE-1, SIZE-1);
 157             g.setColor(Color.black);
 158             g.drawLine(x + 2, y + HALF_SIZE, x + (SIZE - 3), y + HALF_SIZE);
 159         }
 160 
 161         public int getIconWidth() {
 162             Skin skin = getSkin(null);
 163             return (skin != null) ? skin.getWidth() : SIZE;
 164         }
 165 
 166         public int getIconHeight() {
 167             Skin skin = getSkin(null);
 168             return (skin != null) ? skin.getHeight() : SIZE;
 169         }
 170     }
 171 
 172     /**
 173      * The plus sign button icon
 174      * <p>
 175      * <strong>Warning:</strong>
 176      * Serialized objects of this class will not be compatible with
 177      * future Swing releases.  The current serialization support is appropriate
 178      * for short term storage or RMI between applications running the same
 179      * version of Swing.  A future release of Swing will provide support for
 180      * long term persistence.
 181      */
 182     public static class CollapsedIcon extends ExpandedIcon {
 183         static public Icon createCollapsedIcon() {
 184             return new CollapsedIcon();
 185         }
 186 
 187         public void paintIcon(Component c, Graphics g, int x, int y) {
 188             Skin skin = getSkin(c);
 189             if (skin != null) {
 190                 skin.paintSkin(g, x, y, State.CLOSED);
 191             } else {
 192             super.paintIcon(c, g, x, y);
 193             g.drawLine(x + HALF_SIZE, y + 2, x + HALF_SIZE, y + (SIZE - 3));
 194             }
 195         }
 196     }
 197 
 198     public class WindowsTreeCellRenderer extends DefaultTreeCellRenderer {
 199 
 200         /**
 201          * Configures the renderer based on the passed in components.
 202          * The value is set from messaging the tree with
 203          * <code>convertValueToText</code>, which ultimately invokes
 204          * <code>toString</code> on <code>value</code>.
 205          * The foreground color is set based on the selection and the icon
 206          * is set based on on leaf and expanded.
 207          */
 208         public Component getTreeCellRendererComponent(JTree tree, Object value,
 209                                                       boolean sel,
 210                                                       boolean expanded,
 211                                                       boolean leaf, int row,
 212                                                       boolean hasFocus) {
 213             super.getTreeCellRendererComponent(tree, value, sel,
 214                                                expanded, leaf, row,
 215                                                hasFocus);
 216             // Windows displays the open icon when the tree item selected.
 217             if (!tree.isEnabled()) {
 218                 setEnabled(false);
 219                 if (leaf) {
 220                     setDisabledIcon(getLeafIcon());
 221                 } else if (sel) {
 222                     setDisabledIcon(getOpenIcon());
 223                 } else {
 224                     setDisabledIcon(getClosedIcon());
 225                 }
 226             }
 227             else {
 228                 setEnabled(true);
 229                 if (leaf) {
 230                     setIcon(getLeafIcon());
 231                 } else if (sel) {
 232                     setIcon(getOpenIcon());
 233                 } else {
 234                     setIcon(getClosedIcon());
 235                 }
 236             }
 237             return this;
 238         }
 239 
 240     }
 241 
 242 }