src/solaris/classes/sun/awt/X11/XTextAreaPeer.java

Print this page


   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


  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.


 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();


 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             // JTextArea.setText() posts two different events (remove & insert).
 467             // Since we make no differences between text events,
 468             // the document listener has to be disabled while
 469             // JTextArea.setText() is called.
 470             jtext.getDocument().removeDocumentListener(jtext);
 471             jtext.setText(txt);
 472             if (firstChangeSkipped) {
 473                 postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED));
 474             }
 475             jtext.getDocument().addDocumentListener(jtext);
 476         }
 477         return true;
 478     }
 479 
 480     /**
 481      * insert the text "txt on position "pos" in the array lines
 482      * @see java.awt.peer.TextAreaPeer
 483      */
 484     public void insert(String txt, int p) {
 485         if (jtext != null) {
 486             boolean doScroll = (p >= jtext.getDocument().getLength() && jtext.getDocument().getLength() != 0);
 487             jtext.insert(txt,p);
 488             textPane.validate();
 489             if (doScroll) {
 490                 JScrollBar bar = textPane.getVerticalScrollBar();
 491                 if (bar != null) {
 492                     bar.setValue(bar.getMaximum()-bar.getVisibleAmount());
 493                 }
 494             }
 495         }
 496     }
 497 


   1 /*
   2  * Copyright (c) 2003, 2013, 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


  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 final class XTextAreaPeer extends XComponentPeer implements TextAreaPeer {

  68 
  69     private final AWTTextPane textPane;
  70     private final AWTTextArea jtext;
  71     private final boolean firstChangeSkipped;
  72 
  73     private final JavaMouseEventHandler javaMouseEventHandler =
  74             new JavaMouseEventHandler(this);


  75 
  76     /* FIXME  */
  77 
  78     public long filterEvents(long mask) {
  79         Thread.dumpStack();
  80         return 0;
  81     }
  82 
  83     /* FIXME   */
  84     public Rectangle getCharacterBounds(int i) {
  85         Thread.dumpStack();
  86         return null;
  87     }
  88 
  89     public int getIndexAtPoint(int x, int y) {
  90         Thread.dumpStack();
  91         return 0;
  92     }
  93 
  94 
  95     /**
  96      * Create a Text area.
  97      */
  98     XTextAreaPeer(TextArea target) {
  99         super(target);
 100 
 101         // some initializations require that target be set even
 102         // though init(target) has not been called
 103         this.target = target;
 104 
 105         //ComponentAccessor.enableEvents(target,AWTEvent.MOUSE_WHEEL_EVENT_MASK);
 106 
 107         String text = target.getText();

 108         jtext = new AWTTextArea(text, this);
 109         jtext.setWrapStyleWord(true);
 110         jtext.getDocument().addDocumentListener(jtext);
 111         XToolkit.specialPeerMap.put(jtext,this);
 112         textPane = new AWTTextPane(jtext,this, target.getParent());
 113 
 114         setBounds(x, y, width, height, SET_BOUNDS);
 115         textPane.setVisible(true);
 116         textPane.validate();
 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 (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         // set the text of this object to the text of its target
 144         setTextImpl(target.getText());  //?? should this be setText
 145 
 146         int start = target.getSelectionStart();
 147         int end = target.getSelectionEnd();




 148         // Fix for 5100200
 149         // Restoring Motif behaviour
 150         // Since the end position of the selected text can be greater then the length of the text,
 151         // so we should set caret to max position of the text
 152         setCaretPosition(Math.min(end, text.length()));
 153         if (end > start) {
 154             select(start, end);
 155         }
 156         setEditable(target.isEditable());

 157         setScrollBarVisibility();



 158         // After this line we should not change the component's text
 159         firstChangeSkipped = true;
 160     }
 161 
 162     public void dispose() {
 163         XToolkit.specialPeerMap.remove(jtext);
 164         // visible caret has a timer thread which must be stopped
 165         jtext.getCaret().setVisible(false);
 166         jtext.removeNotify();
 167         textPane.removeNotify();
 168         super.dispose();
 169     }
 170 
 171 
 172     /*
 173      * The method overrides one from XComponentPeer
 174      * If ignoreSubComponents=={@code true} it calls super.
 175      * If ignoreSubComponents=={@code false} it uses the XTextArea machinery
 176      * to change cursor appropriately. In particular it changes the cursor to
 177      * default if over scrollbars.


 384         }
 385 //          repaintText();
 386     }
 387 
 388     public void setFont(Font f) {
 389         super.setFont(f);
 390 //          synchronized (getStateLock()) {
 391 //              font = f;
 392 //          }
 393         if (jtext != null) {
 394             jtext.setFont(font);
 395         }
 396         textPane.validate();
 397     }
 398 
 399 
 400     /**
 401      * @see java.awt.peer.TextComponentPeer
 402      */
 403     public void setEditable(boolean editable) {

 404         if (jtext != null) jtext.setEditable(editable);
 405         repaintText();
 406     }
 407 
 408     /**
 409      * @see java.awt.peer.ComponentPeer
 410      */
 411     public void setEnabled(boolean enabled) {
 412         super.setEnabled(enabled);
 413         if (jtext != null) {
 414             jtext.setEnabled(enabled);
 415             jtext.repaint();
 416         }
 417     }
 418 
 419     /**
 420      * @see java.awt.peer.TextComponentPeer
 421      */
 422     public InputMethodRequests getInputMethodRequests() {
 423         if (jtext != null) return jtext.getInputMethodRequests();


 436      */
 437     public int getSelectionEnd() {
 438         return jtext.getSelectionEnd();
 439     }
 440 
 441     /**
 442      * @see java.awt.peer.TextComponentPeer
 443      */
 444     public String getText() {
 445         return jtext.getText();
 446     }
 447 
 448     /**
 449      * @see java.awt.peer.TextComponentPeer
 450      */
 451     public void setText(String txt) {
 452         setTextImpl(txt);
 453         repaintText();
 454     }
 455 
 456     private void setTextImpl(String txt) {
 457         if (jtext != null) {
 458             // JTextArea.setText() posts two different events (remove & insert).
 459             // Since we make no differences between text events,
 460             // the document listener has to be disabled while
 461             // JTextArea.setText() is called.
 462             jtext.getDocument().removeDocumentListener(jtext);
 463             jtext.setText(txt);
 464             if (firstChangeSkipped) {
 465                 postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED));
 466             }
 467             jtext.getDocument().addDocumentListener(jtext);
 468         }

 469     }
 470 
 471     /**
 472      * insert the text "txt on position "pos" in the array lines
 473      * @see java.awt.peer.TextAreaPeer
 474      */
 475     public void insert(String txt, int p) {
 476         if (jtext != null) {
 477             boolean doScroll = (p >= jtext.getDocument().getLength() && jtext.getDocument().getLength() != 0);
 478             jtext.insert(txt,p);
 479             textPane.validate();
 480             if (doScroll) {
 481                 JScrollBar bar = textPane.getVerticalScrollBar();
 482                 if (bar != null) {
 483                     bar.setValue(bar.getMaximum()-bar.getVisibleAmount());
 484                 }
 485             }
 486         }
 487     }
 488