/*
 * AlgebraInfo.java
 *
 * Created on 18 mei 2007, 15:18
 *
 * This file is part of SimpLie.
 * 
 * SimpLie 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.
 * 
 * SimpLie 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 SimpLie.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package edu.simplie.ui;

import edu.simplie.dynkindiagram.*;
import edu.simplie.algebra.*;
import edu.simplie.*;

import javax.swing.table.*;
import java.util.Collection;
import java.util.Iterator;


/**
 * Displays information on a composite of algebras.
 * The information contains the (symmetrized / inverse) Cartan matrix, 
 * the root space metric, the root system, and arbitrary highest
 * weight representations.
 * 
 * @see		AlgebraComposite
 * @author  Teake Nutma
 * @version $Revision$, $Date$
 */
public class AlgebraInfo extends javax.swing.JPanel implements DiagramListener
{
	private DefaultTableModel tableModelRoots;
	private Algebra algebra;
	private AlgebraComposite algebras;
	
	/** Creates new form AlgebraInfo */
	public AlgebraInfo()
	{
		initComponents();
		tableModelRoots = (DefaultTableModel) rootTable.getModel();
		algebra = null;
	}
	
	/** Sets the composite of algebras we want to view info of. */
	public void setAlgebraComposite(AlgebraComposite algebras)
	{
		this.algebras = algebras;
		algebras.dd.addListener(this);
	}
	
	public void diagramChanged()
	{
		switch(algebrasBox.getSelectedIndex())
		{
		case 0:
			algebra = algebras.algebra;
			break;
		case 1:
			algebra = algebras.subAlgebra;
			break;
		case 2:
			algebra = algebras.intAlgebra;
			break;
		default:
			algebra = null;
		}
		
		if(algebra == null)
			return;

		uIAlgebraInfo.update(algebra);

		String matrix;
		switch(matrixBox.getSelectedIndex())
		{
			case 0:
				matrix = Helper.matrixToString(algebra.A);
				break;
			case 1:
				matrix = Helper.matrixToString(algebra.B);
				break;
			case 2:
				matrix = Helper.matrixToString(algebra.invA);
				break;
			case 3:
				matrix = Helper.matrixToString(algebra.G);
				break;
			default:
				matrix = "";
		}
		tfMatrix.setText(matrix);

		if(tableContainer.isVisible())
			fillRootTable();
	}

	private void fillRootTable()
	{
		if(algebra == null)
			return;

		// Clear and fill the table.
		tableModelRoots.setRowCount(0);

		int maxHeight = heightSpinner.getValue();
		algebra.rs.construct(maxHeight);
		maxHeight = (maxHeight == 0) ? algebra.rs.size() : Math.min(algebra.rs.size(),maxHeight+1);
		for (int i = 1; i < maxHeight; i++)
		{
			Collection roots	= algebra.rs.get(i);
			Iterator iterator	= roots.iterator();
			while (iterator.hasNext())
			{
				Root root = (Root) iterator.next();
				Object[] rowData = new Object[6];
				rowData[0] = Helper.arrayToString(root.vector);
				rowData[1] = Helper.arrayToString(algebra.rootToWeight(root.vector));
				rowData[2] = root.norm;
				rowData[3] = root.mult;
				rowData[4] = root.coMult;
				rowData[5] = root.height();
				tableModelRoots.addRow(rowData);
			}
		}
	}
	
