/*
 * AnalyzerAboutDialog.java
 *
 * Copyright (c) 2003, Oracle Corporation. All rights reserved. Oracle grants
 * you ("Licensee") a non-exclusive, royalty-free, worldwide license to use,
 * modify and redistribute this software in source and binary code form, provided
 * that (i) this copyright notice and license appear on all copies of the
 * software; (ii) Licensee does not utilize the software in a manner disparaging
 * to Oracle; and (iii) Licensee does not utilize the software in a manner that
 * violates the terms of any Licensee agreement with Oracle.
 */
package samples.javaclient.analyzer;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Enumeration;
import java.util.Properties;

import javax.naming.NamingException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import oracle.bali.ewt.olaf.OracleLookAndFeel;

import oracle.dss.util.VersionInfo;
import oracle.dss.util.gui.BIExceptionDialog;

/**
 * About dialog for the Analyzer tool.  It displays build information about
 * the BI Beans packages that are used by the application
 *
 */
public class AnalyzerAboutDialog extends JDialog {
  // List of packages to be displayed in the about dialog.  Modify this array if
  // you would like to add or remove packages.
  public static String[] s_packages = new String[]{
    "oracle.dss.addins",
    "oracle.dss.appmodule",
    "oracle.dss.beanInfo",
    "oracle.dss.bicontext",
    "oracle.dss.calculation",
    "oracle.dss.connection",
    "oracle.dss.crosstab",
    "oracle.dss.customPane",
    "oracle.dss.dataSource",
    "oracle.dss.datautil",
    "oracle.dss.dataView",
    "oracle.dss.export",
    "oracle.dss.graph",
    "oracle.dss.gridView",
    "oracle.dss.metadataManager",
    "oracle.dss.metadataUtil",
    "oracle.dss.persistence",
    "oracle.dss.queryBuilder",
    "oracle.dss.resource",
    "oracle.dss.rules",
    "oracle.dss.security",
    "oracle.dss.selection",
    "oracle.dss.thin",
    "oracle.dss.util",
  };

  /**
   * Constructor for AnalyzerAboutDialog.
   *
   * @param frame The parent frame.
   * @param title The title of the JDialog.
   * @param modal Indicates whether the dialog should be modal.
   */
  public AnalyzerAboutDialog(Frame frame, String title, boolean modal) {
    super(frame, title, modal);
    init();
  }

