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