package kiyut.ekspos;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/** About Dialog
 *
 * @author  tonny
 */
public class AboutWindow extends javax.swing.JDialog {
    
    /**
     * Creates new form AboutWindow
     */
    public AboutWindow(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initCustomComponents();
        fillText();
        setAlwaysOnTop(true);
    }
    
    /** 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">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        tabbedPane = new javax.swing.JTabbedPane();
        javax.swing.JPanel aboutPane = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        javax.swing.JPanel licensePane = new javax.swing.JPanel();
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        licenseArea = new javax.swing.JTextArea();
        javax.swing.JPanel creditsPane = new javax.swing.JPanel();
        javax.swing.JScrollPane jScrollPane3 = new javax.swing.JScrollPane();
        creditsArea = new javax.swing.JTextArea();
        javax.swing.JPanel christPane = new javax.swing.JPanel();
        javax.swing.JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
        christArea = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        closeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("About");
        setModal(true);
        setName("aboutDialog"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        tabbedPane.setPreferredSize(new java.awt.Dimension(620, 400));

        aboutPane.setLayout(new java.awt.GridBagLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), javax.swing.BorderFactory.createEmptyBorder(20, 20, 20, 20)));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        jLabel3.setBackground(new java.awt.Color(255, 255, 255));
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kiyut/ekspos/about-logo.png"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.ipadx = 8;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        jPanel2.add(jLabel3, gridBagConstraints);

        nameLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        nameLabel.setText("LONG_NAME VERSION");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(nameLabel, gridBagConstraints);

        jLabel4.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel4.setText("http://www.kiyut.com");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        jPanel2.add(jLabel4, gridBagConstraints);

        jLabel2.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        jLabel2.setText("Copyright © 2010 KIYUT");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel2.add(jLabel2, gridBagConstraints);

        aboutPane.add(jPanel2, new java.awt.GridBagConstraints());

        tabbedPane.addTab("About", aboutPane);

        licensePane.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        licensePane.setLayout(new java.awt.BorderLayout());

        licenseArea.setEditable(false);
        licenseArea.setFont(new java.awt.Font("Monospaced", 0, 12));
        licenseArea.setLineWrap(true);
        licenseArea.setWrapStyleWord(true);
        jScrollPane1.setViewportView(licenseArea);

        licensePane.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        tabbedPane.addTab("License", licensePane);

        creditsPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        creditsPane.setLayout(new java.awt.BorderLayout());

        creditsArea.setEditable(false);
        creditsArea.setFont(new java.awt.Font("Monospaced", 0, 12));
        jScrollPane3.setViewportView(creditsArea);

        creditsPane.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        tabbedPane.addTab("Credits", creditsPane);

        christPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        christPane.setLayout(new java.awt.BorderLayout());

        christArea.setEditable(false);
        christArea.setFont(new java.awt.Font("Monospaced", 0, 12));
        christArea.setLineWrap(true);
        christArea.setWrapStyleWord(true);
        jScrollPane2.setViewportView(christArea);

        christPane.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        tabbedPane.addTab("Christ", christPane);

        getContentPane().add(tabbedPane, java.awt.BorderLayout.CENTER);

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeAction(evt);
            }
        });
        jPanel1.add(closeButton);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents
    
    private void closeAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeAction
        // Add your handling code here:
        closeDialog(null);
    }//GEN-LAST:event_closeAction
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    private void initCustomComponents() {
        setTitle("About - " + Application.getName());
        nameLabel.setText(Application.getLongName() + " " + Application.getVersion());
        
        // for JGoodies Looks 2.0.1 work around
        Color tempColor = javax.swing.UIManager.getDefaults().getColor("TextArea.background");
        Color backColor = new Color(tempColor.getRed(),tempColor.getGreen(),tempColor.getBlue(),tempColor.getAlpha());
        
        licenseArea.setBackground(backColor);
        creditsArea.setBackground(backColor);
        christArea.setBackground(backColor);
    }
    
    private String loadText(InputStream is) {
        int len;
        char[] cBuf = new char[1024];
        StringBuffer sb = new StringBuffer();
        
        try {
            BufferedReader reader  = new BufferedReader(new InputStreamReader(is));
            while ((len=reader.read(cBuf,0,1024)) != -1) {
                sb.append(cBuf,0,len);
            }
            reader.close();
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
        return sb.toString();
    }
    
    private void fillText() {
        String str;
        
        InputStream is = null;
        try {
            is = getClass().getResourceAsStream("/kiyut/ekspos/LICENSE-ekspos.txt");
            str = loadText(is);
            is.close();
            licenseArea.insert(str,0);
            licenseArea.setCaretPosition(0);
            
            is = getClass().getResourceAsStream("/kiyut/ekspos/christianity.txt");
            str = loadText(is);
            is.close();
            christArea.insert(str,0);
            christArea.setCaretPosition(0);
            
            is = getClass().getResourceAsStream("/kiyut/ekspos/credits.txt");
            str = loadText(is);
            is.close();
            creditsArea.insert(str,0);
            creditsArea.setCaretPosition(0);
        } catch (Exception ex){
            System.err.println("AboutWindow unable to load resources file");
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea christArea;
    private javax.swing.JButton closeButton;
    private javax.swing.JTextArea creditsArea;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextArea licenseArea;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables
    
}
