/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2017 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */
package webviewer;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;

import javax.swing.Action;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

import ilog.views.util.IlvProductUtil;
import webviewer.docview.WebSitePanelView;
import webviewer.ui.ToolBarExtensions;
import webviewer.ui.WebPageView;

/**
 * The <code>WebViewer</code> application illustrates how to create a <em>Web
 * Viewer</em> using Rogue Wave JViews Diagrammer.
 */
public class WebViewer extends JFrame {
  final static String WEB_SITE_DOCUMENT_TEMPLATE_NAME = "WebSite";

  private WebSitePanelView websitePanelView;

  private ToolBarExtensions toolbarExtensions;

  // The frame of the application
  private static JFrame frame;

  // the help viewer
  private WebPageView helpView;

  /**
   * The width of the view in the diagrammer.
   */
  public static final int VIEW_WIDTH = 400;

  /**
   * The height of the view in the diagrammer.
   */
  public static final int VIEW_HEIGHT = 600;

  private static final Font LABEL_FONT = new Font("Arial", Font.PLAIN, 12);

  /**
   * Create the main frame.
   *
   * @throws Exception
   *           if an error occurs for any reason.
   */
  public void init(Container contentPane) throws Exception {
    contentPane.setLayout(new BorderLayout());

    JPanel helpPanel = createHelpPanel();
    JPanel mainPanel = createMainPanel();

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, helpPanel, mainPanel);
    splitPane.setDividerSize(10);
    splitPane.setDividerLocation(0.2);
    splitPane.setOneTouchExpandable(true);

    contentPane.add(splitPane, BorderLayout.CENTER);

    // Add a toolbar for controlling the application at the north of the frame.
    JToolBar toolbar = websitePanelView.createToolBar();
    toolbar.setFloatable(false);
    // extend the toolbar
    toolbarExtensions = new ToolBarExtensions(this, toolbar);

    contentPane.add(toolbar, BorderLayout.NORTH);

    updateActions();

    contentPane.validate();
    contentPane.repaint();
  }

  /**
   * Creates the main diagram panel.
   */
  private JPanel createMainPanel() {
    websitePanelView = new WebSitePanelView(this);
    return websitePanelView;
  }

  /**
   * Returns the website panel view.
   */
  public WebSitePanelView getWebSitePanelView() {
    return websitePanelView;
  }

  /**
   * Returns the toolbar extensions.
   */
  public ToolBarExtensions getToolbarExtensions() {
    return toolbarExtensions;
  }

  /**
   * Creates the help panel.
   */
  private JPanel createHelpPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(createTitlePanel("Help"), BorderLayout.NORTH);
    JEditorPane editorPane = new JEditorPane();
    editorPane.setMargin(new Insets(0, 0, 0, 0));
    editorPane.setEditable(false);
    editorPane.setOpaque(false);
    URL helpUrl = getClass().getResource("ui/html/help.html");
    try {
      editorPane.setPage(helpUrl);
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    JScrollPane scrollPane = new JScrollPane(editorPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    panel.add(scrollPane, BorderLayout.CENTER);
    panel.setPreferredSize(new Dimension(200, 200));
    return panel;
  }

  /**
   * Creates a panel for the title.
   */
  private static JPanel createTitlePanel(String title) {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setBorder(new EmptyBorder(4, 4, 4, 4));
    panel.add(createLabel(title), BorderLayout.WEST);
    return panel;
  }

  /**
   * Creates a <code>JLabel</code> for the given text and sets the
   * <code>LABEL_FONT</code> on it.
   */
  private static JLabel createLabel(String txt) {
    JLabel label = new JLabel(txt);
    label.setFont(LABEL_FONT);
    return label;
  }

  /**
   * Handle the help command.
   */
  protected void handleHelp(ActionEvent ae) {
    if (helpView == null)
      helpView = new WebPageView(this);

    URL u = null;
    try {
      u = this.getClass().getResource("/webviewer/ui/html/help.html");
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    helpView.open(u);
  }

  /**
   * Refreshes the presentation of all the actions represented in the toolbar.
   */
  public void updateActions() {
    websitePanelView.updateActions();
  }

  /**
   * Refreshes the presentation of the action represented by the given command.
   */
  public void updateAction(String actionCommand) {
    websitePanelView.updateAction(actionCommand);
  }

  /**
   * Refreshes the presentation of the given action.
   */
  public void updateAction(Action action) {
    websitePanelView.updateAction(action);
  }

  /**
   * Returns the main frame of the application.
   */
  public JFrame getMainFrame() {
    return frame;
  }

  // -------------------------------------------------------------

  /**
   * The main method of the application.
   *
   * @param args
   *          the command line arguments for the application (these arguments
   *          are ignored by this application).
   */
  public static void main(final String[] args) {
    // This sample uses JViews Diagrammer features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Rogue Wave JViews Diagrammer Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(IlvProductUtil.JViews_Diagrammer_Deployment);

    SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        // Create an instance of the application, and show it.
        try {
          frame = new JFrame("Web Viewer");
          WebViewer app = new WebViewer();
          app.init(frame.getContentPane());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocation(50, 50);
          frame.setSize(800, 800);
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace(System.err);
        }
      }
    });
  }
}