KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > windows > WindowsTreeUI


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.windows;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.beans.PropertyChangeEvent JavaDoc;
36 import java.beans.PropertyChangeListener JavaDoc;
37
38 import javax.swing.Icon JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.plaf.ComponentUI JavaDoc;
41
42 import com.jgoodies.looks.Options;
43
44 /**
45  * The JGoodies Windows Look&amp;Feel implementation of <code>TreeUI</code>.
46  * Corrects the position of the tree button icon and provides two line styles:
47  * angled dashed lines, or no lines at all. By default, lines are drawn.<p>
48  *
49  * You can change the line style by setting a client property.
50  * The property key and values are a subset of the values used
51  * by the Metal L&amp;F tree. To hide lines use one of the following:
52  * <pre>
53  * JTree tree1 = new JTree();
54  * tree1.putClientProperty("JTree.lineStyle", "None");
55  *
56  * JTree tree2 = new JTree();
57  * tree1.putClientProperty(Options.TREE_LINE_STYLE_KEY,
58  * Options.TREE_LINE_STYLE_NONE_VALUE);
59  * </pre>
60  *
61  * Although lines are shown by default, you could code:
62  * <pre>
63  * JTree tree1 = new JTree();
64  * tree1.putClientProperty("JTree.lineStyle", "Angled");
65  *
66  * JTree tree2 = new JTree();
67  * tree1.putClientProperty(Options.TREE_LINE_STYLE_KEY,
68  * Options.TREE_LINE_STYLE_ANGLED_VALUE);
69  * </pre>
70  *
71  * @author Karsten Lentzsch
72  * @version $Revision: 1.2 $
73  */

74
75 public final class WindowsTreeUI extends com.sun.java.swing.plaf.windows.WindowsTreeUI {
76     
77     private boolean linesEnabled = true;
78     private PropertyChangeListener JavaDoc lineStyleHandler;
79
80     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
81         return new WindowsTreeUI();
82     }
83     
84
85     // Installation ***********************************************************
86

87     public void installUI(JComponent JavaDoc c) {
88         super.installUI(c);
89         updateLineStyle(c.getClientProperty(Options.TREE_LINE_STYLE_KEY));
90         lineStyleHandler = new LineStyleHandler();
91         c.addPropertyChangeListener(lineStyleHandler);
92     }
93
94     public void uninstallUI(JComponent JavaDoc c) {
95         c.removePropertyChangeListener(lineStyleHandler);
96         super.uninstallUI(c);
97     }
98     
99     
100     // Painting ***************************************************************
101

102     protected void paintVerticalLine(Graphics JavaDoc g, JComponent JavaDoc c, int x, int top, int bottom) {
103         if (linesEnabled) {
104             super.paintVerticalLine(g, c, x, top, bottom);
105         }
106     }
107
108     protected void paintHorizontalLine(Graphics JavaDoc g, JComponent JavaDoc c, int y, int left, int right) {
109         if (linesEnabled) {
110             super.paintHorizontalLine(g, c, y, left, right);
111         }
112     }
113
114     // Draws the icon centered at (x,y)
115
protected void drawCentered(Component JavaDoc c, Graphics JavaDoc graphics, Icon JavaDoc icon, int x, int y) {
116         icon.paintIcon(c, graphics,
117                 x - icon.getIconWidth() / 2 - 1,
118                 y - icon.getIconHeight() / 2);
119     }
120     
121
122     // Helper Code ************************************************************
123

124     private void updateLineStyle(Object JavaDoc lineStyle) {
125         linesEnabled = !Options.TREE_LINE_STYLE_NONE_VALUE.equals(lineStyle);
126     }
127     
128     // Listens for changes of the line style property
129
private class LineStyleHandler implements PropertyChangeListener JavaDoc {
130         public void propertyChange(PropertyChangeEvent JavaDoc e) {
131             String JavaDoc name = e.getPropertyName();
132             Object JavaDoc value = e.getNewValue();
133             if (name.equals(Options.TREE_LINE_STYLE_KEY)) {
134                 updateLineStyle(value);
135             }
136         }
137     }
138     
139 }
Popular Tags