|
1 /* |
|
2 * Copyright 2002-2003 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 sun.awt.X11; |
|
27 |
|
28 import java.awt.*; |
|
29 import java.awt.peer.*; |
|
30 |
|
31 class XLabelPeer extends XComponentPeer implements LabelPeer { |
|
32 /** |
|
33 * Create the label |
|
34 */ |
|
35 |
|
36 static final int TEXT_XPAD = 8; |
|
37 static final int TEXT_YPAD = 6; |
|
38 String label; |
|
39 int alignment; |
|
40 |
|
41 FontMetrics cachedFontMetrics; |
|
42 Font oldfont; |
|
43 |
|
44 FontMetrics getFontMetrics() |
|
45 { |
|
46 if (cachedFontMetrics != null) |
|
47 return cachedFontMetrics; |
|
48 else return getFontMetrics(getPeerFont()); |
|
49 |
|
50 } |
|
51 |
|
52 void preInit(XCreateWindowParams params) { |
|
53 super.preInit(params); |
|
54 Label target = (Label) this.target; |
|
55 label = target.getText(); |
|
56 if (label == null) { |
|
57 label = ""; |
|
58 } |
|
59 alignment = target.getAlignment(); |
|
60 } |
|
61 |
|
62 XLabelPeer(Label target) { |
|
63 super(target); |
|
64 } |
|
65 |
|
66 /** |
|
67 * Minimum size. |
|
68 */ |
|
69 public Dimension getMinimumSize() { |
|
70 FontMetrics fm = getFontMetrics(); |
|
71 int w; |
|
72 try { |
|
73 w = fm.stringWidth(label); |
|
74 } |
|
75 catch (NullPointerException e) { |
|
76 w = 0; |
|
77 } |
|
78 return new Dimension(w + TEXT_XPAD, |
|
79 fm.getAscent() + fm.getMaxDescent() + TEXT_YPAD); |
|
80 } |
|
81 |
|
82 |
|
83 /** |
|
84 * Paint the label |
|
85 */ |
|
86 // NOTE: This method is called by privileged threads. |
|
87 // DO NOT INVOKE CLIENT CODE ON THIS THREAD! |
|
88 public void paint(Graphics g) { |
|
89 int textX = 0; |
|
90 int textY = 0; |
|
91 g.setColor(getPeerBackground()); |
|
92 g.fillRect(0, 0, width, height); |
|
93 |
|
94 Font f = getPeerFont(); |
|
95 g.setFont(f); |
|
96 FontMetrics fm = g.getFontMetrics(); |
|
97 |
|
98 if (cachedFontMetrics == null) |
|
99 { |
|
100 cachedFontMetrics = fm; |
|
101 } |
|
102 else |
|
103 { |
|
104 if (oldfont != f) |
|
105 cachedFontMetrics = fm; |
|
106 } |
|
107 |
|
108 switch (alignment) { |
|
109 case Label.LEFT: |
|
110 textX = 2; |
|
111 textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2; |
|
112 break; |
|
113 case Label.RIGHT: |
|
114 textX = width - (fm.stringWidth(label) + 2); |
|
115 textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2; |
|
116 break; |
|
117 case Label.CENTER: |
|
118 textX = (width - fm.stringWidth(label)) / 2; |
|
119 textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2; |
|
120 break; |
|
121 } |
|
122 if (isEnabled()) { |
|
123 g.setColor(getPeerForeground()); |
|
124 g.drawString(label, textX, textY); |
|
125 } |
|
126 else { |
|
127 g.setColor(getPeerBackground().brighter()); |
|
128 g.drawString(label, textX, textY); |
|
129 g.setColor(getPeerBackground().darker()); |
|
130 g.drawString(label, textX - 1, textY - 1); |
|
131 } |
|
132 } |
|
133 |
|
134 public void setText(String text) { |
|
135 label = text; |
|
136 if (label == null) { |
|
137 label = ""; |
|
138 } |
|
139 repaint(); |
|
140 } |
|
141 public void setFont(Font f) { |
|
142 super.setFont(f); |
|
143 target.repaint(); |
|
144 } |
|
145 |
|
146 public void setAlignment(int align) { |
|
147 alignment = align; |
|
148 repaint(); |
|
149 } |
|
150 } |