1 /*****************************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one                *
3  * or more contributor license agreements.  See the NOTICE file              *
4  * distributed with this work for additional information                     *
5  * regarding copyright ownership.  The ASF licenses this file                *
6  * to you under the Apache License, Version 2.0 (the                         *
7  * "License"); you may not use this file except in compliance                *
8  * with the License.  You may obtain a copy of the License at                *
9  *                                                                           *
10  *     http://www.apache.org/licenses/LICENSE-2.0                            *
11  *                                                                           *
12  * Unless required by applicable law or agreed to in writing,                *
13  * software distributed under the License is distributed on an               *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY                    *
15  * KIND, either express or implied.  See the License for the                 *
16  * specific language governing permissions and limitations                   *
17  * under the License.                                                        *
18  *                                                                           *
19  *                                                                           *
20  * This file is part of the BeanShell Java Scripting distribution.           *
21  * Documentation and updates may be found at http://www.beanshell.org/       *
22  * Patrick Niemeyer (pat@pat.net)                                            *
23  * Author of Learning Java, O'Reilly & Associates                            *
24  *                                                                           *
25  *****************************************************************************/
26 
27 
28 
29 package bsh;
30 
31 import java.awt.event.*;
32 import javax.swing.*;
33 import javax.swing.event.*;
34 import java.io.*;
35 import java.beans.*;
36 
37 /**
38 	JThis is a dynamically loaded extension which extends This and adds
39 	explicit support for AWT and JFC events, etc.  This is a backwards
40 	compatibility measure for JDK 1.2.  With 1.3+ there is a general
41 	reflection proxy mechanism that allows the base This to implement
42 	arbitrary interfaces.
43 
44 	The NameSpace getThis() method will produce instances of JThis if
45 	the java version is prior to 1.3 and swing is available...  (e.g. 1.2
46 	or 1.1 + swing installed)
47 
48 	Users of 1.1 without swing will have minimal interface support (just run()).
49 
50 	Bsh doesn't run on 1.02 and below because there is no reflection!
51 
52 	Note: This module relies on features of Swing and will only compile
53 	with JDK1.2 or JDK1.1 + the swing package.  For other environments simply
54 	do not compile this class.
55 */
56 class JThis extends This implements
57 	// All core AWT listeners
58 	ActionListener, AdjustmentListener, ComponentListener,
59 	ContainerListener, FocusListener, ItemListener, KeyListener,
60 	MouseListener, MouseMotionListener, TextListener, WindowListener,
61 	PropertyChangeListener,
62 	// All listeners in javax.swing.event as of Swing 1.1
63 	AncestorListener, CaretListener, CellEditorListener, ChangeListener,
64 	DocumentListener, HyperlinkListener,
65 	InternalFrameListener, ListDataListener, ListSelectionListener,
66 	MenuDragMouseListener, MenuKeyListener, MenuListener, MouseInputListener,
67 	PopupMenuListener, TableColumnModelListener, TableModelListener,
68 	TreeExpansionListener, TreeModelListener, TreeSelectionListener,
69 	TreeWillExpandListener, UndoableEditListener
70 {
71 
JThis( NameSpace namespace, Interpreter declaringInterp )72 	JThis( NameSpace namespace, Interpreter declaringInterp ) {
73 		super( namespace, declaringInterp );
74 	}
75 
toString()76 	public String toString() {
77 		return "'this' reference (JThis) to Bsh object: " + namespace.getName();
78 	}
79 
event(String name, Object event)80 	void event(String name, Object event)
81 	{
82 		CallStack callstack = new CallStack( namespace );
83 		BshMethod method = null;
84 
85 		// handleEvent gets all events
86 		try {
87 			method = namespace.getMethod(
88 				"handleEvent", new Class [] { null } );
89 		} catch ( UtilEvalError e ) {/*squeltch*/  }
90 
91 		if (method != null)
92 			try {
93 				method.invoke(
94 					new Object[] { event }, declaringInterpreter, callstack, null );
95 			} catch(EvalError e) {
96 				declaringInterpreter.error(
97 					"local event hander method invocation error:" + e );
98 			}
99 
100 		// send to specific event handler
101 		try {
102 			method = namespace.getMethod( name, new Class [] { null } );
103 		} catch ( UtilEvalError e ) { /*squeltch*/ }
104 		if (method != null)
105 			try {
106 				method.invoke(
107 					new Object[] { event }, declaringInterpreter, callstack, null );
108 			} catch(EvalError e) {
109 				declaringInterpreter.error(
110 					"local event hander method invocation error:" + e );
111 			}
112 	}
113 
114 	// Listener interfaces
115 
ancestorAdded(AncestorEvent e)116     public void ancestorAdded(AncestorEvent e) { event("ancestorAdded", e); }
ancestorRemoved(AncestorEvent e)117     public void ancestorRemoved(AncestorEvent e) { event("ancestorRemoved", e); }
ancestorMoved(AncestorEvent e)118     public void ancestorMoved(AncestorEvent e) { event("ancestorMoved", e); }
caretUpdate(CaretEvent e)119     public void caretUpdate(CaretEvent e) { event("caretUpdate", e); }
editingStopped(ChangeEvent e)120     public void editingStopped(ChangeEvent e) { event("editingStopped", e); }
editingCanceled(ChangeEvent e)121     public void editingCanceled(ChangeEvent e) { event("editingCanceled", e); }
stateChanged(ChangeEvent e)122     public void stateChanged(ChangeEvent e) { event("stateChanged", e); }
insertUpdate(DocumentEvent e)123     public void insertUpdate(DocumentEvent e) { event("insertUpdate", e); }
removeUpdate(DocumentEvent e)124     public void removeUpdate(DocumentEvent e) { event("removeUpdate", e); }
changedUpdate(DocumentEvent e)125     public void changedUpdate(DocumentEvent e) { event("changedUpdate", e); }
hyperlinkUpdate(HyperlinkEvent e)126     public void hyperlinkUpdate(HyperlinkEvent e) { event("internalFrameOpened", e); }
internalFrameOpened(InternalFrameEvent e)127     public void internalFrameOpened(InternalFrameEvent e) { event("internalFrameOpened", e); }
internalFrameClosing(InternalFrameEvent e)128     public void internalFrameClosing(InternalFrameEvent e) { event("internalFrameClosing", e); }
internalFrameClosed(InternalFrameEvent e)129     public void internalFrameClosed(InternalFrameEvent e) { event("internalFrameClosed", e); }
internalFrameIconified(InternalFrameEvent e)130     public void internalFrameIconified(InternalFrameEvent e) { event("internalFrameIconified", e); }
internalFrameDeiconified(InternalFrameEvent e)131     public void internalFrameDeiconified(InternalFrameEvent e) { event("internalFrameDeiconified", e); }
internalFrameActivated(InternalFrameEvent e)132     public void internalFrameActivated(InternalFrameEvent e) { event("internalFrameActivated", e); }
internalFrameDeactivated(InternalFrameEvent e)133     public void internalFrameDeactivated(InternalFrameEvent e) { event("internalFrameDeactivated", e); }
intervalAdded(ListDataEvent e)134     public void intervalAdded(ListDataEvent e) { event("intervalAdded", e); }
intervalRemoved(ListDataEvent e)135     public void intervalRemoved(ListDataEvent e) { event("intervalRemoved", e); }
contentsChanged(ListDataEvent e)136     public void contentsChanged(ListDataEvent e) { event("contentsChanged", e); }
valueChanged(ListSelectionEvent e)137   	public void valueChanged(ListSelectionEvent e) { event("valueChanged", e); }
menuDragMouseEntered(MenuDragMouseEvent e)138     public void menuDragMouseEntered(MenuDragMouseEvent e) { event("menuDragMouseEntered", e); }
menuDragMouseExited(MenuDragMouseEvent e)139     public void menuDragMouseExited(MenuDragMouseEvent e) { event("menuDragMouseExited", e); }
menuDragMouseDragged(MenuDragMouseEvent e)140     public void menuDragMouseDragged(MenuDragMouseEvent e) { event("menuDragMouseDragged", e); }
menuDragMouseReleased(MenuDragMouseEvent e)141     public void menuDragMouseReleased(MenuDragMouseEvent e) { event("menuDragMouseReleased", e); }
menuKeyTyped(MenuKeyEvent e)142     public void menuKeyTyped(MenuKeyEvent e) { event("menuKeyTyped", e); }
menuKeyPressed(MenuKeyEvent e)143     public void menuKeyPressed(MenuKeyEvent e) { event("menuKeyPressed", e); }
menuKeyReleased(MenuKeyEvent e)144     public void menuKeyReleased(MenuKeyEvent e) { event("menuKeyReleased", e); }
menuSelected(MenuEvent e)145     public void menuSelected(MenuEvent e) { event("menuSelected", e); }
menuDeselected(MenuEvent e)146     public void menuDeselected(MenuEvent e) { event("menuDeselected", e); }
menuCanceled(MenuEvent e)147     public void menuCanceled(MenuEvent e) { event("menuCanceled", e); }
popupMenuWillBecomeVisible(PopupMenuEvent e)148     public void popupMenuWillBecomeVisible(PopupMenuEvent e) { event("popupMenuWillBecomeVisible", e); }
popupMenuWillBecomeInvisible(PopupMenuEvent e)149     public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { event("popupMenuWillBecomeInvisible", e); }
popupMenuCanceled(PopupMenuEvent e)150     public void popupMenuCanceled(PopupMenuEvent e) { event("popupMenuCanceled", e); }
columnAdded(TableColumnModelEvent e)151     public void columnAdded(TableColumnModelEvent e) { event("columnAdded", e); }
columnRemoved(TableColumnModelEvent e)152     public void columnRemoved(TableColumnModelEvent e) { event("columnRemoved", e); }
columnMoved(TableColumnModelEvent e)153     public void columnMoved(TableColumnModelEvent e) { event("columnMoved", e); }
columnMarginChanged(ChangeEvent e)154     public void columnMarginChanged(ChangeEvent e) { event("columnMarginChanged", e); }
columnSelectionChanged(ListSelectionEvent e)155     public void columnSelectionChanged(ListSelectionEvent e) { event("columnSelectionChanged", e); }
tableChanged(TableModelEvent e)156     public void tableChanged(TableModelEvent e) { event("tableChanged", e); }
treeExpanded(TreeExpansionEvent e)157     public void treeExpanded(TreeExpansionEvent e) { event("treeExpanded", e); }
treeCollapsed(TreeExpansionEvent e)158     public void treeCollapsed(TreeExpansionEvent e) { event("treeCollapsed", e); }
treeNodesChanged(TreeModelEvent e)159     public void treeNodesChanged(TreeModelEvent e) { event("treeNodesChanged", e); }
treeNodesInserted(TreeModelEvent e)160     public void treeNodesInserted(TreeModelEvent e) { event("treeNodesInserted", e); }
treeNodesRemoved(TreeModelEvent e)161     public void treeNodesRemoved(TreeModelEvent e) { event("treeNodesRemoved", e); }
treeStructureChanged(TreeModelEvent e)162     public void treeStructureChanged(TreeModelEvent e) { event("treeStructureChanged", e); }
valueChanged(TreeSelectionEvent e)163     public void valueChanged(TreeSelectionEvent e) { event("valueChanged", e); }
treeWillExpand(TreeExpansionEvent e)164     public void treeWillExpand(TreeExpansionEvent e) { event("treeWillExpand", e); }
treeWillCollapse(TreeExpansionEvent e)165     public void treeWillCollapse(TreeExpansionEvent e) { event("treeWillCollapse", e); }
undoableEditHappened(UndoableEditEvent e)166     public void undoableEditHappened(UndoableEditEvent e) { event("undoableEditHappened", e); }
167 
168 	// Listener interfaces
actionPerformed(ActionEvent e)169 	public void actionPerformed(ActionEvent e) { event("actionPerformed", e); }
adjustmentValueChanged(AdjustmentEvent e)170 	public void adjustmentValueChanged(AdjustmentEvent e) { event("adjustmentValueChanged", e); }
componentResized(ComponentEvent e)171 	public void componentResized(ComponentEvent e) { event("componentResized", e); }
componentMoved(ComponentEvent e)172 	public void componentMoved(ComponentEvent e) { event("componentMoved", e); }
componentShown(ComponentEvent e)173 	public void componentShown(ComponentEvent e) { event("componentShown", e); }
componentHidden(ComponentEvent e)174 	public void componentHidden(ComponentEvent e) { event("componentHidden", e); }
componentAdded(ContainerEvent e)175 	public void componentAdded(ContainerEvent e) { event("componentAdded", e); }
componentRemoved(ContainerEvent e)176 	public void componentRemoved(ContainerEvent e) { event("componentRemoved", e); }
focusGained(FocusEvent e)177 	public void focusGained(FocusEvent e) { event("focusGained", e); }
focusLost(FocusEvent e)178 	public void focusLost(FocusEvent e) { event("focusLost", e); }
itemStateChanged(ItemEvent e)179 	public void itemStateChanged(ItemEvent e) { event("itemStateChanged", e); }
keyTyped(KeyEvent e)180 	public void keyTyped(KeyEvent e) { event("keyTyped", e); }
keyPressed(KeyEvent e)181 	public void keyPressed(KeyEvent e) { event("keyPressed", e); }
keyReleased(KeyEvent e)182 	public void keyReleased(KeyEvent e) { event("keyReleased", e); }
mouseClicked(MouseEvent e)183 	public void mouseClicked(MouseEvent e) { event("mouseClicked", e); }
mousePressed(MouseEvent e)184 	public void mousePressed(MouseEvent e) { event("mousePressed", e); }
mouseReleased(MouseEvent e)185 	public void mouseReleased(MouseEvent e) { event("mouseReleased", e); }
mouseEntered(MouseEvent e)186 	public void mouseEntered(MouseEvent e) { event("mouseEntered", e); }
mouseExited(MouseEvent e)187 	public void mouseExited(MouseEvent e) { event("mouseExited", e); }
mouseDragged(MouseEvent e)188 	public void mouseDragged(MouseEvent e) { event("mouseDragged", e); }
mouseMoved(MouseEvent e)189 	public void mouseMoved(MouseEvent e) { event("mouseMoved", e); }
textValueChanged(TextEvent e)190 	public void textValueChanged(TextEvent e) { event("textValueChanged", e); }
windowOpened(WindowEvent e)191 	public void windowOpened(WindowEvent e) { event("windowOpened", e); }
windowClosing(WindowEvent e)192 	public void windowClosing(WindowEvent e) { event("windowClosing", e); }
windowClosed(WindowEvent e)193 	public void windowClosed(WindowEvent e) { event("windowClosed", e); }
windowIconified(WindowEvent e)194 	public void windowIconified(WindowEvent e) { event("windowIconified", e); }
windowDeiconified(WindowEvent e)195 	public void windowDeiconified(WindowEvent e) { event("windowDeiconified", e); }
windowActivated(WindowEvent e)196 	public void windowActivated(WindowEvent e) { event("windowActivated", e); }
windowDeactivated(WindowEvent e)197 	public void windowDeactivated(WindowEvent e) { event("windowDeactivated", e); }
198 
propertyChange(PropertyChangeEvent e)199 	public void propertyChange(PropertyChangeEvent e) {
200 		event("propertyChange", e ); }
vetoableChange(PropertyChangeEvent e)201     public void vetoableChange(PropertyChangeEvent e) {
202 		event("vetoableChange", e ); }
203 
imageUpdate(java.awt.Image img, int infoflags, int x, int y, int width, int height)204     public boolean imageUpdate(java.awt.Image img, int infoflags,
205                                int x, int y, int width, int height) {
206 
207 		BshMethod method = null;
208 		try {
209 			method = namespace.getMethod( "imageUpdate",
210 				new Class [] { null, null, null, null, null, null } );
211 		} catch ( UtilEvalError e ) {/*squeltch*/ }
212 
213 		if(method != null)
214 			try {
215 				CallStack callstack = new CallStack( namespace );
216 				method.invoke(
217 					new Object[] {
218 						img, new Primitive(infoflags), new Primitive(x),
219 						new Primitive(y), new Primitive(width),
220 						new Primitive(height) },
221 					declaringInterpreter, callstack, null
222 				);
223 			} catch(EvalError e) {
224 				declaringInterpreter.error(
225 					"local event handler imageUpdate: method invocation error:" + e );
226 			}
227 		return true;
228 	}
229 
230 }
231 
232