home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / test / plugin / composer / IFCTest.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  7.3 KB  |  235 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. package netscape.test.plugin.composer;
  20.  
  21. import netscape.plugin.composer.*;
  22. import netscape.plugin.composer.io.*;
  23. import java.util.*;
  24. import java.io.*;
  25.  
  26. import netscape.application.*;
  27. import netscape.util.*;
  28.  
  29. /** Sample Plugin that uses the Internet Foundation Classes.
  30.  * It shows how to use the IFC from within a Composer plug-in.
  31.  * <p>
  32.  * As of Netscape Navigator 4.0b2,
  33.  * the file load/save menu items don't work because Navigator 4.0b2 is
  34.  * missing the AWT File Dialog classes.
  35.  */
  36.  
  37. public class IFCTest extends Plugin {
  38.     static public void main(String[] args){
  39.         Test.perform(args, new IFCTest());
  40.     }
  41.     /** Get the human readable name of the plugin. Defaults to the name of the plugin class.
  42.      * @return the human readable name of the plugin.
  43.      */
  44.     public String getName()
  45.     {
  46.         return bundle().getString("name");
  47.     }
  48.  
  49.     /** Get the human readable category of the plugin. Defaults to the name of the plugin class.
  50.      * @return the human readable category of the plugin.
  51.      */
  52.     public String getCategory()
  53.     {
  54.         return bundle().getString("category");
  55.     }
  56.  
  57.     /** Get the human readable hint for the plugin. This is a one-sentence description of
  58.      * what the plugin does. Defaults to the name of the plugin class.
  59.      * @return the human readable hint for the plugin.
  60.      */
  61.     public String getHint()
  62.     {
  63.         return bundle().getString("hint");
  64.     }
  65.  
  66.     public boolean perform(Document document) throws IOException {
  67.         IFCTestApp app = new IFCTestApp(document);
  68.         app.run();
  69.         boolean result = app.result();
  70.         if ( result ) {
  71.             document.setText(app.getText());
  72.         }
  73.         return result;
  74.     }
  75.  
  76.     static public ResourceBundle bundle() {
  77.         if ( bundle_ == null ){
  78.             bundle_ = ResourceBundle.getBundle(
  79.                 "netscape.test.plugin.composer.IFCTestBundle");
  80.         }
  81.         return bundle_;
  82.     }
  83.     private static ResourceBundle bundle_;
  84. }
  85.  
  86. /** The IFC application that implements the plug-in.
  87.  */
  88.  
  89. class IFCTestApp extends Application implements Target,
  90.     WindowOwner
  91. {
  92.     public IFCTestApp(Document document){
  93.         this.document = document;
  94.     }
  95.     public boolean result() {
  96.         return this.bresult;
  97.     }
  98.     /** This method gets called to initialize an application. We'll take
  99.       * this opportunity to set up the View hierarchy.
  100.       */
  101.     public void init() {
  102.         try {
  103.         super.init();
  104.             init2();
  105.         }
  106.         catch(Throwable t){
  107.             t.printStackTrace();
  108.         }
  109.     }
  110.  
  111.     private void init2(){
  112.         mainWindow = new ExternalWindow();
  113.         mainWindow.setOwner(this);
  114.         mainWindow.setTitle(IFCTest.bundle().getString("title"));
  115.         setMainRootView(mainWindow.rootView());
  116.         mainWindow.sizeTo(620, 450);
  117.         mainWindow.setResizable(false);
  118.  
  119.         RootView v = mainWindow.rootView();
  120.         v.setColor(Color.lightGray);
  121.  
  122.         // How about some text?
  123.  
  124.         textView_ = new TextView();
  125.         textView_.setBounds(0,0,600-5,1000);
  126.         copyTextFromDocument();
  127.  
  128.         ScrollGroup scrollGroup = new ScrollGroup(0,0,600,385);
  129.         scrollGroup.scrollView().setContentView(textView_);
  130.         scrollGroup.setHorizScrollBarDisplay(ScrollGroup.NEVER_DISPLAY);
  131.         scrollGroup.setVertScrollBarDisplay(ScrollGroup.AS_NEEDED_DISPLAY);
  132.         scrollGroup.setBackgroundColor(Color.white);
  133.         v.addSubview(scrollGroup);
  134.  
  135.         int buttonY = 400;
  136.  
  137.         Button ok = Button.createPushButton(25, buttonY, 150, 20);
  138.         ok.setTitle(IFCTest.bundle().getString("ok"));
  139.         ok.setCommand("ok");
  140.         ok.setTarget(this);
  141.         v.addSubview(ok);
  142.  
  143.         Button apply = Button.createPushButton(185, buttonY, 150, 20);
  144.         apply.setTitle(IFCTest.bundle().getString("apply"));
  145.         apply.setCommand("apply");
  146.         apply.setTarget(this);
  147.         v.addSubview(apply);
  148.  
  149.         Button cancel = Button.createPushButton(345, buttonY, 150, 20);
  150.         cancel.setTitle(IFCTest.bundle().getString("cancel"));
  151.         cancel.setCommand("cancel");
  152.         cancel.setTarget(this);
  153.         v.addSubview(cancel);
  154.  
  155.         mainWindow.show();
  156.     }
  157.  
  158.     public void performCommand(String command, Object arg) {
  159.         try {
  160.             if (command.equals("ok")){
  161.                 bresult = true;
  162.                 stopRunning();
  163.             }
  164.             else if (command.equals("apply")){
  165.                 try {
  166.                     document.setText(getText());
  167.                 } catch(IOException e){
  168.                     System.err.println("Error writing document:");
  169.                     e.printStackTrace();
  170.                 }
  171.             }
  172.             else if (command.equals("cancel")){
  173.                 bresult = false;
  174.                 stopRunning();
  175.             }
  176.             else {
  177.                 System.err.println("Unknown command " + command);
  178.             }
  179.         }
  180.         catch (Throwable t){
  181.             System.err.println("Caught exception while performing command:");
  182.             t.printStackTrace();
  183.         }
  184.     }
  185.     /** Copies the text from the document to
  186.      * the dialog box.
  187.      */
  188.     protected void copyTextFromDocument() {
  189.         try {
  190.             setText(document.getText());
  191.         } catch(IOException e){
  192.             System.err.println("Error reading document:");
  193.             e.printStackTrace();
  194.         }
  195.     }
  196.     /** Puts text into the dialog box.
  197.      */
  198.     public void setText(String text){
  199.         textView_.setString(text);
  200.     }
  201.     /** Copies text out of the dialog box.
  202.     */
  203.     public String getText(){
  204.         return textView_.string();
  205.     }
  206.  
  207.  
  208.     // Boiler-plate code to allow us to run as an application
  209.  
  210.     public RootView rootView(){
  211.         if ( mainWindow != null ){
  212.             return mainWindow.rootView();
  213.         }
  214.         return super.mainRootView();
  215.     }
  216.  
  217.     public boolean windowWillShow(Window aWindow){return true;}
  218.     public boolean windowWillHide(Window aWindow){return true;}
  219.     public void windowWillSizeBy(Window aWindow, Size aSize){}
  220.     public void windowDidBecomeMain(Window aWindow){}
  221.     public void windowDidShow(Window aWindow){}
  222.     public void windowDidResignMain(Window aWindow){}
  223.     public void windowDidHide(Window aWindow){
  224.         if ( aWindow == mainWindow ){
  225.             stopRunning();
  226.             }
  227.         }
  228.  
  229.     private TextView textView_;
  230.     private ExternalWindow mainWindow;
  231.     private boolean bresult;
  232.     private Document document;
  233. }
  234.  
  235.