home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / text / html / HTMLEditorKit.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  6.1 KB  |  137 lines

  1. package com.sun.java.swing.text.html;
  2.  
  3. import com.sun.java.swing.Action;
  4. import com.sun.java.swing.JEditorPane;
  5. import com.sun.java.swing.text.BadLocationException;
  6. import com.sun.java.swing.text.Document;
  7. import com.sun.java.swing.text.Element;
  8. import com.sun.java.swing.text.JTextComponent;
  9. import com.sun.java.swing.text.StyleContext;
  10. import com.sun.java.swing.text.StyledDocument;
  11. import com.sun.java.swing.text.StyledEditorKit;
  12. import com.sun.java.swing.text.TextAction;
  13. import com.sun.java.swing.text.ViewFactory;
  14. import java.awt.Color;
  15. import java.awt.Component;
  16. import java.awt.event.MouseListener;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.Reader;
  20. import java.io.Writer;
  21. import java.util.Vector;
  22.  
  23. public class HTMLEditorKit extends StyledEditorKit {
  24.    public static final String DEFAULT_CSS = "default.css";
  25.    private StyleContext styleContext = new StyleContext();
  26.    private MouseListener linkHandler = new LinkController();
  27.    public static final String BOLD_ACTION = "html-bold-action";
  28.    public static final String ITALIC_ACTION = "html-italic-action";
  29.    public static final String PARA_INDENT_LEFT = "html-para-indent-left";
  30.    public static final String PARA_INDENT_RIGHT = "html-para-indent-right";
  31.    public static final String FONT_CHANGE_BIGGER = "html-font-bigger";
  32.    public static final String FONT_CHANGE_SMALLER = "html-font-smaller";
  33.    public static final String COLOR_ACTION = "html-color-action";
  34.    public static final String LOGICAL_STYLE_ACTION = "html-logical-style-action";
  35.    public static final String IMG_ALIGN_TOP = "html-image-align-top";
  36.    public static final String IMG_ALIGN_MIDDLE = "html-image-align-middle";
  37.    public static final String IMG_ALIGN_BOTTOM = "html-image-align-bottom";
  38.    public static final String IMG_BORDER = "html-image-border";
  39.    private static final Action[] defaultActions;
  40.    static final boolean DEBUG = false;
  41.  
  42.    public HTMLEditorKit() {
  43.       this.loadStyleSheet(this.styleContext);
  44.    }
  45.  
  46.    public Object clone() {
  47.       return new HTMLEditorKit();
  48.    }
  49.  
  50.    public String getContentType() {
  51.       return "text/html";
  52.    }
  53.  
  54.    public ViewFactory getViewFactory() {
  55.       return new HTMLFactory();
  56.    }
  57.  
  58.    public Document createDefaultDocument() {
  59.       HTMLDocument var1 = new HTMLDocument(this.styleContext);
  60.       return var1;
  61.    }
  62.  
  63.    public void read(Reader var1, Document var2, int var3) throws IOException, BadLocationException {
  64.       if (var2 instanceof HTMLDocument) {
  65.          HTMLDocument var4 = (HTMLDocument)var2;
  66.          var4.read(var1, var3);
  67.       } else {
  68.          super.read(var1, var2, var3);
  69.       }
  70.    }
  71.  
  72.    public void write(Writer var1, Document var2, int var3, int var4) throws IOException, BadLocationException {
  73.       if (var2 instanceof StyledDocument) {
  74.          try {
  75.             HTMLWriter var5 = new HTMLWriter();
  76.             var5.write(var1, (StyledDocument)var2);
  77.          } catch (HTMLException var6) {
  78.             throw new IOException("HTMLException: " + ((Throwable)var6).getMessage());
  79.          }
  80.       } else {
  81.          super.write(var1, var2, var3, var4);
  82.       }
  83.    }
  84.  
  85.    public void install(JEditorPane var1) {
  86.       ((Component)var1).addMouseListener(this.linkHandler);
  87.       super.install(var1);
  88.    }
  89.  
  90.    public void deinstall(JEditorPane var1) {
  91.       ((Component)var1).removeMouseListener(this.linkHandler);
  92.       super.deinstall(var1);
  93.    }
  94.  
  95.    private void loadStyleSheet(StyleContext var1) {
  96.       InputStream var2 = this.getClass().getResourceAsStream("default.css");
  97.       if (var2 == null) {
  98.          System.out.println("HTMLEditorKit.loadStyleSheet: default.css file not found");
  99.       } else {
  100.          StyleReader var3 = new StyleReader(var1);
  101.          var3.read(var1, 0, var2);
  102.       }
  103.    }
  104.  
  105.    public Action[] getActions() {
  106.       return TextAction.augmentList(super.getActions(), defaultActions);
  107.    }
  108.  
  109.    static Vector getCharacterElements(JEditorPane var0, int var1, int var2) {
  110.       StyledDocument var3 = (StyledDocument)((JTextComponent)var0).getDocument();
  111.       Vector var4 = new Vector();
  112.       int var7 = var1 > var2 ? var2 : var1;
  113.       var2 = var7 > var2 ? var1 : var2;
  114.       int var9 = var7;
  115.  
  116.       while(var9 <= var2) {
  117.          Element var6 = var3.getCharacterElement(var9);
  118.          if (var6 == null) {
  119.             ++var9;
  120.          } else {
  121.             var4.addElement(var6);
  122.             if (var9 < var6.getEndOffset() + 1) {
  123.                var9 = var6.getEndOffset() + 1;
  124.             } else {
  125.                ++var9;
  126.             }
  127.          }
  128.       }
  129.  
  130.       return var4;
  131.    }
  132.  
  133.    static {
  134.       defaultActions = new Action[]{new HTMLBoldAction(), new HTMLItalicAction(), new FontSizeChangeAction("html-font-bigger", true), new FontSizeChangeAction("html-font-smaller", false), new ParagraphIndentAction("html-para-indent-left", true), new ParagraphIndentAction("html-para-indent-right", false), new ImgAlignAction("html-image-align-top", "top"), new ImgAlignAction("html-image-align-middle", "middle"), new ImgAlignAction("html-image-align-bottom", "bottom"), new ImgBorderAction("html-image-border"), new StyledEditorKit.ForegroundAction("html-color-action", Color.black), new LogicalStyleAction("Normal")};
  135.    }
  136. }
  137.