author | serb |
Thu, 24 Oct 2013 14:32:07 +0400 | |
changeset 21270 | 8a0fc12b81a2 |
parent 12822 | 611c4a708b4e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
21270
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
2 |
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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! |
|
10096
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
88 |
@Override |
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
89 |
void paintPeer(final Graphics g) { |
2 | 90 |
int textX = 0; |
91 |
int textY = 0; |
|
92 |
g.setColor(getPeerBackground()); |
|
93 |
g.fillRect(0, 0, width, height); |
|
94 |
||
95 |
Font f = getPeerFont(); |
|
96 |
g.setFont(f); |
|
97 |
FontMetrics fm = g.getFontMetrics(); |
|
98 |
||
99 |
if (cachedFontMetrics == null) |
|
100 |
{ |
|
101 |
cachedFontMetrics = fm; |
|
102 |
} |
|
103 |
else |
|
104 |
{ |
|
105 |
if (oldfont != f) |
|
106 |
cachedFontMetrics = fm; |
|
107 |
} |
|
108 |
||
109 |
switch (alignment) { |
|
110 |
case Label.LEFT: |
|
111 |
textX = 2; |
|
112 |
textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2; |
|
113 |
break; |
|
114 |
case Label.RIGHT: |
|
115 |
textX = width - (fm.stringWidth(label) + 2); |
|
116 |
textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2; |
|
117 |
break; |
|
118 |
case Label.CENTER: |
|
119 |
textX = (width - fm.stringWidth(label)) / 2; |
|
120 |
textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2; |
|
121 |
break; |
|
122 |
} |
|
123 |
if (isEnabled()) { |
|
124 |
g.setColor(getPeerForeground()); |
|
125 |
g.drawString(label, textX, textY); |
|
126 |
} |
|
127 |
else { |
|
128 |
g.setColor(getPeerBackground().brighter()); |
|
129 |
g.drawString(label, textX, textY); |
|
130 |
g.setColor(getPeerBackground().darker()); |
|
131 |
g.drawString(label, textX - 1, textY - 1); |
|
132 |
} |
|
133 |
} |
|
134 |
||
21270
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
135 |
@Override |
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
136 |
public void setText(String label) { |
2 | 137 |
if (label == null) { |
138 |
label = ""; |
|
139 |
} |
|
21270
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
140 |
if (!label.equals(this.label)) { |
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
141 |
this.label = label; |
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
142 |
repaint(); |
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
143 |
} |
2 | 144 |
} |
145 |
||
21270
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
146 |
@Override |
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
147 |
public void setAlignment(final int alignment) { |
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
148 |
if (this.alignment != alignment) { |
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
149 |
this.alignment = alignment; |
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
150 |
repaint(); |
8a0fc12b81a2
7090424: TestGlyphVectorLayout failed automately with java.lang.StackOverflowError
serb
parents:
12822
diff
changeset
|
151 |
} |
2 | 152 |
} |
153 |
} |