PageRenderTime 92ms CodeModel.GetById 59ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
Java | 229 lines | 154 code | 18 blank | 57 comment | 6 complexity | e11bfe596ac3b8eedef36f01d07576bc 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		CallStack callstack = newCallStack();
 89		BshMethod method;
 90
 91		// handleEvent gets all events
 92		method = namespace.getMethod( 
 93			"handleEvent", new Class [] { null } );
 94
 95		if (method != null)
 96			try {
 97				method.invokeDeclaredMethod( 
 98					new Object[] { event }, declaringInterpreter, callstack, null );
 99			} catch(EvalError e) {
100				declaringInterpreter.error(
101					"local event hander method invocation error:" + e );
102			}
103
104		// send to specific event handler
105		method = namespace.getMethod( name, new Class [] { null } );
106		if (method != null)
107			try {
108				method.invokeDeclaredMethod( 
109					new Object[] { event }, declaringInterpreter, callstack, null );
110			} catch(EvalError e) {
111				declaringInterpreter.error(
112					"local event hander method invocation error:" + e );
113			}
114	}
115
116	// Listener interfaces
117
118    public void ancestorAdded(AncestorEvent e) { event("ancestorAdded", e); }
119    public void ancestorRemoved(AncestorEvent e) { event("ancestorRemoved", e); }
120    public void ancestorMoved(AncestorEvent e) { event("ancestorMoved", e); }
121    public void caretUpdate(CaretEvent e) { event("caretUpdate", e); }
122    public void editingStopped(ChangeEvent e) { event("editingStopped", e); }
123    public void editingCanceled(ChangeEvent e) { event("editingCanceled", e); }
124    public void stateChanged(ChangeEvent e) { event("stateChanged", e); }
125    public void insertUpdate(DocumentEvent e) { event("insertUpdate", e); }
126    public void removeUpdate(DocumentEvent e) { event("removeUpdate", e); }
127    public void changedUpdate(DocumentEvent e) { event("changedUpdate", e); }
128    public void hyperlinkUpdate(HyperlinkEvent e) { event("internalFrameOpened", e); }
129    public void internalFrameOpened(InternalFrameEvent e) { event("internalFrameOpened", e); }
130    public void internalFrameClosing(InternalFrameEvent e) { event("internalFrameClosing", e); }
131    public void internalFrameClosed(InternalFrameEvent e) { event("internalFrameClosed", e); }
132    public void internalFrameIconified(InternalFrameEvent e) { event("internalFrameIconified", e); }
133    public void internalFrameDeiconified(InternalFrameEvent e) { event("internalFrameDeiconified", e); }
134    public void internalFrameActivated(InternalFrameEvent e) { event("internalFrameActivated", e); }
135    public void internalFrameDeactivated(InternalFrameEvent e) { event("internalFrameDeactivated", e); }
136    public void intervalAdded(ListDataEvent e) { event("intervalAdded", e); }
137    public void intervalRemoved(ListDataEvent e) { event("intervalRemoved", e); }
138    public void contentsChanged(ListDataEvent e) { event("contentsChanged", e); }
139  	public void valueChanged(ListSelectionEvent e) { event("valueChanged", e); }
140    public void menuDragMouseEntered(MenuDragMouseEvent e) { event("menuDragMouseEntered", e); }
141    public void menuDragMouseExited(MenuDragMouseEvent e) { event("menuDragMouseExited", e); }
142    public void menuDragMouseDragged(MenuDragMouseEvent e) { event("menuDragMouseDragged", e); }
143    public void menuDragMouseReleased(MenuDragMouseEvent e) { event("menuDragMouseReleased", e); }
144    public void menuKeyTyped(MenuKeyEvent e) { event("menuKeyTyped", e); }
145    public void menuKeyPressed(MenuKeyEvent e) { event("menuKeyPressed", e); }
146    public void menuKeyReleased(MenuKeyEvent e) { event("menuKeyReleased", e); }
147    public void menuSelected(MenuEvent e) { event("menuSelected", e); }
148    public void menuDeselected(MenuEvent e) { event("menuDeselected", e); }
149    public void menuCanceled(MenuEvent e) { event("menuCanceled", e); }
150    public void popupMenuWillBecomeVisible(PopupMenuEvent e) { event("popupMenuWillBecomeVisible", e); }
151    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { event("popupMenuWillBecomeInvisible", e); }
152    public void popupMenuCanceled(PopupMenuEvent e) { event("popupMenuCanceled", e); }
153    public void columnAdded(TableColumnModelEvent e) { event("columnAdded", e); }
154    public void columnRemoved(TableColumnModelEvent e) { event("columnRemoved", e); }
155    public void columnMoved(TableColumnModelEvent e) { event("columnMoved", e); }
156    public void columnMarginChanged(ChangeEvent e) { event("columnMarginChanged", e); }
157    public void columnSelectionChanged(ListSelectionEvent e) { event("columnSelectionChanged", e); }
158    public void tableChanged(TableModelEvent e) { event("tableChanged", e); }
159    public void treeExpanded(TreeExpansionEvent e) { event("treeExpanded", e); }
160    public void treeCollapsed(TreeExpansionEvent e) { event("treeCollapsed", e); }
161    public void treeNodesChanged(TreeModelEvent e) { event("treeNodesChanged", e); }
162    public void treeNodesInserted(TreeModelEvent e) { event("treeNodesInserted", e); }
163    public void treeNodesRemoved(TreeModelEvent e) { event("treeNodesRemoved", e); }
164    public void treeStructureChanged(TreeModelEvent e) { event("treeStructureChanged", e); }
165    public void valueChanged(TreeSelectionEvent e) { event("valueChanged", e); }
166    public void treeWillExpand(TreeExpansionEvent e) { event("treeWillExpand", e); }
167    public void treeWillCollapse(TreeExpansionEvent e) { event("treeWillCollapse", e); }
168    public void undoableEditHappened(UndoableEditEvent e) { event("undoableEditHappened", e); }
169
170	// Listener interfaces
171	public void actionPerformed(ActionEvent e) { event("actionPerformed", e); }
172	public void adjustmentValueChanged(AdjustmentEvent e) { event("adjustmentValueChanged", e); }
173	public void componentResized(ComponentEvent e) { event("componentResized", e); }
174	public void componentMoved(ComponentEvent e) { event("componentMoved", e); }
175	public void componentShown(ComponentEvent e) { event("componentShown", e); }
176	public void componentHidden(ComponentEvent e) { event("componentHidden", e); }
177	public void componentAdded(ContainerEvent e) { event("componentAdded", e); }
178	public void componentRemoved(ContainerEvent e) { event("componentRemoved", e); }
179	public void focusGained(FocusEvent e) { event("focusGained", e); }
180	public void focusLost(FocusEvent e) { event("focusLost", e); }
181	public void itemStateChanged(ItemEvent e) { event("itemStateChanged", e); }
182	public void keyTyped(KeyEvent e) { event("keyTyped", e); }
183	public void keyPressed(KeyEvent e) { event("keyPressed", e); }
184	public void keyReleased(KeyEvent e) { event("keyReleased", e); }
185	public void mouseClicked(MouseEvent e) { event("mouseClicked", e); }
186	public void mousePressed(MouseEvent e) { event("mousePressed", e); }
187	public void mouseReleased(MouseEvent e) { event("mouseReleased", e); }
188	public void mouseEntered(MouseEvent e) { event("mouseEntered", e); }
189	public void mouseExited(MouseEvent e) { event("mouseExited", e); }
190	public void mouseDragged(MouseEvent e) { event("mouseDragged", e); }
191	public void mouseMoved(MouseEvent e) { event("mouseMoved", e); }
192	public void textValueChanged(TextEvent e) { event("textValueChanged", e); }
193	public void windowOpened(WindowEvent e) { event("windowOpened", e); }
194	public void windowClosing(WindowEvent e) { event("windowClosing", e); }
195	public void windowClosed(WindowEvent e) { event("windowClosed", e); }
196	public void windowIconified(WindowEvent e) { event("windowIconified", e); }
197	public void windowDeiconified(WindowEvent e) { event("windowDeiconified", e); }
198	public void windowActivated(WindowEvent e) { event("windowActivated", e); }
199	public void windowDeactivated(WindowEvent e) { event("windowDeactivated", e); }
200
201	public void propertyChange(PropertyChangeEvent e) { 
202		event("propertyChange", e ); }
203    public void vetoableChange(PropertyChangeEvent e) {
204		event("vetoableChange", e ); }
205
206    public boolean imageUpdate(java.awt.Image img, int infoflags,
207                               int x, int y, int width, int height) {
208
209		BshMethod method = namespace.getMethod( "imageUpdate",
210			new Class [] { null, null, null, null, null, null } );
211		if(method != null)
212			try {
213				CallStack callstack = newCallStack();
214				method.invokeDeclaredMethod( 
215					new Object[] { 
216						img, new Primitive(infoflags), new Primitive(x), 
217						new Primitive(y), new Primitive(width), 
218						new Primitive(height) }, 
219					declaringInterpreter, callstack, null
220				);
221			} catch(EvalError e) {
222				declaringInterpreter.error(
223					"local event handler imageUpdate: method invocation error:" + e );
224			}
225		return true;
226	}
227
228}
229