2
|
1 |
/*
|
|
2 |
* Copyright 2003-2007 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.ComponentPeer;
|
|
30 |
import java.awt.peer.TextAreaPeer;
|
|
31 |
import java.awt.event.*;
|
|
32 |
import javax.swing.event.DocumentListener;
|
|
33 |
import javax.swing.event.DocumentEvent;
|
|
34 |
import javax.swing.JTextArea;
|
|
35 |
import javax.swing.JComponent;
|
|
36 |
import javax.swing.JScrollPane;
|
|
37 |
import javax.swing.JScrollBar;
|
|
38 |
import javax.swing.plaf.ComponentUI;
|
|
39 |
import com.sun.java.swing.plaf.motif.MotifTextAreaUI;
|
|
40 |
import javax.swing.plaf.UIResource;
|
|
41 |
import javax.swing.UIDefaults;
|
|
42 |
import javax.swing.border.Border;
|
|
43 |
import javax.swing.border.EmptyBorder;
|
|
44 |
import javax.swing.border.CompoundBorder;
|
|
45 |
import javax.swing.border.AbstractBorder;
|
|
46 |
import javax.swing.JButton;
|
|
47 |
import javax.swing.JViewport;
|
|
48 |
import javax.swing.InputMap;
|
|
49 |
import javax.swing.SwingUtilities;
|
|
50 |
import javax.swing.TransferHandler;
|
|
51 |
import javax.swing.plaf.basic.BasicArrowButton;
|
|
52 |
import javax.swing.plaf.basic.BasicScrollBarUI;
|
|
53 |
import javax.swing.plaf.basic.BasicScrollPaneUI;
|
|
54 |
import java.beans.PropertyChangeEvent;
|
|
55 |
import java.beans.PropertyChangeListener;
|
|
56 |
import javax.swing.text.Caret;
|
|
57 |
import javax.swing.text.DefaultCaret;
|
|
58 |
import javax.swing.text.JTextComponent;
|
|
59 |
|
|
60 |
import javax.swing.plaf.BorderUIResource;
|
|
61 |
import java.awt.im.InputMethodRequests;
|
|
62 |
import sun.awt.CausedFocusEvent;
|
|
63 |
import sun.awt.ComponentAccessor;
|
|
64 |
|
|
65 |
|
|
66 |
class XTextAreaPeer extends XComponentPeer implements TextAreaPeer {
|
|
67 |
boolean editable;
|
|
68 |
|
|
69 |
AWTTextPane textPane;
|
|
70 |
AWTTextArea jtext;
|
|
71 |
|
|
72 |
boolean firstChangeSkipped;
|
|
73 |
|
|
74 |
private final JavaMouseEventHandler javaMouseEventHandler
|
|
75 |
= new JavaMouseEventHandler( this );
|
|
76 |
|
|
77 |
/* FIXME */
|
|
78 |
|
|
79 |
public long filterEvents(long mask) {
|
|
80 |
Thread.dumpStack();
|
|
81 |
return 0;
|
|
82 |
}
|
|
83 |
|
|
84 |
/* FIXME */
|
|
85 |
public Rectangle getCharacterBounds(int i) {
|
|
86 |
Thread.dumpStack();
|
|
87 |
return null;
|
|
88 |
}
|
|
89 |
|
|
90 |
public int getIndexAtPoint(int x, int y) {
|
|
91 |
Thread.dumpStack();
|
|
92 |
return 0;
|
|
93 |
}
|
|
94 |
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Create a Text area.
|
|
98 |
*/
|
|
99 |
XTextAreaPeer(TextArea target) {
|
|
100 |
super( target );
|
|
101 |
|
|
102 |
// some initializations require that target be set even
|
|
103 |
// though init(target) has not been called
|
|
104 |
this.target = target;
|
|
105 |
|
|
106 |
//ComponentAccessor.enableEvents(target,AWTEvent.MOUSE_WHEEL_EVENT_MASK);
|
|
107 |
target.enableInputMethods(true);
|
|
108 |
|
|
109 |
firstChangeSkipped = false;
|
|
110 |
String text = ((TextArea)target).getText();
|
|
111 |
jtext = new AWTTextArea(text, this);
|
|
112 |
jtext.setWrapStyleWord(true);
|
|
113 |
jtext.getDocument().addDocumentListener(jtext);
|
|
114 |
XToolkit.specialPeerMap.put(jtext,this);
|
|
115 |
jtext.enableInputMethods(true);
|
|
116 |
textPane = new AWTTextPane(jtext,this, target.getParent());
|
|
117 |
|
|
118 |
setBounds(x, y, width, height, SET_BOUNDS);
|
|
119 |
textPane.setVisible(true);
|
|
120 |
textPane.validate();
|
|
121 |
|
|
122 |
foreground = ComponentAccessor.getForeground(target);
|
|
123 |
if (foreground == null) {
|
|
124 |
foreground = SystemColor.textText;
|
|
125 |
}
|
|
126 |
setForeground(foreground);
|
|
127 |
|
|
128 |
background = ComponentAccessor.getBackground(target);
|
|
129 |
if (background == null) {
|
|
130 |
if (target.isEditable()) background = SystemColor.text;
|
|
131 |
else background = SystemColor.control;
|
|
132 |
}
|
|
133 |
setBackground(background);
|
|
134 |
|
|
135 |
if (!target.isBackgroundSet()) {
|
|
136 |
// This is a way to set the background color of the TextArea
|
|
137 |
// without calling setBackground - go through reflection
|
|
138 |
ComponentAccessor.setBackground(target, background);
|
|
139 |
}
|
|
140 |
if (!target.isForegroundSet()) {
|
|
141 |
target.setForeground(SystemColor.textText);
|
|
142 |
}
|
|
143 |
|
|
144 |
setFont(font);
|
|
145 |
|
|
146 |
int start = target.getSelectionStart();
|
|
147 |
int end = target.getSelectionEnd();
|
|
148 |
|
|
149 |
if (end > start) {
|
|
150 |
select(start, end);
|
|
151 |
}
|
|
152 |
// Fix for 5100200
|
|
153 |
// Restoring Motif behaviour
|
|
154 |
// Since the end position of the selected text can be greater then the length of the text,
|
|
155 |
// so we should set caret to max position of the text
|
|
156 |
int caretPosition = Math.min(end, text.length());
|
|
157 |
setCaretPosition(caretPosition);
|
|
158 |
|
|
159 |
setEditable(target.isEditable());
|
|
160 |
|
|
161 |
setScrollBarVisibility();
|
|
162 |
// set the text of this object to the text of its target
|
|
163 |
setTextImpl(target.getText()); //?? should this be setText
|
|
164 |
|
|
165 |
// After this line we should not change the component's text
|
|
166 |
firstChangeSkipped = true;
|
|
167 |
}
|
|
168 |
|
|
169 |
public void dispose() {
|
|
170 |
XToolkit.specialPeerMap.remove(jtext);
|
|
171 |
jtext.removeNotify();
|
|
172 |
textPane.removeNotify();
|
|
173 |
super.dispose();
|
|
174 |
}
|
|
175 |
|
|
176 |
void setScrollBarVisibility() {
|
|
177 |
int visibility = ((TextArea)target).getScrollbarVisibility();
|
|
178 |
jtext.setLineWrap(false);
|
|
179 |
|
|
180 |
if (visibility == TextArea.SCROLLBARS_NONE) {
|
|
181 |
textPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
|
182 |
textPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
|
|
183 |
jtext.setLineWrap(true);
|
|
184 |
}
|
|
185 |
else if (visibility == TextArea.SCROLLBARS_BOTH) {
|
|
186 |
|
|
187 |
textPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
|
|
188 |
textPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
|
|
189 |
}
|
|
190 |
else if (visibility == TextArea.SCROLLBARS_VERTICAL_ONLY) {
|
|
191 |
textPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
|
192 |
textPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
|
|
193 |
jtext.setLineWrap(true);
|
|
194 |
}
|
|
195 |
else if (visibility == TextArea.SCROLLBARS_HORIZONTAL_ONLY) {
|
|
196 |
textPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
|
|
197 |
textPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
/**
|
|
202 |
* Compute minimum size.
|
|
203 |
*/
|
|
204 |
public Dimension getMinimumSize() {
|
|
205 |
return getMinimumSize(10, 60);
|
|
206 |
}
|
|
207 |
|
|
208 |
public Dimension getPreferredSize(int rows, int cols) {
|
|
209 |
return getMinimumSize(rows, cols);
|
|
210 |
}
|
|
211 |
|
|
212 |
/**
|
|
213 |
* @see java.awt.peer.TextAreaPeer
|
|
214 |
*/
|
|
215 |
|
|
216 |
public Dimension getMinimumSize(int rows, int cols) {
|
|
217 |
/* Dimension d = null;
|
|
218 |
if (jtext != null) {
|
|
219 |
d = jtext.getMinimumSize(rows,cols);
|
|
220 |
}
|
|
221 |
return d;
|
|
222 |
*/
|
|
223 |
|
|
224 |
int vsbwidth=0;
|
|
225 |
int hsbheight=0;
|
|
226 |
|
|
227 |
JScrollBar vsb = textPane.getVerticalScrollBar();
|
|
228 |
if (vsb != null) {
|
|
229 |
vsbwidth = vsb.getMinimumSize().width;
|
|
230 |
}
|
|
231 |
|
|
232 |
JScrollBar hsb = textPane.getHorizontalScrollBar();
|
|
233 |
if (hsb != null) {
|
|
234 |
hsbheight = hsb.getMinimumSize().height;
|
|
235 |
}
|
|
236 |
|
|
237 |
Font f = jtext.getFont();
|
|
238 |
FontMetrics fm = jtext.getFontMetrics(f);
|
|
239 |
|
|
240 |
return new Dimension(fm.charWidth('0') * cols + /*2*XMARGIN +*/ vsbwidth,
|
|
241 |
fm.getHeight() * rows + /*2*YMARGIN +*/ hsbheight);
|
|
242 |
}
|
|
243 |
|
|
244 |
public boolean isFocusable() {
|
|
245 |
return true;
|
|
246 |
}
|
|
247 |
|
|
248 |
public void setVisible(boolean b) {
|
|
249 |
super.setVisible(b);
|
|
250 |
if (textPane != null)
|
|
251 |
textPane.setVisible(b);
|
|
252 |
}
|
|
253 |
|
|
254 |
void repaintText() {
|
|
255 |
jtext.repaintNow();
|
|
256 |
}
|
|
257 |
|
|
258 |
public void focusGained(FocusEvent e) {
|
|
259 |
super.focusGained(e);
|
|
260 |
jtext.forwardFocusGained(e);
|
|
261 |
}
|
|
262 |
|
|
263 |
public void focusLost(FocusEvent e) {
|
|
264 |
super.focusLost(e);
|
|
265 |
jtext.forwardFocusLost(e);
|
|
266 |
}
|
|
267 |
|
|
268 |
|
|
269 |
/**
|
|
270 |
* Paint the component
|
|
271 |
* this method is called when the repaint instruction has been used
|
|
272 |
*/
|
|
273 |
|
|
274 |
public void repaint() {
|
|
275 |
if (textPane != null) {
|
|
276 |
//textPane.validate();
|
|
277 |
textPane.repaint();
|
|
278 |
}
|
|
279 |
}
|
|
280 |
|
|
281 |
public void paint(Graphics g) {
|
|
282 |
if (textPane != null) {
|
|
283 |
textPane.paint(g);
|
|
284 |
}
|
|
285 |
}
|
|
286 |
|
|
287 |
public void setBounds(int x, int y, int width, int height, int op) {
|
|
288 |
super.setBounds(x, y, width, height, op);
|
|
289 |
if (textPane != null) {
|
|
290 |
/*
|
|
291 |
* Fixed 6277332, 6198290:
|
|
292 |
* the coordinates is coming (to peer): relatively to closest HW parent
|
|
293 |
* the coordinates is setting (to textPane): relatively to closest ANY parent
|
|
294 |
* the parent of peer is target.getParent()
|
|
295 |
* the parent of textPane is the same
|
|
296 |
* see 6277332, 6198290 for more information
|
|
297 |
*/
|
|
298 |
int childX = x;
|
|
299 |
int childY = y;
|
|
300 |
Component parent = target.getParent();
|
|
301 |
// we up to heavyweight parent in order to be sure
|
|
302 |
// that the coordinates of the text pane is relatively to closest parent
|
|
303 |
while (parent.isLightweight()){
|
|
304 |
childX -= parent.getX();
|
|
305 |
childY -= parent.getY();
|
|
306 |
parent = parent.getParent();
|
|
307 |
}
|
|
308 |
textPane.setBounds(childX,childY,width,height);
|
|
309 |
textPane.validate();
|
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
void handleJavaKeyEvent(KeyEvent e) {
|
|
314 |
ComponentAccessor.processEvent(jtext,e);
|
|
315 |
}
|
|
316 |
|
|
317 |
public boolean handlesWheelScrolling() { return true; }
|
|
318 |
|
|
319 |
void handleJavaMouseWheelEvent(MouseWheelEvent e) {
|
|
320 |
ComponentAccessor.processEvent(textPane,e);
|
|
321 |
}
|
|
322 |
|
|
323 |
public void handleJavaMouseEvent( MouseEvent e ) {
|
|
324 |
super.handleJavaMouseEvent( e );
|
|
325 |
javaMouseEventHandler.handle( e );
|
|
326 |
}
|
|
327 |
|
|
328 |
void handleJavaInputMethodEvent(InputMethodEvent e) {
|
|
329 |
if (jtext != null)
|
|
330 |
jtext.processInputMethodEventPublic((InputMethodEvent)e);
|
|
331 |
}
|
|
332 |
|
|
333 |
/**
|
|
334 |
* @see java.awt.peer.TextComponentPeer
|
|
335 |
*/
|
|
336 |
public void select(int s, int e) {
|
|
337 |
jtext.select(s,e);
|
|
338 |
// Fixed 5100806
|
|
339 |
// We must take care that Swing components repainted correctly
|
|
340 |
jtext.repaint();
|
|
341 |
}
|
|
342 |
|
|
343 |
public void setBackground(Color c) {
|
|
344 |
super.setBackground(c);
|
|
345 |
// synchronized (getStateLock()) {
|
|
346 |
// background = c;
|
|
347 |
// }
|
|
348 |
if (jtext != null) {
|
|
349 |
jtext.setBackground(c);
|
|
350 |
jtext.setSelectedTextColor(c);
|
|
351 |
}
|
|
352 |
// repaintText();
|
|
353 |
}
|
|
354 |
|
|
355 |
public void setForeground(Color c) {
|
|
356 |
super.setForeground(c);
|
|
357 |
// synchronized (getStateLock()) {
|
|
358 |
// foreground = c;
|
|
359 |
// }
|
|
360 |
if (jtext != null) {
|
|
361 |
jtext.setForeground(foreground);
|
|
362 |
jtext.setSelectionColor(foreground);
|
|
363 |
jtext.setCaretColor(foreground);
|
|
364 |
}
|
|
365 |
// repaintText();
|
|
366 |
}
|
|
367 |
|
|
368 |
public void setFont(Font f) {
|
|
369 |
super.setFont(f);
|
|
370 |
// synchronized (getStateLock()) {
|
|
371 |
// font = f;
|
|
372 |
// }
|
|
373 |
if (jtext != null) {
|
|
374 |
jtext.setFont(font);
|
|
375 |
}
|
|
376 |
textPane.validate();
|
|
377 |
}
|
|
378 |
|
|
379 |
|
|
380 |
/**
|
|
381 |
* @see java.awt.peer.TextComponentPeer
|
|
382 |
*/
|
|
383 |
public void setEditable(boolean editable) {
|
|
384 |
this.editable = editable;
|
|
385 |
if (jtext != null) jtext.setEditable(editable);
|
|
386 |
repaintText();
|
|
387 |
}
|
|
388 |
|
|
389 |
/**
|
|
390 |
* @see java.awt.peer.ComponentPeer
|
|
391 |
*/
|
|
392 |
public void setEnabled(boolean enabled) {
|
|
393 |
super.setEnabled(enabled);
|
|
394 |
if (jtext != null) {
|
|
395 |
jtext.setEnabled(enabled);
|
|
396 |
jtext.repaint();
|
|
397 |
}
|
|
398 |
}
|
|
399 |
|
|
400 |
/**
|
|
401 |
* @see java.awt.peer.TextComponentPeer
|
|
402 |
*/
|
|
403 |
public InputMethodRequests getInputMethodRequests() {
|
|
404 |
if (jtext != null) return jtext.getInputMethodRequests();
|
|
405 |
else return null;
|
|
406 |
}
|
|
407 |
|
|
408 |
/**
|
|
409 |
* @see java.awt.peer.TextComponentPeer
|
|
410 |
*/
|
|
411 |
public int getSelectionStart() {
|
|
412 |
return jtext.getSelectionStart();
|
|
413 |
}
|
|
414 |
|
|
415 |
/**
|
|
416 |
* @see java.awt.peer.TextComponentPeer
|
|
417 |
*/
|
|
418 |
public int getSelectionEnd() {
|
|
419 |
return jtext.getSelectionEnd();
|
|
420 |
}
|
|
421 |
|
|
422 |
/**
|
|
423 |
* @see java.awt.peer.TextComponentPeer
|
|
424 |
*/
|
|
425 |
public String getText() {
|
|
426 |
return jtext.getText();
|
|
427 |
}
|
|
428 |
|
|
429 |
/**
|
|
430 |
* @see java.awt.peer.TextComponentPeer
|
|
431 |
*/
|
|
432 |
public void setText(String txt) {
|
|
433 |
setTextImpl(txt);
|
|
434 |
repaintText();
|
|
435 |
}
|
|
436 |
|
|
437 |
protected boolean setTextImpl(String txt) {
|
|
438 |
if (jtext != null) {
|
|
439 |
// Please note that we do not want to post an event
|
|
440 |
// if setText() replaces an empty text by an empty text,
|
|
441 |
// that is, if component's text remains unchanged.
|
|
442 |
if (jtext.getDocument().getLength() == 0 && txt.length() == 0) {
|
|
443 |
return true;
|
|
444 |
}
|
|
445 |
|
|
446 |
// JTextArea.setText() posts two different events (remove & insert).
|
|
447 |
// Since we make no differences between text events,
|
|
448 |
// the document listener has to be disabled while
|
|
449 |
// JTextArea.setText() is called.
|
|
450 |
jtext.getDocument().removeDocumentListener(jtext);
|
|
451 |
jtext.setText(txt);
|
|
452 |
if (firstChangeSkipped) {
|
|
453 |
postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED));
|
|
454 |
}
|
|
455 |
jtext.getDocument().addDocumentListener(jtext);
|
|
456 |
}
|
|
457 |
return true;
|
|
458 |
}
|
|
459 |
|
|
460 |
/**
|
|
461 |
* insert the text "txt on position "pos" in the array lines
|
|
462 |
* @see java.awt.peer.TextAreaPeer
|
|
463 |
*/
|
|
464 |
public void insert(String txt, int p) {
|
|
465 |
if (jtext != null) {
|
|
466 |
boolean doScroll = (p >= jtext.getDocument().getLength() && jtext.getDocument().getLength() != 0);
|
|
467 |
jtext.insert(txt,p);
|
|
468 |
textPane.validate();
|
|
469 |
if (doScroll) {
|
|
470 |
JScrollBar bar = textPane.getVerticalScrollBar();
|
|
471 |
if (bar != null) {
|
|
472 |
bar.setValue(bar.getMaximum()-bar.getVisibleAmount());
|
|
473 |
}
|
|
474 |
}
|
|
475 |
}
|
|
476 |
}
|
|
477 |
|
|
478 |
/**
|
|
479 |
* replace the text between the position "s" and "e" with "txt"
|
|
480 |
* @see java.awt.peer.TextAreaPeer
|
|
481 |
*/
|
|
482 |
public void replaceRange(String txt, int s, int e) {
|
|
483 |
if (jtext != null) {
|
|
484 |
// JTextArea.replaceRange() posts two different events.
|
|
485 |
// Since we make no differences between text events,
|
|
486 |
// the document listener has to be disabled while
|
|
487 |
// JTextArea.replaceRange() is called.
|
|
488 |
jtext.getDocument().removeDocumentListener(jtext);
|
|
489 |
jtext.replaceRange(txt, s, e);
|
|
490 |
postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED));
|
|
491 |
jtext.getDocument().addDocumentListener(jtext);
|
|
492 |
}
|
|
493 |
}
|
|
494 |
|
|
495 |
/**
|
|
496 |
* to be implemented.
|
|
497 |
* @see java.awt.peer.TextComponentPeer
|
|
498 |
*/
|
|
499 |
public void setCaretPosition(int position) {
|
|
500 |
jtext.setCaretPosition(position);
|
|
501 |
}
|
|
502 |
|
|
503 |
/**
|
|
504 |
* to be implemented.
|
|
505 |
* @see java.awt.peer.TextComponentPeer
|
|
506 |
*/
|
|
507 |
public int getCaretPosition() {
|
|
508 |
return jtext.getCaretPosition();
|
|
509 |
}
|
|
510 |
|
|
511 |
/**
|
|
512 |
* DEPRECATED
|
|
513 |
* @see java.awt.peer.TextAreaPeer
|
|
514 |
*/
|
|
515 |
public void insertText(String txt, int pos) {
|
|
516 |
insert(txt, pos);
|
|
517 |
}
|
|
518 |
|
|
519 |
/**
|
|
520 |
* DEPRECATED
|
|
521 |
* @see java.awt.peer.TextAreaPeer
|
|
522 |
*/
|
|
523 |
public void replaceText(String txt, int start, int end) {
|
|
524 |
replaceRange(txt, start, end);
|
|
525 |
}
|
|
526 |
|
|
527 |
/**
|
|
528 |
* DEPRECATED
|
|
529 |
* @see java.awt.peer.TextAreaPeer
|
|
530 |
*/
|
|
531 |
public Dimension minimumSize(int rows, int cols) {
|
|
532 |
return getMinimumSize(rows, cols);
|
|
533 |
}
|
|
534 |
|
|
535 |
/**
|
|
536 |
* DEPRECATED
|
|
537 |
* @see java.awt.peer.TextAreaPeer
|
|
538 |
*/
|
|
539 |
public Dimension preferredSize(int rows, int cols) {
|
|
540 |
return getPreferredSize(rows, cols);
|
|
541 |
}
|
|
542 |
|
|
543 |
|
|
544 |
class AWTTextAreaUI extends MotifTextAreaUI {
|
|
545 |
/**
|
|
546 |
* Creates a UI for a JTextArea.
|
|
547 |
*
|
|
548 |
* @param c the text field
|
|
549 |
* @return the UI
|
|
550 |
*/
|
|
551 |
JTextArea jta;
|
|
552 |
|
|
553 |
protected String getPropertyPrefix() { return "TextArea"; }
|
|
554 |
|
|
555 |
public void installUI(JComponent c) {
|
|
556 |
super.installUI(c);
|
|
557 |
|
|
558 |
jta = (JTextArea) c;
|
|
559 |
|
|
560 |
JTextArea editor = jta;
|
|
561 |
|
|
562 |
UIDefaults uidefaults = XToolkit.getUIDefaults();
|
|
563 |
|
|
564 |
String prefix = getPropertyPrefix();
|
|
565 |
Font f = editor.getFont();
|
|
566 |
if ((f == null) || (f instanceof UIResource)) {
|
|
567 |
editor.setFont(uidefaults.getFont(prefix + ".font"));
|
|
568 |
}
|
|
569 |
|
|
570 |
Color bg = editor.getBackground();
|
|
571 |
if ((bg == null) || (bg instanceof UIResource)) {
|
|
572 |
editor.setBackground(uidefaults.getColor(prefix + ".background"));
|
|
573 |
}
|
|
574 |
|
|
575 |
Color fg = editor.getForeground();
|
|
576 |
if ((fg == null) || (fg instanceof UIResource)) {
|
|
577 |
editor.setForeground(uidefaults.getColor(prefix + ".foreground"));
|
|
578 |
}
|
|
579 |
|
|
580 |
Color color = editor.getCaretColor();
|
|
581 |
if ((color == null) || (color instanceof UIResource)) {
|
|
582 |
editor.setCaretColor(uidefaults.getColor(prefix + ".caretForeground"));
|
|
583 |
}
|
|
584 |
|
|
585 |
Color s = editor.getSelectionColor();
|
|
586 |
if ((s == null) || (s instanceof UIResource)) {
|
|
587 |
editor.setSelectionColor(uidefaults.getColor(prefix + ".selectionBackground"));
|
|
588 |
}
|
|
589 |
|
|
590 |
Color sfg = editor.getSelectedTextColor();
|
|
591 |
if ((sfg == null) || (sfg instanceof UIResource)) {
|
|
592 |
editor.setSelectedTextColor(uidefaults.getColor(prefix + ".selectionForeground"));
|
|
593 |
}
|
|
594 |
|
|
595 |
Color dfg = editor.getDisabledTextColor();
|
|
596 |
if ((dfg == null) || (dfg instanceof UIResource)) {
|
|
597 |
editor.setDisabledTextColor(uidefaults.getColor(prefix + ".inactiveForeground"));
|
|
598 |
}
|
|
599 |
|
|
600 |
Border b = new BevelBorder(false,SystemColor.controlDkShadow,SystemColor.controlLtHighlight);
|
|
601 |
editor.setBorder(new BorderUIResource.CompoundBorderUIResource(
|
|
602 |
b,new EmptyBorder(2, 2, 2, 2)));
|
|
603 |
|
|
604 |
Insets margin = editor.getMargin();
|
|
605 |
if (margin == null || margin instanceof UIResource) {
|
|
606 |
editor.setMargin(uidefaults.getInsets(prefix + ".margin"));
|
|
607 |
}
|
|
608 |
}
|
|
609 |
|
|
610 |
protected void installKeyboardActions() {
|
|
611 |
super.installKeyboardActions();
|
|
612 |
|
|
613 |
JTextComponent comp = getComponent();
|
|
614 |
|
|
615 |
UIDefaults uidefaults = XToolkit.getUIDefaults();
|
|
616 |
|
|
617 |
String prefix = getPropertyPrefix();
|
|
618 |
|
|
619 |
InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
|
|
620 |
|
|
621 |
if (map != null) {
|
|
622 |
SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
|
|
623 |
map);
|
|
624 |
}
|
|
625 |
}
|
|
626 |
|
|
627 |
protected Caret createCaret() {
|
|
628 |
return new XAWTCaret();
|
|
629 |
}
|
|
630 |
}
|
|
631 |
|
|
632 |
|
|
633 |
// TODO : fix this duplicate code
|
|
634 |
class XAWTCaret extends DefaultCaret {
|
|
635 |
public void focusGained(FocusEvent e) {
|
|
636 |
super.focusGained(e);
|
|
637 |
getComponent().repaint();
|
|
638 |
}
|
|
639 |
|
|
640 |
public void focusLost(FocusEvent e) {
|
|
641 |
super.focusLost(e);
|
|
642 |
getComponent().repaint();
|
|
643 |
}
|
|
644 |
|
|
645 |
// Fix for 5100950: textarea.getSelectedText() returns the de-selected text, on XToolkit
|
|
646 |
// Restoring Motif behaviour
|
|
647 |
// If the text is unhighlighted then we should sets the selection range to zero
|
|
648 |
public void setSelectionVisible(boolean vis) {
|
|
649 |
if (vis){
|
|
650 |
super.setSelectionVisible(vis);
|
|
651 |
}else{
|
|
652 |
// In order to de-select the selection
|
|
653 |
setDot(getDot());
|
|
654 |
}
|
|
655 |
}
|
|
656 |
}
|
|
657 |
|
|
658 |
|
|
659 |
class XAWTScrollBarButton extends BasicArrowButton
|
|
660 |
{
|
|
661 |
UIDefaults uidefaults = XToolkit.getUIDefaults();
|
|
662 |
private Color darkShadow = SystemColor.controlShadow;
|
|
663 |
private Color lightShadow = SystemColor.controlLtHighlight;
|
|
664 |
private Color buttonBack = uidefaults.getColor("ScrollBar.track");
|
|
665 |
|
|
666 |
public XAWTScrollBarButton(int direction)
|
|
667 |
{
|
|
668 |
super(direction);
|
|
669 |
|
|
670 |
switch (direction) {
|
|
671 |
case NORTH:
|
|
672 |
case SOUTH:
|
|
673 |
case EAST:
|
|
674 |
case WEST:
|
|
675 |
this.direction = direction;
|
|
676 |
break;
|
|
677 |
default:
|
|
678 |
throw new IllegalArgumentException("invalid direction");
|
|
679 |
}
|
|
680 |
|
|
681 |
setRequestFocusEnabled(false);
|
|
682 |
setOpaque(true);
|
|
683 |
setBackground(uidefaults.getColor("ScrollBar.thumb"));
|
|
684 |
setForeground(uidefaults.getColor("ScrollBar.foreground"));
|
|
685 |
}
|
|
686 |
|
|
687 |
public Dimension getPreferredSize() {
|
|
688 |
switch (direction) {
|
|
689 |
case NORTH:
|
|
690 |
case SOUTH:
|
|
691 |
return new Dimension(11, 12);
|
|
692 |
case EAST:
|
|
693 |
case WEST:
|
|
694 |
default:
|
|
695 |
return new Dimension(12, 11);
|
|
696 |
}
|
|
697 |
}
|
|
698 |
|
|
699 |
public Dimension getMinimumSize() {
|
|
700 |
return getPreferredSize();
|
|
701 |
}
|
|
702 |
|
|
703 |
public Dimension getMaximumSize() {
|
|
704 |
return getPreferredSize();
|
|
705 |
}
|
|
706 |
|
|
707 |
public boolean isFocusTraversable() {
|
|
708 |
return false;
|
|
709 |
}
|
|
710 |
|
|
711 |
public void paint(Graphics g)
|
|
712 |
{
|
|
713 |
int w = getWidth();
|
|
714 |
int h = getHeight();
|
|
715 |
|
|
716 |
if (isOpaque()) {
|
|
717 |
g.setColor(buttonBack);
|
|
718 |
g.fillRect(0, 0, w, h);
|
|
719 |
}
|
|
720 |
|
|
721 |
boolean isPressed = getModel().isPressed();
|
|
722 |
Color lead = (isPressed) ? darkShadow : lightShadow;
|
|
723 |
Color trail = (isPressed) ? lightShadow : darkShadow;
|
|
724 |
Color fill = getBackground();
|
|
725 |
|
|
726 |
int cx = w / 2;
|
|
727 |
int cy = h / 2;
|
|
728 |
int s = Math.min(w, h);
|
|
729 |
|
|
730 |
switch (direction) {
|
|
731 |
case NORTH:
|
|
732 |
g.setColor(lead);
|
|
733 |
g.drawLine(cx, 0, cx, 0);
|
|
734 |
for (int x = cx - 1, y = 1, dx = 1; y <= s - 2; y += 2) {
|
|
735 |
g.setColor(lead);
|
|
736 |
g.drawLine(x, y, x, y);
|
|
737 |
if (y >= (s - 2)) {
|
|
738 |
g.drawLine(x, y + 1, x, y + 1);
|
|
739 |
}
|
|
740 |
g.setColor(fill);
|
|
741 |
g.drawLine(x + 1, y, x + dx, y);
|
|
742 |
if (y < (s - 2)) {
|
|
743 |
g.drawLine(x, y + 1, x + dx + 1, y + 1);
|
|
744 |
}
|
|
745 |
g.setColor(trail);
|
|
746 |
g.drawLine(x + dx + 1, y, x + dx + 1, y);
|
|
747 |
if (y >= (s - 2)) {
|
|
748 |
g.drawLine(x + 1, y + 1, x + dx + 1, y + 1);
|
|
749 |
}
|
|
750 |
dx += 2;
|
|
751 |
x -= 1;
|
|
752 |
}
|
|
753 |
break;
|
|
754 |
|
|
755 |
case SOUTH:
|
|
756 |
g.setColor(trail);
|
|
757 |
g.drawLine(cx, s, cx, s);
|
|
758 |
for (int x = cx - 1, y = s - 1, dx = 1; y >= 1; y -= 2) {
|
|
759 |
g.setColor(lead);
|
|
760 |
g.drawLine(x, y, x, y);
|
|
761 |
if (y <= 2) {
|
|
762 |
g.drawLine(x, y - 1, x + dx + 1, y - 1);
|
|
763 |
}
|
|
764 |
g.setColor(fill);
|
|
765 |
g.drawLine(x + 1, y, x + dx, y);
|
|
766 |
if (y > 2) {
|
|
767 |
g.drawLine(x, y - 1, x + dx + 1, y - 1);
|
|
768 |
}
|
|
769 |
g.setColor(trail);
|
|
770 |
g.drawLine(x + dx + 1, y, x + dx + 1, y);
|
|
771 |
|
|
772 |
dx += 2;
|
|
773 |
x -= 1;
|
|
774 |
}
|
|
775 |
break;
|
|
776 |
|
|
777 |
case EAST:
|
|
778 |
g.setColor(lead);
|
|
779 |
g.drawLine(s, cy, s, cy);
|
|
780 |
for (int y = cy - 1, x = s - 1, dy = 1; x >= 1; x -= 2) {
|
|
781 |
g.setColor(lead);
|
|
782 |
g.drawLine(x, y, x, y);
|
|
783 |
if (x <= 2) {
|
|
784 |
g.drawLine(x - 1, y, x - 1, y + dy + 1);
|
|
785 |
}
|
|
786 |
g.setColor(fill);
|
|
787 |
g.drawLine(x, y + 1, x, y + dy);
|
|
788 |
if (x > 2) {
|
|
789 |
g.drawLine(x - 1, y, x - 1, y + dy + 1);
|
|
790 |
}
|
|
791 |
g.setColor(trail);
|
|
792 |
g.drawLine(x, y + dy + 1, x, y + dy + 1);
|
|
793 |
|
|
794 |
dy += 2;
|
|
795 |
y -= 1;
|
|
796 |
}
|
|
797 |
break;
|
|
798 |
|
|
799 |
case WEST:
|
|
800 |
g.setColor(trail);
|
|
801 |
g.drawLine(0, cy, 0, cy);
|
|
802 |
for (int y = cy - 1, x = 1, dy = 1; x <= s - 2; x += 2) {
|
|
803 |
g.setColor(lead);
|
|
804 |
g.drawLine(x, y, x, y);
|
|
805 |
if (x >= (s - 2)) {
|
|
806 |
g.drawLine(x + 1, y, x + 1, y);
|
|
807 |
}
|
|
808 |
g.setColor(fill);
|
|
809 |
g.drawLine(x, y + 1, x, y + dy);
|
|
810 |
if (x < (s - 2)) {
|
|
811 |
g.drawLine(x + 1, y, x + 1, y + dy + 1);
|
|
812 |
}
|
|
813 |
g.setColor(trail);
|
|
814 |
g.drawLine(x, y + dy + 1, x, y + dy + 1);
|
|
815 |
if (x >= (s - 2)) {
|
|
816 |
g.drawLine(x + 1, y + 1, x + 1, y + dy + 1);
|
|
817 |
}
|
|
818 |
dy += 2;
|
|
819 |
y -= 1;
|
|
820 |
}
|
|
821 |
break;
|
|
822 |
}
|
|
823 |
}
|
|
824 |
}
|
|
825 |
|
|
826 |
|
|
827 |
class XAWTScrollBarUI extends BasicScrollBarUI
|
|
828 |
{
|
|
829 |
public XAWTScrollBarUI() {
|
|
830 |
super();
|
|
831 |
}
|
|
832 |
|
|
833 |
protected void installDefaults()
|
|
834 |
{
|
|
835 |
super.installDefaults();
|
|
836 |
scrollbar.setBorder(new BevelBorder(false,SystemColor.controlDkShadow,SystemColor.controlLtHighlight) );
|
|
837 |
}
|
|
838 |
|
|
839 |
protected void configureScrollBarColors() {
|
|
840 |
UIDefaults uidefaults = XToolkit.getUIDefaults();
|
|
841 |
Color bg = scrollbar.getBackground();
|
|
842 |
if (bg == null || bg instanceof UIResource) {
|
|
843 |
scrollbar.setBackground(uidefaults.getColor("ScrollBar.background"));
|
|
844 |
}
|
|
845 |
|
|
846 |
Color fg = scrollbar.getForeground();
|
|
847 |
if (fg == null || fg instanceof UIResource) {
|
|
848 |
scrollbar.setForeground(uidefaults.getColor("ScrollBar.foreground"));
|
|
849 |
}
|
|
850 |
|
|
851 |
thumbHighlightColor = uidefaults.getColor("ScrollBar.thumbHighlight");
|
|
852 |
thumbLightShadowColor = uidefaults.getColor("ScrollBar.thumbShadow");
|
|
853 |
thumbDarkShadowColor = uidefaults.getColor("ScrollBar.thumbDarkShadow");
|
|
854 |
thumbColor = uidefaults.getColor("ScrollBar.thumb");
|
|
855 |
trackColor = uidefaults.getColor("ScrollBar.track");
|
|
856 |
|
|
857 |
trackHighlightColor = uidefaults.getColor("ScrollBar.trackHighlight");
|
|
858 |
|
|
859 |
}
|
|
860 |
|
|
861 |
protected JButton createDecreaseButton(int orientation) {
|
|
862 |
JButton b = new XAWTScrollBarButton(orientation);
|
|
863 |
return b;
|
|
864 |
|
|
865 |
}
|
|
866 |
|
|
867 |
protected JButton createIncreaseButton(int orientation) {
|
|
868 |
JButton b = new XAWTScrollBarButton(orientation);
|
|
869 |
return b;
|
|
870 |
}
|
|
871 |
|
|
872 |
public JButton getDecreaseButton(){
|
|
873 |
return decrButton;
|
|
874 |
}
|
|
875 |
|
|
876 |
public JButton getIncreaseButton(){
|
|
877 |
return incrButton;
|
|
878 |
}
|
|
879 |
|
|
880 |
public void paint(Graphics g, JComponent c) {
|
|
881 |
paintTrack(g, c, getTrackBounds());
|
|
882 |
Rectangle thumbBounds = getThumbBounds();
|
|
883 |
paintThumb(g, c, thumbBounds);
|
|
884 |
}
|
|
885 |
|
|
886 |
public void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
|
|
887 |
{
|
|
888 |
if(!scrollbar.isEnabled()) {
|
|
889 |
return;
|
|
890 |
}
|
|
891 |
|
|
892 |
if (thumbBounds.isEmpty())
|
|
893 |
thumbBounds = getTrackBounds();
|
|
894 |
|
|
895 |
int w = thumbBounds.width;
|
|
896 |
int h = thumbBounds.height;
|
|
897 |
|
|
898 |
g.translate(thumbBounds.x, thumbBounds.y);
|
|
899 |
g.setColor(thumbColor);
|
|
900 |
g.fillRect(0, 0, w-1, h-1);
|
|
901 |
|
|
902 |
g.setColor(thumbHighlightColor);
|
|
903 |
g.drawLine(0, 0, 0, h-1);
|
|
904 |
g.drawLine(1, 0, w-1, 0);
|
|
905 |
|
|
906 |
g.setColor(thumbLightShadowColor);
|
|
907 |
g.drawLine(1, h-1, w-1, h-1);
|
|
908 |
g.drawLine(w-1, 1, w-1, h-2);
|
|
909 |
|
|
910 |
g.translate(-thumbBounds.x, -thumbBounds.y);
|
|
911 |
}
|
|
912 |
}
|
|
913 |
|
|
914 |
|
|
915 |
class AWTTextArea extends JTextArea implements DocumentListener {
|
|
916 |
boolean isFocused = false;
|
|
917 |
XTextAreaPeer peer;
|
|
918 |
|
|
919 |
public AWTTextArea(String text, XTextAreaPeer peer) {
|
|
920 |
super(text);
|
|
921 |
setFocusable(false);
|
|
922 |
this.peer = peer;
|
|
923 |
}
|
|
924 |
|
|
925 |
public void insertUpdate(DocumentEvent e) {
|
|
926 |
if (peer != null) {
|
|
927 |
peer.postEvent(new TextEvent(peer.target,
|
|
928 |
TextEvent.TEXT_VALUE_CHANGED));
|
|
929 |
}
|
|
930 |
}
|
|
931 |
|
|
932 |
public void removeUpdate(DocumentEvent e) {
|
|
933 |
if (peer != null) {
|
|
934 |
peer.postEvent(new TextEvent(peer.target,
|
|
935 |
TextEvent.TEXT_VALUE_CHANGED));
|
|
936 |
}
|
|
937 |
}
|
|
938 |
|
|
939 |
public void changedUpdate(DocumentEvent e) {
|
|
940 |
if (peer != null) {
|
|
941 |
peer.postEvent(new TextEvent(peer.target,
|
|
942 |
TextEvent.TEXT_VALUE_CHANGED));
|
|
943 |
}
|
|
944 |
}
|
|
945 |
|
|
946 |
void forwardFocusGained( FocusEvent e) {
|
|
947 |
isFocused = true;
|
|
948 |
FocusEvent fe = CausedFocusEvent.retarget(e, this);
|
|
949 |
super.processFocusEvent(fe);
|
|
950 |
}
|
|
951 |
|
|
952 |
|
|
953 |
void forwardFocusLost( FocusEvent e) {
|
|
954 |
isFocused = false;
|
|
955 |
FocusEvent fe = CausedFocusEvent.retarget(e, this);
|
|
956 |
super.processFocusEvent(fe);
|
|
957 |
}
|
|
958 |
|
|
959 |
public boolean hasFocus() {
|
|
960 |
return isFocused;
|
|
961 |
}
|
|
962 |
|
|
963 |
public void repaintNow() {
|
|
964 |
paintImmediately(getBounds());
|
|
965 |
}
|
|
966 |
|
|
967 |
public void processMouseEventPublic(MouseEvent e) {
|
|
968 |
processMouseEvent(e);
|
|
969 |
}
|
|
970 |
|
|
971 |
public void processMouseMotionEventPublic(MouseEvent e) {
|
|
972 |
processMouseMotionEvent(e);
|
|
973 |
}
|
|
974 |
|
|
975 |
public void processInputMethodEventPublic(InputMethodEvent e) {
|
|
976 |
processInputMethodEvent(e);
|
|
977 |
}
|
|
978 |
|
|
979 |
public void updateUI() {
|
|
980 |
ComponentUI ui = new AWTTextAreaUI();
|
|
981 |
setUI(ui);
|
|
982 |
}
|
|
983 |
|
|
984 |
// Fix for 4915454 - override the default implementation to avoid
|
|
985 |
// loading SystemFlavorMap and associated classes.
|
|
986 |
public void setTransferHandler(TransferHandler newHandler) {
|
|
987 |
TransferHandler oldHandler = (TransferHandler)
|
|
988 |
getClientProperty(XTextTransferHelper.getTransferHandlerKey());
|
|
989 |
putClientProperty(XTextTransferHelper.getTransferHandlerKey(),
|
|
990 |
newHandler);
|
|
991 |
|
|
992 |
firePropertyChange("transferHandler", oldHandler, newHandler);
|
|
993 |
}
|
|
994 |
}
|
|
995 |
|
|
996 |
|
|
997 |
class XAWTScrollPaneUI extends BasicScrollPaneUI
|
|
998 |
{
|
|
999 |
private final Border vsbMarginBorderR = new EmptyBorder(0, 2, 0, 0);
|
|
1000 |
private final Border vsbMarginBorderL = new EmptyBorder(0, 0, 0, 2);
|
|
1001 |
private final Border hsbMarginBorder = new EmptyBorder(2, 0, 0, 0);
|
|
1002 |
|
|
1003 |
private Border vsbBorder;
|
|
1004 |
private Border hsbBorder;
|
|
1005 |
|
|
1006 |
private PropertyChangeListener propertyChangeHandler;
|
|
1007 |
|
|
1008 |
protected void installListeners(JScrollPane scrollPane) {
|
|
1009 |
super.installListeners(scrollPane);
|
|
1010 |
propertyChangeHandler = createPropertyChangeHandler();
|
|
1011 |
scrollPane.addPropertyChangeListener(propertyChangeHandler);
|
|
1012 |
}
|
|
1013 |
|
|
1014 |
public void paint(Graphics g, JComponent c) {
|
|
1015 |
Border vpBorder = scrollpane.getViewportBorder();
|
|
1016 |
if (vpBorder != null) {
|
|
1017 |
Rectangle r = scrollpane.getViewportBorderBounds();
|
|
1018 |
vpBorder.paintBorder(scrollpane, g, r.x, r.y, r.width, r.height);
|
|
1019 |
}
|
|
1020 |
}
|
|
1021 |
|
|
1022 |
protected void uninstallListeners(JScrollPane scrollPane) {
|
|
1023 |
super.uninstallListeners(scrollPane);
|
|
1024 |
scrollPane.removePropertyChangeListener(propertyChangeHandler);
|
|
1025 |
}
|
|
1026 |
|
|
1027 |
private PropertyChangeListener createPropertyChangeHandler() {
|
|
1028 |
return new PropertyChangeListener() {
|
|
1029 |
public void propertyChange(PropertyChangeEvent e) {
|
|
1030 |
String propertyName = e.getPropertyName();
|
|
1031 |
|
|
1032 |
if (propertyName.equals("componentOrientation")) {
|
|
1033 |
JScrollPane pane = (JScrollPane)e.getSource();
|
|
1034 |
JScrollBar vsb = pane.getVerticalScrollBar();
|
|
1035 |
if (vsb != null) {
|
|
1036 |
if (isLeftToRight(pane)) {
|
|
1037 |
vsbBorder = new CompoundBorder(new EmptyBorder(0, 4, 0, -4),
|
|
1038 |
vsb.getBorder());
|
|
1039 |
} else {
|
|
1040 |
vsbBorder = new CompoundBorder(new EmptyBorder(0, -4, 0, 4),
|
|
1041 |
vsb.getBorder());
|
|
1042 |
}
|
|
1043 |
vsb.setBorder(vsbBorder);
|
|
1044 |
}
|
|
1045 |
}
|
|
1046 |
}};
|
|
1047 |
}
|
|
1048 |
|
|
1049 |
boolean isLeftToRight( Component c ) {
|
|
1050 |
return c.getComponentOrientation().isLeftToRight();
|
|
1051 |
}
|
|
1052 |
|
|
1053 |
|
|
1054 |
protected void installDefaults(JScrollPane scrollpane) {
|
|
1055 |
Border b = scrollpane.getBorder();
|
|
1056 |
UIDefaults uidefaults = XToolkit.getUIDefaults();
|
|
1057 |
scrollpane.setBorder(uidefaults.getBorder("ScrollPane.border"));
|
|
1058 |
scrollpane.setBackground(uidefaults.getColor("ScrollPane.background"));
|
|
1059 |
scrollpane.setViewportBorder(uidefaults.getBorder("TextField.border"));
|
|
1060 |
JScrollBar vsb = scrollpane.getVerticalScrollBar();
|
|
1061 |
if (vsb != null) {
|
|
1062 |
if (isLeftToRight(scrollpane)) {
|
|
1063 |
vsbBorder = new CompoundBorder(vsbMarginBorderR,
|
|
1064 |
vsb.getBorder());
|
|
1065 |
}
|
|
1066 |
else {
|
|
1067 |
vsbBorder = new CompoundBorder(vsbMarginBorderL,
|
|
1068 |
vsb.getBorder());
|
|
1069 |
}
|
|
1070 |
vsb.setBorder(vsbBorder);
|
|
1071 |
}
|
|
1072 |
|
|
1073 |
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
|
|
1074 |
if (hsb != null) {
|
|
1075 |
hsbBorder = new CompoundBorder(hsbMarginBorder, hsb.getBorder());
|
|
1076 |
hsb.setBorder(hsbBorder);
|
|
1077 |
}
|
|
1078 |
}
|
|
1079 |
|
|
1080 |
protected void uninstallDefaults(JScrollPane c) {
|
|
1081 |
super.uninstallDefaults(c);
|
|
1082 |
|
|
1083 |
JScrollBar vsb = scrollpane.getVerticalScrollBar();
|
|
1084 |
if (vsb != null) {
|
|
1085 |
if (vsb.getBorder() == vsbBorder) {
|
|
1086 |
vsb.setBorder(null);
|
|
1087 |
}
|
|
1088 |
vsbBorder = null;
|
|
1089 |
}
|
|
1090 |
|
|
1091 |
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
|
|
1092 |
if (hsb != null) {
|
|
1093 |
if (hsb.getBorder() == hsbBorder) {
|
|
1094 |
hsb.setBorder(null);
|
|
1095 |
}
|
|
1096 |
hsbBorder = null;
|
|
1097 |
}
|
|
1098 |
}
|
|
1099 |
}
|
|
1100 |
|
|
1101 |
|
|
1102 |
private class AWTTextPane extends JScrollPane implements FocusListener {
|
|
1103 |
JTextArea jtext;
|
|
1104 |
XWindow xwin;
|
|
1105 |
|
|
1106 |
Color control = SystemColor.control;
|
|
1107 |
Color focus = SystemColor.activeCaptionBorder;
|
|
1108 |
|
|
1109 |
public AWTTextPane(JTextArea jt, XWindow xwin, Container parent) {
|
|
1110 |
super(jt);
|
|
1111 |
this.xwin = xwin;
|
|
1112 |
setDoubleBuffered(true);
|
|
1113 |
jt.addFocusListener(this);
|
|
1114 |
ComponentAccessor.setParent(this,parent);
|
|
1115 |
setViewportBorder(new BevelBorder(false,SystemColor.controlDkShadow,SystemColor.controlLtHighlight) );
|
|
1116 |
this.jtext = jt;
|
|
1117 |
setFocusable(false);
|
|
1118 |
addNotify();
|
|
1119 |
}
|
|
1120 |
|
|
1121 |
public void focusGained(FocusEvent e) {
|
|
1122 |
Graphics g = getGraphics();
|
|
1123 |
Rectangle r = getViewportBorderBounds();
|
|
1124 |
g.setColor(focus);
|
|
1125 |
g.drawRect(r.x,r.y,r.width,r.height);
|
|
1126 |
g.dispose();
|
|
1127 |
}
|
|
1128 |
|
|
1129 |
public void focusLost(FocusEvent e) {
|
|
1130 |
Graphics g = getGraphics();
|
|
1131 |
Rectangle r = getViewportBorderBounds();
|
|
1132 |
g.setColor(control);
|
|
1133 |
g.drawRect(r.x,r.y,r.width,r.height);
|
|
1134 |
g.dispose();
|
|
1135 |
}
|
|
1136 |
|
|
1137 |
public Window getRealParent() {
|
|
1138 |
return (Window) xwin.target;
|
|
1139 |
}
|
|
1140 |
|
|
1141 |
public ComponentPeer getPeer() {
|
|
1142 |
return (ComponentPeer) (xwin);
|
|
1143 |
}
|
|
1144 |
|
|
1145 |
public void updateUI() {
|
|
1146 |
ComponentUI ui = new XAWTScrollPaneUI();
|
|
1147 |
setUI(ui);
|
|
1148 |
}
|
|
1149 |
|
|
1150 |
public JScrollBar createVerticalScrollBar() {
|
|
1151 |
return new XAWTScrollBar(JScrollBar.VERTICAL);
|
|
1152 |
}
|
|
1153 |
|
|
1154 |
public JScrollBar createHorizontalScrollBar() {
|
|
1155 |
return new XAWTScrollBar(JScrollBar.HORIZONTAL);
|
|
1156 |
}
|
|
1157 |
|
|
1158 |
public JTextArea getTextArea () {
|
|
1159 |
return this.jtext;
|
|
1160 |
}
|
|
1161 |
|
|
1162 |
public Graphics getGraphics() {
|
|
1163 |
return xwin.getGraphics();
|
|
1164 |
}
|
|
1165 |
|
|
1166 |
|
|
1167 |
class XAWTScrollBar extends ScrollBar {
|
|
1168 |
|
|
1169 |
public XAWTScrollBar(int i) {
|
|
1170 |
super(i);
|
|
1171 |
setFocusable(false);
|
|
1172 |
}
|
|
1173 |
|
|
1174 |
public void updateUI() {
|
|
1175 |
ComponentUI ui = new XAWTScrollBarUI();
|
|
1176 |
setUI(ui);
|
|
1177 |
}
|
|
1178 |
}
|
|
1179 |
}
|
|
1180 |
|
|
1181 |
static class BevelBorder extends AbstractBorder implements UIResource {
|
|
1182 |
private Color darkShadow = SystemColor.controlDkShadow;
|
|
1183 |
private Color lightShadow = SystemColor.controlLtHighlight;
|
|
1184 |
private Color control = SystemColor.controlShadow;
|
|
1185 |
private boolean isRaised;
|
|
1186 |
|
|
1187 |
public BevelBorder(boolean isRaised, Color darkShadow, Color lightShadow) {
|
|
1188 |
this.isRaised = isRaised;
|
|
1189 |
this.darkShadow = darkShadow;
|
|
1190 |
this.lightShadow = lightShadow;
|
|
1191 |
}
|
|
1192 |
|
|
1193 |
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
|
|
1194 |
g.setColor((isRaised) ? lightShadow : darkShadow);
|
|
1195 |
g.drawLine(x, y, x+w-1, y); // top
|
|
1196 |
g.drawLine(x, y+h-1, x, y+1); // left
|
|
1197 |
|
|
1198 |
g.setColor(control);
|
|
1199 |
g.drawLine(x+1, y+1, x+w-2, y+1); // top
|
|
1200 |
g.drawLine(x+1, y+h-1, x+1, y+1); // left
|
|
1201 |
|
|
1202 |
g.setColor((isRaised) ? darkShadow : lightShadow);
|
|
1203 |
g.drawLine(x+1, y+h-1, x+w-1, y+h-1); // bottom
|
|
1204 |
g.drawLine(x+w-1, y+h-1, x+w-1, y+1); // right
|
|
1205 |
|
|
1206 |
g.setColor(control);
|
|
1207 |
g.drawLine(x+1, y+h-2, x+w-2, y+h-2); // bottom
|
|
1208 |
g.drawLine(x+w-2, y+h-2, x+w-2, y+1); // right
|
|
1209 |
}
|
|
1210 |
|
|
1211 |
public Insets getBorderInsets(Component c) {
|
|
1212 |
return getBorderInsets(c, new Insets(0,0,0,0));
|
|
1213 |
}
|
|
1214 |
|
|
1215 |
public Insets getBorderInsets(Component c, Insets insets) {
|
|
1216 |
insets.top = insets.left = insets.bottom = insets.right = 2;
|
|
1217 |
return insets;
|
|
1218 |
}
|
|
1219 |
|
|
1220 |
public boolean isOpaque(Component c) {
|
|
1221 |
return true;
|
|
1222 |
}
|
|
1223 |
}
|
|
1224 |
|
|
1225 |
|
|
1226 |
// This class dispatches 'MouseEvent's to 'XTextAreaPeer''s (hidden)
|
|
1227 |
// subcomponents, and overrides mouse cursor, e.g. for scrollbars.
|
|
1228 |
//
|
|
1229 |
// However, current dispatching is a kind of fake, and is tuned to do only
|
|
1230 |
// what is necessary/possible. E.g. no additional mouse-exited/entered
|
|
1231 |
// events are generated, when mouse exits scrollbar and enters viewport
|
|
1232 |
// with JTextArea inside. Actually, no events are ever generated here (for
|
|
1233 |
// now). They are only dispatched as correctly as possible/neccessary.
|
|
1234 |
//
|
|
1235 |
// In future, it would be better to replace fake-emulation of grab-detection
|
|
1236 |
// and event-dispatching here, by reusing some common implementation of this
|
|
1237 |
// functionality. Mouse-cursor setting should also be freed of hacked
|
|
1238 |
// overloading here.
|
|
1239 |
|
|
1240 |
private static final class JavaMouseEventHandler {
|
|
1241 |
private final XTextAreaPeer outer;
|
|
1242 |
private final Pointer current = new Pointer();
|
|
1243 |
private boolean grabbed = false;
|
|
1244 |
|
|
1245 |
JavaMouseEventHandler( XTextAreaPeer outer ) {
|
|
1246 |
this.outer = outer;
|
|
1247 |
}
|
|
1248 |
|
|
1249 |
|
|
1250 |
// 1. We can make grab-tracking emulation here more robust to variations in
|
|
1251 |
// in mouse-events order and consistence. E.g. by using such code:
|
|
1252 |
// if( grabbed && event.getID()==MouseEvent.MOUSE_MOVED ) grabbed = false;
|
|
1253 |
// Or we can also use 'assert'ions.
|
|
1254 |
// 2. WARNING: Currently, while grab-detection mechanism _here_ says, that
|
|
1255 |
// grab is in progress, we do not update 'current'. In case 'current'
|
|
1256 |
// is set to a scrollbar or to a scroll-button, then references to their
|
|
1257 |
// 'Component'-instances are "remembered". And events are dispatched to
|
|
1258 |
// these remembered components, without checking, if XTextAreaPeer has
|
|
1259 |
// replaced these instances with another ones. This also aplies to
|
|
1260 |
// mouse-drags-from-outside (see comment in 'grabbed_update' method).
|
|
1261 |
|
|
1262 |
void handle( MouseEvent event ) {
|
|
1263 |
if ( ! grabbed ) {
|
|
1264 |
// dispatch() needs up-to-date pointer in ungrabbed case.
|
|
1265 |
setPointerToUnderEventPoint( event );
|
|
1266 |
}
|
|
1267 |
dispatch( event );
|
|
1268 |
boolean wasGrabbed = grabbed;
|
|
1269 |
grabbed_update( event );
|
|
1270 |
if ( wasGrabbed && ! grabbed ) {
|
|
1271 |
setPointerToUnderEventPoint( event );
|
|
1272 |
}
|
|
1273 |
setCursor();
|
|
1274 |
}
|
|
1275 |
|
|
1276 |
// Following is internally private:
|
|
1277 |
|
|
1278 |
// Here dispatching is performed, of 'MouseEvent's to (some)
|
|
1279 |
// 'XTextAreaPeer''s (hidden) subcomponents.
|
|
1280 |
private void dispatch( MouseEvent event ) {
|
|
1281 |
switch( current.getType() )
|
|
1282 |
{
|
|
1283 |
case TEXT:
|
|
1284 |
Point point = toViewportChildLocalSpace(
|
|
1285 |
outer.textPane.getViewport(), event.getPoint() );
|
|
1286 |
XTextAreaPeer.AWTTextArea jtext = outer.jtext;
|
|
1287 |
MouseEvent newEvent = newMouseEvent( jtext, point, event );
|
|
1288 |
int id = newEvent.getID();
|
|
1289 |
if ( id==MouseEvent.MOUSE_MOVED || id==MouseEvent.MOUSE_DRAGGED ) {
|
|
1290 |
jtext.processMouseMotionEventPublic( newEvent );
|
|
1291 |
} else {
|
|
1292 |
jtext.processMouseEventPublic( newEvent );
|
|
1293 |
}
|
|
1294 |
break;
|
|
1295 |
|
|
1296 |
// We perform (additional) dispatching of events to buttons of
|
|
1297 |
// scrollbar, instead of leaving it to JScrollbar. This is
|
|
1298 |
// required, because of different listeners in Swing and AWT,
|
|
1299 |
// which trigger scrolling (ArrowButtonListener vs. TrackListener,
|
|
1300 |
// accordingly). So we dispatch events to scroll-buttons, to
|
|
1301 |
// invoke a correct Swing button listener.
|
|
1302 |
// See CR 6175401 for more information.
|
|
1303 |
case BAR:
|
|
1304 |
case BUTTON:
|
|
1305 |
Component c = current.getBar();
|
|
1306 |
Point p = toLocalSpace( c, event.getPoint() );
|
|
1307 |
if ( current.getType()==Pointer.Type.BUTTON ) {
|
|
1308 |
c = current.getButton();
|
|
1309 |
p = toLocalSpace( c, p );
|
|
1310 |
}
|
|
1311 |
ComponentAccessor.processEvent( c, newMouseEvent( c, p, event ) );
|
|
1312 |
break;
|
|
1313 |
}
|
|
1314 |
}
|
|
1315 |
|
|
1316 |
private static MouseEvent newMouseEvent(
|
|
1317 |
Component source, Point point, MouseEvent template )
|
|
1318 |
{
|
|
1319 |
MouseEvent e = template;
|
|
1320 |
return new MouseEvent(
|
|
1321 |
source,
|
|
1322 |
e.getID(), e.getWhen(),
|
|
1323 |
e.getModifiersEx() | e.getModifiers(),
|
|
1324 |
point.x, point.y,
|
|
1325 |
e.getXOnScreen(), e.getYOnScreen(),
|
|
1326 |
e.getClickCount(), e.isPopupTrigger(), e.getButton() );
|
|
1327 |
}
|
|
1328 |
|
|
1329 |
private void setCursor() {
|
|
1330 |
if ( current.getType()==Pointer.Type.TEXT ) {
|
|
1331 |
// 'target.getCursor()' is also applied from elsewhere
|
|
1332 |
// (at least now), but only when mouse "entered", and
|
|
1333 |
// before 'XTextAreaPeer.handleJavaMouseEvent' is invoked.
|
|
1334 |
outer.pSetCursor( outer.target.getCursor() );
|
|
1335 |
}
|
|
1336 |
else {
|
|
1337 |
// We can write here a more intelligent cursor selection
|
|
1338 |
// mechanism, like getting cursor from 'current' component.
|
|
1339 |
// However, I see no point in doing so now. But if you feel
|
|
1340 |
// like implementing it, you'll probably need to introduce
|
|
1341 |
// 'Pointer.Type.PANEL'.
|
|
1342 |
outer.pSetCursor( outer.textPane.getCursor() );
|
|
1343 |
}
|
|
1344 |
}
|
|
1345 |
|
|
1346 |
|
|
1347 |
// Current way of grab-detection causes interesting (but harmless)
|
|
1348 |
// side-effect. If mouse is draged from outside to inside of TextArea,
|
|
1349 |
// we will then (in some cases) be asked to dispatch mouse-entered/exited
|
|
1350 |
// events. But, as at least one mouse-button is down, we will detect
|
|
1351 |
// grab-mode is on (though the grab isn't ours).
|
|
1352 |
//
|
|
1353 |
// Thus, we will not update 'current' (see 'handle' method), and will
|
|
1354 |
// dispatch events to the last subcomponent, the 'current' was set to.
|
|
1355 |
// As always, we set cursor in this case also. But, all this seems
|
|
1356 |
// harmless, because mouse entered/exited events seem to have no effect
|
|
1357 |
// here, and cursor setting is ignored in case of drags from outside.
|
|
1358 |
//
|
|
1359 |
// Grab-detection can be further improved, e.g. by taking into account
|
|
1360 |
// current event-ID, but I see not point in doing it now.
|
|
1361 |
|
|
1362 |
private void grabbed_update( MouseEvent event ) {
|
|
1363 |
final int allButtonsMask
|
|
1364 |
= MouseEvent.BUTTON1_DOWN_MASK
|
|
1365 |
| MouseEvent.BUTTON2_DOWN_MASK
|
|
1366 |
| MouseEvent.BUTTON3_DOWN_MASK;
|
|
1367 |
grabbed = ( (event.getModifiersEx() & allButtonsMask) != 0 );
|
|
1368 |
}
|
|
1369 |
|
|
1370 |
// 'toLocalSpace' and 'toViewportChildLocalSpace' can be "optimized" to
|
|
1371 |
// 'return' 'void' and use 'Point' input-argument also as output.
|
|
1372 |
private static Point toLocalSpace( Component local, Point inParentSpace )
|
|
1373 |
{
|
|
1374 |
Point p = inParentSpace;
|
|
1375 |
Point l = local.getLocation();
|
|
1376 |
return new Point( p.x - l.x, p.y - l.y );
|
|
1377 |
}
|
|
1378 |
private static Point toViewportChildLocalSpace( JViewport v, Point inViewportParentSpace )
|
|
1379 |
{
|
|
1380 |
Point l = toLocalSpace(v, inViewportParentSpace);
|
|
1381 |
Point p = v.getViewPosition();
|
|
1382 |
l.x += p.x;
|
|
1383 |
l.y += p.y;
|
|
1384 |
return l;
|
|
1385 |
}
|
|
1386 |
|
|
1387 |
private void setPointerToUnderEventPoint( MouseEvent event ) {
|
|
1388 |
Point point = event.getPoint();
|
|
1389 |
if ( outer.textPane.getViewport().getBounds().contains( point ) ) {
|
|
1390 |
current.setText();
|
|
1391 |
}
|
|
1392 |
else if ( ! setPointerIfPointOverScrollbar(
|
|
1393 |
outer.textPane.getVerticalScrollBar(), point ) )
|
|
1394 |
{
|
|
1395 |
if ( ! setPointerIfPointOverScrollbar(
|
|
1396 |
outer.textPane.getHorizontalScrollBar(), point ) )
|
|
1397 |
{
|
|
1398 |
current.setNone();
|
|
1399 |
}
|
|
1400 |
}
|
|
1401 |
}
|
|
1402 |
|
|
1403 |
private boolean setPointerIfPointOverScrollbar( JScrollBar bar, Point point ) {
|
|
1404 |
if ( ! bar.getBounds().contains( point ) ) {
|
|
1405 |
return false;
|
|
1406 |
}
|
|
1407 |
current.setBar( bar );
|
|
1408 |
Point local = toLocalSpace( bar, point );
|
|
1409 |
|
|
1410 |
XTextAreaPeer.XAWTScrollBarUI ui =
|
|
1411 |
(XTextAreaPeer.XAWTScrollBarUI) bar.getUI();
|
|
1412 |
|
|
1413 |
if ( ! setPointerIfPointOverButton( ui.getIncreaseButton(), local ) ) {
|
|
1414 |
setPointerIfPointOverButton( ui.getDecreaseButton(), local );
|
|
1415 |
}
|
|
1416 |
|
|
1417 |
return true;
|
|
1418 |
}
|
|
1419 |
|
|
1420 |
private boolean setPointerIfPointOverButton( JButton button, Point point ) {
|
|
1421 |
if ( ! button.getBounds().contains( point ) ) {
|
|
1422 |
return false;
|
|
1423 |
}
|
|
1424 |
current.setButton( button );
|
|
1425 |
return true;
|
|
1426 |
}
|
|
1427 |
|
|
1428 |
private static final class Pointer {
|
|
1429 |
static enum Type {
|
|
1430 |
NONE, TEXT, BAR, BUTTON // , PANEL
|
|
1431 |
}
|
|
1432 |
Type getType() {
|
|
1433 |
return type;
|
|
1434 |
}
|
|
1435 |
boolean isNone() {
|
|
1436 |
return type==Type.NONE;
|
|
1437 |
}
|
|
1438 |
JScrollBar getBar() {
|
|
1439 |
boolean ok = type==Type.BAR || type==Type.BUTTON;
|
|
1440 |
assert ok;
|
|
1441 |
return ok ? bar : null;
|
|
1442 |
}
|
|
1443 |
JButton getButton() {
|
|
1444 |
boolean ok = type==Type.BUTTON;
|
|
1445 |
assert ok;
|
|
1446 |
return ok ? button : null;
|
|
1447 |
}
|
|
1448 |
void setNone() {
|
|
1449 |
type = Type.NONE;
|
|
1450 |
}
|
|
1451 |
void setText() {
|
|
1452 |
type = Type.TEXT;
|
|
1453 |
}
|
|
1454 |
void setBar( JScrollBar bar ) {
|
|
1455 |
this.bar=bar;
|
|
1456 |
type=Type.BAR;
|
|
1457 |
}
|
|
1458 |
void setButton( JButton button ) {
|
|
1459 |
this.button=button;
|
|
1460 |
type=Type.BUTTON;
|
|
1461 |
}
|
|
1462 |
|
|
1463 |
private Type type;
|
|
1464 |
private JScrollBar bar;
|
|
1465 |
private JButton button;
|
|
1466 |
}
|
|
1467 |
}
|
|
1468 |
}
|