author | serb |
Fri, 15 Jul 2011 19:23:03 +0400 | |
changeset 10096 | f9ac9a52952d |
parent 5506 | 202f599c92aa |
child 21270 | 8a0fc12b81a2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2002, 2003, 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 |
package sun.awt.X11; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.awt.peer.*; |
|
29 |
import java.awt.event.MouseEvent; |
|
30 |
import java.awt.event.FocusEvent; |
|
31 |
import java.awt.event.KeyEvent; |
|
32 |
import java.awt.event.ActionEvent; |
|
33 |
import javax.swing.plaf.basic.*; |
|
34 |
import javax.swing.SwingUtilities; |
|
35 |
import javax.swing.SwingConstants; |
|
36 |
public class XButtonPeer extends XComponentPeer implements ButtonPeer { |
|
10096
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
37 |
private boolean pressed; |
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
38 |
private boolean armed; |
2 | 39 |
private Insets focusInsets; |
40 |
private Insets borderInsets; |
|
41 |
private Insets contentAreaInsets; |
|
42 |
||
43 |
private final static String propertyPrefix = "Button" + "."; |
|
44 |
protected Color focusColor = SystemColor.windowText; |
|
45 |
||
46 |
private boolean disposed = false; |
|
47 |
||
48 |
String label; |
|
49 |
||
50 |
protected String getPropertyPrefix() { |
|
51 |
return propertyPrefix; |
|
52 |
} |
|
53 |
||
54 |
void preInit(XCreateWindowParams params) { |
|
55 |
super.preInit(params); |
|
56 |
borderInsets = new Insets(2,2,2,2); |
|
57 |
focusInsets = new Insets(0,0,0,0); |
|
58 |
contentAreaInsets = new Insets(3,3,3,3); |
|
59 |
} |
|
60 |
||
61 |
||
62 |
public XButtonPeer(Button target) { |
|
63 |
super(target); |
|
64 |
pressed = false; |
|
65 |
armed = false; |
|
66 |
label = target.getLabel(); |
|
67 |
updateMotifColors(getPeerBackground()); |
|
68 |
} |
|
69 |
||
70 |
public void dispose() { |
|
71 |
synchronized (target) |
|
72 |
{ |
|
73 |
disposed = true; |
|
74 |
} |
|
75 |
super.dispose(); |
|
76 |
} |
|
77 |
||
78 |
public boolean isFocusable() { |
|
79 |
return true; |
|
80 |
} |
|
81 |
||
82 |
public void setLabel(java.lang.String label) { |
|
83 |
this.label = label; |
|
84 |
repaint(); |
|
85 |
} |
|
86 |
public void setBackground(Color c) { |
|
87 |
updateMotifColors(c); |
|
88 |
super.setBackground(c); |
|
89 |
} |
|
90 |
||
91 |
void handleJavaMouseEvent(MouseEvent e) { |
|
92 |
super.handleJavaMouseEvent(e); |
|
93 |
int id = e.getID(); |
|
94 |
switch (id) { |
|
95 |
case MouseEvent.MOUSE_PRESSED: |
|
96 |
if (XToolkit.isLeftMouseButton(e) ) { |
|
97 |
Button b = (Button) e.getSource(); |
|
98 |
||
99 |
if(b.contains(e.getX(), e.getY())) { |
|
100 |
if (!isEnabled()) { |
|
101 |
// Disabled buttons ignore all input... |
|
102 |
return; |
|
103 |
} |
|
104 |
pressed = true; |
|
105 |
armed = true; |
|
106 |
repaint(); |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
break; |
|
111 |
||
112 |
case MouseEvent.MOUSE_RELEASED: |
|
113 |
if (XToolkit.isLeftMouseButton(e)) { |
|
114 |
if (armed) |
|
115 |
{ |
|
116 |
action(e.getWhen(),e.getModifiers()); |
|
117 |
} |
|
118 |
pressed = false; |
|
119 |
armed = false; |
|
120 |
repaint(); |
|
121 |
} |
|
122 |
||
123 |
break; |
|
124 |
||
125 |
case MouseEvent.MOUSE_ENTERED: |
|
126 |
if (pressed) |
|
127 |
armed = true; |
|
128 |
break; |
|
129 |
case MouseEvent.MOUSE_EXITED: |
|
130 |
armed = false; |
|
131 |
break; |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
||
136 |
// NOTE: This method is called by privileged threads. |
|
137 |
// DO NOT INVOKE CLIENT CODE ON THIS THREAD! |
|
138 |
public void action(final long when, final int modifiers) { |
|
139 |
postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED, |
|
140 |
((Button)target).getActionCommand(), |
|
141 |
when, modifiers)); |
|
142 |
} |
|
143 |
||
144 |
||
145 |
public void focusGained(FocusEvent e) { |
|
146 |
super.focusGained(e); |
|
147 |
repaint(); |
|
148 |
} |
|
149 |
||
150 |
public void focusLost(FocusEvent e) { |
|
151 |
super.focusLost(e); |
|
152 |
repaint(); |
|
153 |
} |
|
154 |
||
155 |
void handleJavaKeyEvent(KeyEvent e) { |
|
156 |
int id = e.getID(); |
|
157 |
switch (id) { |
|
158 |
case KeyEvent.KEY_PRESSED: |
|
159 |
if (e.getKeyCode() == KeyEvent.VK_SPACE) |
|
160 |
{ |
|
161 |
pressed=true; |
|
162 |
armed=true; |
|
163 |
repaint(); |
|
164 |
action(e.getWhen(),e.getModifiers()); |
|
165 |
} |
|
166 |
||
167 |
break; |
|
168 |
||
169 |
case KeyEvent.KEY_RELEASED: |
|
170 |
if (e.getKeyCode() == KeyEvent.VK_SPACE) |
|
171 |
{ |
|
172 |
pressed = false; |
|
173 |
armed = false; |
|
174 |
repaint(); |
|
175 |
} |
|
176 |
||
177 |
break; |
|
178 |
||
179 |
||
180 |
} |
|
181 |
} |
|
182 |
||
183 |
public Dimension getMinimumSize() { |
|
184 |
FontMetrics fm = getFontMetrics(getPeerFont()); |
|
185 |
if ( label == null ) { |
|
186 |
label = ""; |
|
187 |
} |
|
188 |
return new Dimension(fm.stringWidth(label) + 14, |
|
189 |
fm.getHeight() + 8); |
|
190 |
} |
|
191 |
||
192 |
/** |
|
193 |
* DEPRECATED |
|
194 |
*/ |
|
195 |
public Dimension minimumSize() { |
|
196 |
return getMinimumSize(); |
|
197 |
} |
|
10096
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
198 |
/** |
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
199 |
* This method is called from Toolkit Thread and so it should not call any |
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
200 |
* client code. |
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
201 |
*/ |
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
202 |
@Override |
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
203 |
void paintPeer(final Graphics g) { |
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
204 |
if (!disposed) { |
2 | 205 |
Dimension size = getPeerSize(); |
206 |
g.setColor( getPeerBackground() ); /* erase the existing button remains */ |
|
207 |
g.fillRect(0,0, size.width , size.height); |
|
208 |
paintBorder(g,borderInsets.left, |
|
209 |
borderInsets.top, |
|
210 |
size.width-(borderInsets.left+borderInsets.right), |
|
211 |
size.height-(borderInsets.top+borderInsets.bottom)); |
|
212 |
||
213 |
FontMetrics fm = g.getFontMetrics(); |
|
214 |
||
215 |
Rectangle textRect,iconRect,viewRect; |
|
216 |
||
217 |
textRect = new Rectangle(); |
|
218 |
viewRect = new Rectangle(); |
|
219 |
iconRect = new Rectangle(); |
|
220 |
||
221 |
||
222 |
viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right); |
|
223 |
viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom); |
|
224 |
viewRect.x = contentAreaInsets.left; |
|
10096
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
225 |
viewRect.y = contentAreaInsets.top; |
2 | 226 |
String llabel = (label != null) ? label : ""; |
227 |
// layout the text and icon |
|
228 |
String text = SwingUtilities.layoutCompoundLabel( |
|
229 |
fm, llabel, null, |
|
230 |
SwingConstants.CENTER, SwingConstants.CENTER, |
|
231 |
SwingConstants.CENTER, SwingConstants.CENTER, |
|
232 |
viewRect, iconRect, textRect, 0); |
|
233 |
||
234 |
Font f = getPeerFont(); |
|
235 |
||
236 |
g.setFont(f); |
|
237 |
||
238 |
// perform UI specific press action, e.g. Windows L&F shifts text |
|
239 |
if (pressed && armed) { |
|
240 |
paintButtonPressed(g,target); |
|
241 |
} |
|
242 |
||
243 |
paintText(g, target, textRect, text); |
|
244 |
||
245 |
if (hasFocus()) { |
|
246 |
// paint UI specific focus |
|
247 |
paintFocus(g,focusInsets.left, |
|
248 |
focusInsets.top, |
|
249 |
size.width-(focusInsets.left+focusInsets.right)-1, |
|
250 |
size.height-(focusInsets.top+focusInsets.bottom)-1); |
|
251 |
} |
|
252 |
} |
|
253 |
flush(); |
|
254 |
} |
|
255 |
||
256 |
public void paintBorder(Graphics g, int x, int y, int w, int h) { |
|
257 |
drawMotif3DRect(g, x, y, w-1, h-1, pressed); |
|
258 |
} |
|
259 |
||
260 |
public void setFont(Font f) { |
|
261 |
super.setFont(f); |
|
262 |
target.repaint(); |
|
263 |
} |
|
264 |
protected void paintFocus(Graphics g, int x, int y, int w, int h){ |
|
265 |
g.setColor(focusColor); |
|
266 |
g.drawRect(x,y,w,h); |
|
267 |
} |
|
268 |
||
269 |
protected void paintButtonPressed(Graphics g, Component b) { |
|
270 |
Dimension size = getPeerSize(); |
|
271 |
g.setColor(selectColor); |
|
272 |
g.fillRect(contentAreaInsets.left, |
|
273 |
contentAreaInsets.top, |
|
274 |
size.width-(contentAreaInsets.left+contentAreaInsets.right), |
|
275 |
size.height-(contentAreaInsets.top+contentAreaInsets.bottom)); |
|
276 |
||
277 |
} |
|
278 |
protected void paintText(Graphics g, Component c, Rectangle textRect, String text) { |
|
279 |
FontMetrics fm = g.getFontMetrics(); |
|
280 |
||
281 |
int mnemonicIndex = -1; |
|
282 |
||
283 |
/* Draw the Text */ |
|
284 |
if(isEnabled()) { |
|
285 |
/*** paint the text normally */ |
|
286 |
g.setColor(getPeerForeground()); |
|
287 |
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() ); |
|
288 |
} |
|
289 |
else { |
|
290 |
/*** paint the text disabled ***/ |
|
291 |
g.setColor(getPeerBackground().brighter()); |
|
292 |
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex, |
|
293 |
textRect.x, textRect.y + fm.getAscent()); |
|
10096
f9ac9a52952d
6596915: JCK-runtime-6a/tests/api/java_awt/Component/index.html tesPaintAll fails
serb
parents:
5506
diff
changeset
|
294 |
g.setColor(getPeerBackground().darker()); |
2 | 295 |
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex, |
296 |
textRect.x - 1, textRect.y + fm.getAscent() - 1); |
|
297 |
} |
|
298 |
} |
|
299 |
} |