2 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
\r
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
\r
5 * This code is free software; you can redistribute it and/or modify it
\r
6 * under the terms of the GNU General Public License version 2 only, as
\r
7 * published by the Free Software Foundation. Oracle designates this
\r
8 * particular file as subject to the "Classpath" exception as provided
\r
9 * by Oracle in the LICENSE file that accompanied this code.
\r
11 * This code is distributed in the hope that it will be useful, but WITHOUT
\r
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
\r
14 * version 2 for more details (a copy is included in the LICENSE file that
\r
15 * accompanied this code).
\r
17 * You should have received a copy of the GNU General Public License version
\r
18 * 2 along with this work; if not, write to the Free Software Foundation,
\r
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
\r
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
\r
22 * or visit www.oracle.com if you need additional information or have any
\r
26 package swingjs.plaf;
\r
28 import jsjava.awt.event.FocusEvent;
\r
29 import jsjava.awt.event.FocusListener;
\r
30 import jsjava.awt.event.KeyEvent;
\r
31 import jsjava.awt.event.MouseEvent;
\r
32 import jsjava.awt.event.MouseListener;
\r
33 import jsjava.awt.event.MouseMotionListener;
\r
34 import jsjava.beans.PropertyChangeEvent;
\r
35 import jsjava.beans.PropertyChangeListener;
\r
36 import jsjavax.swing.SwingUtilities;
\r
37 import jsjavax.swing.event.ChangeEvent;
\r
38 import jsjavax.swing.event.ChangeListener;
\r
39 import jsjavax.swing.event.DocumentEvent;
\r
40 import jsjavax.swing.event.DocumentListener;
\r
41 import jsjavax.swing.text.JTextComponent;
\r
43 public class TextListener implements MouseListener, MouseMotionListener,
\r
44 FocusListener, ChangeListener, PropertyChangeListener, DocumentListener, JSEventHandler
\r
47 private JTextComponent txtComp;
\r
49 boolean haveDocument;
\r
51 private JSTextUI ui;
\r
53 public TextListener(JSTextUI ui, JTextComponent txtComp) {
\r
54 this.txtComp = txtComp;
\r
58 void checkDocument() {
\r
59 if (!haveDocument && txtComp.getDocument() != null) {
\r
60 haveDocument = true;
\r
61 txtComp.getDocument().addDocumentListener(this);
\r
64 public void propertyChange(PropertyChangeEvent e) {
\r
65 String prop = e.getPropertyName();
\r
66 System.out.println("JSTextListener property change: " + prop + " " + e.getSource());
\r
67 if ("font" == prop || "foreground" == prop || "preferredSize" == prop) {
\r
68 JTextComponent txtComp = (JTextComponent) e.getSource();
\r
69 ((JSComponentUI) (Object) txtComp.getUI()).notifyPropertyChanged(prop);
\r
71 if ("editable" == prop)
\r
72 ui.setEditable(((Boolean) e.getNewValue()).booleanValue());
\r
75 public void stateChanged(ChangeEvent e) {
\r
76 JTextComponent txtComp = (JTextComponent) e.getSource();
\r
80 public void focusGained(FocusEvent e) {
\r
83 public void focusLost(FocusEvent e) {
\r
84 // JTextComponent b = (JTextComponent) e.getSource();
\r
87 public void mouseMoved(MouseEvent e) {
\r
91 public void mouseDragged(MouseEvent e) {
\r
94 public void mouseClicked(MouseEvent e) {
\r
97 public void mousePressed(MouseEvent e) {
\r
98 if (SwingUtilities.isLeftMouseButton(e)) {
\r
99 JTextComponent txtComp = (JTextComponent) e.getSource();
\r
100 if (!txtComp.contains(e.getX(), e.getY()))
\r
102 if (!txtComp.hasFocus() && txtComp.isRequestFocusEnabled()) {
\r
103 txtComp.requestFocus();
\r
108 public void mouseReleased(MouseEvent e) {
\r
111 public void mouseEntered(MouseEvent e) {
\r
114 public void mouseExited(MouseEvent e) {
\r
115 // JTextComponent b = (JTextComponent) e.getSource();
\r
116 // ButtonModel model = b.getModel();
\r
117 // if(b.isRolloverEnabled()) {
\r
118 // model.setRollover(false);
\r
120 // model.setArmed(false);
\r
125 public boolean handleJSEvent(Object target, int eventType, Object jQueryEvent) {
\r
126 JSTextUI ui = (JSTextUI) target;
\r
127 int dot = 0, mark = 0;
\r
128 String evType = null;
\r
133 * mark = jQueryEvent.target.selectionStart;
\r
134 * dot = jQueryEvent.target.selectionEnd;
\r
135 * evType = jQueryEvent.type;
\r
136 * keyCode = jQueryEvent.keyCode;
\r
137 * if (keyCode == 13) keyCode = 10;
\r
142 // HTML5 selection is always mark....dot
\r
143 // but Java can be oldDot....oldMark
\r
145 int oldDot = ui.editor.getCaret().getDot();
\r
146 int oldMark = ui.editor.getCaret().getMark();
\r
147 if (dot != mark && oldMark == dot) {
\r
151 switch (eventType) {
\r
152 case MouseEvent.MOUSE_PRESSED:
\r
153 case MouseEvent.MOUSE_RELEASED:
\r
154 case MouseEvent.MOUSE_CLICKED:
\r
156 case KeyEvent.KEY_PRESSED:
\r
157 case KeyEvent.KEY_RELEASED:
\r
158 case KeyEvent.KEY_TYPED:
\r
159 if (keyCode == KeyEvent.VK_ENTER && ui.handleEnter(eventType))
\r
161 String val = ui.getJSTextValue();
\r
162 if (!val.equals(ui.currentText)) {
\r
163 String oldval = ui.currentText;
\r
164 ui.editor.setText(val);
\r
165 // the text may have been filtered, but we should not change it yet
\r
166 //val = ui.getComponentText();
\r
167 ui.editor.firePropertyChangeObject("text", oldval, val);
\r
168 ui.domNode.setSelectionRange(dot, dot);
\r
172 if (dot != oldDot || mark != oldMark) {
\r
173 ui.editor.getCaret().setDot(dot);
\r
175 ui.editor.getCaret().moveDot(mark);
\r
176 ui.editor.caretEvent.fire();
\r
178 System.out.println(ui.id + " TextListener handling event " + evType + " " + eventType
\r
179 + " " + ui.editor.getCaret() + " " + ui.getComponentText().length());
\r
184 public void insertUpdate(DocumentEvent e) {
\r
189 public void removeUpdate(DocumentEvent e) {
\r
194 public void changedUpdate(DocumentEvent e) {
\r
197 private void setText() {
\r
198 // this method will only be run in JavaScript; so as not to
\r
199 // have to modify the actual javax.swing code so much, we use
\r
200 // the double qualification to prevent Java compilation errors.
\r
201 // Not a great idea in general....
\r
203 ((JSComponentUI) (Object) txtComp.getUI()).notifyPropertyChanged("text");
\r