FileDocCategorySizeDatePackage
HTMLEditor.javaAPI DocExample5628Thu Oct 24 20:14:26 BST 2002None

HTMLEditor

public class HTMLEditor extends StyledEditor

Fields Summary
private static final String
COPY_HTML
Constructors Summary
Methods Summary
protected javax.swing.JMenuBarcreateMenuBar()

    JMenuBar menubar = super.createMenuBar();

    JMenu view = new JMenu("View");
    JMenuItem preview = new JMenuItem("Preview");
    view.add(preview);
    preview.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
	JEditorPane editor = (JEditorPane)getTextComponent();
	JEditorPane display = new JEditorPane();
	display.setEditable(false);
        display.setEditorKit(new HTMLEditorKit());
        JScrollPane sp = new JScrollPane(display);
        sp.setPreferredSize(new Dimension(500,500));
	try {
	  //display.setPage(currentFile.toURL());
          display.setDocument(editor.getDocument());
	}
	catch (Exception e) {
	  display.setText("Failed to preview file.\n" +
			  "Make sure file has been saved.");
	}
	JOptionPane.showMessageDialog(null, sp, "Preview",
				      JOptionPane.PLAIN_MESSAGE);
      }
    });
    menubar.add(view);

    return menubar;
  
protected javax.swing.text.JTextComponentcreateTextComponent()

    JEditorPane jep = new JEditorPane();
    jep.setEditorKit(new HTMLEditorKit());
    return jep;
  
protected javax.swing.JToolBarcreateToolBar()

    JToolBar bar = super.createToolBar();
    bar.addSeparator();

    bar.add(getTextComponent().getActionMap().get("add-copyright")).setText("");
    bar.add(getTextComponent().getActionMap().get("InsertHR")).setText("");
    bar.add(getTextComponent().getActionMap().get("InsertIMG")).setText("");
    bar.add(getTextComponent().getActionMap().get("anchor-link")).setText("");
    bar.add(getTextComponent().getActionMap().get("anchor-name")).setText("");
    return bar;
  
public static voidmain(java.lang.String[] args)


       
    HTMLEditor editor = new HTMLEditor();
    editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    editor.setVisible(true);
  
protected voidmakeActionsPretty()

    super.makeActionsPretty();

    Action a;
    // HTML Actions
    a = getTextComponent().getActionMap().get("InsertHR");
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/hr.gif"));
    a.putValue(Action.NAME, "HR");

    // Custom HTML Actions, need to store these in the map...
    // First, an easy use of the InsertHTMLTextAction class
    a = new HTMLEditorKit.InsertHTMLTextAction("InsertCopyright", COPY_HTML,
					       HTML.Tag.BODY, HTML.Tag.P);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/copyright.gif"));
    a.putValue(Action.NAME, "Teletype Text");
    getTextComponent().getActionMap().put("add-copyright", a);

    // And some of our own Tags that require a little more work.
    // See the TagAction inner class below.
    a = new TagAction(HTML.Tag.A, "URL", HTML.Attribute.HREF);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/link.gif"));
    a.putValue(Action.NAME, "Anchor Link");
    getTextComponent().getActionMap().put("anchor-link", a);

    a = new TagAction(HTML.Tag.A, "Name", HTML.Attribute.NAME);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/anchor.gif"));
    a.putValue(Action.NAME, "Anchor Name");
    getTextComponent().getActionMap().put("anchor-name", a);

    a = new ImageAction();
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/picture.gif"));
    a.putValue(Action.NAME, "Image");
    getTextComponent().getActionMap().put("InsertIMG", a);