/*
 Copyright (c) 2006, 2007, The Cytoscape Consortium (www.cytoscape.org)

 The Cytoscape Consortium is:
 - Institute for Systems Biology
 - University of California San Diego
 - Memorial Sloan-Kettering Cancer Center
 - Institut Pasteur
 - Agilent Technologies

 This library is free software; you can redistribute it and/or modify it
 under the terms of the GNU Lesser General Public License as published
 by the Free Software Foundation; either version 2.1 of the License, or
 any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
 documentation provided hereunder is on an "as is" basis, and the
 Institute for Systems Biology and the Whitehead Institute
 have no obligations to provide maintenance, support,
 updates, enhancements or modifications.  In no event shall the
 Institute for Systems Biology and the Whitehead Institute
 be liable to any party for direct, indirect, special,
 incidental or consequential damages, including lost profits, arising
 out of the use of this software and its documentation, even if the
 Institute for Systems Biology and the Whitehead Institute
 have been advised of the possibility of such damage.  See
 the GNU Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation,
 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package cytoscape.util.swing;

import cytoscape.Cytoscape;

import cytoscape.logger.CyLogger;
import cytoscape.util.OpenBrowser;

import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;


/**
 * About page for web service clients or other plugins.
 * Accepts HTML as the argument.
 *
 * @author  kono
 */
public class AboutDialog extends javax.swing.JDialog implements HyperlinkListener {
	// Singleton.
	private static final AboutDialog about;
	private static CyLogger logger = CyLogger.getLogger(AboutDialog.class);

	static {
		about = new AboutDialog(Cytoscape.getDesktop(), true);
	}

	// Show method with full parameter set.
	/**
	 *  DOCUMENT ME!
	 *
	 * @param title DOCUMENT ME!
	 * @param icon DOCUMENT ME!
	 * @param description DOCUMENT ME!
	 */
	public static void showDialog(String title, Icon icon, String description) {
		about.titleLabel.setText(title);
		about.titleLabel.setIcon(icon);

		URL target = null;
        about.mainEditorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
                    Boolean.TRUE);
        try {
			target = new URL(description);
		} catch (MalformedURLException e) {
            about.mainEditorPane.setContentType("text/html");
			about.mainEditorPane.setText(description);
			about.repaint();
			about.setVisible(true);
			return;
		}

		try {
			about.mainEditorPane.setPage(target);
			about.pack();
			about.repaint();
			about.setVisible(true);
		} catch (IOException e) {
			about.mainEditorPane.setText("Could not connect to " + target.toString());
			logger.warn("Could not connect to " + target.toString(), e);
			about.pack();
			about.repaint();
			about.setVisible(true);
		}
	}

	/** Creates new form WSAboutDialog */
	public AboutDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
		mainEditorPane.setEditable(false);
		mainEditorPane.addHyperlinkListener(this);
		setLocationRelativeTo(Cytoscape.getDesktop());
		setAlwaysOnTop(true);
	}

	/**
	 * Creates a new AboutDialog object.
	 *
	 * @param parent  DOCUMENT ME!
	 * @param modal  DOCUMENT ME!
	 * @param title  DOCUMENT ME!
	 * @param icon  DOCUMENT ME!
	 * @param contentURL  DOCUMENT ME!
	 */
	public AboutDialog(java.awt.Frame parent, boolean modal, String title, Icon icon, URL contentURL) {
		super(parent, modal);
		initComponents();
		mainEditorPane.setContentType("text/html");
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */

	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		titlePanel = new javax.swing.JPanel();
		titleLabel = new javax.swing.JLabel();
		mainPanel = new javax.swing.JPanel();
		mainScrollPane = new javax.swing.JScrollPane();
		mainEditorPane = new javax.swing.JEditorPane();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("About");
		setAlwaysOnTop(true);

		titlePanel.setBackground(new java.awt.Color(255, 255, 255));

		titleLabel.setFont(new java.awt.Font("SansSerif", 0, 18));
		titleLabel.setText("Client Name Here");

		org.jdesktop.layout.GroupLayout titlePanelLayout = new org.jdesktop.layout.GroupLayout(titlePanel);
		titlePanel.setLayout(titlePanelLayout);
		titlePanelLayout.setHorizontalGroup(titlePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                                                    .add(titlePanelLayout.createSequentialGroup()
		                                                                         .addContainerGap()
		                                                                         .add(titleLabel,
		                                                                              org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                                                              339,
		                                                                              Short.MAX_VALUE)
		                                                                         .addContainerGap()));
		titlePanelLayout.setVerticalGroup(titlePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                                                  .add(titlePanelLayout.createSequentialGroup()
		                                                                       .addContainerGap()
		                                                                       .add(titleLabel,
		                                                                            org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                                                            32,
		                                                                            Short.MAX_VALUE)
		                                                                       .addContainerGap()));

		mainPanel.setBackground(new java.awt.Color(255, 255, 255));

		mainScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		mainScrollPane.setFont(new java.awt.Font("SansSerif", 0, 12));

		mainEditorPane.setEditable(false);
		mainScrollPane.setViewportView(mainEditorPane);

		org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
		mainPanel.setLayout(mainPanelLayout);
		mainPanelLayout.setHorizontalGroup(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                                                  .add(mainPanelLayout.createSequentialGroup()
		                                                                      .addContainerGap()
		                                                                      .add(mainScrollPane)
		                                                                      .addContainerGap()));
		mainPanelLayout.setVerticalGroup(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                                                .add(mainPanelLayout.createSequentialGroup()
		                                                                    .addContainerGap()
		                                                                    .add(mainScrollPane,
		                                                                         org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                                                         215,
		                                                                         Short.MAX_VALUE)
		                                                                    .addContainerGap()));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                                .add(titlePanel,
		                                     org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                     org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                     Short.MAX_VALUE)
		                                .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanel,
		                                     org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                     org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                     Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
		                              .add(layout.createSequentialGroup()
		                                         .add(titlePanel,
		                                              org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
		                                              org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                              org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
		                                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
		                                         .add(mainPanel,
		                                              org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                              org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
		                                              Short.MAX_VALUE)));

		pack();
	} // </editor-fold>

	// Variables declaration - do not modify
	private javax.swing.JEditorPane mainEditorPane;
	private javax.swing.JScrollPane mainScrollPane;
	private javax.swing.JLabel titleLabel;
	private javax.swing.JPanel titlePanel;
	private javax.swing.JPanel mainPanel;

	/**
	 *  DOCUMENT ME!
	 *
	 * @param e DOCUMENT ME!
	 */
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
			return;

		String url = e.getURL().toString();

		try {
			OpenBrowser.openURL(url);
		} catch (Exception err) {
			logger.warn("Unable to open browser for "+url.toString(), err);
		}
	}

	// End of variables declaration
}
