EditLive! 9 Documentation : Setting the Document in the Swing SDK Code

/*
 * 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 populate the HTML
 * Document stored in EditLive!, at both load-time and run-time.
 *
 */
public class SetDocument extends JFrame implements ActionListener {
       /**        Buttons used to get html contents of EditLive! and copy to JTextArea source */
       private JButton documentButton = new JButton("Set 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 SetDocument() throws Exception {
               super("Tutorial - Setting 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("<html><head><title>New Document Title</title></head><body><p>A New HTML Document to load into <b>EditLive!</b></p></body></html>");
 
               // 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() {
                                               editLiveBean.setDocument(source.getText());
                                       }
                               });
                       } 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 SetDocument();
       }
}