PageRenderTime 49ms CodeModel.GetById 30ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/bsh/JThis.java

#
Java | 251 lines | 161 code | 22 blank | 68 comment | 6 complexity | 6deb2f422fe4bada6ab01d138e8d6f97 MD5 | raw file
  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
 35package bsh;
 36
 37import java.awt.event.*;
 38import javax.swing.*;
 39import javax.swing.event.*;
 40import java.io.*;
 41import 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*/
 62class 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 toString() {
 83		return "'this' reference (JThis) to Bsh object: " + namespace.name;
 84	}
 85
 86	void event(String name, Object event)
 87	{
 88		BshMethod method;
 89
 90		// handleEvent gets all events
 91		method = namespace.getMethod( 
 92			"handleEvent", new Class [] { null } );
 93
 94		if (method != null)
 95			try {
 96				method.invokeDeclaredMethod( 
 97					new Object[] { event }, declaringInterpreter, callstack, null );
 98			} catch(EvalError e) {
 99				declaringInterpreter.error(
100					"local event hander method invocation error:" + e );
101			}
102
103		// send to specific event handler
104		method = namespace.getMethod( name, new Class [] { null } );
105		if (method != null)
106			try {
107				method.invokeDeclaredMethod( 
108					new Object[] { event }, declaringInterpreter, callstack, null );
109			} catch(EvalError e) {
110				declaringInterpreter.error(
111					"local event hander method invocation error:" + e );
112			}
113	}
114
115	// Listener interfaces
116
117    public void ancestorAdded(AncestorEvent e) { event("ancestorAdded", e); }
118    public void ancestorRemoved(AncestorEvent e) { event("ancestorRemoved", e); }
119    public void ancestorMoved(AncestorEvent e) { event("ancestorMoved", e); }
120    public void caretUpdate(CaretEvent e) { event("caretUpdate", e); }
121    public void editingStopped(ChangeEvent e) { event("editingStopped", e); }
122    public void editingCanceled(ChangeEvent e) { event("editingCanceled", e); }
123    public void stateChanged(ChangeEvent e) { event("stateChanged", e); }
124    public void insertUpdate(DocumentEvent e) { event("insertUpdate", e); }
125    public void removeUpdate(DocumentEvent e) { event("removeUpdate", e); }
126    public void changedUpdate(DocumentEvent e) { event("changedUpdate", e); }
127    public void hyperlinkUpdate(HyperlinkEvent e) { event("internalFrameOpened", e); }
128    public void internalFrameOpened(InternalFrameEvent e) { event("internalFrameOpened", e); }
129    public void internalFrameClosing(InternalFrameEvent e) { event("internalFrameClosing", e); }
130    public void internalFrameClosed(InternalFrameEvent e) { event("internalFrameClosed", e); }
131    public void internalFrameIconified(InternalFrameEvent e) { event("internalFrameIconified", e); }
132    public void internalFrameDeiconified(InternalFrameEvent e) { event("internalFrameDeiconified", e); }
133    public void internalFrameActivated(InternalFrameEvent e) { event("internalFrameActivated", e); }
134    public void internalFrameDeactivated(InternalFrameEvent e) { event("internalFrameDeactivated", e); }
135    public void intervalAdded(ListDataEvent e) { event("intervalAdded", e); }
136    public void intervalRemoved(ListDataEvent e) { event("intervalRemoved", e); }
137    public void contentsChanged(ListDataEvent e) { event("contentsChanged", e); }
138  	public void valueChanged(ListSelectionEvent e) { event("valueChanged", e); }
139    public void menuDragMouseEntered(MenuDragMouseEvent e) { event("menuDragMouseEntered", e); }
140    public void menuDragMouseExited(MenuDragMouseEvent e) { event("menuDragMouseExited", e); }
141    public void menuDragMouseDragged(MenuDragMouseEvent e) { event("menuDragMouseDragged", e); }
142    public void menuDragMouseReleased(MenuDragMouseEvent e) { event("menuDragMouseReleased", e); }
143    public void menuKeyTyped(MenuKeyEvent e) { event("menuKeyTyped", e); }
144    public void menuKeyPressed(MenuKeyEvent e) { event("menuKeyPressed", e); }
145    public void menuKeyReleased(MenuKeyEvent e) { event("menuKeyReleased", e); }
146    public void menuSelected(MenuEvent e) { event("menuSelected", e); }
147    public void menuDeselected(MenuEvent e) { event("menuDeselected", e); }
148    public void menuCanceled(MenuEvent e) { event("menuCanceled", e); }
149    public void popupMenuWillBecomeVisible(PopupMenuEvent e) { event("popupMenuWillBecomeVisible", e); }
150    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { event("popupMenuWillBecomeInvisible", e); }
151    public void popupMenuCanceled(PopupMenuEvent e) { event("popupMenuCanceled", e); }
152    public void columnAdded(TableColumnModelEvent e) { event("columnAdded", e); }
153    public void columnRemoved(TableColumnModelEvent e) { event("columnRemoved", e); }
154    public void columnMoved(TableColumnModelEvent e) { event("columnMoved", e); }
155    public void columnMarginChanged(ChangeEvent e) { event("columnMarginChanged", e); }
156    public void columnSelectionChanged(ListSelectionEvent e) { event("columnSelectionChanged", e); }
157    public void tableChanged(TableModelEvent e) { event("tableChanged", e); }
158    public void treeExpanded(TreeExpansionEvent e) { event("treeExpanded", e); }
159    public void treeCollapsed(TreeExpansionEvent e) { event("treeCollapsed", e); }
160    public void treeNodesChanged(TreeModelEvent e) { event("treeNodesChanged", e); }
161    public void treeNodesInserted(TreeModelEvent e) { event("treeNodesInserted", e); }
162    public void treeNodesRemoved(TreeModelEvent e) { event("treeNodesRemoved", e); }
163    public void treeStructureChanged(TreeModelEvent e) { event("treeStructureChanged", e); }
164    public void valueChanged(TreeSelectionEvent e) { event("valueChanged", e); }
165    public void treeWillExpand(TreeExpansionEvent e) { event("treeWillExpand", e); }
166    public void treeWillCollapse(TreeExpansionEvent e) { event("treeWillCollapse", e); }
167    public void undoableEditHappened(UndoableEditEvent e) { event("undoableEditHappened", e); }
168
169	// Listener interfaces
170	public void actionPerformed(ActionEvent e) { event("actionPerformed", e); }
171	public void adjustmentValueChanged(AdjustmentEvent e) { event("adjustmentValueChanged", e); }
172	public void componentResized(ComponentEvent e) { event("componentResized", e); }
173	public void componentMoved(ComponentEvent e) { event("componentMoved", e); }
174	public void componentShown(ComponentEvent e) { event("componentShown", e); }
175	public void componentHidden(ComponentEvent e) { event("componentHidden", e); }
176	public void componentAdded(ContainerEvent e) { event("componentAdded", e); }
177	public void componentRemoved(ContainerEvent e) { event("componentRemoved", e); }
178	public void focusGained(FocusEvent e) { event("focusGained", e); }
179	public void focusLost(FocusEvent e) { event("focusLost", e); }
180	public void itemStateChanged(ItemEvent e) { event("itemStateChanged", e); }
181	public void keyTyped(KeyEvent e) { event("keyTyped", e); }
182	public void keyPressed(KeyEvent e) { event("keyPressed", e); }
183	public void keyReleased(KeyEvent e) { event("keyReleased", e); }
184	public void mouseClicked(MouseEvent e) { event("mouseClicked", e); }
185	public void mousePressed(MouseEvent e) { event("mousePressed", e); }
186	public void mouseReleased(MouseEvent e) { event("mouseReleased", e); }
187	public void mouseEntered(MouseEvent e) { event("mouseEntered", e); }
188	public void mouseExited(MouseEvent e) { event("mouseExited", e); }
189	public void mouseDragged(MouseEvent e) { event("mouseDragged", e); }
190	public void mouseMoved(MouseEvent e) { event("mouseMoved", e); }
191	public void textValueChanged(TextEvent e) { event("textValueChanged", e); }
192	public void windowOpened(WindowEvent e) { event("windowOpened", e); }
193	public void windowClosing(WindowEvent e) { event("windowClosing", e); }
194	public void windowClosed(WindowEvent e) { event("windowClosed", e); }
195	public void windowIconified(WindowEvent e) { event("windowIconified", e); }
196	public void windowDeiconified(WindowEvent e) { event("windowDeiconified", e); }
197	public void windowActivated(WindowEvent e) { event("windowActivated", e); }
198	public void windowDeactivated(WindowEvent e) { event("windowDeactivated", e); }
199
200	public void propertyChange(PropertyChangeEvent e) { 
201		event("propertyChange", e ); }
202    public void vetoableChange(PropertyChangeEvent e) {
203		event("vetoableChange", e ); }
204
205    public boolean imageUpdate(java.awt.Image img, int infoflags,
206                               int x, int y, int width, int height) {
207
208		BshMethod method = namespace.getMethod( "imageUpdate",
209			new Class [] { null, null, null, null, null, null } );
210		if(method != null)
211			try {
212				method.invokeDeclaredMethod( 
213					new Object[] { 
214						img, new Primitive(infoflags), new Primitive(x), 
215						new Primitive(y), new Primitive(width), 
216						new Primitive(height) }, 
217					declaringInterpreter, callstack, null
218				);
219			} catch(EvalError e) {
220				declaringInterpreter.error(
221					"local event handler imageUpdate: method invocation error:" + e );
222			}
223		return true;
224	}
225
226
227	/**
228		For serialization.
229		Note: this is copied from superclass... 
230		It must be private, but we can probably add an accessor to allow
231		us to call the super method explicitly.
232		Just testing to see if this is causing a problem.
233	*/
234    private synchronized void writeObject(java.io.ObjectOutputStream s)
235        throws IOException {
236
237		// Temporarily prune the namespace.
238
239		NameSpace parent = namespace.getParent();
240		// Bind would set the interpreter, but it's possible that the parent
241		// is null (it's the root).  So save it...
242		Interpreter interpreter = declaringInterpreter;
243		namespace.prune();
244		s.defaultWriteObject();
245		// put it back
246		namespace.setParent( parent );
247		declaringInterpreter = interpreter;
248	}
249
250}
251