/*
 * PokusDlg.java
 *
 * Created on 20. říjen 2000, 14:38
 */

package timetable.gui;

import timetable.data.*;
import timetable.util.*;
import timetable.test.*;
/**
 *
 * @author  Administrator
 * @version 
 */
public class TimeTableDlg extends javax.swing.JDialog {
//    TimeTable ttable;
    TimeTablePanel ttable;
    Problem problem;
    int NrResource=0;

    /** Creates new form PokusDlg */
    public TimeTableDlg(java.awt.Frame parent,boolean modal,Config k,Problem p) throws TimetableException {
        super (parent, modal);
        try {
            
//        JActionField a = new JActionField();
//        JActionField b = new JActionField();
//        this.getContentPane().add(a, java.awt.BorderLayout.CENTER);
//        this.getContentPane().add(b, java.awt.BorderLayout.WEST);
        initComponents ();
        pack ();
        this.problem = p;
        
        
        for (int i=0;i<problem.resources.length;i++)
            jComboBox2.addItem(problem.resources[i].name);
        for (int i=0;i<problem.resources[NrResource].size();i++)
            jComboBox1.addItem(problem.resources[NrResource].get(i).name);
        jComboBox1.addActionListener (new java.awt.event.ActionListener () {
             public void actionPerformed (java.awt.event.ActionEvent evt) {
                 try {
                   ttable.setResource(problem.resources[NrResource].get(((javax.swing.JComboBox)evt.getSource()).getSelectedIndex()));
                   ttable.updateUI();
                 } catch (TimetableException e) {
                     System.err.println("Error: "+e);
                 }
             }
          });
        jComboBox2.addActionListener (new java.awt.event.ActionListener () {
             public void actionPerformed (java.awt.event.ActionEvent evt) {
                 try {
                   jComboBox1.removeAllItems();
                   NrResource=((javax.swing.JComboBox)evt.getSource()).getSelectedIndex();
                   for (int i=0;i<problem.resources[NrResource].size();i++)
                     jComboBox1.addItem(problem.resources[NrResource].get(i).name);
//                   jScrollPane1.setEnabled(false);
//                   jScrollPane1.setVisible(false);
                   ttable.setResources(problem.resources[NrResource]);               
                   jScrollPane1.updateUI();
//                   jScrollPane1.setVisible(true);
//                   jScrollPane1.setEnabled(true);
                 } catch (TimetableException e) {
                     System.err.println("Error: "+e);
                 }
             }
          });
//          ttable = new TimeTable(new TimeTableModel(k,problem.resources[NrResource].get(0)),jScrollPane1);
          ttable = new TimeTablePanel(k,problem.resources[NrResource]/*.get(0)*/);
          jScrollPane1.setViewportView(ttable);
        } catch (Exception e) {
            System.err.println("Nastala chyba: "+e.getMessage());
            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 FormEditor.
     */
private void initComponents() {//GEN-BEGIN:initComponents
jScrollPane1 = new javax.swing.JScrollPane();
jComboBox1 = new javax.swing.JComboBox();
jComboBox2 = new javax.swing.JComboBox();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
}
);

jScrollPane1.setPreferredSize(new java.awt.Dimension(500, 200));

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



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



getContentPane().add(jComboBox2, java.awt.BorderLayout.NORTH);

}//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible (false);
        dispose ();
    }//GEN-LAST:event_closeDialog

    /**
    * @param args the command line arguments
    */
    public static void main (String args[]) {
        try {  
        Config k = new Config();
        if (args.length==0) {
          SmartGenerator sg = new SmartGenerator(k,false);
          sg.generujCele().save("Rozvrh.shd");
          new TimeTableDlg (new javax.swing.JFrame (), true, k, sg.generujCele()).show ();
        } else {
            for (int i=0;i<args.length;i++) {
              Problem p = new Problem(k);
              p=Problem.load(args[i]);
              System.out.println("Problem: "+p);
              new TimeTableDlg (new javax.swing.JFrame (), true, k, p).show ();
            }
        }
        } catch (Exception e) {
            System.err.println("Error: "+e);
        }
    }


// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
// End of variables declaration//GEN-END:variables

}