	/** 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() {

        tabbedPane = new javax.swing.JTabbedPane();
        matricesContainer = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tfMatrix = new javax.swing.JTextArea();
        tableContainer = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        rootTable = new edu.simplie.ui.reusable.UIPrintableColorTable();
        jLabel1 = new javax.swing.JLabel();
        heightSpinner = new edu.simplie.ui.reusable.UISpinner();
        uIAlgebraInfo = new edu.simplie.ui.reusable.UIAlgebraInfo();
        jPanel2 = new javax.swing.JPanel();
        algebrasBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        matrixBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();

        tabbedPane.setMinimumSize(new java.awt.Dimension(0, 0));

        tfMatrix.setColumns(20);
        tfMatrix.setRows(5);
        jScrollPane1.setViewportView(tfMatrix);

        javax.swing.GroupLayout matricesContainerLayout = new javax.swing.GroupLayout(matricesContainer);
        matricesContainer.setLayout(matricesContainerLayout);
        matricesContainerLayout.setHorizontalGroup(
            matricesContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(matricesContainerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE)
                .addContainerGap())
        );
        matricesContainerLayout.setVerticalGroup(
            matricesContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, matricesContainerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabbedPane.addTab("Matrices", matricesContainer);

        tableContainer.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                tableContainerComponentShown(evt);
            }
        });

        rootTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Vector", "Dynkin labels", "Norm", "Multiplicity", "CoMultiplicity", "Height"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Long.class, java.lang.Long.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        rootTable.setColumnSelectionAllowed(true);
        jScrollPane2.setViewportView(rootTable);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(edu.simplie.SimpLieApp.class).getContext().getResourceMap(AlgebraInfo.class);
        jLabel1.setText(resourceMap.getString("algebraInfo.maxHeight")); // NOI18N

        heightSpinner.setMinValue(0);
        heightSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                heightSpinnerStateChanged(evt);
            }
        });

        javax.swing.GroupLayout tableContainerLayout = new javax.swing.GroupLayout(tableContainer);
        tableContainer.setLayout(tableContainerLayout);
        tableContainerLayout.setHorizontalGroup(
            tableContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tableContainerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tableContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE)
                    .addGroup(tableContainerLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(heightSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        tableContainerLayout.setVerticalGroup(
            tableContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tableContainerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tableContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(heightSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabbedPane.addTab("Positive roots", tableContainer);

        uIAlgebraInfo.setTitle(""); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        algebrasBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Full algebra", "Regular subalgebra", "Internal algebra" }));
        algebrasBox.setToolTipText(resourceMap.getString("algebraInfo.selectTooltip")); // NOI18N
        algebrasBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                algebrasBoxActionPerformed(evt);
            }
        });

        jLabel3.setText("Select algebra:"); // NOI18N
        jLabel3.setToolTipText(resourceMap.getString("algebraInfo.selectTooltip")); // NOI18N

        matrixBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cartan matrix", "Root space metric", "Inverse of Cartan matrix", "Quadratic form matrix" }));
        matrixBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                matrixBoxActionPerformed(evt);
            }
        });

        jLabel4.setText("Select matrix:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(10, 10, 10)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(matrixBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(algebrasBox, 0, 222, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(algebrasBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(matrixBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(uIAlgebraInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(uIAlgebraInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
				
	private void algebrasBoxActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_algebrasBoxActionPerformed
	{//GEN-HEADEREND:event_algebrasBoxActionPerformed
		diagramChanged();
}//GEN-LAST:event_algebrasBoxActionPerformed
	
	private void matrixBoxActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_matrixBoxActionPerformed
	{//GEN-HEADEREND:event_matrixBoxActionPerformed
		diagramChanged();
		tabbedPane.setSelectedIndex(0);
	}//GEN-LAST:event_matrixBoxActionPerformed

	private void tableContainerComponentShown(java.awt.event.ComponentEvent evt)//GEN-FIRST:event_tableContainerComponentShown
	{//GEN-HEADEREND:event_tableContainerComponentShown
		fillRootTable();
}//GEN-LAST:event_tableContainerComponentShown

	private void heightSpinnerStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_heightSpinnerStateChanged
	{//GEN-HEADEREND:event_heightSpinnerStateChanged
		fillRootTable();
	}//GEN-LAST:event_heightSpinnerStateChanged
	
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox algebrasBox;
    private edu.simplie.ui.reusable.UISpinner heightSpinner;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel matricesContainer;
    private javax.swing.JComboBox matrixBox;
    private edu.simplie.ui.reusable.UIPrintableColorTable rootTable;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JPanel tableContainer;
    private javax.swing.JTextArea tfMatrix;
    private edu.simplie.ui.reusable.UIAlgebraInfo uIAlgebraInfo;
    // End of variables declaration//GEN-END:variables
	
	}
