home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / jsj / classes / netscape / javascript / adapters / JSTargetAdapter.java next >
Encoding:
Java Source  |  1998-04-08  |  1.9 KB  |  64 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. package netscape.javascript.adapters;
  21.  
  22. import netscape.javascript.*;
  23. import netscape.application.Target;
  24.  
  25. /**
  26.  * JSTargetAdapter is used in JavaScript code to deliver target
  27.  * commands to a JavaScript object.
  28.  */
  29. public final class JSTargetAdapter implements Target {
  30.     /* the real target object in JavaScript */
  31.     private JSObject jsObj;
  32.  
  33.     /**
  34.      * construct a new JSTargetAdapter for a given JSObject
  35.      */
  36.     public JSTargetAdapter(JSObject jsObj) {
  37.         this.jsObj = jsObj;
  38.     }
  39.  
  40.     /**
  41.      * Check to see whether the JavaScript object has an ["on" + command]
  42.      * member, and if so invoke it with the data parameter.
  43.      */
  44.     public void performCommand(String command, Object data) {
  45.         if (jsObj == null)
  46.             return;
  47.  
  48.         Object member = jsObj.getMember("on" + command);
  49.         if (member == null)
  50.             return;
  51.  
  52.         Object[] args;
  53.         args = new Object[1];
  54.         args[0] = data;
  55.  
  56.         try {
  57.             jsObj.call("on" + command, args);
  58.         } catch (Exception e) {
  59.             System.out.println("Exception: " + e);
  60.             e.printStackTrace();
  61.         }
  62.     }
  63. }
  64.