KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsTableHeaderUI


1 /*
2  * @(#)WindowsTableHeaderUI.java 1.15 06/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.windows;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.plaf.basic.*;
13 import javax.swing.plaf.*;
14 import javax.swing.table.*;
15 import javax.swing.border.*;
16 import javax.swing.*;
17 import javax.swing.event.*;
18
19 import com.sun.java.swing.plaf.windows.TMSchema.*;
20 import com.sun.java.swing.plaf.windows.XPStyle.Skin;
21
22
23 public class WindowsTableHeaderUI extends BasicTableHeaderUI {
24     private TableCellRenderer originalHeaderRenderer;
25     private int rolloverColumn = -1;
26
27     public static ComponentUI createUI(JComponent h) {
28         return new WindowsTableHeaderUI();
29     }
30
31     public void installUI(JComponent c) {
32     super.installUI(c);
33
34     if (XPStyle.getXP() != null) {
35         originalHeaderRenderer = header.getDefaultRenderer();
36         if (originalHeaderRenderer instanceof UIResource) {
37         header.setDefaultRenderer(new XPDefaultRenderer());
38         }
39     }
40     }
41
42     private void updateRolloverColumn(MouseEvent e) {
43     if (header.getDraggedColumn() == null &&
44         header.contains(e.getPoint())) {
45
46         int col = header.columnAtPoint(e.getPoint());
47         if (col != rolloverColumn) {
48         rolloverColumn = col;
49         header.repaint();
50         }
51     }
52     }
53
54     protected MouseInputListener createMouseInputListener() {
55     if (XPStyle.getXP() != null) {
56         return new MouseInputHandler() {
57         public void mouseMoved(MouseEvent e) {
58             super.mouseMoved(e);
59             updateRolloverColumn(e);
60         }
61
62         public void mouseEntered(MouseEvent e) {
63             super.mouseEntered(e);
64             updateRolloverColumn(e);
65         }
66
67         public void mouseExited(MouseEvent e) {
68             super.mouseExited(e);
69             rolloverColumn = -1;
70             header.repaint();
71         }
72
73         public void mousePressed(MouseEvent e) {
74             super.mousePressed(e);
75             if (header.getReorderingAllowed()) {
76             rolloverColumn = -1;
77             header.repaint();
78             }
79         }
80
81         public void mouseDragged(MouseEvent e) {
82             super.mouseDragged(e);
83             updateRolloverColumn(e);
84         }
85
86         public void mouseReleased(MouseEvent e) {
87             super.mouseReleased(e);
88             updateRolloverColumn(e);
89         }
90         };
91     } else {
92         return super.createMouseInputListener();
93     }
94     }
95
96     public void uninstallUI(JComponent c) {
97     if (header.getDefaultRenderer() instanceof XPDefaultRenderer) {
98         header.setDefaultRenderer(originalHeaderRenderer);
99     }
100     super.uninstallUI(c);
101     }
102
103     private class XPDefaultRenderer extends DefaultTableCellRenderer implements UIResource {
104         Skin skin;
105     boolean isSelected, hasFocus, hasRollover;
106     int column;
107
108     public Component getTableCellRendererComponent(JTable table, Object JavaDoc value,
109                                boolean isSelected, boolean hasFocus,
110                                int row, int column) {
111         if (table != null) {
112         JTableHeader header = table.getTableHeader();
113         if (header != null) {
114             setFont(header.getFont());
115         }
116         }
117         this.isSelected = isSelected;
118         this.hasFocus = hasFocus;
119         this.column = column;
120         this.hasRollover = (column == rolloverColumn);
121             if (skin == null || skin.getContentMargin() == null) {
122                 skin = XPStyle.getXP().getSkin(header, Part.HP_HEADERITEM);
123             }
124         setText((value == null) ? "" : value.toString());
125         setBorder(new EmptyBorder(skin.getContentMargin()));
126
127         return this;
128     }
129
130     private int viewIndexForColumn(TableColumn aColumn) {
131         TableColumnModel cm = header.getColumnModel();
132         for (int column = 0; column < cm.getColumnCount(); column++) {
133         if (cm.getColumn(column) == aColumn) {
134             return column;
135         }
136         }
137         return -1;
138     }
139
140     public void paint(Graphics g) {
141         Dimension size = getSize();
142             State state = State.NORMAL;
143         if (column == viewIndexForColumn(header.getDraggedColumn())) {
144                 state = State.PRESSED;
145         } else if (isSelected || hasFocus || hasRollover) {
146                 state = State.HOT;
147         }
148             skin.paintSkin(g, 0, 0, size.width-1, size.height-1, state);
149         super.paint(g);
150     }
151     }
152 }
153
154
Popular Tags