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 sun.util.logging.PlatformLogger; 56 57 import sun.awt.CausedFocusEvent; 58 import sun.awt.AWTAccessor; 59 60 public class XTextFieldPeer extends XComponentPeer implements TextFieldPeer { 61 private static final PlatformLogger log = PlatformLogger.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 XToolkit.specialPeerMap.put(xtext,this); 77 78 TextField txt = (TextField) target; 79 initTextField(); 80 setText(txt.getText()); 81 if (txt.echoCharIsSet()) { 82 setEchoChar(txt.getEchoChar()); 83 } 84 else setEchoChar((char)0); 85 86 start = txt.getSelectionStart(); 87 end = txt.getSelectionEnd(); 88 89 if (end > start) { 90 select(start, end); 91 } 92 // Fix for 5100200 93 // Restoring Motif behaviour 94 // Since the end position of the selected text can be greater then the length of the text, 95 // so we should set caret to max position of the text 96 int caretPosition = Math.min(end, text.length()); 97 setCaretPosition(caretPosition); 98 99 setEditable(txt.isEditable()); 100 101 // After this line we should not change the component's text 102 firstChangeSkipped = true; 103 } 104 105 public void dispose() { 106 XToolkit.specialPeerMap.remove(xtext); 107 // visible caret has a timer thread which must be stopped 108 xtext.getCaret().setVisible(false); 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 AWTAccessor.ComponentAccessor compAccessor = AWTAccessor.getComponentAccessor(); 119 foreground = compAccessor.getForeground(target); 120 if (foreground == null) 121 foreground = SystemColor.textText; 122 123 setForeground(foreground); 124 125 background = compAccessor.getBackground(target); 126 if (background == null) { 127 if (((TextField)target).isEditable()) background = SystemColor.text; 128 else background = SystemColor.control; 129 } 130 setBackground(background); 131 132 if (!target.isBackgroundSet()) { 133 // This is a way to set the background color of the TextArea 134 // without calling setBackground - go through accessor 135 compAccessor.setBackground(target, background); 136 } 137 if (!target.isForegroundSet()) { 138 target.setForeground(SystemColor.textText); 139 } 140 141 setFont(font); 142 } 143 144 145 /** 146 * @see java.awt.peer.TextComponentPeer 147 */ 148 public void setEditable(boolean editable) { 149 if (xtext != null) { 150 xtext.setEditable(editable); 151 xtext.repaint(); 152 } 153 } 154 155 /** 156 * @see java.awt.peer.ComponentPeer 157 */ 158 public void setEnabled(boolean enabled) { 159 super.setEnabled(enabled); 160 if (xtext != null) { 161 xtext.setEnabled(enabled); 162 xtext.repaint(); 163 } 164 } 165 166 /** 167 * @see java.awt.peer.TextComponentPeer 168 */ 169 170 public InputMethodRequests getInputMethodRequests() { 171 if (xtext != null) return xtext.getInputMethodRequests(); 172 else return null; 173 174 } 175 176 void handleJavaInputMethodEvent(InputMethodEvent e) { 177 if (xtext != null) 178 xtext.processInputMethodEventImpl(e); 179 } 180 181 182 /** 183 * @see java.awt.peer.TextFieldPeer 184 */ 185 public void setEchoChar(char c) { 186 if (xtext != null) { 187 xtext.setEchoChar(c); 188 xtext.putClientProperty("JPasswordField.cutCopyAllowed", 189 xtext.echoCharIsSet() ? Boolean.FALSE : Boolean.TRUE); 190 } 191 } 192 193 /** 194 * @see java.awt.peer.TextComponentPeer 195 */ 196 public int getSelectionStart() { 197 return xtext.getSelectionStart(); 198 } 199 200 /** 201 * @see java.awt.peer.TextComponentPeer 202 */ 203 public int getSelectionEnd() { 204 return xtext.getSelectionEnd(); 205 } 206 207 /** 208 * @see java.awt.peer.TextComponentPeer 209 */ 210 public String getText() { 211 return xtext.getText(); 212 } 213 214 /** 215 * @see java.awt.peer.TextComponentPeer 216 */ 217 public void setText(String txt) { 218 setXAWTTextField(txt); 219 repaint(); 220 } 221 222 protected boolean setXAWTTextField(String txt) { 223 text = txt; 224 if (xtext != null) { 225 // JTextField.setText() posts two different events (remove & insert). 226 // Since we make no differences between text events, 227 // the document listener has to be disabled while 228 // JTextField.setText() is called. 229 xtext.getDocument().removeDocumentListener(xtext); 230 xtext.setText(txt); 231 if (firstChangeSkipped) { 232 postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED)); 233 } 234 xtext.getDocument().addDocumentListener(xtext); 235 xtext.setCaretPosition(0); 236 } 237 return true; 238 } 239 240 /** 241 * to be implemented. 242 * @see java.awt.peer.TextComponentPeer 243 */ 244 public void setCaretPosition(int position) { 245 if (xtext != null) xtext.setCaretPosition(position); 246 } 247 248 /** 249 * DEPRECATED 250 * @see java.awt.peer.TextFieldPeer 251 */ 252 public void setEchoCharacter(char c) { 253 setEchoChar(c); 254 } 255 256 void repaintText() { 257 xtext.repaintNow(); 258 } 259 260 public void setBackground(Color c) { 261 if (log.isLoggable(PlatformLogger.FINE)) log.fine("target="+ target + ", old=" + background + ", new=" + c); 262 background = c; 263 if (xtext != null) { 264 xtext.setBackground(c); 265 xtext.setSelectedTextColor(c); 266 } 267 repaintText(); 268 } 269 270 public void setForeground(Color c) { 271 foreground = c; 272 if (xtext != null) { 273 xtext.setForeground(foreground); 274 xtext.setSelectionColor(foreground); 275 xtext.setCaretColor(foreground); 276 } 277 repaintText(); 278 } 279 280 public void setFont(Font f) { 281 synchronized (getStateLock()) { 282 font = f; 283 if (xtext != null) { 284 xtext.setFont(font); 285 } 286 } 287 xtext.validate(); 288 } 289 290 /** 291 * DEPRECATED 292 * @see java.awt.peer.TextFieldPeer 293 */ 294 public Dimension preferredSize(int cols) { 295 return getPreferredSize(cols); 296 } 297 298 /** 299 * Deselects the the highlighted text. 300 */ 301 public void deselect() { 302 int selStart=xtext.getSelectionStart(); 303 int selEnd=xtext.getSelectionEnd(); 304 if (selStart != selEnd) { 305 xtext.select(selStart,selStart); 306 } 307 } 308 309 310 /** 311 * to be implemented. 312 * @see java.awt.peer.TextComponentPeer 313 */ 314 public int getCaretPosition() { 315 return xtext.getCaretPosition(); 316 } 317 318 319 320 /** 321 * @see java.awt.peer.TextComponentPeer 322 */ 323 public void select(int s, int e) { 324 xtext.select(s,e); 325 // Fixed 5100806 326 // We must take care that Swing components repainted correctly 327 xtext.repaint(); 328 } 329 330 331 public Dimension getMinimumSize() { 332 return xtext.getMinimumSize(); 333 } 334 335 public Dimension getPreferredSize() { 336 return xtext.getPreferredSize(); 337 } 338 339 public Dimension getPreferredSize(int cols) { 340 return getMinimumSize(cols); 341 } 342 343 private static final int PADDING = 16; 344 345 public Dimension getMinimumSize(int cols) { 346 Font f = xtext.getFont(); 347 FontMetrics fm = xtext.getFontMetrics(f); 348 return new Dimension(fm.charWidth('0') * cols + 10, 349 fm.getMaxDescent() + fm.getMaxAscent() + PADDING); 350 351 } 352 353 public boolean isFocusable() { 354 return true; 355 } 356 357 // NOTE: This method is called by privileged threads. 358 // DO NOT INVOKE CLIENT CODE ON THIS THREAD! 359 public void action(final long when, final int modifiers) { 360 postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED, 361 text, when, 362 modifiers)); 363 } 364 365 366 protected void disposeImpl() { 367 } 368 369 370 public void repaint() { 371 if (xtext != null) xtext.repaint(); 372 } 373 @Override 374 void paintPeer(final Graphics g) { 375 if (xtext != null) xtext.paint(g); 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 AWTAccessor.getComponentAccessor().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 XTextAreaPeer.XAWTCaret(); 582 } 583 } 584 585 class XAWTTextField extends JPasswordField 586 implements ActionListener, 587 DocumentListener 588 { 589 590 boolean isFocused = false; 591 592 XComponentPeer peer; 593 594 public XAWTTextField(String text, XComponentPeer peer, Container parent) { 595 super(text); 596 this.peer = peer; 597 setDoubleBuffered(true); 598 setFocusable(false); 599 AWTAccessor.getComponentAccessor().setParent(this,parent); 600 setBackground(peer.getPeerBackground()); 601 setForeground(peer.getPeerForeground()); 602 setFont(peer.getPeerFont()); 603 setCaretPosition(0); 604 addActionListener(this); 605 addNotify(); 606 607 } 608 609 public void actionPerformed( ActionEvent actionEvent ) { 610 peer.postEvent(new ActionEvent(peer.target, 611 ActionEvent.ACTION_PERFORMED, 612 getText(), 613 actionEvent.getWhen(), 614 actionEvent.getModifiers())); 615 616 } 617 618 public void insertUpdate(DocumentEvent e) { 619 if (peer != null) { 620 peer.postEvent(new TextEvent(peer.target, 621 TextEvent.TEXT_VALUE_CHANGED)); 622 } 623 } 624 625 public void removeUpdate(DocumentEvent e) { 626 if (peer != null) { 627 peer.postEvent(new TextEvent(peer.target, 628 TextEvent.TEXT_VALUE_CHANGED)); 629 } 630 } 631 632 public void changedUpdate(DocumentEvent e) { 633 if (peer != null) { 634 peer.postEvent(new TextEvent(peer.target, 635 TextEvent.TEXT_VALUE_CHANGED)); 636 } 637 } 638 639 public ComponentPeer getPeer() { 640 return (ComponentPeer) peer; 641 } 642 643 644 public void repaintNow() { 645 paintImmediately(getBounds()); 646 } 647 648 public Graphics getGraphics() { 649 return peer.getGraphics(); 650 } 651 652 public void updateUI() { 653 ComponentUI ui = new AWTTextFieldUI(); 654 setUI(ui); 655 } 656 657 658 void forwardFocusGained( FocusEvent e) { 659 isFocused = true; 660 FocusEvent fe = CausedFocusEvent.retarget(e, this); 661 super.processFocusEvent(fe); 662 663 } 664 665 666 void forwardFocusLost( FocusEvent e) { 667 isFocused = false; 668 FocusEvent fe = CausedFocusEvent.retarget(e, this); 669 super.processFocusEvent(fe); 670 671 } 672 673 public boolean hasFocus() { 674 return isFocused; 675 } 676 677 678 public void processInputMethodEventImpl(InputMethodEvent e) { 679 processInputMethodEvent(e); 680 } 681 682 public void processMouseEventImpl(MouseEvent e) { 683 processMouseEvent(e); 684 } 685 686 public void processMouseMotionEventImpl(MouseEvent e) { 687 processMouseMotionEvent(e); 688 } 689 690 // Fix for 4915454 - override the default implementation to avoid 691 // loading SystemFlavorMap and associated classes. 692 public void setTransferHandler(TransferHandler newHandler) { 693 TransferHandler oldHandler = (TransferHandler) 694 getClientProperty(XTextTransferHelper.getTransferHandlerKey()); 695 putClientProperty(XTextTransferHelper.getTransferHandlerKey(), 696 newHandler); 697 698 firePropertyChange("transferHandler", oldHandler, newHandler); 699 } 700 701 public void setEchoChar(char c) { 702 super.setEchoChar(c); 703 ((AWTTextFieldUI)ui).installKeyboardActions(); 704 } 705 } 706 }