PageRenderTime 42ms CodeModel.GetById 22ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package bsh;
  34. import java.awt.event.*;
  35. import javax.swing.*;
  36. import javax.swing.event.*;
  37. import java.io.*;
  38. import java.beans.*;
  39. /**
  40. JThis is a dynamically loaded extension which extends This and adds
  41. explicit support for AWT and JFC events, etc. This is a backwards
  42. compatability measure for JDK 1.2. With 1.3+ there is a general
  43. reflection proxy mechanism that allows the base This to implement
  44. arbitrary interfaces.
  45. The NameSpace getThis() method will produce instances of JThis if
  46. the java version is prior to 1.3 and swing is available... (e.g. 1.2
  47. or 1.1 + swing installed)
  48. Users of 1.1 without swing will have minimal interface support (just run()).
  49. Bsh doesn't run on 1.02 and below because there is no reflection!
  50. Note: This module relies on features of Swing and will only compile
  51. with JDK1.2 or JDK1.1 + the swing package. For other environments simply
  52. do not compile this class.
  53. */
  54. class JThis extends This implements
  55. // All core AWT listeners
  56. ActionListener, AdjustmentListener, ComponentListener,
  57. ContainerListener, FocusListener, ItemListener, KeyListener,
  58. MouseListener, MouseMotionListener, TextListener, WindowListener,
  59. PropertyChangeListener,
  60. // All listeners in javax.swing.event as of Swing 1.1
  61. AncestorListener, CaretListener, CellEditorListener, ChangeListener,
  62. DocumentListener, HyperlinkListener,
  63. InternalFrameListener, ListDataListener, ListSelectionListener,
  64. MenuDragMouseListener, MenuKeyListener, MenuListener, MouseInputListener,
  65. PopupMenuListener, TableColumnModelListener, TableModelListener,
  66. TreeExpansionListener, TreeModelListener, TreeSelectionListener,
  67. TreeWillExpandListener, UndoableEditListener
  68. {
  69. JThis( NameSpace namespace, Interpreter declaringInterp ) {
  70. super( namespace, declaringInterp );
  71. }
  72. public String toString() {
  73. return "'this' reference (JThis) to Bsh object: " + namespace.name;
  74. }
  75. void event(String name, Object event)
  76. {
  77. BshMethod method;
  78. // handleEvent gets all events
  79. method = namespace.getMethod(
  80. "handleEvent", new Class [] { null } );
  81. if (method != null)
  82. try {
  83. method.invokeDeclaredMethod(
  84. new Object[] { event }, declaringInterpreter, callstack, null );
  85. } catch(EvalError e) {
  86. declaringInterpreter.error(
  87. "local event hander method invocation error:" + e );
  88. }
  89. // send to specific event handler
  90. method = namespace.getMethod( name, new Class [] { null } );
  91. if (method != null)
  92. try {
  93. method.invokeDeclaredMethod(
  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. // Listener interfaces
  101. public void ancestorAdded(AncestorEvent e) { event("ancestorAdded", e); }
  102. public void ancestorRemoved(AncestorEvent e) { event("ancestorRemoved", e); }
  103. public void ancestorMoved(AncestorEvent e) { event("ancestorMoved", e); }
  104. public void caretUpdate(CaretEvent e) { event("caretUpdate", e); }
  105. public void editingStopped(ChangeEvent e) { event("editingStopped", e); }
  106. public void editingCanceled(ChangeEvent e) { event("editingCanceled", e); }
  107. public void stateChanged(ChangeEvent e) { event("stateChanged", e); }
  108. public void insertUpdate(DocumentEvent e) { event("insertUpdate", e); }
  109. public void removeUpdate(DocumentEvent e) { event("removeUpdate", e); }
  110. public void changedUpdate(DocumentEvent e) { event("changedUpdate", e); }
  111. public void hyperlinkUpdate(HyperlinkEvent e) { event("internalFrameOpened", e); }
  112. public void internalFrameOpened(InternalFrameEvent e) { event("internalFrameOpened", e); }
  113. public void internalFrameClosing(InternalFrameEvent e) { event("internalFrameClosing", e); }
  114. public void internalFrameClosed(InternalFrameEvent e) { event("internalFrameClosed", e); }
  115. public void internalFrameIconified(InternalFrameEvent e) { event("internalFrameIconified", e); }
  116. public void internalFrameDeiconified(InternalFrameEvent e) { event("internalFrameDeiconified", e); }
  117. public void internalFrameActivated(InternalFrameEvent e) { event("internalFrameActivated", e); }
  118. public void internalFrameDeactivated(InternalFrameEvent e) { event("internalFrameDeactivated", e); }
  119. public void intervalAdded(ListDataEvent e) { event("intervalAdded", e); }
  120. public void intervalRemoved(ListDataEvent e) { event("intervalRemoved", e); }
  121. public void contentsChanged(ListDataEvent e) { event("contentsChanged", e); }
  122. public void valueChanged(ListSelectionEvent e) { event("valueChanged", e); }
  123. public void menuDragMouseEntered(MenuDragMouseEvent e) { event("menuDragMouseEntered", e); }
  124. public void menuDragMouseExited(MenuDragMouseEvent e) { event("menuDragMouseExited", e); }
  125. public void menuDragMouseDragged(MenuDragMouseEvent e) { event("menuDragMouseDragged", e); }
  126. public void menuDragMouseReleased(MenuDragMouseEvent e) { event("menuDragMouseReleased", e); }
  127. public void menuKeyTyped(MenuKeyEvent e) { event("menuKeyTyped", e); }
  128. public void menuKeyPressed(MenuKeyEvent e) { event("menuKeyPressed", e); }
  129. public void menuKeyReleased(MenuKeyEvent e) { event("menuKeyReleased", e); }
  130. public void menuSelected(MenuEvent e) { event("menuSelected", e); }
  131. public void menuDeselected(MenuEvent e) { event("menuDeselected", e); }
  132. public void menuCanceled(MenuEvent e) { event("menuCanceled", e); }
  133. public void popupMenuWillBecomeVisible(PopupMenuEvent e) { event("popupMenuWillBecomeVisible", e); }
  134. public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { event("popupMenuWillBecomeInvisible", e); }
  135. public void popupMenuCanceled(PopupMenuEvent e) { event("popupMenuCanceled", e); }
  136. public void columnAdded(TableColumnModelEvent e) { event("columnAdded", e); }
  137. public void columnRemoved(TableColumnModelEvent e) { event("columnRemoved", e); }
  138. public void columnMoved(TableColumnModelEvent e) { event("columnMoved", e); }
  139. public void columnMarginChanged(ChangeEvent e) { event("columnMarginChanged", e); }
  140. public void columnSelectionChanged(ListSelectionEvent e) { event("columnSelectionChanged", e); }
  141. public void tableChanged(TableModelEvent e) { event("tableChanged", e); }
  142. public void treeExpanded(TreeExpansionEvent e) { event("treeExpanded", e); }
  143. public void treeCollapsed(TreeExpansionEvent e) { event("treeCollapsed", e); }
  144. public void treeNodesChanged(TreeModelEvent e) { event("treeNodesChanged", e); }
  145. public void treeNodesInserted(TreeModelEvent e) { event("treeNodesInserted", e); }
  146. public void treeNodesRemoved(TreeModelEvent e) { event("treeNodesRemoved", e); }
  147. public void treeStructureChanged(TreeModelEvent e) { event("treeStructureChanged", e); }
  148. public void valueChanged(TreeSelectionEvent e) { event("valueChanged", e); }
  149. public void treeWillExpand(TreeExpansionEvent e) { event("treeWillExpand", e); }
  150. public void treeWillCollapse(TreeExpansionEvent e) { event("treeWillCollapse", e); }
  151. public void undoableEditHappened(UndoableEditEvent e) { event("undoableEditHappened", e); }
  152. // Listener interfaces
  153. public void actionPerformed(ActionEvent e) { event("actionPerformed", e); }
  154. public void adjustmentValueChanged(AdjustmentEvent e) { event("adjustmentValueChanged", e); }
  155. public void componentResized(ComponentEvent e) { event("componentResized", e); }
  156. public void componentMoved(ComponentEvent e) { event("componentMoved", e); }
  157. public void componentShown(ComponentEvent e) { event("componentShown", e); }
  158. public void componentHidden(ComponentEvent e) { event("componentHidden", e); }
  159. public void componentAdded(ContainerEvent e) { event("componentAdded", e); }
  160. public void componentRemoved(ContainerEvent e) { event("componentRemoved", e); }
  161. public void focusGained(FocusEvent e) { event("focusGained", e); }
  162. public void focusLost(FocusEvent e) { event("focusLost", e); }
  163. public void itemStateChanged(ItemEvent e) { event("itemStateChanged", e); }
  164. public void keyTyped(KeyEvent e) { event("keyTyped", e); }
  165. public void keyPressed(KeyEvent e) { event("keyPressed", e); }
  166. public void keyReleased(KeyEvent e) { event("keyReleased", e); }
  167. public void mouseClicked(MouseEvent e) { event("mouseClicked", e); }
  168. public void mousePressed(MouseEvent e) { event("mousePressed", e); }
  169. public void mouseReleased(MouseEvent e) { event("mouseReleased", e); }
  170. public void mouseEntered(MouseEvent e) { event("mouseEntered", e); }
  171. public void mouseExited(MouseEvent e) { event("mouseExited", e); }
  172. public void mouseDragged(MouseEvent e) { event("mouseDragged", e); }
  173. public void mouseMoved(MouseEvent e) { event("mouseMoved", e); }
  174. public void textValueChanged(TextEvent e) { event("textValueChanged", e); }
  175. public void windowOpened(WindowEvent e) { event("windowOpened", e); }
  176. public void windowClosing(WindowEvent e) { event("windowClosing", e); }
  177. public void windowClosed(WindowEvent e) { event("windowClosed", e); }
  178. public void windowIconified(WindowEvent e) { event("windowIconified", e); }
  179. public void windowDeiconified(WindowEvent e) { event("windowDeiconified", e); }
  180. public void windowActivated(WindowEvent e) { event("windowActivated", e); }
  181. public void windowDeactivated(WindowEvent e) { event("windowDeactivated", e); }
  182. public void propertyChange(PropertyChangeEvent e) {
  183. event("propertyChange", e ); }
  184. public void vetoableChange(PropertyChangeEvent e) {
  185. event("vetoableChange", e ); }
  186. public boolean imageUpdate(java.awt.Image img, int infoflags,
  187. int x, int y, int width, int height) {
  188. BshMethod method = namespace.getMethod( "imageUpdate",
  189. new Class [] { null, null, null, null, null, null } );
  190. if(method != null)
  191. try {
  192. method.invokeDeclaredMethod(
  193. new Object[] {
  194. img, new Primitive(infoflags), new Primitive(x),
  195. new Primitive(y), new Primitive(width),
  196. new Primitive(height) },
  197. declaringInterpreter, callstack, null
  198. );
  199. } catch(EvalError e) {
  200. declaringInterpreter.error(
  201. "local event handler imageUpdate: method invocation error:" + e );
  202. }
  203. return true;
  204. }
  205. /**
  206. For serialization.
  207. Note: this is copied from superclass...
  208. It must be private, but we can probably add an accessor to allow
  209. us to call the super method explicitly.
  210. Just testing to see if this is causing a problem.
  211. */
  212. private synchronized void writeObject(java.io.ObjectOutputStream s)
  213. throws IOException {
  214. // Temporarily prune the namespace.
  215. NameSpace parent = namespace.getParent();
  216. // Bind would set the interpreter, but it's possible that the parent
  217. // is null (it's the root). So save it...
  218. Interpreter interpreter = declaringInterpreter;
  219. namespace.prune();
  220. s.defaultWriteObject();
  221. // put it back
  222. namespace.setParent( parent );
  223. declaringInterpreter = interpreter;
  224. }
  225. }