001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.looks.windows;
032:
033: import java.awt.Component;
034: import java.awt.Graphics;
035: import java.beans.PropertyChangeEvent;
036: import java.beans.PropertyChangeListener;
037:
038: import javax.swing.Icon;
039: import javax.swing.JComponent;
040: import javax.swing.plaf.ComponentUI;
041:
042: import com.jgoodies.looks.Options;
043:
044: /**
045: * The JGoodies Windows Look&Feel implementation of <code>TreeUI</code>.
046: * Corrects the position of the tree button icon and provides two line styles:
047: * angled dashed lines, or no lines at all. By default, lines are drawn.<p>
048: *
049: * You can change the line style by setting a client property.
050: * The property key and values are a subset of the values used
051: * by the Metal L&F tree. To hide lines use one of the following:
052: * <pre>
053: * JTree tree1 = new JTree();
054: * tree1.putClientProperty("JTree.lineStyle", "None");
055: *
056: * JTree tree2 = new JTree();
057: * tree1.putClientProperty(Options.TREE_LINE_STYLE_KEY,
058: * Options.TREE_LINE_STYLE_NONE_VALUE);
059: * </pre>
060: *
061: * Although lines are shown by default, you could code:
062: * <pre>
063: * JTree tree1 = new JTree();
064: * tree1.putClientProperty("JTree.lineStyle", "Angled");
065: *
066: * JTree tree2 = new JTree();
067: * tree1.putClientProperty(Options.TREE_LINE_STYLE_KEY,
068: * Options.TREE_LINE_STYLE_ANGLED_VALUE);
069: * </pre>
070: *
071: * @author Karsten Lentzsch
072: * @version $Revision: 1.3 $
073: */
074:
075: public final class WindowsTreeUI extends
076: com.sun.java.swing.plaf.windows.WindowsTreeUI {
077:
078: private boolean linesEnabled = true;
079: private PropertyChangeListener lineStyleHandler;
080:
081: public static ComponentUI createUI(JComponent b) {
082: return new WindowsTreeUI();
083: }
084:
085: // Installation ***********************************************************
086:
087: public void installUI(JComponent c) {
088: super .installUI(c);
089: updateLineStyle(c
090: .getClientProperty(Options.TREE_LINE_STYLE_KEY));
091: lineStyleHandler = new LineStyleHandler();
092: c.addPropertyChangeListener(lineStyleHandler);
093: }
094:
095: public void uninstallUI(JComponent c) {
096: c.removePropertyChangeListener(lineStyleHandler);
097: super .uninstallUI(c);
098: }
099:
100: // Painting ***************************************************************
101:
102: protected void paintVerticalLine(Graphics g, JComponent c, int x,
103: int top, int bottom) {
104: if (linesEnabled) {
105: super .paintVerticalLine(g, c, x, top, bottom);
106: }
107: }
108:
109: protected void paintHorizontalLine(Graphics g, JComponent c, int y,
110: int left, int right) {
111: if (linesEnabled) {
112: super .paintHorizontalLine(g, c, y, left, right);
113: }
114: }
115:
116: // Draws the icon centered at (x,y)
117: protected void drawCentered(Component c, Graphics graphics,
118: Icon icon, int x, int y) {
119: icon.paintIcon(c, graphics, x - icon.getIconWidth() / 2 - 1, y
120: - icon.getIconHeight() / 2);
121: }
122:
123: // Helper Code ************************************************************
124:
125: private void updateLineStyle(Object lineStyle) {
126: linesEnabled = !Options.TREE_LINE_STYLE_NONE_VALUE
127: .equals(lineStyle);
128: }
129:
130: // Listens for changes of the line style property
131: private class LineStyleHandler implements PropertyChangeListener {
132: public void propertyChange(PropertyChangeEvent e) {
133: String name = e.getPropertyName();
134: Object value = e.getNewValue();
135: if (name.equals(Options.TREE_LINE_STYLE_KEY)) {
136: updateLineStyle(value);
137: }
138: }
139: }
140:
141: }
|