JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / swingjs / plaf / TextListener.java
1 /*
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.
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
23  * questions.
24  */
25
26 package swingjs.plaf;
27
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;
42
43 public class TextListener implements MouseListener, MouseMotionListener,
44                                    FocusListener, ChangeListener, PropertyChangeListener, DocumentListener, JSEventHandler
45 {
46
47     private JTextComponent txtComp;
48     
49     boolean haveDocument;
50
51                 private JSTextUI ui;
52
53                 public TextListener(JSTextUI ui, JTextComponent txtComp) {
54         this.txtComp = txtComp;
55         this.ui = ui;
56     }
57
58   void checkDocument() {
59         if (!haveDocument && txtComp.getDocument() != null) {
60                 haveDocument = true;
61                 txtComp.getDocument().addDocumentListener(this);
62         }
63   }
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);
70                 }
71                 if ("editable" == prop)
72                         ui.setEditable(((Boolean) e.getNewValue()).booleanValue());
73         }
74
75   public void stateChanged(ChangeEvent e) {
76         JTextComponent txtComp = (JTextComponent) e.getSource();
77         txtComp.repaint();
78     }
79
80     public void focusGained(FocusEvent e) {
81     }
82
83     public void focusLost(FocusEvent e) {
84 //        JTextComponent b = (JTextComponent) e.getSource();
85     }
86
87     public void mouseMoved(MouseEvent e) {
88     }
89
90
91     public void mouseDragged(MouseEvent e) {
92     }
93
94     public void mouseClicked(MouseEvent e) {
95     }
96
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()))
101                                 return;
102                         if (!txtComp.hasFocus() && txtComp.isRequestFocusEnabled()) {
103                                 txtComp.requestFocus();
104                         }
105                 }
106         };
107
108     public void mouseReleased(MouseEvent e) {
109     };
110
111     public void mouseEntered(MouseEvent e) {
112     };
113
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);
119 //        }
120 //        model.setArmed(false);
121     };
122
123
124         @Override
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;
129                 int keyCode = 0;
130                 /**
131                  * @j2sNative
132                  * 
133                  *            mark = jQueryEvent.target.selectionStart; 
134                  *            dot = jQueryEvent.target.selectionEnd; 
135                  *            evType = jQueryEvent.type;
136                  *            keyCode = jQueryEvent.keyCode;
137                  *            if (keyCode == 13) keyCode = 10; 
138                  */
139                 {
140                 }
141
142                 // HTML5 selection is always mark....dot
143                 // but Java can be oldDot....oldMark
144
145                 int oldDot = ui.editor.getCaret().getDot();
146                 int oldMark = ui.editor.getCaret().getMark();
147                 if (dot != mark && oldMark == dot) {
148                         dot = mark;
149                         mark = oldMark;
150                 }
151                 switch (eventType) {
152                 case MouseEvent.MOUSE_PRESSED:
153                 case MouseEvent.MOUSE_RELEASED:
154                 case MouseEvent.MOUSE_CLICKED:
155                         break;
156                 case KeyEvent.KEY_PRESSED:
157                 case KeyEvent.KEY_RELEASED:
158                 case KeyEvent.KEY_TYPED:
159                         if (keyCode == KeyEvent.VK_ENTER && ui.handleEnter(eventType))
160                                 break;
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);
169                         }
170                         break;
171                 }
172                 if (dot != oldDot || mark != oldMark) {
173                         ui.editor.getCaret().setDot(dot);
174                         if (dot != mark)
175                                 ui.editor.getCaret().moveDot(mark);
176                         ui.editor.caretEvent.fire();
177                 }
178                 System.out.println(ui.id + " TextListener handling event " + evType + " " + eventType
179                                 + " " + ui.editor.getCaret() + " " + ui.getComponentText().length());
180                 return true;
181         }
182
183         @Override
184         public void insertUpdate(DocumentEvent e) {
185                 setText();
186         }
187
188         @Override
189         public void removeUpdate(DocumentEvent e) {
190                 setText();
191         }
192
193         @Override
194         public void changedUpdate(DocumentEvent e) {
195         }
196
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....
202         
203                 ((JSComponentUI) (Object) txtComp.getUI()).notifyPropertyChanged("text");       
204         }
205 }
206