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

Print this page


   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


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


 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


   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


  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 final class XTextFieldPeer extends XComponentPeer implements TextFieldPeer {
  61     private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XTextField");
  62 
  63     private String text;
  64     private final XAWTTextField xtext;
  65     private final boolean firstChangeSkipped;
  66 
  67     XTextFieldPeer(TextField target) {


  68         super(target);


  69         text = target.getText();
  70         xtext = new XAWTTextField(text,this, target.getParent());
  71         xtext.getDocument().addDocumentListener(xtext);
  72         xtext.setCursor(target.getCursor());
  73         XToolkit.specialPeerMap.put(xtext,this);
  74 

  75         initTextField();
  76         setText(target.getText());
  77         if (target.echoCharIsSet()) {
  78             setEchoChar(target.getEchoChar());
  79         }
  80         else setEchoChar((char)0);
  81 
  82         int start = target.getSelectionStart();
  83         int end = target.getSelectionEnd();




  84         // Fix for 5100200
  85         // Restoring Motif behaviour
  86         // Since the end position of the selected text can be greater then the length of the text,
  87         // so we should set caret to max position of the text
  88         setCaretPosition(Math.min(end, text.length()));
  89         if (end > start) {
  90             select(start, end);
  91         }
  92 
  93         setEditable(target.isEditable());
  94 
  95         // After this line we should not change the component's text
  96         firstChangeSkipped = true;
  97     }
  98 
  99     public void dispose() {
 100         XToolkit.specialPeerMap.remove(xtext);
 101         // visible caret has a timer thread which must be stopped
 102         xtext.getCaret().setVisible(false);
 103         xtext.removeNotify();
 104         super.dispose();
 105     }
 106 
 107     void initTextField() {
 108         setVisible(target.isVisible());
 109 
 110         setBounds(x, y, width, height, SET_BOUNDS);
 111 
 112         AWTAccessor.ComponentAccessor compAccessor = AWTAccessor.getComponentAccessor();
 113         foreground = compAccessor.getForeground(target);


 196      */
 197     public int getSelectionEnd() {
 198         return xtext.getSelectionEnd();
 199     }
 200 
 201     /**
 202      * @see java.awt.peer.TextComponentPeer
 203      */
 204     public String getText() {
 205         return xtext.getText();
 206     }
 207 
 208     /**
 209      * @see java.awt.peer.TextComponentPeer
 210      */
 211     public void setText(String txt) {
 212         setXAWTTextField(txt);
 213         repaint();
 214     }
 215 
 216     private boolean setXAWTTextField(String txt) {
 217         text = txt;
 218         if (xtext != null)  {
 219             // JTextField.setText() posts two different events (remove & insert).
 220             // Since we make no differences between text events,
 221             // the document listener has to be disabled while
 222             // JTextField.setText() is called.
 223             xtext.getDocument().removeDocumentListener(xtext);
 224             xtext.setText(txt);
 225             if (firstChangeSkipped) {
 226                 postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED));
 227             }
 228             xtext.getDocument().addDocumentListener(xtext);
 229             xtext.setCaretPosition(0);
 230         }
 231         return true;
 232     }
 233 
 234     /**
 235      * to be implemented.
 236      * @see java.awt.peer.TextComponentPeer