/*
 * Copyright (c) 2005 Ephox Corporation.
 */
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
import com.ephox.editlive.*;
import com.ephox.editlive.common.TextEvent;
import com.ephox.editlive.common.EventListener;
import javax.jnlp.*;
 
/** Class loads a JFrame with a single panel, containing the
 * Ephox EditLive! Editor
 */
public class CustomToolbarItem extends JFrame implements EventListener {
       /** html content to appear in the instance of EditLive! for Java Swing */
       private static final String INITIAL_HTML = "<html><head><title>Initial Title</title></head><body><p></p></body></html>";
 
       /** Base class for EditLive! for Java Swing */
       private ELJBean editLiveBean = new ELJBean(INITIAL_HTML, "", 700, 570, getClass().getResource("customItem.xml"), false);
 
       /** Creates JFrame and adds all class properties. Adds action listener to JButtons in JFrame
        *
        */
       public CustomToolbarItem() throws Exception {
               super("Tutorial - Custom Toolbar Items");
 
               SwingUtilities.invokeAndWait(new Runnable() {
                       public void run() {
                               // initialize EditLive! and add this class as an event listner
                               editLiveBean.init();
                               editLiveBean.addEditorEventListener(CustomToolbarItem.this);                                
                       }
               });                
 
               this.getContentPane().setLayout(new FlowLayout());
 
               // 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);
 
               // Display the JFrame.
               this.setSize(new Dimension(710, 620));
               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);
                       }
               });
 
       }
 
    /** raiseEvent method inherited from EventListener. Every action in EditLive!
     * raises a TextEvent.
     *
     * @param e TextEvent information sent on action.
     */
    public void raiseEvent(TextEvent e) {
               if(e.getActionCommand() == TextEvent.CUSTOM_ACTION && e.getExtraString().equals("displayDialog")) {
                       ExampleDialog exDialog = new ExampleDialog(editLiveBean);
                       exDialog.setVisible(true);
               }
    }
 
       /** 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 CustomToolbarItem();
       }
}
 
class ExampleDialog extends JFrame implements ActionListener {
       // Base class for EditLive!
       private ELJBean elBean;
 
       private JLabel headingLabel = new JLabel("Example Dialog");
       private JLabel infoLabel = new JLabel("Click the OK button to load the string \"<b>dialog called</b>\" into EditLive! and close this window.");
       private JButton OKButton = new JButton("OK");
 
 
       ExampleDialog(ELJBean _elBean) {
               super("Example Dialog");
               elBean = _elBean;
 
               OKButton.addActionListener(this);
               headingLabel.setFont(new java.awt.Font("Arial", Font.BOLD, 14));
 
               this.getContentPane().setLayout(new GridLayout(3, 1));
               this.getContentPane().add(headingLabel);
               this.getContentPane().add(infoLabel);
 
               JPanel buttonPanel = new JPanel(new FlowLayout());
               buttonPanel.add(OKButton);
 
               this.getContentPane().add(buttonPanel);
 
               // Display the JFrame.
               this.setSize(new Dimension(500, 175));
               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) {
                               dispose();
                       }
               });
 
       }
 
       /** ActionListener for JButtons on the JFrame
        *
        * @param e ActionEvent sent by JButton
        */
       public void actionPerformed(ActionEvent e) {
               if (e.getSource() == OKButton) {
                       try {
                               SwingUtilities.invokeAndWait(new Runnable() {
                                       public void run() {
                                               elBean.insertHTMLAtCursor("<b>dialog called</b>");
 
                                               // close window
                                               dispose();
                                       }
                               });                                                
                       } catch (Exception exception) {
                               exception.printStackTrace();
                       }                        
               }
       }
}