1 /* 2 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 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. 24 */ 25 26 package sun.awt.X11; 27 28 import java.awt.*; 29 import java.awt.peer.*; 30 import java.awt.event.*; 31 import java.awt.event.ActionEvent; 32 import java.awt.event.ActionListener; 33 import java.awt.event.TextEvent; 34 import javax.swing.text.*; 35 import javax.swing.event.DocumentListener; 36 import javax.swing.event.DocumentEvent; 37 import javax.swing.plaf.ComponentUI; 38 import javax.swing.InputMap; 39 import javax.swing.JPasswordField; 40 import javax.swing.SwingUtilities; 41 import javax.swing.TransferHandler; 42 43 import java.awt.event.MouseEvent; 44 import java.awt.event.FocusEvent; 45 import java.awt.event.KeyEvent; 46 47 import javax.swing.plaf.UIResource; 48 import javax.swing.UIDefaults; 49 import javax.swing.JTextField; 50 import javax.swing.JComponent; 51 import javax.swing.border.Border; 52 import com.sun.java.swing.plaf.motif.*; 53 import java.awt.im.InputMethodRequests; 54 55 import java.util.logging.*; 56 import sun.awt.CausedFocusEvent; 57 import sun.awt.ComponentAccessor; 58 import sun.awt.AWTAccessor; 59 60 public class XTextFieldPeer extends XComponentPeer implements TextFieldPeer { 61 private static final Logger log = Logger.getLogger("sun.awt.X11.XTextField"); 62 63 String text; 64 XAWTTextField xtext; 65 66 boolean firstChangeSkipped; 67 68 public XTextFieldPeer(TextField target) { 69 super(target); 70 int start, end; 71 firstChangeSkipped = false; 72 text = target.getText(); 73 xtext = new XAWTTextField(text,this, target.getParent()); 74 xtext.getDocument().addDocumentListener(xtext); 75 xtext.setCursor(target.getCursor()); 76 target.enableInputMethods(true); 77 xtext.enableInputMethods(true); 78 XToolkit.specialPeerMap.put(xtext,this); 79 80 TextField txt = (TextField) target; 81 initTextField(); 82 setText(txt.getText()); 83 if (txt.echoCharIsSet()) { 84 setEchoChar(txt.getEchoChar()); 85 } 86 else setEchoChar((char)0); 87 88 start = txt.getSelectionStart(); 89 end = txt.getSelectionEnd(); 90 91 if (end > start) { 92 select(start, end); 93 } 94 // Fix for 5100200 95 // Restoring Motif behaviour 96 // Since the end position of the selected text can be greater then the length of the text, 97 // so we should set caret to max position of the text 98 int caretPosition = Math.min(end, text.length()); 99 setCaretPosition(caretPosition); 100 101 setEditable(txt.isEditable()); 102 103 // After this line we should not change the component's text 104 firstChangeSkipped = true; 105 } 106 107 public void dispose() { 108 XToolkit.specialPeerMap.remove(xtext); 109 xtext.removeNotify(); 110 super.dispose(); 111 } 112 113 void initTextField() { 114 setVisible(target.isVisible()); 115 116 setBounds(x, y, width, height, SET_BOUNDS); 117 118 foreground = ComponentAccessor.getForeground(target); 119 if (foreground == null) 120 foreground = SystemColor.textText; 121 122 setForeground(foreground); 123 124 background = ComponentAccessor.getBackground(target); 125 if (background == null) { 126 if (((TextField)target).isEditable()) background = SystemColor.text; 127 else background = SystemColor.control; 128 } 129 setBackground(background); 130 131 if (!target.isBackgroundSet()) { 132 // This is a way to set the background color of the TextArea 133 // without calling setBackground - go through reflection 134 ComponentAccessor.setBackground(target, background); 135 } 136 if (!target.isForegroundSet()) { 137 target.setForeground(SystemColor.textText); 138 } 139 140 setFont(font); 141 } 142 143 144 /** 145 * @see java.awt.peer.TextComponentPeer 146 */ 147 public void setEditable(boolean editable) { 148 if (xtext != null) { 149 xtext.setEditable(editable); 150 xtext.repaint(); 151 } 152 } 153 154 /** 155 * @see java.awt.peer.ComponentPeer 156 */ 157 public void setEnabled(boolean enabled) { 158 super.setEnabled(enabled); 159 if (xtext != null) { 160 xtext.setEnabled(enabled); 161 xtext.repaint(); 162 } 163 } 164 165 /** 166 * @see java.awt.peer.TextComponentPeer 167 */ 168 169 public InputMethodRequests getInputMethodRequests() { 170 if (xtext != null) return xtext.getInputMethodRequests(); 171 else return null; 172 173 } 174 175 void handleJavaInputMethodEvent(InputMethodEvent e) { 176 if (xtext != null) 177 xtext.processInputMethodEventImpl(e); 178 } 179 180 181 /** 182 * @see java.awt.peer.TextFieldPeer 183 */ 184 public void setEchoChar(char c) { 185 if (xtext != null) { 186 xtext.setEchoChar(c); 187 xtext.putClientProperty("JPasswordField.cutCopyAllowed", 188 xtext.echoCharIsSet() ? Boolean.FALSE : Boolean.TRUE); 189 } 190 } 191 192 /** 193 * @see java.awt.peer.TextComponentPeer 194 */ 195 public int getSelectionStart() { 196 return xtext.getSelectionStart(); 197 } 198 199 /** 200 * @see java.awt.peer.TextComponentPeer 201 */ 202 public int getSelectionEnd() { 203 return xtext.getSelectionEnd(); 204 } 205 206 /** 207 * @see java.awt.peer.TextComponentPeer 208 */ 209 public String getText() { 210 return xtext.getText(); 211 } 212 213 /** 214 * @see java.awt.peer.TextComponentPeer 215 */ 216 public void setText(String txt) { 217 setXAWTTextField(txt); 218 repaint(); 219 } 220 221 protected boolean setXAWTTextField(String txt) { 222 text = txt; 223 if (xtext != null) { 224 // JTextField.setText() posts two different events (remove & insert). 225 // Since we make no differences between text events, 226 // the document listener has to be disabled while 227 // JTextField.setText() is called. 228 xtext.getDocument().removeDocumentListener(xtext); 229 xtext.setText(txt); 230 if (firstChangeSkipped) { 231 postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED)); 232 } 233 xtext.getDocument().addDocumentListener(xtext); 234 xtext.setCaretPosition(0); 235 } 236 return true; 237 } 238 239 /** 240 * to be implemented. 241 * @see java.awt.peer.TextComponentPeer 242 */ 243 public void setCaretPosition(int position) { 244 if (xtext != null) xtext.setCaretPosition(position); 245 } 246 247 /** 248 * DEPRECATED 249 * @see java.awt.peer.TextFieldPeer 250 */ 251 public void setEchoCharacter(char c) { 252 setEchoChar(c); 253 } 254 255 void repaintText() { 256 xtext.repaintNow(); 257 } 258 259 public void setBackground(Color c) { 260 if (log.isLoggable(Level.FINE)) log.fine("target="+ target + ", old=" + background + ", new=" + c); 261 background = c; 262 if (xtext != null) { 263 xtext.setBackground(c); 264 xtext.setSelectedTextColor(c); 265 } 266 repaintText(); 267 } 268 269 public void setForeground(Color c) { 270 foreground = c; 271 if (xtext != null) { 272 xtext.setForeground(foreground); 273 xtext.setSelectionColor(foreground); 274 xtext.setCaretColor(foreground); 275 } 276 repaintText(); 277 } 278 279 public void setFont(Font f) { 280 synchronized (getStateLock()) { 281 font = f; 282 if (xtext != null) { 283 xtext.setFont(font); 284 } 285 } 286 xtext.validate(); 287 } 288 289 /** 290 * DEPRECATED 291 * @see java.awt.peer.TextFieldPeer 292 */ 293 public Dimension preferredSize(int cols) { 294 return getPreferredSize(cols); 295 } 296 297 /** 298 * Deselects the the highlighted text. 299 */ 300 public void deselect() { 301 int selStart=xtext.getSelectionStart(); 302 int selEnd=xtext.getSelectionEnd(); 303 if (selStart != selEnd) { 304 xtext.select(selStart,selStart); 305 } 306 } 307 308 309 /** 310 * to be implemented. 311 * @see java.awt.peer.TextComponentPeer 312 */ 313 public int getCaretPosition() { 314 return xtext.getCaretPosition(); 315 } 316 317 318 319 /** 320 * @see java.awt.peer.TextComponentPeer 321 */ 322 public void select(int s, int e) { 323 xtext.select(s,e); 324 // Fixed 5100806 325 // We must take care that Swing components repainted correctly 326 xtext.repaint(); 327 } 328 329 330 public Dimension getMinimumSize() { 331 return xtext.getMinimumSize(); 332 } 333 334 public Dimension getPreferredSize() { 335 return xtext.getPreferredSize(); 336 } 337 338 public Dimension getPreferredSize(int cols) { 339 return getMinimumSize(cols); 340 } 341 342 private static final int PADDING = 16; 343 344 public Dimension getMinimumSize(int cols) { 345 Font f = xtext.getFont(); 346 FontMetrics fm = xtext.getFontMetrics(f); 347 return new Dimension(fm.charWidth('0') * cols + 10, 348 fm.getMaxDescent() + fm.getMaxAscent() + PADDING); 349 350 } 351 352 public boolean isFocusable() { 353 return true; 354 } 355 356 // NOTE: This method is called by privileged threads. 357 // DO NOT INVOKE CLIENT CODE ON THIS THREAD! 358 public void action(final long when, final int modifiers) { 359 postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED, 360 text, when, 361 modifiers)); 362 } 363 364 365 protected void disposeImpl() { 366 } 367 368 369 public void repaint() { 370 if (xtext != null) xtext.repaint(); 371 } 372 373 public void paint(Graphics g) { 374 if (xtext != null) xtext.paint(g); 375 } 376 377 378 public void print(Graphics g) { 379 if (xtext != null) { 380 xtext.print(g); 381 } 382 } 383 384 public void focusLost(FocusEvent e) { 385 super.focusLost(e); 386 xtext.forwardFocusLost(e); 387 } 388 389 public void focusGained(FocusEvent e) { 390 super.focusGained(e); 391 xtext.forwardFocusGained(e); 392 } 393 394 void handleJavaKeyEvent(KeyEvent e) { 395 ComponentAccessor.processEvent(xtext,e); 396 } 397 398 399 public void handleJavaMouseEvent( MouseEvent mouseEvent ) { 400 super.handleJavaMouseEvent(mouseEvent); 401 if (xtext != null) { 402 mouseEvent.setSource(xtext); 403 int id = mouseEvent.getID(); 404 if (id == MouseEvent.MOUSE_DRAGGED || id == MouseEvent.MOUSE_MOVED) 405 xtext.processMouseMotionEventImpl(mouseEvent); 406 else 407 xtext.processMouseEventImpl(mouseEvent); 408 } 409 } 410 411 412 /** 413 * DEPRECATED 414 */ 415 public Dimension minimumSize() { 416 return getMinimumSize(); 417 } 418 419 /** 420 * DEPRECATED 421 */ 422 public Dimension minimumSize(int cols) { 423 return getMinimumSize(cols); 424 } 425 426 public void setVisible(boolean b) { 427 super.setVisible(b); 428 if (xtext != null) xtext.setVisible(b); 429 } 430 431 public void setBounds(int x, int y, int width, int height, int op) { 432 super.setBounds(x, y, width, height, op); 433 if (xtext != null) { 434 /* 435 * Fixed 6277332, 6198290: 436 * the coordinates is coming (to peer): relatively to closest HW parent 437 * the coordinates is setting (to textField): relatively to closest ANY parent 438 * the parent of peer is target.getParent() 439 * the parent of textField is the same 440 * see 6277332, 6198290 for more information 441 */ 442 int childX = x; 443 int childY = y; 444 Component parent = target.getParent(); 445 // we up to heavyweight parent in order to be sure 446 // that the coordinates of the text pane is relatively to closest parent 447 while (parent.isLightweight()){ 448 childX -= parent.getX(); 449 childY -= parent.getY(); 450 parent = parent.getParent(); 451 } 452 xtext.setBounds(childX,childY,width,height); 453 xtext.validate(); 454 } 455 } 456 457 458 // 459 // Accessibility support 460 // 461 462 // stub functions: to be fully implemented in a future release 463 public int getIndexAtPoint(int x, int y) { return -1; } 464 public Rectangle getCharacterBounds(int i) { return null; } 465 public long filterEvents(long mask) { return 0; } 466 467 468 /* To be fully implemented in a future release 469 470 int oldSelectionStart; 471 int oldSelectionEnd; 472 473 public native int getIndexAtPoint(int x, int y); 474 public native Rectangle getCharacterBounds(int i); 475 public native long filterEvents(long mask); 476 477 /** 478 * Handle a change in the text selection endpoints 479 * (Note: could be simply a change in the caret location) 480 * 481 public void selectionValuesChanged(int start, int end) { 482 return; // Need to write implemetation of this. 483 } 484 */ 485 486 487 class AWTTextFieldUI extends MotifPasswordFieldUI { 488 489 /** 490 * Creates a UI for a JTextField. 491 * 492 * @param c the text field 493 * @return the UI 494 */ 495 JTextField jtf; 496 497 498 protected String getPropertyPrefix() { 499 JTextComponent comp = getComponent(); 500 if (comp instanceof JPasswordField && ((JPasswordField)comp).echoCharIsSet()) { 501 return "PasswordField"; 502 } else { 503 return "TextField"; 504 } 505 } 506 507 public void installUI(JComponent c) { 508 super.installUI(c); 509 510 jtf = (JTextField) c; 511 512 JTextField editor = jtf; 513 514 UIDefaults uidefaults = XToolkit.getUIDefaults(); 515 516 String prefix = getPropertyPrefix(); 517 Font f = editor.getFont(); 518 if ((f == null) || (f instanceof UIResource)) { 519 editor.setFont(uidefaults.getFont(prefix + ".font")); 520 } 521 522 Color bg = editor.getBackground(); 523 if ((bg == null) || (bg instanceof UIResource)) { 524 editor.setBackground(uidefaults.getColor(prefix + ".background")); 525 } 526 527 Color fg = editor.getForeground(); 528 if ((fg == null) || (fg instanceof UIResource)) { 529 editor.setForeground(uidefaults.getColor(prefix + ".foreground")); 530 } 531 532 Color color = editor.getCaretColor(); 533 if ((color == null) || (color instanceof UIResource)) { 534 editor.setCaretColor(uidefaults.getColor(prefix + ".caretForeground")); 535 } 536 537 Color s = editor.getSelectionColor(); 538 if ((s == null) || (s instanceof UIResource)) { 539 editor.setSelectionColor(uidefaults.getColor(prefix + ".selectionBackground")); 540 } 541 542 Color sfg = editor.getSelectedTextColor(); 543 if ((sfg == null) || (sfg instanceof UIResource)) { 544 editor.setSelectedTextColor(uidefaults.getColor(prefix + ".selectionForeground")); 545 } 546 547 Color dfg = editor.getDisabledTextColor(); 548 if ((dfg == null) || (dfg instanceof UIResource)) { 549 editor.setDisabledTextColor(uidefaults.getColor(prefix + ".inactiveForeground")); 550 } 551 552 Border b = editor.getBorder(); 553 if ((b == null) || (b instanceof UIResource)) { 554 editor.setBorder(uidefaults.getBorder(prefix + ".border")); 555 } 556 557 Insets margin = editor.getMargin(); 558 if (margin == null || margin instanceof UIResource) { 559 editor.setMargin(uidefaults.getInsets(prefix + ".margin")); 560 } 561 } 562 563 protected void installKeyboardActions() { 564 super.installKeyboardActions(); 565 566 JTextComponent comp = getComponent(); 567 568 UIDefaults uidefaults = XToolkit.getUIDefaults(); 569 570 String prefix = getPropertyPrefix(); 571 572 InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap"); 573 574 if (map != null) { 575 SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED, 576 map); 577 } 578 } 579 580 protected Caret createCaret() { 581 return new XAWTCaret(); 582 } 583 } 584 585 class XAWTCaret extends DefaultCaret { 586 public void focusGained(FocusEvent e) { 587 super.focusGained(e); 588 getComponent().repaint(); 589 } 590 591 public void focusLost(FocusEvent e) { 592 super.focusLost(e); 593 getComponent().repaint(); 594 } 595 596 // Fix for 5100950: textarea.getSelectedText() returns the de-selected text, on XToolkit 597 // Restoring Motif behaviour 598 // If the text is unhighlighted then we should sets the selection range to zero 599 public void setSelectionVisible(boolean vis) { 600 if (vis){ 601 super.setSelectionVisible(vis); 602 }else{ 603 // In order to de-select the selection 604 setDot(getDot()); 605 } 606 } 607 } 608 609 class XAWTTextField extends JPasswordField 610 implements ActionListener, 611 DocumentListener 612 { 613 614 boolean isFocused = false; 615 616 XComponentPeer peer; 617 618 public XAWTTextField(String text, XComponentPeer peer, Container parent) { 619 super(text); 620 this.peer = peer; 621 setDoubleBuffered(true); 622 setFocusable(false); 623 ComponentAccessor.setParent(this,parent); 624 setBackground(peer.getPeerBackground()); 625 setForeground(peer.getPeerForeground()); 626 setFont(peer.getPeerFont()); 627 setCaretPosition(0); 628 addActionListener(this); 629 addNotify(); 630 631 } 632 633 public void actionPerformed( ActionEvent actionEvent ) { 634 peer.postEvent(new ActionEvent(peer.target, 635 ActionEvent.ACTION_PERFORMED, 636 getText(), 637 actionEvent.getWhen(), 638 actionEvent.getModifiers())); 639 640 } 641 642 public void insertUpdate(DocumentEvent e) { 643 if (peer != null) { 644 peer.postEvent(new TextEvent(peer.target, 645 TextEvent.TEXT_VALUE_CHANGED)); 646 } 647 } 648 649 public void removeUpdate(DocumentEvent e) { 650 if (peer != null) { 651 peer.postEvent(new TextEvent(peer.target, 652 TextEvent.TEXT_VALUE_CHANGED)); 653 } 654 } 655 656 public void changedUpdate(DocumentEvent e) { 657 if (peer != null) { 658 peer.postEvent(new TextEvent(peer.target, 659 TextEvent.TEXT_VALUE_CHANGED)); 660 } 661 } 662 663 public ComponentPeer getPeer() { 664 return (ComponentPeer) peer; 665 } 666 667 668 public void repaintNow() { 669 paintImmediately(getBounds()); 670 } 671 672 public Graphics getGraphics() { 673 return peer.getGraphics(); 674 } 675 676 public void updateUI() { 677 ComponentUI ui = new AWTTextFieldUI(); 678 setUI(ui); 679 } 680 681 682 void forwardFocusGained( FocusEvent e) { 683 isFocused = true; 684 FocusEvent fe = CausedFocusEvent.retarget(e, this); 685 super.processFocusEvent(fe); 686 687 } 688 689 690 void forwardFocusLost( FocusEvent e) { 691 isFocused = false; 692 FocusEvent fe = CausedFocusEvent.retarget(e, this); 693 super.processFocusEvent(fe); 694 695 } 696 697 public boolean hasFocus() { 698 return isFocused; 699 } 700 701 702 public void processInputMethodEventImpl(InputMethodEvent e) { 703 processInputMethodEvent(e); 704 } 705 706 public void processMouseEventImpl(MouseEvent e) { 707 processMouseEvent(e); 708 } 709 710 public void processMouseMotionEventImpl(MouseEvent e) { 711 processMouseMotionEvent(e); 712 } 713 714 // Fix for 4915454 - override the default implementation to avoid 715 // loading SystemFlavorMap and associated classes. 716 public void setTransferHandler(TransferHandler newHandler) { 717 TransferHandler oldHandler = (TransferHandler) 718 getClientProperty(AWTAccessor.getClientPropertyKeyAccessor() 719 .getJComponent_TRANSFER_HANDLER()); 720 putClientProperty(AWTAccessor.getClientPropertyKeyAccessor() 721 .getJComponent_TRANSFER_HANDLER(), 722 newHandler); 723 724 firePropertyChange("transferHandler", oldHandler, newHandler); 725 } 726 727 public void setEchoChar(char c) { 728 super.setEchoChar(c); 729 ((AWTTextFieldUI)ui).installKeyboardActions(); 730 } 731 } 732 }