2 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28 import jsjava.awt.event.FocusEvent;
29 import jsjava.awt.event.FocusListener;
30 import jsjava.awt.event.KeyEvent;
31 import jsjava.awt.event.MouseEvent;
32 import jsjava.awt.event.MouseListener;
33 import jsjava.awt.event.MouseMotionListener;
34 import jsjava.beans.PropertyChangeEvent;
35 import jsjava.beans.PropertyChangeListener;
36 import jsjavax.swing.SwingUtilities;
37 import jsjavax.swing.event.ChangeEvent;
38 import jsjavax.swing.event.ChangeListener;
39 import jsjavax.swing.event.DocumentEvent;
40 import jsjavax.swing.event.DocumentListener;
41 import jsjavax.swing.text.JTextComponent;
43 public class TextListener implements MouseListener, MouseMotionListener,
44 FocusListener, ChangeListener, PropertyChangeListener, DocumentListener, JSEventHandler
47 private JTextComponent txtComp;
53 public TextListener(JSTextUI ui, JTextComponent txtComp) {
54 this.txtComp = txtComp;
58 void checkDocument() {
59 if (!haveDocument && txtComp.getDocument() != null) {
61 txtComp.getDocument().addDocumentListener(this);
64 public void propertyChange(PropertyChangeEvent e) {
65 String prop = e.getPropertyName();
66 System.out.println("JSTextListener property change: " + prop + " " + e.getSource());
67 if ("font" == prop || "foreground" == prop || "preferredSize" == prop) {
68 JTextComponent txtComp = (JTextComponent) e.getSource();
69 ((JSComponentUI) (Object) txtComp.getUI()).notifyPropertyChanged(prop);
71 if ("editable" == prop)
72 ui.setEditable(((Boolean) e.getNewValue()).booleanValue());
75 public void stateChanged(ChangeEvent e) {
76 JTextComponent txtComp = (JTextComponent) e.getSource();
80 public void focusGained(FocusEvent e) {
83 public void focusLost(FocusEvent e) {
84 // JTextComponent b = (JTextComponent) e.getSource();
87 public void mouseMoved(MouseEvent e) {
91 public void mouseDragged(MouseEvent e) {
94 public void mouseClicked(MouseEvent e) {
97 public void mousePressed(MouseEvent e) {
98 if (SwingUtilities.isLeftMouseButton(e)) {
99 JTextComponent txtComp = (JTextComponent) e.getSource();
100 if (!txtComp.contains(e.getX(), e.getY()))
102 if (!txtComp.hasFocus() && txtComp.isRequestFocusEnabled()) {
103 txtComp.requestFocus();
108 public void mouseReleased(MouseEvent e) {
111 public void mouseEntered(MouseEvent e) {
114 public void mouseExited(MouseEvent e) {
115 // JTextComponent b = (JTextComponent) e.getSource();
116 // ButtonModel model = b.getModel();
117 // if(b.isRolloverEnabled()) {
118 // model.setRollover(false);
120 // model.setArmed(false);
125 public boolean handleJSEvent(Object target, int eventType, Object jQueryEvent) {
126 JSTextUI ui = (JSTextUI) target;
127 int dot = 0, mark = 0;
128 String evType = null;
133 * mark = jQueryEvent.target.selectionStart;
134 * dot = jQueryEvent.target.selectionEnd;
135 * evType = jQueryEvent.type;
136 * keyCode = jQueryEvent.keyCode;
137 * if (keyCode == 13) keyCode = 10;
142 // HTML5 selection is always mark....dot
143 // but Java can be oldDot....oldMark
145 int oldDot = ui.editor.getCaret().getDot();
146 int oldMark = ui.editor.getCaret().getMark();
147 if (dot != mark && oldMark == dot) {
152 case MouseEvent.MOUSE_PRESSED:
153 case MouseEvent.MOUSE_RELEASED:
154 case MouseEvent.MOUSE_CLICKED:
156 case KeyEvent.KEY_PRESSED:
157 case KeyEvent.KEY_RELEASED:
158 case KeyEvent.KEY_TYPED:
159 if (keyCode == KeyEvent.VK_ENTER && ui.handleEnter(eventType))
161 String val = ui.getJSTextValue();
162 if (!val.equals(ui.currentText)) {
163 String oldval = ui.currentText;
164 ui.editor.setText(val);
165 // the text may have been filtered, but we should not change it yet
166 //val = ui.getComponentText();
167 ui.editor.firePropertyChangeObject("text", oldval, val);
168 ui.domNode.setSelectionRange(dot, dot);
172 if (dot != oldDot || mark != oldMark) {
173 ui.editor.getCaret().setDot(dot);
175 ui.editor.getCaret().moveDot(mark);
176 ui.editor.caretEvent.fire();
178 System.out.println(ui.id + " TextListener handling event " + evType + " " + eventType
179 + " " + ui.editor.getCaret() + " " + ui.getComponentText().length());
184 public void insertUpdate(DocumentEvent e) {
189 public void removeUpdate(DocumentEvent e) {
194 public void changedUpdate(DocumentEvent e) {
197 private void setText() {
198 // this method will only be run in JavaScript; so as not to
199 // have to modify the actual javax.swing code so much, we use
200 // the double qualification to prevent Java compilation errors.
201 // Not a great idea in general....
203 ((JSComponentUI) (Object) txtComp.getUI()).notifyPropertyChanged("text");