Java Tutorial/Intermediate GUIs/A wiki editor
A wiki editor
editThis wiki editor is a component based on JPanel that embeds two JEditorPane components, one configured as non-editable display for an HTMLDocument and one as an editor for a PlainDocument, which can be used to edit wiki syntax. A HyperlinkListener is used to track the selection of links in the resulting HTML document. A parser that has yet to be written is meant to translate the wiki syntax to HTML or XML.
Exercises
edit- Write an ANTLR parser that translates the wiki syntax into an HTML document.
- Use an intermediate XML format as the output of the parser and use XSLT to transform it into an HTML document.
- Add [Edit]-hyperlinks to the HTML document that allow to edit only a single section of the wiki document.
- Improve the parser to allow more Wikipedia syntax.
Source code
editpackage org.wikiversity.java_tutorial;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.PlainDocument;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
public class WikiEditor extends JPanel implements ActionListener
{
private JPanel documentPanel = new JPanel ();
private CardLayout cardLayout = new CardLayout ();
private JEditorPane wiki = new JEditorPane ();
private JEditorPane editor = new JEditorPane ();
private HTMLDocument htmlDocument;
private PlainDocument wikiDocument;
private final static String WIKI = "Wiki";
private final static String EDITOR = "Editor";
private JButton wikiButton = new JButton ("Wiki"),
editorButton = new JButton ("Editor");
class HyperlinkHandler implements HyperlinkListener
{
public void hyperlinkUpdate (HyperlinkEvent e)
{
if (e.getEventType () == HyperlinkEvent.EventType.ACTIVATED)
{
try {
JEditorPane pane = (JEditorPane) e.getSource ();
if (e instanceof HTMLFrameHyperlinkEvent)
{
HTMLFrameHyperlinkEvent hfe = (HTMLFrameHyperlinkEvent) e;
HTMLDocument doc = (HTMLDocument) pane.getDocument ();
doc.processHTMLFrameHyperlinkEvent (hfe);
}
else
{
pane.setPage (e.getURL ());
}
}
catch (Throwable t) {
displayError (t);
}
}
}
}
public WikiEditor ()
{
setLayout (new BorderLayout ());
JPanel panel = new JPanel ();
panel.setLayout (new FlowLayout ());
wikiButton.addActionListener (this);
panel.add (wikiButton);
editorButton.addActionListener (this);
panel.add (editorButton);
add (panel, BorderLayout.NORTH);
documentPanel = new JPanel ();
add (documentPanel, BorderLayout.CENTER);
documentPanel.setLayout (cardLayout);
wiki.setContentType ("text/plain");
wiki.setText (WIKI);
wiki.setEditable (false);
wikiDocument = (PlainDocument) wiki.getDocument ();
documentPanel.add (wiki, WIKI);
editor.setContentType ("text/html");
editor.setText (EDITOR);
editor.setEditable (true);
htmlDocument = (HTMLDocument) editor.getDocument ();
documentPanel.add (editor, EDITOR);
}
public void actionPerformed (ActionEvent e)
{
Object src = e.getSource ();
if (src == wikiButton) {
showWiki ();
}
else if (src == editorButton) {
showEditor ();
}
}
private void showWiki ()
{
cardLayout.show (documentPanel, WIKI);
}
private void showEditor ()
{
cardLayout.show (documentPanel, EDITOR);
}
private void displayError (Throwable t)
{
t.printStackTrace ();
}
public final static void main (String[] args)
{
new WikiEditor ().run (args);
}
public void run (String[] args)
{
JFrame frame = new JFrame ("WikiEditor");
frame.add (this);
frame.setSize (800, 600);
frame.setVisible (true);
}
}