KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > JThis


1 /*****************************************************************************
2  * *
3  * This file is part of the BeanShell Java Scripting distribution. *
4  * Documentation and updates may be found at http://www.beanshell.org/ *
5  * *
6  * Sun Public License Notice: *
7  * *
8  * The contents of this file are subject to the Sun Public License Version *
9  * 1.0 (the "License"); you may not use this file except in compliance with *
10  * the License. A copy of the License is available at http://www.sun.com *
11  * *
12  * The Original Code is BeanShell. The Initial Developer of the Original *
13  * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14  * (C) 2000. All Rights Reserved. *
15  * *
16  * GNU Public License Notice: *
17  * *
18  * Alternatively, the contents of this file may be used under the terms of *
19  * the GNU Lesser General Public License (the "LGPL"), in which case the *
20  * provisions of LGPL are applicable instead of those above. If you wish to *
21  * allow use of your version of this file only under the terms of the LGPL *
22  * and not to allow others to use your version of this file under the SPL, *
23  * indicate your decision by deleting the provisions above and replace *
24  * them with the notice and other provisions required by the LGPL. If you *
25  * do not delete the provisions above, a recipient may use your version of *
26  * this file under either the SPL or the LGPL. *
27  * *
28  * Patrick Niemeyer (pat@pat.net) *
29  * Author of Learning Java, O'Reilly & Associates *
30  * http://www.pat.net/~pat/ *
31  * *
32  *****************************************************************************/

33
34
35 package bsh;
36
37 import java.awt.event.*;
38 import javax.swing.*;
39 import javax.swing.event.*;
40 import java.io.*;
41 import java.beans.*;
42
43 /**
44     JThis is a dynamically loaded extension which extends This and adds
45     explicit support for AWT and JFC events, etc. This is a backwards
46     compatability measure for JDK 1.2. With 1.3+ there is a general
47     reflection proxy mechanism that allows the base This to implement
48     arbitrary interfaces.
49
50     The NameSpace getThis() method will produce instances of JThis if
51     the java version is prior to 1.3 and swing is available... (e.g. 1.2
52     or 1.1 + swing installed)
53
54     Users of 1.1 without swing will have minimal interface support (just run()).
55     
56     Bsh doesn't run on 1.02 and below because there is no reflection!
57
58     Note: This module relies on features of Swing and will only compile
59     with JDK1.2 or JDK1.1 + the swing package. For other environments simply
60     do not compile this class.
61 */

