EditLive! 9 Documentation : Getting the Document in the Swing SDK Code
Created by Jessica Hardy, last modified by Kristin Repsher on May 22, 2012
/*
* Copyright (c) 2005 Ephox Corporation.
*/
import java.io.*;
import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.ephox.editlive.*;
import javax.jnlp.*;
/** This tutorial shows developers how to extract the HTML
* Document stored in EditLive! for Java Swing
*
*/
public class GetDocument extends JFrame implements ActionListener {
/** Buttons used to get html contents of EditLive! and copy to JTextArea source */
private JButton documentButton = new JButton("Get HTML Document");
/** Allows users to enter html content to insert into EditLive!, or holds a copy of the html contents of EditLive! */
private JTextArea source = new JTextArea(10, 30);
/** html content to appear in the instance of EditLive! */
private static final String INITIAL_HTML = "<html><head><title>Default Document Title</title></head><body><p>Original <i>HTML Document</i> loaded into EditLive!</p></body></html>";
/** Base class for EditLive! */
private ELJBean editLiveBean = new ELJBean(INITIAL_HTML, "", 700, 300, getClass().getResource("sample_eljconfig.xml"), false);
/** Creates JFrame and adds all class properties. Adds action listener to JButtons in JFrame
*
*/
public GetDocument() throws Exception {
super("Tutorial - Getting the HTML of the EditLive! Document");
this.getContentPane().setLayout(new GridLayout(2, 1));
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
// initialize EditLive!
editLiveBean.init();
}
});
// Create a JPanel to hold the ELJBean
JPanel editorHolder = new JPanel(new FlowLayout());
editorHolder.add(editLiveBean);
// Add the JPanel to the JFrame
this.getContentPane().add(editorHolder);
// Create a JPanel to hold the button and the text area
JPanel buttonAndText = new JPanel(new BorderLayout());
// create a JPanel to hold the button
JPanel buttonHolder = new JPanel(new FlowLayout());
buttonHolder.add(documentButton);
// add listener to button
documentButton.addActionListener(this);
// add button holding panel
buttonAndText.add(buttonHolder, BorderLayout.NORTH);
// specify textarea content
source.setText("Pressing the above button will copy the contents of the HTML Document in EditLive! into this textarea.");
// create scrollable pane to hold text area
JScrollPane textAreaHolder = new JScrollPane(source);
buttonAndText.add(textAreaHolder);
// add button and textarea to frame
this.getContentPane().add(buttonAndText, BorderLayout.CENTER);
// Display the JFrame.
this.setSize(new Dimension(710, 640));
this.setVisible(true);
// Adding a listener to detect if the JFrame is closing, to close the application if needed.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/** ActionListener for JButtons on the JFrame
*
* @param e ActionEvent sent by JButton
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == documentButton) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
source.setText(editLiveBean.getDocument());
}
});
} catch (Exception exception) {
// TODO Auto-generated catch block
exception.printStackTrace();
}
}
}
/** Sets up the application and begins its execution
*
* @param args the command line arguments - ignored
*/
public static void main(String args[]) throws Exception {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Unable to locate UIManager class");
e.printStackTrace();
}
new GetDocument();
}
}