X-Git-Url: http://source.jalview.org/gitweb/?p=jalviewjs.git;a=blobdiff_plain;f=site%2Fj2s%2Fswingjs%2Fplaf%2FTextListener.java;fp=site%2Fj2s%2Fswingjs%2Fplaf%2FTextListener.java;h=9bce1b1d6b5156ffb8d227c131fffe04ac12b68a;hp=03710425f637bf412e75201c71cb2c9d106bdf8b;hb=b9b7a352eee79b7764c3b09c9d19663075061d8c;hpb=7301a2415adab88038b291fc54caeeb3a5a47a44 diff --git a/site/j2s/swingjs/plaf/TextListener.java b/site/j2s/swingjs/plaf/TextListener.java index 0371042..9bce1b1 100644 --- a/site/j2s/swingjs/plaf/TextListener.java +++ b/site/j2s/swingjs/plaf/TextListener.java @@ -1,206 +1,206 @@ -/* - * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package swingjs.plaf; - -import jsjava.awt.event.FocusEvent; -import jsjava.awt.event.FocusListener; -import jsjava.awt.event.KeyEvent; -import jsjava.awt.event.MouseEvent; -import jsjava.awt.event.MouseListener; -import jsjava.awt.event.MouseMotionListener; -import jsjava.beans.PropertyChangeEvent; -import jsjava.beans.PropertyChangeListener; -import jsjavax.swing.SwingUtilities; -import jsjavax.swing.event.ChangeEvent; -import jsjavax.swing.event.ChangeListener; -import jsjavax.swing.event.DocumentEvent; -import jsjavax.swing.event.DocumentListener; -import jsjavax.swing.text.JTextComponent; - -public class TextListener implements MouseListener, MouseMotionListener, - FocusListener, ChangeListener, PropertyChangeListener, DocumentListener, JSEventHandler -{ - - private JTextComponent txtComp; - - boolean haveDocument; - - private JSTextUI ui; - - public TextListener(JSTextUI ui, JTextComponent txtComp) { - this.txtComp = txtComp; - this.ui = ui; - } - - void checkDocument() { - if (!haveDocument && txtComp.getDocument() != null) { - haveDocument = true; - txtComp.getDocument().addDocumentListener(this); - } - } - public void propertyChange(PropertyChangeEvent e) { - String prop = e.getPropertyName(); - System.out.println("JSTextListener property change: " + prop + " " + e.getSource()); - if ("font" == prop || "foreground" == prop || "preferredSize" == prop) { - JTextComponent txtComp = (JTextComponent) e.getSource(); - ((JSComponentUI) (Object) txtComp.getUI()).notifyPropertyChanged(prop); - } - if ("editable" == prop) - ui.setEditable(((Boolean) e.getNewValue()).booleanValue()); - } - - public void stateChanged(ChangeEvent e) { - JTextComponent txtComp = (JTextComponent) e.getSource(); - txtComp.repaint(); - } - - public void focusGained(FocusEvent e) { - } - - public void focusLost(FocusEvent e) { -// JTextComponent b = (JTextComponent) e.getSource(); - } - - public void mouseMoved(MouseEvent e) { - } - - - public void mouseDragged(MouseEvent e) { - } - - public void mouseClicked(MouseEvent e) { - } - - public void mousePressed(MouseEvent e) { - if (SwingUtilities.isLeftMouseButton(e)) { - JTextComponent txtComp = (JTextComponent) e.getSource(); - if (!txtComp.contains(e.getX(), e.getY())) - return; - if (!txtComp.hasFocus() && txtComp.isRequestFocusEnabled()) { - txtComp.requestFocus(); - } - } - }; - - public void mouseReleased(MouseEvent e) { - }; - - public void mouseEntered(MouseEvent e) { - }; - - public void mouseExited(MouseEvent e) { -// JTextComponent b = (JTextComponent) e.getSource(); -// ButtonModel model = b.getModel(); -// if(b.isRolloverEnabled()) { -// model.setRollover(false); -// } -// model.setArmed(false); - }; - - - @Override - public boolean handleJSEvent(Object target, int eventType, Object jQueryEvent) { - JSTextUI ui = (JSTextUI) target; - int dot = 0, mark = 0; - String evType = null; - int keyCode = 0; - /** - * @j2sNative - * - * mark = jQueryEvent.target.selectionStart; - * dot = jQueryEvent.target.selectionEnd; - * evType = jQueryEvent.type; - * keyCode = jQueryEvent.keyCode; - * if (keyCode == 13) keyCode = 10; - */ - { - } - - // HTML5 selection is always mark....dot - // but Java can be oldDot....oldMark - - int oldDot = ui.editor.getCaret().getDot(); - int oldMark = ui.editor.getCaret().getMark(); - if (dot != mark && oldMark == dot) { - dot = mark; - mark = oldMark; - } - switch (eventType) { - case MouseEvent.MOUSE_PRESSED: - case MouseEvent.MOUSE_RELEASED: - case MouseEvent.MOUSE_CLICKED: - break; - case KeyEvent.KEY_PRESSED: - case KeyEvent.KEY_RELEASED: - case KeyEvent.KEY_TYPED: - if (keyCode == KeyEvent.VK_ENTER && ui.handleEnter(eventType)) - break; - String val = ui.getJSTextValue(); - if (!val.equals(ui.currentText)) { - String oldval = ui.currentText; - ui.editor.setText(val); - // the text may have been filtered, but we should not change it yet - //val = ui.getComponentText(); - ui.editor.firePropertyChangeObject("text", oldval, val); - ui.domNode.setSelectionRange(dot, dot); - } - break; - } - if (dot != oldDot || mark != oldMark) { - ui.editor.getCaret().setDot(dot); - if (dot != mark) - ui.editor.getCaret().moveDot(mark); - ui.editor.caretEvent.fire(); - } - System.out.println(ui.id + " TextListener handling event " + evType + " " + eventType - + " " + ui.editor.getCaret() + " " + ui.getComponentText().length()); - return true; - } - - @Override - public void insertUpdate(DocumentEvent e) { - setText(); - } - - @Override - public void removeUpdate(DocumentEvent e) { - setText(); - } - - @Override - public void changedUpdate(DocumentEvent e) { - } - - private void setText() { - // this method will only be run in JavaScript; so as not to - // have to modify the actual javax.swing code so much, we use - // the double qualification to prevent Java compilation errors. - // Not a great idea in general.... - - ((JSComponentUI) (Object) txtComp.getUI()).notifyPropertyChanged("text"); - } -} +/* + * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package swingjs.plaf; + +import jsjava.awt.event.FocusEvent; +import jsjava.awt.event.FocusListener; +import jsjava.awt.event.KeyEvent; +import jsjava.awt.event.MouseEvent; +import jsjava.awt.event.MouseListener; +import jsjava.awt.event.MouseMotionListener; +import jsjava.beans.PropertyChangeEvent; +import jsjava.beans.PropertyChangeListener; +import jsjavax.swing.SwingUtilities; +import jsjavax.swing.event.ChangeEvent; +import jsjavax.swing.event.ChangeListener; +import jsjavax.swing.event.DocumentEvent; +import jsjavax.swing.event.DocumentListener; +import jsjavax.swing.text.JTextComponent; + +public class TextListener implements MouseListener, MouseMotionListener, + FocusListener, ChangeListener, PropertyChangeListener, DocumentListener, JSEventHandler +{ + + private JTextComponent txtComp; + + boolean haveDocument; + + private JSTextUI ui; + + public TextListener(JSTextUI ui, JTextComponent txtComp) { + this.txtComp = txtComp; + this.ui = ui; + } + + void checkDocument() { + if (!haveDocument && txtComp.getDocument() != null) { + haveDocument = true; + txtComp.getDocument().addDocumentListener(this); + } + } + public void propertyChange(PropertyChangeEvent e) { + String prop = e.getPropertyName(); + System.out.println("JSTextListener property change: " + prop + " " + e.getSource()); + if ("font" == prop || "foreground" == prop || "preferredSize" == prop) { + JTextComponent txtComp = (JTextComponent) e.getSource(); + ((JSComponentUI) (Object) txtComp.getUI()).notifyPropertyChanged(prop); + } + if ("editable" == prop) + ui.setEditable(((Boolean) e.getNewValue()).booleanValue()); + } + + public void stateChanged(ChangeEvent e) { + JTextComponent txtComp = (JTextComponent) e.getSource(); + txtComp.repaint(); + } + + public void focusGained(FocusEvent e) { + } + + public void focusLost(FocusEvent e) { +// JTextComponent b = (JTextComponent) e.getSource(); + } + + public void mouseMoved(MouseEvent e) { + } + + + public void mouseDragged(MouseEvent e) { + } + + public void mouseClicked(MouseEvent e) { + } + + public void mousePressed(MouseEvent e) { + if (SwingUtilities.isLeftMouseButton(e)) { + JTextComponent txtComp = (JTextComponent) e.getSource(); + if (!txtComp.contains(e.getX(), e.getY())) + return; + if (!txtComp.hasFocus() && txtComp.isRequestFocusEnabled()) { + txtComp.requestFocus(); + } + } + }; + + public void mouseReleased(MouseEvent e) { + }; + + public void mouseEntered(MouseEvent e) { + }; + + public void mouseExited(MouseEvent e) { +// JTextComponent b = (JTextComponent) e.getSource(); +// ButtonModel model = b.getModel(); +// if(b.isRolloverEnabled()) { +// model.setRollover(false); +// } +// model.setArmed(false); + }; + + + @Override + public boolean handleJSEvent(Object target, int eventType, Object jQueryEvent) { + JSTextUI ui = (JSTextUI) target; + int dot = 0, mark = 0; + String evType = null; + int keyCode = 0; + /** + * @j2sNative + * + * mark = jQueryEvent.target.selectionStart; + * dot = jQueryEvent.target.selectionEnd; + * evType = jQueryEvent.type; + * keyCode = jQueryEvent.keyCode; + * if (keyCode == 13) keyCode = 10; + */ + { + } + + // HTML5 selection is always mark....dot + // but Java can be oldDot....oldMark + + int oldDot = ui.editor.getCaret().getDot(); + int oldMark = ui.editor.getCaret().getMark(); + if (dot != mark && oldMark == dot) { + dot = mark; + mark = oldMark; + } + switch (eventType) { + case MouseEvent.MOUSE_PRESSED: + case MouseEvent.MOUSE_RELEASED: + case MouseEvent.MOUSE_CLICKED: + break; + case KeyEvent.KEY_PRESSED: + case KeyEvent.KEY_RELEASED: + case KeyEvent.KEY_TYPED: + if (keyCode == KeyEvent.VK_ENTER && ui.handleEnter(eventType)) + break; + String val = ui.getJSTextValue(); + if (!val.equals(ui.currentText)) { + String oldval = ui.currentText; + ui.editor.setText(val); + // the text may have been filtered, but we should not change it yet + //val = ui.getComponentText(); + ui.editor.firePropertyChangeObject("text", oldval, val); + ui.domNode.setSelectionRange(dot, dot); + } + break; + } + if (dot != oldDot || mark != oldMark) { + ui.editor.getCaret().setDot(dot); + if (dot != mark) + ui.editor.getCaret().moveDot(mark); + ui.editor.caretEvent.fire(); + } + System.out.println(ui.id + " TextListener handling event " + evType + " " + eventType + + " " + ui.editor.getCaret() + " " + ui.getComponentText().length()); + return true; + } + + @Override + public void insertUpdate(DocumentEvent e) { + setText(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + setText(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + } + + private void setText() { + // this method will only be run in JavaScript; so as not to + // have to modify the actual javax.swing code so much, we use + // the double qualification to prevent Java compilation errors. + // Not a great idea in general.... + + ((JSComponentUI) (Object) txtComp.getUI()).notifyPropertyChanged("text"); + } +} \ No newline at end of file