home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / test / plugin / composer / EditRaw.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  5.9 KB  |  181 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 java.io.*;
  22. import java.awt.*;
  23. import netscape.plugin.composer.*;
  24.  
  25. /** Allow the user to view and edit the raw html of the document.
  26.  * Shows how to call awt (and by extension any user interface
  27.  * toolkit) from a plugin.
  28.  */
  29.  
  30. public class EditRaw extends Plugin {
  31.     /** Test the plugin. Not required for normal operation of the plugin.
  32.      * You can use this to run the plugin from the command line:
  33.      * java -classpath <your-class-path> <your-plugin-name> args...
  34.      * where args... are passed on to the Test class.
  35.      * You can remove this code before shipping your plugin.
  36.      */
  37.     static public void main(String[] args) {
  38.         Test.perform(args, new EditRaw());
  39.     }
  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 "Edit HTML";
  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 "HTML Tools";
  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 "Allows editing of the raw HTML of the document.";
  64.     }
  65.  
  66.     /** Execute the command.
  67.      * @param document the current document state.
  68.      */
  69.     public boolean perform(Document document) throws IOException{
  70.  
  71.         MyDialog dialog = new MyDialog("Edit Raw HTML", document);
  72.         dialog.reshape(50,50,300,300);
  73.         dialog.show(); // make the window visible.
  74.         dialog.requestFocus(); // Make sure the window is on top and gets focus.
  75.         boolean result = dialog.waitForExit(); //Wait for the user to exit the dialog.
  76.         dialog.dispose(); // Cleans up the native OS window associated with the dialog.
  77.         if ( result ) {
  78.             document.setText(dialog.getText());
  79.         }
  80.         return result;
  81.     }
  82.  
  83. }
  84.  
  85. /** An awt dialog for interacting with the user. This is like
  86.  * the java.awt.Dialog class, except that it doesn't require a
  87.  * parent Frame.
  88.  */
  89. class MyDialog extends Frame {
  90.     public MyDialog(String title, Document document) {
  91.         super(title);
  92.         this.document = document;
  93.         Panel buttons = new Panel();
  94.         buttons.add("East", ok = new Button("OK"));
  95.         buttons.add("Center", apply = new Button("Apply"));
  96.         buttons.add("West", cancel = new Button("Cancel"));
  97.         add("Center", text = new TextArea());
  98.         add("South", buttons);
  99.  
  100.         copyTextFromDocument();
  101.      }
  102.     /** Handle window close event.
  103.     */
  104.     public boolean handleEvent(Event event) {
  105.         if (event.id == Event.WINDOW_DESTROY) {
  106.             hide();
  107.             signalExit();
  108.             return true;
  109.         } else {
  110.             return super.handleEvent(event);
  111.         }
  112.     }
  113.     /** Handle the actions of the dialog.
  114.      */
  115.     public boolean action(Event evt, Object what){
  116.         if ( evt.target == ok || evt.target == cancel) {
  117.             success = evt.target == ok;
  118.             hide();
  119.             signalExit();
  120.             return true;
  121.         }
  122.         else if ( evt.target == apply ) {
  123.             try {
  124.                 document.setText(getText());
  125.             } catch(IOException e){
  126.                 System.err.println("Error writing document:");
  127.                 e.printStackTrace();
  128.             }
  129.             return true;
  130.         }
  131.         return false;
  132.     }
  133.     /** Copies the text from the document to
  134.      * the dialog box.
  135.      */
  136.     protected void copyTextFromDocument() {
  137.         try {
  138.             setText(document.getText());
  139.         } catch(IOException e){
  140.             System.err.println("Error reading document:");
  141.             e.printStackTrace();
  142.         }
  143.     }
  144.     /** Puts text into the dialog box.
  145.      */
  146.     public void setText(String text){
  147.         this.text.setText(text);
  148.     }
  149.     /** Copies text out of the dialog box.
  150.     */
  151.     public String getText(){
  152.         return this.text.getText();
  153.     }
  154.     /** Called by the main plug-in thread. This method waits for
  155.      * the dialog thread to call signalExit(), and then it returns.
  156.      */
  157.     synchronized public boolean waitForExit() {
  158.         while ( ! bExited ) {
  159.             try {
  160.                 wait();
  161.             } catch ( InterruptedException e){
  162.             }
  163.         }
  164.         return success;
  165.     }
  166.     /** Called from the dialog thread to signal to the plug-in thread
  167.      * that the dialog is finished.
  168.      */
  169.     synchronized public void signalExit() {
  170.         bExited = true;
  171.         notifyAll();
  172.     }
  173.     private Button ok;
  174.     private Button apply;
  175.     private Button cancel;
  176.     private TextArea text;
  177.     private boolean bExited = false;
  178.     private boolean success = false;
  179.     private Document document;
  180. }
  181.