[Back]
/* This tutorial creates a simple plug-in that shows */ /* plug-in construction, which you can compile and run */ /* if you like. This plug-in includes only a minimum */ /* set of features and performs one simple function */ /* (so that you can see that the plug-in works in */ /* the Composer). RedLetter turns selected text red. */ /* Note the similarities to the Colorize sample, on */ /* which it is based.
/* After you complete this tutorial, you'll be familiar with: */ /* * Importing packages and classes */ /* * Defining a plug-in class */ /* * Overriding methods */ /* * Using the Composer Plug-in token interface to handle text */ /* * Adding additional methods */ /* * Using Tag class methods to construct an HTML tag */
/* To return to Chapter 2, click . */
/* 1. Import the packages and classes to provide plug-in */ /* functionality. The plug-in requires Java color classes. */ /* Using * ensures that all needed classes are included. */ import java.io.*; import netscape.plugin.composer.*; import netscape.plugin.composer.io.*; import java.awt.*; /* 2. Define the plug-in class. */ public class RedLetter extends Plugin{
/* 3. Optionally, add a main method to your perform method */ /* so that you can run the plug-in from the command line */ /* and test it in the Composer Testbed. The plug-in does */ /* not need this for normal operation. */ /* For the run command, see "Using the Testbed." */ static public void main(String[] args) { netscape.test.plugin.composer.Test.perform(args, new RedLetter()); }
/* 4. Override basic information methods: */ /* getName (user interface selection name for Tool menu), */ /* getCategory (plug-in category for Tool menu), */ /* getHint (one-line description of what plug-in does) */ public String getName(){ return "Red Letter"; } public String getCategory(){ return "Character Tools"; } public String getHint(){ return "Turns text red"; }
/* 5. Override the perform method, which does the */ /* work of the plug-in. The document parameter is */ /* the document being worked on. */ /* perform returns a boolean: True, plug-in succeeds (OK). */ /* The method must find the selection and apply the color, */ /* get output stream to hold document text, */ /* tokenize (create lexical stream), */ /* get tokens and strip color tag, create new tag */ /* to set color, and apply color to the string. */ public boolean perform(Document document) throws IOException{ PrintWriter out = new PrintWriter(document.getOutput()); LexicalStream in = new LexicalStream(new SelectedHTMLReader( document.getInput(), out)); for(;;){ Token token = in.next(); if (token == null) break; else if (token instanceof Text) { Text text = (Text) token; turnred(text.getText(), out); continue; } else if (token instanceof Tag) { Tag tag = (Tag) token; if (tag.getName().equals("FONT")) tag.removeAttribute("COLOR"); } out.print(token); } out.close(); return true; )
/* 6. Add additional methods as needed. */ /* The turnred method creates a new color tag */ /* for the selected text. */ private void turnred(String string, PrintWriter out){ int len = string.length(); Tag colorTag = new Tag("Font"); Tag colorEnd = new Tag("Font",false); for(int i = 0; i < len; i ++){ char c = string.charAt(i); int rgb = 0xff0000; /* RRxGGxBB */ colorTag.addAttribute("color", formatColor(rgb)); out.print(colorTag); out.print(c); out.print(colorEnd); } } private String formatColor(int rgb){ return "#" + Integer.toHexString((rgb >> 20) & 0xf) + Integer.toHexString((rgb >> 16) & 0xf) + Integer.toHexString((rgb >> 12) & 0xf) + Integer.toHexString((rgb >> 8) & 0xf) + Integer.toHexString((rgb >> 4) & 0xf) + Integer.toHexString(rgb & 0xf); } }
/* You've constructed a plug-in class that has the */ /* minimum set of features needed to run, and you've */ /* seen some basic text-handling operations in action. */ /* You've performed these tasks: */ /* * Importing packages and classes your plug-in requires */ /* * Defining a plug-in class. */ /* * Including a main method so that you can run it */ /* in the Testbed. */ /* * Overriding the Plugin class methods that provide basic */ /* information and functionality and calls other methods. */ /* * Using the Token interface to handle text. */ /* * Adding a method that your plug-in's perform method calls. */ /* * Constructing an HTML tag. */
/* If you want to test this plug-in in Composer, */ /* at a minimum you'll need to compile, install, */ /* test-run and possibly debug. Packaging the plug-in is */ /* also an option, but you don't need to package the */ /* plug-in in order to test it. "Development Overview" */ /* covers each of these operations. */
Copyright © 1997 Netscape Communications Corporation