home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / test / plugin / composer / AddLayer.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  5.7 KB  |  169 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. import netscape.plugin.composer.io.*;
  25.  
  26. /** Sample Plugin that edits layers.
  27.  */
  28.  
  29. public class AddLayer extends Plugin {
  30.     /** Test the plugin. Not required for normal operation of the plugin.
  31.      * You can use this to run the plugin from the command line:
  32.      * java -classpath <your-class-path> <your-plugin-name> args...
  33.      * where args... are passed on to the Test class.
  34.      * You can remove this code before shipping your plugin.
  35.      */
  36.     static public void main(String[] args) {
  37.         Test.perform(args, new AddLayer());
  38.     }
  39.     /** Get the human readable name of the plugin. Defaults to the name of the plugin class.
  40.      * @return the human readable name of the plugin.
  41.      */
  42.     public String getName()
  43.     {
  44.         return "Create Layer...";
  45.     }
  46.  
  47.     /** Get the human readable category of the plugin. Defaults to the name of the plugin class.
  48.      * @return the human readable category of the plugin.
  49.      */
  50.     public String getCategory()
  51.     {
  52.         return "Layers";
  53.     }
  54.  
  55.     /** Get the human readable hint for the plugin. This is a one-sentence description of
  56.      * what the plugin does. Defaults to the name of the plugin class.
  57.      * @return the human readable hint for the plugin.
  58.      */
  59.     public String getHint()
  60.     {
  61.         return "Turns the selection into a layer.";
  62.     }
  63.  
  64.     /** Perform the action of the plugin.
  65.      *
  66.      * @param document the current document.
  67.      */
  68.     public boolean perform(Document document) throws IOException{
  69.         // Interact with the user to get the layer tag/
  70.  
  71.         Tag layerTag = getLayerTagFromUser(document);
  72.         if ( layerTag == null ) return false; // User cancled.
  73.         Tag endTag = new Tag(layerTag.getName(), false);
  74.         // Get the output stream to hold the new document text.
  75.         PrintWriter out = new PrintWriter(document.getOutput());
  76.         // Create a lexical stream to tokenize the old document text.
  77.         LexicalStream in = new LexicalStream(new SelectedHTMLReader(document.getInput(), out));
  78.         for(;;){
  79.             // Get the next token of the document.
  80.             Token token = in.next();
  81.             if ( token == null ) break; //  Null means we've finished the document.
  82.             else if ( token instanceof Comment ) {
  83.                 Comment comment = (Comment) token;
  84.                 if ( comment.isSelectionStart() ) {
  85.                     out.print(layerTag);
  86.                 }
  87.                 else if ( comment.isSelectionEnd() ) {
  88.                     out.print(comment);
  89.                     out.print(endTag);
  90.                     continue; // So comment isn't printed twice.
  91.                 }
  92.             }
  93.             out.print(token);
  94.         }
  95.         out.close();
  96.         return true;
  97.     }
  98.  
  99.     private Tag getLayerTagFromUser(Document document){
  100.         AddLayerDialog dialog = new AddLayerDialog("Add Layer", document);
  101.         dialog.reshape(50,50,300,300);
  102.         dialog.show();
  103.         dialog.requestFocus();
  104.  
  105.         /* Wait for the user to exit the dialog. */
  106.         boolean result = dialog.waitForExit();
  107.         dialog.dispose(); // Cleans up the native OS window associated with the dialog.
  108.         if ( result ) {
  109.             Tag layer = new Tag("LAYER");
  110.             return layer;
  111.         }
  112.         else return null;
  113.     }
  114. }
  115.  
  116. /** An awt dialog for interacting with the user. This is like
  117.  * the java.awt.Dialog class, except that it doesn't require a
  118.  * parent Frame.
  119.  */
  120. class AddLayerDialog extends Frame {
  121.     public AddLayerDialog(String title, Document document) {
  122.         super(title);
  123.         this.document = document;
  124.         Panel buttons = new Panel();
  125.         buttons.add("East", ok = new Button("OK"));
  126.         buttons.add("West", cancel = new Button("Cancel"));
  127.         // add("Center", text = new TextArea());
  128.         add("South", buttons);
  129.      }
  130.     /** Handle window close event.
  131.     */
  132.     public boolean handleEvent(Event event) {
  133.         if (event.id == Event.WINDOW_DESTROY) {
  134.             hide();
  135.             signalExit();
  136.             return true;
  137.         } else {
  138.             return super.handleEvent(event);
  139.         }
  140.     }
  141.     public boolean action(Event evt, Object what){
  142.         if ( evt.target == ok || evt.target == cancel) {
  143.             success = evt.target == ok;
  144.             hide();
  145.             signalExit();
  146.             return true;
  147.         }
  148.         return false;
  149.     }
  150.     synchronized public boolean waitForExit() {
  151.         while ( ! bExited ) {
  152.             try {
  153.                 wait();
  154.             } catch ( InterruptedException e){
  155.             }
  156.         }
  157.         return success;
  158.     }
  159.     synchronized public void signalExit() {
  160.         bExited = true;
  161.         notifyAll();
  162.     }
  163.     private Button ok;
  164.     private Button cancel;
  165.     private boolean bExited = false;
  166.     private boolean success = false;
  167.     private Document document;
  168. }
  169.