home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / jsj / classes / netscape / javascript / adapters / PropertyChangeAdapter.java < prev   
Encoding:
Java Source  |  1998-04-08  |  3.7 KB  |  96 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* ** */
  19.  
  20. /*
  21.  * PropertyChangeAdapter.java
  22.  *
  23.  */
  24.  
  25. package netscape.javascript.adapters;
  26.  
  27. import netscape.javascript.JSObject;
  28.  
  29. import java.beans.PropertyChangeListener;
  30. import java.beans.PropertyChangeEvent;
  31.  
  32. /**
  33.  * PropertyChangeAdaptor allows PropertyChangeEvents to be handled in JavaScript.
  34.  * It implements java.beans.PropertyChangeListener which is required in order to
  35.  * order to catch PropertyChangeEvents, but impossible to do in JavaScript due to
  36.  * Java's type safe implementation.  Once the event is caught, it is passed along
  37.  * to a JavaScript method on a JavaScript object (JSObject). This method is
  38.  * "onChange" by default, or an arbitrary method name can be supplied.
  39.  */
  40.  public class PropertyChangeAdapter implements PropertyChangeListener {
  41.  
  42.     /**
  43.      *  Construct a PropertyChangeAdaptor for given JavaScript object.
  44.      *  propertyChange events will be relayed to a method called onChange on
  45.      *  the JavaScript object.  This onChange method must have a single parameter,
  46.      *  which is a java.beans.PropertyChangeEvent object.
  47.      */
  48.     public PropertyChangeAdapter( JSObject oJS ) {
  49.         moJSObject = oJS;
  50.     }
  51.  
  52.     /**
  53.      *  Construct a PropertyChangeAdaptor for given JavaScript object.
  54.      *  propertyChange events will be relayed to a the specified method on
  55.      *  the JavaScript object.  The method must have a single parameter, which
  56.      *  is a java.beans.PropertyChangeEvent object.
  57.      */
  58.     public PropertyChangeAdapter( JSObject oJS, String sJSMethodName ) {
  59.         moJSObject = oJS;
  60.         msJSMethodName = sJSMethodName;
  61.     }
  62.  
  63.     /**
  64.      * propertyChange() is called by a source component (bean) when any of
  65.      * its "bound" properties are altered.  We catch it and call a JavaScript
  66.      * method so that handling code can occur using user's JavaScript.
  67.      */
  68.     public void propertyChange( PropertyChangeEvent oEvent ) {
  69.         if ( null != moJSObject ) {
  70.             // don't try to call mehtod which does not exist!
  71.             Object oMethod = moJSObject.getMember( msJSMethodName );
  72.             if ( null != oMethod ) {
  73.                 // pass oEvent as single parameter
  74.                 Object[] aArgs = new Object[1];
  75.                 aArgs[0] = oEvent;
  76.                 try {
  77.                     moJSObject.call( msJSMethodName, aArgs );
  78.                 } catch (Exception ex) {
  79.                     System.out.println("Exception: " + ex);
  80.                     ex.printStackTrace();
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     //-------------------------------------------------------------
  87.     // member variables
  88.  
  89.     // ---------- static variables --------------------------------
  90.     public static final String gsDefaultJSMethodName = "onChange";
  91.  
  92.     // ---------- instance variables ------------------------------
  93.     private String      msJSMethodName   = gsDefaultJSMethodName;
  94.     private JSObject    moJSObject       = null;
  95. }
  96.