001: /*****************************************************************************
002: * *
003: * This file is part of the BeanShell Java Scripting distribution. *
004: * Documentation and updates may be found at http://www.beanshell.org/ *
005: * *
006: * Sun Public License Notice: *
007: * *
008: * The contents of this file are subject to the Sun Public License Version *
009: * 1.0 (the "License"); you may not use this file except in compliance with *
010: * the License. A copy of the License is available at http://www.sun.com *
011: * *
012: * The Original Code is BeanShell. The Initial Developer of the Original *
013: * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014: * (C) 2000. All Rights Reserved. *
015: * *
016: * GNU Public License Notice: *
017: * *
018: * Alternatively, the contents of this file may be used under the terms of *
019: * the GNU Lesser General Public License (the "LGPL"), in which case the *
020: * provisions of LGPL are applicable instead of those above. If you wish to *
021: * allow use of your version of this file only under the terms of the LGPL *
022: * and not to allow others to use your version of this file under the SPL, *
023: * indicate your decision by deleting the provisions above and replace *
024: * them with the notice and other provisions required by the LGPL. If you *
025: * do not delete the provisions above, a recipient may use your version of *
026: * this file under either the SPL or the LGPL. *
027: * *
028: * Patrick Niemeyer (pat@pat.net) *
029: * Author of Learning Java, O'Reilly & Associates *
030: * http://www.pat.net/~pat/ *
031: * *
032: *****************************************************************************/package bsh;
033:
034: import java.awt.event.*;
035: import javax.swing.*;
036: import javax.swing.event.*;
037: import java.io.*;
038: import java.beans.*;
039:
040: /**
041: JThis is a dynamically loaded extension which extends This and adds
042: explicit support for AWT and JFC events, etc. This is a backwards
043: compatability measure for JDK 1.2. With 1.3+ there is a general
044: reflection proxy mechanism that allows the base This to implement
045: arbitrary interfaces.
046:
047: The NameSpace getThis() method will produce instances of JThis if
048: the java version is prior to 1.3 and swing is available... (e.g. 1.2
049: or 1.1 + swing installed)
050:
051: Users of 1.1 without swing will have minimal interface support (just run()).
052:
053: Bsh doesn't run on 1.02 and below because there is no reflection!
054:
055: Note: This module relies on features of Swing and will only compile
056: with JDK1.2 or JDK1.1 + the swing package. For other environments simply
057: do not compile this class.
058: */
059: class JThis extends This
060: implements
061: // All core AWT listeners
062: ActionListener, AdjustmentListener,
063: ComponentListener,
064: ContainerListener,
065: FocusListener,
066: ItemListener,
067: KeyListener,
068: MouseListener,
069: MouseMotionListener,
070: TextListener,
071: WindowListener,
072: PropertyChangeListener,
073: // All listeners in javax.swing.event as of Swing 1.1
074: AncestorListener, CaretListener, CellEditorListener,
075: ChangeListener, DocumentListener, HyperlinkListener,
076: InternalFrameListener, ListDataListener, ListSelectionListener,
077: MenuDragMouseListener, MenuKeyListener, MenuListener,
078: MouseInputListener, PopupMenuListener,
079: TableColumnModelListener, TableModelListener,
080: TreeExpansionListener, TreeModelListener,
081: TreeSelectionListener, TreeWillExpandListener,
082: UndoableEditListener {
083:
084: JThis(NameSpace namespace, Interpreter declaringInterp) {
085: super (namespace, declaringInterp);
086: }
087:
088: public String toString() {
089: return "'this' reference (JThis) to Bsh object: "
090: + namespace.getName();
091: }
092:
093: void event(String name, Object event) {
094: CallStack callstack = new CallStack(namespace);
095: BshMethod method = null;
096:
097: // handleEvent gets all events
098: try {
099: method = namespace.getMethod("handleEvent",
100: new Class[] { null });
101: } catch (UtilEvalError e) {/*squeltch*/
102: }
103:
104: if (method != null)
105: try {
106: method.invoke(new Object[] { event },
107: declaringInterpreter, callstack, null);
108: } catch (EvalError e) {
109: declaringInterpreter
110: .error("local event hander method invocation error:"
111: + e);
112: }
113:
114: // send to specific event handler
115: try {
116: method = namespace.getMethod(name, new Class[] { null });
117: } catch (UtilEvalError e) { /*squeltch*/
118: }
119: if (method != null)
120: try {
121: method.invoke(new Object[] { event },
122: declaringInterpreter, callstack, null);
123: } catch (EvalError e) {
124: declaringInterpreter
125: .error("local event hander method invocation error:"
126: + e);
127: }
128: }
129:
130: // Listener interfaces
131:
132: public void ancestorAdded(AncestorEvent e) {
133: event("ancestorAdded", e);
134: }
135:
136: public void ancestorRemoved(AncestorEvent e) {
137: event("ancestorRemoved", e);
138: }
139:
140: public void ancestorMoved(AncestorEvent e) {
141: event("ancestorMoved", e);
142: }
143:
144: public void caretUpdate(CaretEvent e) {
145: event("caretUpdate", e);
146: }
147:
148: public void editingStopped(ChangeEvent e) {
149: event("editingStopped", e);
150: }
151:
152: public void editingCanceled(ChangeEvent e) {
153: event("editingCanceled", e);
154: }
155:
156: public void stateChanged(ChangeEvent e) {
157: event("stateChanged", e);
158: }
159:
160: public void insertUpdate(DocumentEvent e) {
161: event("insertUpdate", e);
162: }
163:
164: public void removeUpdate(DocumentEvent e) {
165: event("removeUpdate", e);
166: }
167:
168: public void changedUpdate(DocumentEvent e) {
169: event("changedUpdate", e);
170: }
171:
172: public void hyperlinkUpdate(HyperlinkEvent e) {
173: event("internalFrameOpened", e);
174: }
175:
176: public void internalFrameOpened(InternalFrameEvent e) {
177: event("internalFrameOpened", e);
178: }
179:
180: public void internalFrameClosing(InternalFrameEvent e) {
181: event("internalFrameClosing", e);
182: }
183:
184: public void internalFrameClosed(InternalFrameEvent e) {
185: event("internalFrameClosed", e);
186: }
187:
188: public void internalFrameIconified(InternalFrameEvent e) {
189: event("internalFrameIconified", e);
190: }
191:
192: public void internalFrameDeiconified(InternalFrameEvent e) {
193: event("internalFrameDeiconified", e);
194: }
195:
196: public void internalFrameActivated(InternalFrameEvent e) {
197: event("internalFrameActivated", e);
198: }
199:
200: public void internalFrameDeactivated(InternalFrameEvent e) {
201: event("internalFrameDeactivated", e);
202: }
203:
204: public void intervalAdded(ListDataEvent e) {
205: event("intervalAdded", e);
206: }
207:
208: public void intervalRemoved(ListDataEvent e) {
209: event("intervalRemoved", e);
210: }
211:
212: public void contentsChanged(ListDataEvent e) {
213: event("contentsChanged", e);
214: }
215:
216: public void valueChanged(ListSelectionEvent e) {
217: event("valueChanged", e);
218: }
219:
220: public void menuDragMouseEntered(MenuDragMouseEvent e) {
221: event("menuDragMouseEntered", e);
222: }
223:
224: public void menuDragMouseExited(MenuDragMouseEvent e) {
225: event("menuDragMouseExited", e);
226: }
227:
228: public void menuDragMouseDragged(MenuDragMouseEvent e) {
229: event("menuDragMouseDragged", e);
230: }
231:
232: public void menuDragMouseReleased(MenuDragMouseEvent e) {
233: event("menuDragMouseReleased", e);
234: }
235:
236: public void menuKeyTyped(MenuKeyEvent e) {
237: event("menuKeyTyped", e);
238: }
239:
240: public void menuKeyPressed(MenuKeyEvent e) {
241: event("menuKeyPressed", e);
242: }
243:
244: public void menuKeyReleased(MenuKeyEvent e) {
245: event("menuKeyReleased", e);
246: }
247:
248: public void menuSelected(MenuEvent e) {
249: event("menuSelected", e);
250: }
251:
252: public void menuDeselected(MenuEvent e) {
253: event("menuDeselected", e);
254: }
255:
256: public void menuCanceled(MenuEvent e) {
257: event("menuCanceled", e);
258: }
259:
260: public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
261: event("popupMenuWillBecomeVisible", e);
262: }
263:
264: public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
265: event("popupMenuWillBecomeInvisible", e);
266: }
267:
268: public void popupMenuCanceled(PopupMenuEvent e) {
269: event("popupMenuCanceled", e);
270: }
271:
272: public void columnAdded(TableColumnModelEvent e) {
273: event("columnAdded", e);
274: }
275:
276: public void columnRemoved(TableColumnModelEvent e) {
277: event("columnRemoved", e);
278: }
279:
280: public void columnMoved(TableColumnModelEvent e) {
281: event("columnMoved", e);
282: }
283:
284: public void columnMarginChanged(ChangeEvent e) {
285: event("columnMarginChanged", e);
286: }
287:
288: public void columnSelectionChanged(ListSelectionEvent e) {
289: event("columnSelectionChanged", e);
290: }
291:
292: public void tableChanged(TableModelEvent e) {
293: event("tableChanged", e);
294: }
295:
296: public void treeExpanded(TreeExpansionEvent e) {
297: event("treeExpanded", e);
298: }
299:
300: public void treeCollapsed(TreeExpansionEvent e) {
301: event("treeCollapsed", e);
302: }
303:
304: public void treeNodesChanged(TreeModelEvent e) {
305: event("treeNodesChanged", e);
306: }
307:
308: public void treeNodesInserted(TreeModelEvent e) {
309: event("treeNodesInserted", e);
310: }
311:
312: public void treeNodesRemoved(TreeModelEvent e) {
313: event("treeNodesRemoved", e);
314: }
315:
316: public void treeStructureChanged(TreeModelEvent e) {
317: event("treeStructureChanged", e);
318: }
319:
320: public void valueChanged(TreeSelectionEvent e) {
321: event("valueChanged", e);
322: }
323:
324: public void treeWillExpand(TreeExpansionEvent e) {
325: event("treeWillExpand", e);
326: }
327:
328: public void treeWillCollapse(TreeExpansionEvent e) {
329: event("treeWillCollapse", e);
330: }
331:
332: public void undoableEditHappened(UndoableEditEvent e) {
333: event("undoableEditHappened", e);
334: }
335:
336: // Listener interfaces
337: public void actionPerformed(ActionEvent e) {
338: event("actionPerformed", e);
339: }
340:
341: public void adjustmentValueChanged(AdjustmentEvent e) {
342: event("adjustmentValueChanged", e);
343: }
344:
345: public void componentResized(ComponentEvent e) {
346: event("componentResized", e);
347: }
348:
349: public void componentMoved(ComponentEvent e) {
350: event("componentMoved", e);
351: }
352:
353: public void componentShown(ComponentEvent e) {
354: event("componentShown", e);
355: }
356:
357: public void componentHidden(ComponentEvent e) {
358: event("componentHidden", e);
359: }
360:
361: public void componentAdded(ContainerEvent e) {
362: event("componentAdded", e);
363: }
364:
365: public void componentRemoved(ContainerEvent e) {
366: event("componentRemoved", e);
367: }
368:
369: public void focusGained(FocusEvent e) {
370: event("focusGained", e);
371: }
372:
373: public void focusLost(FocusEvent e) {
374: event("focusLost", e);
375: }
376:
377: public void itemStateChanged(ItemEvent e) {
378: event("itemStateChanged", e);
379: }
380:
381: public void keyTyped(KeyEvent e) {
382: event("keyTyped", e);
383: }
384:
385: public void keyPressed(KeyEvent e) {
386: event("keyPressed", e);
387: }
388:
389: public void keyReleased(KeyEvent e) {
390: event("keyReleased", e);
391: }
392:
393: public void mouseClicked(MouseEvent e) {
394: event("mouseClicked", e);
395: }
396:
397: public void mousePressed(MouseEvent e) {
398: event("mousePressed", e);
399: }
400:
401: public void mouseReleased(MouseEvent e) {
402: event("mouseReleased", e);
403: }
404:
405: public void mouseEntered(MouseEvent e) {
406: event("mouseEntered", e);
407: }
408:
409: public void mouseExited(MouseEvent e) {
410: event("mouseExited", e);
411: }
412:
413: public void mouseDragged(MouseEvent e) {
414: event("mouseDragged", e);
415: }
416:
417: public void mouseMoved(MouseEvent e) {
418: event("mouseMoved", e);
419: }
420:
421: public void textValueChanged(TextEvent e) {
422: event("textValueChanged", e);
423: }
424:
425: public void windowOpened(WindowEvent e) {
426: event("windowOpened", e);
427: }
428:
429: public void windowClosing(WindowEvent e) {
430: event("windowClosing", e);
431: }
432:
433: public void windowClosed(WindowEvent e) {
434: event("windowClosed", e);
435: }
436:
437: public void windowIconified(WindowEvent e) {
438: event("windowIconified", e);
439: }
440:
441: public void windowDeiconified(WindowEvent e) {
442: event("windowDeiconified", e);
443: }
444:
445: public void windowActivated(WindowEvent e) {
446: event("windowActivated", e);
447: }
448:
449: public void windowDeactivated(WindowEvent e) {
450: event("windowDeactivated", e);
451: }
452:
453: public void propertyChange(PropertyChangeEvent e) {
454: event("propertyChange", e);
455: }
456:
457: public void vetoableChange(PropertyChangeEvent e) {
458: event("vetoableChange", e);
459: }
460:
461: public boolean imageUpdate(java.awt.Image img, int infoflags,
462: int x, int y, int width, int height) {
463:
464: BshMethod method = null;
465: try {
466: method = namespace.getMethod("imageUpdate", new Class[] {
467: null, null, null, null, null, null });
468: } catch (UtilEvalError e) {/*squeltch*/
469: }
470:
471: if (method != null)
472: try {
473: CallStack callstack = new CallStack(namespace);
474: method.invoke(new Object[] { img,
475: new Primitive(infoflags), new Primitive(x),
476: new Primitive(y), new Primitive(width),
477: new Primitive(height) }, declaringInterpreter,
478: callstack, null);
479: } catch (EvalError e) {
480: declaringInterpreter
481: .error("local event handler imageUpdate: method invocation error:"
482: + e);
483: }
484: return true;
485: }
486:
487: }
|