62 class JThis extends This implements
63     // All core AWT listeners
64
ActionListener, AdjustmentListener, ComponentListener,
65     ContainerListener, FocusListener, ItemListener, KeyListener,
66     MouseListener, MouseMotionListener, TextListener, WindowListener,
67     PropertyChangeListener,
68     // All listeners in javax.swing.event as of Swing 1.1
69
AncestorListener, CaretListener, CellEditorListener, ChangeListener,
70     DocumentListener, HyperlinkListener,
71     InternalFrameListener, ListDataListener, ListSelectionListener,
72     MenuDragMouseListener, MenuKeyListener, MenuListener, MouseInputListener,
73     PopupMenuListener, TableColumnModelListener, TableModelListener,
74     TreeExpansionListener, TreeModelListener, TreeSelectionListener,
75     TreeWillExpandListener, UndoableEditListener
76 {
77
78     JThis( NameSpace namespace, Interpreter declaringInterp ) {
79         super( namespace, declaringInterp );
80     }
81
82     public String JavaDoc toString() {
83         return "'this' reference (JThis) to Bsh object: " + namespace.getName();
84     }
85
86     void event(String JavaDoc name, Object JavaDoc event)
87     {
88         CallStack callstack = new CallStack( namespace );
89         BshMethod method = null;
90
91         // handleEvent gets all events
92
try {
93             method = namespace.getMethod(
94                 "handleEvent", new Class JavaDoc [] { null } );
95         } catch ( UtilEvalError e ) {/*squeltch*/ }
96
97         if (method != null)
98             try {
99                 method.invoke(
100                     new Object JavaDoc[] { event }, declaringInterpreter, callstack, null );
101             } catch(EvalError e) {
102                 declaringInterpreter.error(
103                     "local event hander method invocation error:" + e );
104             }
105
106         // send to specific event handler
107
try {
108             method = namespace.getMethod( name, new Class JavaDoc [] { null } );
109         } catch ( UtilEvalError e ) { /*squeltch*/ }
110         if (method != null)
111             try {
112                 method.invoke(
113                     new Object JavaDoc[] { event }, declaringInterpreter, callstack, null );
114             } catch(EvalError e) {
115                 declaringInterpreter.error(
116                     "local event hander method invocation error:" + e );
117             }
118     }
119
120     // Listener interfaces
121

122     public void ancestorAdded(AncestorEvent e) { event("ancestorAdded", e); }
123     public void ancestorRemoved(AncestorEvent e) { event("ancestorRemoved", e); }
124     public void ancestorMoved(AncestorEvent e) { event("ancestorMoved", e); }
125     public void caretUpdate(CaretEvent e) { event("caretUpdate", e); }
126     public void editingStopped(ChangeEvent e) { event("editingStopped", e); }
127     public void editingCanceled(ChangeEvent e) { event("editingCanceled", e); }
128     public void stateChanged(ChangeEvent e) { event("stateChanged", e); }
129     public void insertUpdate(DocumentEvent e) { event("insertUpdate", e); }
130     public void removeUpdate(DocumentEvent e) { event("removeUpdate", e); }
131     public void changedUpdate(DocumentEvent e) { event("changedUpdate", e); }
132     public void hyperlinkUpdate(HyperlinkEvent e) { event("internalFrameOpened", e); }
133     public void internalFrameOpened(InternalFrameEvent e) { event("internalFrameOpened", e); }
134     public void internalFrameClosing(InternalFrameEvent e) { event("internalFrameClosing", e); }
135     public void internalFrameClosed(InternalFrameEvent e) { event("internalFrameClosed", e); }
136     public void internalFrameIconified(InternalFrameEvent e) { event("internalFrameIconified", e); }
137     public void internalFrameDeiconified(InternalFrameEvent e) { event("internalFrameDeiconified", e); }
138     public void internalFrameActivated(InternalFrameEvent e) { event("internalFrameActivated", e); }
139     public void internalFrameDeactivated(InternalFrameEvent e) { event("internalFrameDeactivated", e); }
140     public void intervalAdded(ListDataEvent e) { event("intervalAdded", e); }
141     public void intervalRemoved(ListDataEvent e) { event("intervalRemoved", e); }
142     public void contentsChanged(ListDataEvent e) { event("contentsChanged", e); }
143     public void valueChanged(ListSelectionEvent e) { event("valueChanged", e); }
144     public void menuDragMouseEntered(MenuDragMouseEvent e) { event("menuDragMouseEntered", e); }
145     public void menuDragMouseExited(MenuDragMouseEvent e) { event("menuDragMouseExited", e); }
146     public void menuDragMouseDragged(MenuDragMouseEvent e) { event("menuDragMouseDragged", e); }
147     public void menuDragMouseReleased(MenuDragMouseEvent e) { event("menuDragMouseReleased", e); }
148     public void menuKeyTyped(MenuKeyEvent e) { event("menuKeyTyped", e); }
149     public void menuKeyPressed(MenuKeyEvent e) { event("menuKeyPressed", e); }
150     public void menuKeyReleased(MenuKeyEvent e) { event("menuKeyReleased", e); }
151     public void menuSelected(MenuEvent e) { event("menuSelected", e); }
152     public void menuDeselected(MenuEvent e) { event("menuDeselected", e); }
153     public void menuCanceled(MenuEvent e) { event("menuCanceled", e); }
154     public void popupMenuWillBecomeVisible(PopupMenuEvent e) { event("popupMenuWillBecomeVisible", e); }
155     public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { event("popupMenuWillBecomeInvisible", e); }
156     public void popupMenuCanceled(PopupMenuEvent e) { event("popupMenuCanceled", e); }
157     public void columnAdded(TableColumnModelEvent e) { event("columnAdded", e); }
158     public void columnRemoved(TableColumnModelEvent e) { event("columnRemoved", e); }
159     public void columnMoved(TableColumnModelEvent e) { event("columnMoved", e); }
160     public void columnMarginChanged(ChangeEvent e) { event("columnMarginChanged", e); }
161     public void columnSelectionChanged(ListSelectionEvent e) { event("columnSelectionChanged", e); }
162     public void tableChanged(TableModelEvent e) { event("tableChanged", e); }
163     public void treeExpanded(TreeExpansionEvent e) { event("treeExpanded", e); }
164     public void treeCollapsed(TreeExpansionEvent e) { event("treeCollapsed", e); }
165     public void treeNodesChanged(TreeModelEvent e) { event("treeNodesChanged", e); }
166     public void treeNodesInserted(TreeModelEvent e) { event("treeNodesInserted", e); }
167     public void treeNodesRemoved(TreeModelEvent e) { event("treeNodesRemoved", e); }
168     public void treeStructureChanged(TreeModelEvent e) { event("treeStructureChanged", e); }
169     public void valueChanged(TreeSelectionEvent e) { event("valueChanged", e); }
170     public void treeWillExpand(TreeExpansionEvent e) { event("treeWillExpand", e); }
171     public void treeWillCollapse(TreeExpansionEvent e) { event("treeWillCollapse", e); }
172     public void undoableEditHappened(UndoableEditEvent e) { event("undoableEditHappened", e); }
173
174     // Listener interfaces
175
public void actionPerformed(ActionEvent e) { event("actionPerformed", e); }
176     public void adjustmentValueChanged(AdjustmentEvent e) { event("adjustmentValueChanged", e); }
177     public void componentResized(ComponentEvent e) { event("componentResized", e); }
178     public void componentMoved(ComponentEvent e) { event("componentMoved", e); }
179     public void componentShown(ComponentEvent e) { event("componentShown", e); }
180     public void componentHidden(ComponentEvent e) { event("componentHidden", e); }
181     public void componentAdded(ContainerEvent e) { event("componentAdded", e); }
182     public void componentRemoved(ContainerEvent e) { event("componentRemoved", e); }
183     public void focusGained(FocusEvent e) { event("focusGained", e); }
184     public void focusLost(FocusEvent e) { event("focusLost", e); }
185     public void itemStateChanged(ItemEvent e) { event("itemStateChanged", e); }
186     public void keyTyped(KeyEvent e) { event("keyTyped", e); }
187     public void keyPressed(KeyEvent e) { event("keyPressed", e); }
188     public void keyReleased(KeyEvent e) { event("keyReleased", e); }
189     public void mouseClicked(MouseEvent e) { event("mouseClicked", e); }
190     public void mousePressed(MouseEvent e) { event("mousePressed", e); }
191     public void mouseReleased(MouseEvent e) { event("mouseReleased", e); }
192     public void mouseEntered(MouseEvent e) { event("mouseEntered", e); }
193     public void mouseExited(MouseEvent e) { event("mouseExited", e); }
194     public void mouseDragged(MouseEvent e) { event("mouseDragged", e); }
195     public void mouseMoved(MouseEvent e) { event("mouseMoved", e); }
196     public void textValueChanged(TextEvent e) { event("textValueChanged", e); }
197     public void windowOpened(WindowEvent e) { event("windowOpened", e); }
198     public void windowClosing(WindowEvent e) { event("windowClosing", e); }
199     public void windowClosed(WindowEvent e) { event("windowClosed", e); }
200     public void windowIconified(WindowEvent e) { event("windowIconified", e); }
201     public void windowDeiconified(WindowEvent e) { event("windowDeiconified", e); }
202     public void windowActivated(WindowEvent e) { event("windowActivated", e); }
203     public void windowDeactivated(WindowEvent e) { event("windowDeactivated", e); }
204
205     public void propertyChange(PropertyChangeEvent e) {
206         event("propertyChange", e ); }
207     public void vetoableChange(PropertyChangeEvent e) {
208         event("vetoableChange", e ); }
209
210     public boolean imageUpdate(java.awt.Image JavaDoc img, int infoflags,
211                                int x, int y, int width, int height) {
212
213         BshMethod method = null;
214         try {
215             method = namespace.getMethod( "imageUpdate",
216                 new Class JavaDoc [] { null, null, null, null, null, null } );
217         } catch ( UtilEvalError e ) {/*squeltch*/ }
218
219         if(method != null)
220             try {
221                 CallStack callstack = new CallStack( namespace );
222                 method.invoke(
223                     new Object JavaDoc[] {
224                         img, new Primitive(infoflags), new Primitive(x),
225                         new Primitive(y), new Primitive(width),
226                         new Primitive(height) },
227                     declaringInterpreter, callstack, null
228                 );
229             } catch(EvalError e) {
230                 declaringInterpreter.error(
231                     "local event handler imageUpdate: method invocation error:" + e );
232             }
233         return true;
234     }
235
236 }
237
238
Popular Tags