  /**
   * Initialize and construct contents of the AnalyzerAboutDialog.
   */
  protected void init() {
    // Create the top level panel with the copyright info.
    JPanel _topPanel = new JPanel();
    _topPanel.setBorder(new TitledBorder(""));
    _topPanel.setLayout(new GridLayout(3, 1, 10, 10));
    _topPanel.add(new JLabel("Sample version " + ((Analyzer) getParent()).getVersion()));
    _topPanel.add(new JLabel("Copyright © 2003, Oracle Corporation"));
    _topPanel.add(new JLabel("All rights reserved"));

    // Create the middle panel with the version info.
    JPanel _middlePanel = new JPanel();
    _middlePanel.setBorder(new TitledBorder("Version information"));
    _middlePanel.setLayout(new GridLayout(2, 1, 10, 10));

    // Create a table with the version number for every package
    JTable _table = new JTable(new PackagesModel());
    JScrollPane scrollPane = new JScrollPane(_table);
    _middlePanel.add(scrollPane);

    // Create a table with detailed information for a selected package
    final JTable _table2 = new JTable(new PackageModel(s_packages[0]));
    JScrollPane scrollPane2 = new JScrollPane(_table2);
    _middlePanel.add(scrollPane2);

    // Add a listener to the summary packages, that waits for a user to select
    // a package
    ListSelectionModel _model = _table.getSelectionModel();
    _model.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    _model.addListSelectionListener(new ListSelectionListener(){
      public void valueChanged(ListSelectionEvent e) {
        ListSelectionModel _sourceModel = (ListSelectionModel)e.getSource();
        int _selectedRow = _sourceModel.getMinSelectionIndex();
        _table2.setModel(new PackageModel(s_packages
                                            [_selectedRow!=-1?_selectedRow:0]));
      }
    });

    // Add the bottom level dialog with the OK button that closes the dialog
    JPanel _bottomPanel = new JPanel();
    _bottomPanel.setLayout(new GridLayout(1,4));

    JButton _ok = new JButton("OK");
    _ok.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        AnalyzerAboutDialog.this.setVisible(false);
      }
    });

    _bottomPanel.add(Box.createRigidArea(new Dimension(10, 10)));
    _bottomPanel.add(Box.createRigidArea(new Dimension(10, 10)));
    _bottomPanel.add(Box.createRigidArea(new Dimension(10, 10)));
    _bottomPanel.add(_ok);

    // Create the panel containing the top level, middle and bottom panel.
    JPanel _pane = new JPanel();
    _pane.setLayout(new BoxLayout(_pane, BoxLayout.Y_AXIS));
    _pane.add(_topPanel);
    _pane.add(Box.createRigidArea(new Dimension(10, 10)));
    _pane.add(_middlePanel);
    _pane.add(Box.createRigidArea(new Dimension(10, 10)));
    _pane.add(_bottomPanel);

    JPanel _paneOuter = new JPanel();
    _paneOuter.setLayout(new BoxLayout(_paneOuter, BoxLayout.X_AXIS));
    // Add the panel to the outer panel
    _paneOuter.add(_pane);

    // Add the panel to dialog.
    getContentPane().add(_paneOuter);
  }

  /**
   * Tells if a package is loaded in the VM
   */
  private boolean isPackageLoaded(String packageName) {
    Package _package = Package.getPackage(packageName);
    return _package != null;
  }

 /**
  * A class that provides the table model for the list of packages
  */
  private class PackagesModel extends AbstractTableModel {
    /**
     * Return number of columns (package name, version number and loaded in VM)
     * in the table
     *
     * @return the number of columns in the table
     */
    public int getColumnCount() {
      return 3;
    }

    /**
     * Return the column headers
     *
     * @return the name of the specified column
     */
    public String getColumnName(int col) {
      switch (col) {
        case 0:
          return "Package";
        case 1:
          return "Version number";
        case 2:
          return "Loaded in VM?";
        default:
          return "";
      }
    }

    /**
     * Return number of rows (the packages) in the table
     *
     * @return the number of rows in the table
     */
    public int getRowCount() {
      return s_packages.length;
    }

    /**
     * Return the table contents
     *
     * @return the content at the specified row and column
     */
    public Object getValueAt(int row, int col) {
      switch (col) {
        case 0:
          return s_packages[row];
        case 1:
          String _version = VersionInfo.getProperty(s_packages[row],
                              VersionInfo.BUILD_VERSION);
          return _version;
        case 2:
          return (isPackageLoaded(s_packages[row])?"true":"");
      }
      return "";
    }
  }

 /**
  * A class that provides the table model for a deatiled list of properties
  * for a package
  */
  private class PackageModel extends AbstractTableModel {
    private String m_package;
    private Properties m_properties;
    private String[] m_keys;

    /**
     * Constructor that takes the package name to construct the model for.
     */
    public PackageModel(String _package) {
      m_package = _package;
      m_properties = VersionInfo.getProperties(_package);

      if (m_properties == null)
        return;

      m_keys = new String[m_properties.size()];
      int i = 0;
      for (Enumeration e = m_properties.propertyNames();e.hasMoreElements();) {
        m_keys[i++] = (String)e.nextElement();
      }
    }

    /**
     * Return number of columns (name, value) in the table
     *
     * @return the number of columns in the table
     */
    public int getColumnCount() {
      return 2;
    }

    /**
     * Return the column names (name, value)
     *
     * @return the name of the specified column
     */
    public String getColumnName(int col) {
      switch (col) {
        case 0:
          return "Name";
        case 1:
          return "Value";
        default:
          return "";
      }
    }

    /**
     * Return number of rows (properties) in the table
     *
     * @return the number of rows in the table
     */
    public int getRowCount() {
      return (m_properties != null?m_properties.size():0);
    }

    /**
     * Return the table contents (the name and value for each property)
     *
     * @return the content at the specified row and column
     */
    public Object getValueAt(int row, int col) {
      switch (col) {
        case 0:
          return m_keys[row];
        case 1:
          return m_properties.get(m_keys[row]);
      }
      return "";
    }
  }
}
