/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2023 Wandora Team
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 
 * TopicMapStatisticsDialog.java
 *
 * Created on 25. toukokuuta 2006, 19:11
 */

package org.wandora.application.gui;



import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JSeparator;

import org.wandora.application.Wandora;
import org.wandora.application.gui.simple.SimpleLabel;
import org.wandora.topicmap.TopicMap;
import org.wandora.topicmap.TopicMapStatOptions;
import org.wandora.topicmap.layered.Layer;
import org.wandora.utils.ClipboardBox;


/**
 *
 * @author  akivela
 */
public class TopicMapStatisticsDialog extends javax.swing.JDialog {


	private static final long serialVersionUID = 1L;

	
	private Wandora wandora = null;
    private TopicMap map = null;
    private StringBuffer stats = null;
    
    
    
    /**
     * Creates new form TopicMapStatisticsDialog
     */
    public TopicMapStatisticsDialog(Wandora w, Layer layer) {
        super(w, true);
        this.wandora = w;
        this.map = layer.getTopicMap();
        initComponents();
        initStatistics();
        setSize(500, 290);
        if(w != null) w.centerWindow(this);
        this.setTitle("Layer '" + layer.getName() + "' statistics");
    }
    
    
    public TopicMapStatisticsDialog(Wandora w, TopicMap map, String name) {
        super(w, true);
        this.wandora = w;
        this.map = map;
        initComponents();
        initStatistics();
        setSize(450, 290);
        if(w != null) w.centerWindow(this);
        if(name != null) this.setTitle("Layer '" + name + "' statistics");
    }
    
    
    
    private void initStatistics() {
        int[] statOptions = TopicMapStatOptions.getAvailableOptions();
        int bagCount = 0;
        stats = new StringBuffer();
        GridBagConstraints gbc = null;
        SimpleLabel statDescription = null;
        SimpleLabel statValue = null;
        String statDescriptionString = null;
        String statString = null;
        gbc=new GridBagConstraints();
        Insets rowInsets = new Insets(0, 9,0, 9);
        gbc.insets = new Insets(9, 9,0, 9);
        
        for(int i=0; i<statOptions.length; i++) {
            try {
                gbc.gridy=bagCount++;
                gbc.gridx=0;
                gbc.weightx=0.5;
                gbc.gridwidth=1;
                gbc.fill=gbc.HORIZONTAL;

                statDescriptionString = TopicMapStatOptions.describeStatOption(statOptions[i]);
                stats.append(statDescriptionString);
                statDescription = new SimpleLabel(statDescriptionString);
                statPanel.add(statDescription, gbc);
               
                gbc.gridx=1;
                statString = "n.a.";
                try {
                    statString = map.getStatistics(new TopicMapStatOptions(statOptions[i])).toString();
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                stats.append("\t").append(statString).append("\n");
                statValue = new SimpleLabel(statString);
                statValue.setHorizontalAlignment(SimpleLabel.RIGHT);
                statPanel.add(statValue, gbc);
                
                gbc.gridy=bagCount++;
                gbc.gridx=0;
                gbc.weightx=1;
                gbc.gridwidth=2;
                gbc.insets = rowInsets;
                JSeparator js = new JSeparator(JSeparator.HORIZONTAL);
                js.setForeground(new Color(0x909090));
                statPanel.add(js, gbc);             
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
    
    
    /** 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;

        buttonPanel = new javax.swing.JPanel();
        copyButton = new org.wandora.application.gui.simple.SimpleButton();
        closeButton = new org.wandora.application.gui.simple.SimpleButton();
        scrollPanel = new javax.swing.JPanel();
        scrollPane = new javax.swing.JScrollPane();
        statPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Topic map statistics");

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

        copyButton.setText("Copy");
        copyButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        copyButton.setMaximumSize(new java.awt.Dimension(70, 23));
        copyButton.setMinimumSize(new java.awt.Dimension(70, 23));
        copyButton.setPreferredSize(new java.awt.Dimension(70, 23));
        copyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(copyButton, new java.awt.GridBagConstraints());

        closeButton.setText("Close");
        closeButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        closeButton.setMaximumSize(new java.awt.Dimension(70, 23));
        closeButton.setMinimumSize(new java.awt.Dimension(70, 23));
        closeButton.setPreferredSize(new java.awt.Dimension(70, 23));
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(4, 3, 4, 3);
        buttonPanel.add(closeButton, gridBagConstraints);

        getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);

        scrollPanel.setLayout(new java.awt.BorderLayout());

        statPanel.setLayout(new java.awt.GridBagLayout());
        scrollPane.setViewportView(statPanel);

        scrollPanel.add(scrollPane, java.awt.BorderLayout.CENTER);

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

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void copyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyButtonActionPerformed
        ClipboardBox.setClipboard(stats.toString());
    }//GEN-LAST:event_copyButtonActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_closeButtonActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton copyButton;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JPanel scrollPanel;
    private javax.swing.JPanel statPanel;
    // End of variables declaration//GEN-END:variables
    
}
