2
|
1 |
/*
|
|
2 |
* Copyright 1997-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 com.sun.java.swing.plaf.windows;
|
|
27 |
|
|
28 |
import java.awt.*;
|
|
29 |
import javax.swing.*;
|
|
30 |
import javax.swing.border.*;
|
|
31 |
import javax.swing.plaf.*;
|
|
32 |
import javax.swing.plaf.basic.*;
|
|
33 |
import javax.swing.table.*;
|
|
34 |
|
|
35 |
import static com.sun.java.swing.plaf.windows.TMSchema.*;
|
|
36 |
import static com.sun.java.swing.plaf.windows.XPStyle.*;
|
|
37 |
import sun.swing.table.*;
|
|
38 |
|
|
39 |
|
|
40 |
public class WindowsTableHeaderUI extends BasicTableHeaderUI {
|
|
41 |
private TableCellRenderer originalHeaderRenderer;
|
|
42 |
|
|
43 |
public static ComponentUI createUI(JComponent h) {
|
|
44 |
return new WindowsTableHeaderUI();
|
|
45 |
}
|
|
46 |
|
|
47 |
public void installUI(JComponent c) {
|
|
48 |
super.installUI(c);
|
|
49 |
|
|
50 |
if (XPStyle.getXP() != null) {
|
|
51 |
originalHeaderRenderer = header.getDefaultRenderer();
|
|
52 |
if (originalHeaderRenderer instanceof UIResource) {
|
|
53 |
header.setDefaultRenderer(new XPDefaultRenderer());
|
|
54 |
}
|
|
55 |
}
|
|
56 |
}
|
|
57 |
|
|
58 |
public void uninstallUI(JComponent c) {
|
|
59 |
if (header.getDefaultRenderer() instanceof XPDefaultRenderer) {
|
|
60 |
header.setDefaultRenderer(originalHeaderRenderer);
|
|
61 |
}
|
|
62 |
super.uninstallUI(c);
|
|
63 |
}
|
|
64 |
|
|
65 |
@Override
|
|
66 |
protected void rolloverColumnUpdated(int oldColumn, int newColumn) {
|
|
67 |
if (XPStyle.getXP() != null) {
|
|
68 |
header.repaint(header.getHeaderRect(oldColumn));
|
|
69 |
header.repaint(header.getHeaderRect(newColumn));
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
private class XPDefaultRenderer extends DefaultTableCellHeaderRenderer {
|
|
74 |
Skin skin;
|
|
75 |
boolean isSelected, hasFocus, hasRollover;
|
|
76 |
int column;
|
|
77 |
|
|
78 |
XPDefaultRenderer() {
|
|
79 |
setHorizontalAlignment(LEADING);
|
|
80 |
}
|
|
81 |
|
|
82 |
public Component getTableCellRendererComponent(JTable table, Object value,
|
|
83 |
boolean isSelected, boolean hasFocus,
|
|
84 |
int row, int column) {
|
|
85 |
super.getTableCellRendererComponent(table, value, isSelected,
|
|
86 |
hasFocus, row, column);
|
|
87 |
this.isSelected = isSelected;
|
|
88 |
this.hasFocus = hasFocus;
|
|
89 |
this.column = column;
|
|
90 |
this.hasRollover = (column == getRolloverColumn());
|
|
91 |
if (skin == null) {
|
|
92 |
skin = XPStyle.getXP().getSkin(header, Part.HP_HEADERITEM);
|
|
93 |
}
|
|
94 |
Insets margins = skin.getContentMargin();
|
|
95 |
Border border = null;
|
|
96 |
int contentTop = 0;
|
|
97 |
int contentLeft = 0;
|
|
98 |
int contentBottom = 0;
|
|
99 |
int contentRight = 0;
|
|
100 |
if (margins != null) {
|
|
101 |
contentTop = margins.top;
|
|
102 |
contentLeft = margins.left;
|
|
103 |
contentBottom = margins.bottom;
|
|
104 |
contentRight = margins.right;
|
|
105 |
}
|
|
106 |
/* idk:
|
|
107 |
* Both on Vista and XP there is some offset to the
|
|
108 |
* HP_HEADERITEM content. It does not seem to come from
|
|
109 |
* Prop.CONTENTMARGINS. Do not know where it is defined.
|
|
110 |
* using some hardcoded values.
|
|
111 |
*/
|
|
112 |
contentLeft += 5;
|
|
113 |
contentBottom += 4;
|
|
114 |
contentRight += 5;
|
|
115 |
|
|
116 |
/* On Vista sortIcon is painted above the header's text.
|
|
117 |
* We use border to paint it.
|
|
118 |
*/
|
|
119 |
Icon sortIcon;
|
|
120 |
if (WindowsLookAndFeel.isOnVista()
|
|
121 |
&& ((sortIcon = getIcon()) instanceof javax.swing.plaf.UIResource
|
|
122 |
|| sortIcon == null)) {
|
|
123 |
contentTop += 1;
|
|
124 |
setIcon(null);
|
|
125 |
sortIcon = null;
|
|
126 |
SortOrder sortOrder =
|
|
127 |
getColumnSortOrder(header.getTable(), column);
|
|
128 |
if (sortOrder != null) {
|
|
129 |
switch (sortOrder) {
|
|
130 |
case ASCENDING:
|
|
131 |
sortIcon =
|
|
132 |
UIManager.getIcon("Table.ascendingSortIcon");
|
|
133 |
break;
|
|
134 |
case DESCENDING:
|
|
135 |
sortIcon =
|
|
136 |
UIManager.getIcon("Table.descendingSortIcon");
|
|
137 |
break;
|
|
138 |
}
|
|
139 |
}
|
|
140 |
if (sortIcon != null) {
|
|
141 |
contentBottom = sortIcon.getIconHeight();
|
|
142 |
border = new IconBorder(sortIcon, contentTop, contentLeft,
|
|
143 |
contentBottom, contentRight);
|
|
144 |
} else {
|
|
145 |
sortIcon =
|
|
146 |
UIManager.getIcon("Table.ascendingSortIcon");
|
|
147 |
int sortIconHeight =
|
|
148 |
(sortIcon != null) ? sortIcon.getIconHeight() : 0;
|
|
149 |
if (sortIconHeight != 0) {
|
|
150 |
contentBottom = sortIconHeight;
|
|
151 |
}
|
|
152 |
border =
|
|
153 |
new EmptyBorder(
|
|
154 |
sortIconHeight + contentTop, contentLeft,
|
|
155 |
contentBottom, contentRight);
|
|
156 |
}
|
|
157 |
} else {
|
|
158 |
contentTop += 3;
|
|
159 |
border = new EmptyBorder(contentTop, contentLeft,
|
|
160 |
contentBottom, contentRight);
|
|
161 |
}
|
|
162 |
setBorder(border);
|
|
163 |
return this;
|
|
164 |
}
|
|
165 |
|
|
166 |
private int viewIndexForColumn(TableColumn aColumn) {
|
|
167 |
if (aColumn != null) {
|
|
168 |
return header.getTable().convertColumnIndexToView(
|
|
169 |
aColumn.getModelIndex());
|
|
170 |
}
|
|
171 |
return -1;
|
|
172 |
}
|
|
173 |
|
|
174 |
public void paint(Graphics g) {
|
|
175 |
Dimension size = getSize();
|
|
176 |
State state = State.NORMAL;
|
|
177 |
if (column == viewIndexForColumn(header.getDraggedColumn())) {
|
|
178 |
state = State.PRESSED;
|
|
179 |
} else if (isSelected || hasFocus || hasRollover) {
|
|
180 |
state = State.HOT;
|
|
181 |
}
|
|
182 |
/* on Vista there are more states for sorted columns */
|
|
183 |
if (WindowsLookAndFeel.isOnVista()) {
|
|
184 |
SortOrder sortOrder = getColumnSortOrder(header.getTable(), column);
|
|
185 |
if (sortOrder != null) {
|
|
186 |
switch(sortOrder) {
|
|
187 |
case ASCENDING:
|
|
188 |
/* falls through */
|
|
189 |
case DESCENDING:
|
|
190 |
switch (state) {
|
|
191 |
case NORMAL:
|
|
192 |
state = State.SORTEDNORMAL;
|
|
193 |
break;
|
|
194 |
case PRESSED:
|
|
195 |
state = State.SORTEDPRESSED;
|
|
196 |
break;
|
|
197 |
case HOT:
|
|
198 |
state = State.SORTEDHOT;
|
|
199 |
break;
|
|
200 |
default:
|
|
201 |
/* do nothing */
|
|
202 |
}
|
|
203 |
default :
|
|
204 |
/* do nothing */
|
|
205 |
}
|
|
206 |
}
|
|
207 |
}
|
|
208 |
skin.paintSkin(g, 0, 0, size.width-1, size.height-1, state);
|
|
209 |
super.paint(g);
|
|
210 |
}
|
|
211 |
}
|
|
212 |
|
|
213 |
/**
|
|
214 |
* A border with an Icon at the middle of the top side.
|
|
215 |
* Outer insets can be provided for this border.
|
|
216 |
*/
|
|
217 |
private static class IconBorder implements Border, UIResource{
|
|
218 |
private final Icon icon;
|
|
219 |
private final int top;
|
|
220 |
private final int left;
|
|
221 |
private final int bottom;
|
|
222 |
private final int right;
|
|
223 |
/**
|
|
224 |
* Creates this border;
|
|
225 |
* @param icon - icon to paint for this border
|
|
226 |
* @param top, left, bottom, right - outer insets for this border
|
|
227 |
*/
|
|
228 |
public IconBorder(Icon icon, int top, int left,
|
|
229 |
int bottom, int right) {
|
|
230 |
this.icon = icon;
|
|
231 |
this.top = top;
|
|
232 |
this.left = left;
|
|
233 |
this.bottom = bottom;
|
|
234 |
this.right = right;
|
|
235 |
}
|
|
236 |
public Insets getBorderInsets(Component c) {
|
|
237 |
return new Insets(icon.getIconHeight() + top, left, bottom, right);
|
|
238 |
}
|
|
239 |
public boolean isBorderOpaque() {
|
|
240 |
return false;
|
|
241 |
}
|
|
242 |
public void paintBorder(Component c, Graphics g, int x, int y,
|
|
243 |
int width, int height) {
|
|
244 |
icon.paintIcon(c, g,
|
|
245 |
x + left + (width - left - right - icon.getIconWidth()) / 2,
|
|
246 |
y + top);
|
|
247 |
}
|
|
248 |
}
|
|
249 |
}
|