1 /*
2 * Copyright 2002-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.swing.plaf.synth;
27
28 import java.awt;
29 import java.beans;
30 import javax.swing;
31 import javax.swing.border;
32 import javax.swing.plaf;
33 import javax.swing.plaf.basic;
34 import javax.swing.table;
35
36 import sun.swing.plaf.synth;
37 import sun.swing.table;
38
39 /**
40 * SynthTableHeaderUI implementation
41 *
42 * @author Alan Chung
43 * @author Philip Milne
44 */
45 class SynthTableHeaderUI extends BasicTableHeaderUI implements
46 PropertyChangeListener, SynthUI {
47
48 //
49 // Instance Variables
50 //
51
52 private TableCellRenderer prevRenderer = null;
53
54 private SynthStyle style;
55
56 public static ComponentUI createUI(JComponent h) {
57 return new SynthTableHeaderUI();
58 }
59
60 protected void installDefaults() {
61 prevRenderer = header.getDefaultRenderer();
62 if (prevRenderer instanceof UIResource) {
63 header.setDefaultRenderer(new HeaderRenderer());
64 }
65 updateStyle(header);
66 }
67
68 private void updateStyle(JTableHeader c) {
69 SynthContext context = getContext(c, ENABLED);
70 SynthStyle oldStyle = style;
71 style = SynthLookAndFeel.updateStyle(context, this);
72 if (style != oldStyle) {
73 if (oldStyle != null) {
74 uninstallKeyboardActions();
75 installKeyboardActions();
76 }
77 }
78 context.dispose();
79 }
80
81 protected void installListeners() {
82 super.installListeners();
83 header.addPropertyChangeListener(this);
84 }
85
86 protected void uninstallDefaults() {
87 if (header.getDefaultRenderer() instanceof HeaderRenderer) {
88 header.setDefaultRenderer(prevRenderer);
89 }
90
91 SynthContext context = getContext(header, ENABLED);
92
93 style.uninstallDefaults(context);
94 context.dispose();
95 style = null;
96 }
97
98 protected void uninstallListeners() {
99 header.removePropertyChangeListener(this);
100 super.uninstallListeners();
101 }
102
103 public void update(Graphics g, JComponent c) {
104 SynthContext context = getContext(c);
105
106 SynthLookAndFeel.update(context, g);
107 context.getPainter().paintTableHeaderBackground(context,
108 g, 0, 0, c.getWidth(), c.getHeight());
109 paint(context, g);
110 context.dispose();
111 }
112
113 public void paint(Graphics g, JComponent c) {
114 SynthContext context = getContext(c);
115
116 paint(context, g);
117 context.dispose();
118 }
119
120 protected void paint(SynthContext context, Graphics g) {
121 super.paint(g, context.getComponent());
122 }
123
124 public void paintBorder(SynthContext context, Graphics g, int x,
125 int y, int w, int h) {
126 context.getPainter().paintTableHeaderBorder(context, g, x, y, w, h);
127 }
128 //
129 // SynthUI
130 //
131 public SynthContext getContext(JComponent c) {
132 return getContext(c, getComponentState(c));
133 }
134
135 private SynthContext getContext(JComponent c, int state) {
136 return SynthContext.getContext(SynthContext.class, c,
137 SynthLookAndFeel.getRegion(c), style, state);
138 }
139
140 private Region getRegion(JComponent c) {
141 return SynthLookAndFeel.getRegion(c);
142 }
143
144 private int getComponentState(JComponent c) {
145 return SynthLookAndFeel.getComponentState(c);
146 }
147
148 public void propertyChange(PropertyChangeEvent evt) {
149 if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
150 updateStyle((JTableHeader)evt.getSource());
151 }
152 }
153
154 @Override
155 protected void rolloverColumnUpdated(int oldColumn, int newColumn) {
156 header.repaint(header.getHeaderRect(oldColumn));
157 header.repaint(header.getHeaderRect(newColumn));
158 }
159
160 private class HeaderRenderer extends DefaultTableCellHeaderRenderer {
161 HeaderRenderer() {
162 setHorizontalAlignment(JLabel.LEADING);
163 }
164
165 @Override
166 public Component getTableCellRendererComponent(JTable table, Object value,
167 boolean isSelected,
168 boolean hasFocus,
169 int row, int column) {
170
171 boolean hasRollover = (column == getRolloverColumn());
172 if (isSelected || hasRollover || hasFocus) {
173 SynthLookAndFeel.setSelectedUI((SynthLabelUI)SynthLookAndFeel.
174 getUIOfType(getUI(), SynthLabelUI.class),
175 isSelected, hasFocus, table.isEnabled(),
176 hasRollover);
177 } else {
178 SynthLookAndFeel.resetSelectedUI();
179 }
180
181 super.getTableCellRendererComponent(table, value, isSelected,
182 hasFocus, row, column);
183
184 return this;
185 }
186
187 public void setBorder(Border border) {
188 if (border instanceof SynthBorder) {
189 super.setBorder(border);
190 }
191 }
192
193 public String getName() {
194 String name = super.getName();
195 if (name == null) {
196 return "TableHeader.renderer";
197 }
198 return name;
199 }
200 }
201 }