KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticTreeUI


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.plastic;
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 import javax.swing.plaf.basic.BasicTreeUI JavaDoc;
42
43 import com.jgoodies.looks.Options;
44
45 /**
46  * The JGoodies Plastic Look&amp;Feel implementation of <code>TreeUI</code>.
47  * It provides two line styles: angled dashed lines, or no lines at all.
48  * By default, lines are drawn.
49  * <p>
50  * You can change the line style by setting a client property.
51  * The property key and values are a subset of the values used
52  * by the Metal L&amp;F tree. To hide lines use one of the following:
53  * <pre>
54  * JTree tree1 = new JTree();
55  * tree1.putClientProperty("JTree.lineStyle", "None");
56  *
57  * JTree tree2 = new JTree();
58  * tree1.putClientProperty(Options.TREE_LINE_STYLE_KEY,
59  * Options.TREE_LINE_STYLE_NONE_VALUE);
60  * </pre>
61  *
62  * Although lines are shown by default, you could code:
63  * <pre>
64  * JTree tree1 = new JTree();
65  * tree1.putClientProperty("JTree.lineStyle", "Angled");
66  *
67  * JTree tree2 = new JTree();
68  * tree1.putClientProperty(Options.TREE_LINE_STYLE_KEY,
69  * Options.TREE_LINE_STYLE_ANGLED_VALUE);
70  * </pre>
71  *
72  * @author Karsten Lentzsch
73  * @version $Revision: 1.2 $
74  */

75
76 public final class PlasticTreeUI extends BasicTreeUI JavaDoc {
77
78     private boolean linesEnabled = true;
79     private PropertyChangeListener JavaDoc lineStyleHandler;
80
81
82     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
83         return new PlasticTreeUI();
84     }
85
86     // Installation ***********************************************************
87

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

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

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