001: /*
002: * @(#)QtTextComponentPeer.java 1.13 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package sun.awt.qt;
028:
029: import java.security.*;
030:
031: import java.awt.*;
032: import java.awt.event.*;
033: import java.awt.datatransfer.*;
034:
035: import sun.awt.peer.*;
036:
037: /**
038: *
039: *
040: * @author Nicholas Allen
041: */
042:
043: abstract class QtTextComponentPeer extends QtComponentPeer implements
044: TextComponentPeer {
045: private static native void initIDs();
046:
047: private QtClipboard systemClipboard;
048: private int pasteStart, pasteEnd;
049:
050: static {
051: initIDs();
052: }
053:
054: /** Creates a new QtTextComponentPeer. */
055:
056: QtTextComponentPeer(QtToolkit toolkit, TextComponent target) {
057: super (toolkit, target);
058: setText(target.getText());
059: setEditable(target.isEditable());
060:
061: target.enableInputMethods(true);
062:
063: final QtToolkit tmpToolkit = toolkit;
064: AccessController.doPrivileged(new PrivilegedAction() {
065: public Object run() {
066: systemClipboard = (QtClipboard) (tmpToolkit
067: .getSystemClipboard());
068:
069: return null;
070: }
071: });
072: }
073:
074: public boolean isFocusTraversable() {
075: return true;
076: }
077:
078: public native void setEditable(boolean editable);
079:
080: protected native String getTextNative();
081:
082: public String getText() {
083: return getTextNative();
084: }
085:
086: protected native void setTextNative(String text);
087:
088: public void setText(String text) {
089: if (text != null)
090: if ((text.length() > 0) || (getText().length() > 0))
091: setTextNative(text);
092: }
093:
094: public Dimension getPreferredSize(int rows, int columns) {
095: return getMinimumSize(rows, columns);
096: }
097:
098: public Dimension getMinimumSize(int rows, int columns) {
099: FontMetrics fm = getFontMetrics(target.getFont());
100:
101: /* Calculate proper size for text area
102: */
103: int colWidth = fm.charWidth('0');
104: int rowHeight = fm.getHeight();
105:
106: /* adding 1 to both columns and row to extend visual space
107: * of the minimumSize - fixes bug 4707759
108: */
109: return new Dimension((columns + 1) * colWidth, (rows + 1)
110: * rowHeight);
111: }
112:
113: public native int getSelectionStart();
114:
115: public native int getSelectionEnd();
116:
117: public native void select(int selStart, int selEnd);
118:
119: public native void setCaretPosition(int pos);
120:
121: public native int getCaretPosition();
122:
123: /*
124: * Post a new TextEvent when the value of a text component changes.
125: */
126:
127: private void postTextEvent() {
128: QtToolkit.postEvent(new TextEvent(target,
129: TextEvent.TEXT_VALUE_CHANGED));
130: }
131:
132: void pasteData(byte[] data) {
133:
134: String text = getText();
135: int textLen = text.length();
136:
137: if (pasteEnd > textLen)
138: pasteEnd = textLen;
139:
140: if (pasteStart > pasteEnd)
141: pasteStart = pasteEnd;
142:
143: String preText = text.substring(0, pasteStart);
144: String postText = text.substring(pasteEnd, textLen);
145:
146: text = preText + new String(data) + postText;
147:
148: setText(text);
149: setCaretPosition(text.length() - postText.length());
150: }
151: }
|