/*
 * Copyright (c) 2005 Ephox Corporation.
 */
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
import com.ephox.editlive.*;
import javax.jnlp.*;
 
/**  This tutorial shows developers how to specify the encoding for instances of EditLive! for Java Swing
 *        and how to use several instances of EditLive! for Java Swing in the one JFrame.
 */
public class CharEncoding extends JFrame implements ActionListener {
       /**        Buttons used to get html contents of EditLive! and copy to JTextArea source */
       private JButton body1Button = new JButton("Get <BODY> for the above instance of EditLive!");
       private JButton body2Button = new JButton("Get <BODY> for the above instance of EditLive!");
 
       /** 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_HTML1 = "<p>Original <i>HTML</i> loaded into EditLive! Instance 1</p><p>Text with &#8220;Smart Quotes&#8221;</p>";
       private static final String INITIAL_HTML2 = "<p>Original <i>HTML</i> loaded into EditLive! Instance 2</p><p>Text with &#8220;Smart Quotes&#8221;</p>";
 
 
       /** Base class for EditLive! */
       private ELJBean editLiveBean1 = new ELJBean(INITIAL_HTML1, "", 600, 300, getClass().getResource("charEncoding.xml"), false);
       private ELJBean editLiveBean2 = new ELJBean(INITIAL_HTML2, "", 600, 300, getClass().getResource("charEncoding.xml"), false);
 
       /** Creates JFrame and adds all class properties. Adds action listener to JButtons in JFrame
        *
        */
       public CharEncoding() throws Exception {
               super("Tutorial - Specifying the Character Encoding");
 
               SwingUtilities.invokeAndWait(new Runnable() {
                       public void run() {
                               // initialize EditLive! instances
                               editLiveBean1.init();
                               editLiveBean2.init();                                                                
                               
                               // Setting the output character set to use 'ASCII' encoding.
                               // If the setOutoutCharset property is not specified, the default encoding
                               // for character output is 'UTF-8'.                                
                               editLiveBean2.setOutputCharacterSet("ASCII");                                
                       }
               });                                
 
               // Setting overall layout for frame
               this.getContentPane().setLayout(new GridLayout(2, 1));
 
               // Creating panel for two EditLive! instances.
               JPanel multiEditorHolder = new JPanel(new GridLayout(1, 2));
 
               // Create a JPanel to hold the ELJBean
               JPanel editorHolder1 = new JPanel(new FlowLayout());
               editorHolder1.add(editLiveBean1);
               // Add the JPanel to the JFrame
               multiEditorHolder.add(editorHolder1);
 
               // Create a JPanel to hold the ELJBean
               JPanel editorHolder2 = new JPanel(new FlowLayout());
               editorHolder2.add(editLiveBean2);
               // Add the JPanel to the JFrame
               multiEditorHolder.add(editorHolder2);
 
               this.getContentPane().add(multiEditorHolder);
 
               // Create a JPanel to hold the buttons and the text area
               JPanel buttonsAndText = new JPanel(new BorderLayout());
 
               // Creating a panel for the two buttons
               JPanel buttonsPanel = new JPanel(new GridLayout(1, 2));
               buttonsPanel.add(body1Button);
               // add listener to button
               body1Button.addActionListener(this);
               buttonsPanel.add(body2Button);
               // add listener to button
               body2Button.addActionListener(this);
 
               // add button holding panel
               buttonsAndText.add(buttonsPanel, BorderLayout.NORTH);
 
               // specify textarea content
               source.setText("Pressing the above button will copy the contents of the <BODY> attribute in EditLive! into this textarea.");
 
               // create scrollable pane to hold text area
               JScrollPane textAreaHolder = new JScrollPane(source);
               buttonsAndText.add(textAreaHolder);
 
               // add button and textarea to frame
               this.getContentPane().add(buttonsAndText, BorderLayout.CENTER);
 
               // Display the JFrame.
               this.setSize(new Dimension(1210, 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(final ActionEvent e) {
               try {
                       SwingUtilities.invokeAndWait(new Runnable() {
                               public void run() {
                                       if (e.getSource() == body1Button) {
                                               source.setText(editLiveBean1.getBody());
                                       }
                                       if (e.getSource() == body2Button) {
                                               source.setText(editLiveBean2.getBody());
                                       }
                               }
                       });                                        
               } catch (Exception exception) {
                       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 CharEncoding();
       }
}