/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RPrenotazioneP.java
 *
 * Created on 9-lug-2011, 19.37.38
 */

package app.gui;


import app.Main;
import archive.interfacce.*;
import archive.system.*;
import javax.swing.JOptionPane;

public class RPrenotazioneP extends javax.swing.JFrame {
  private RichiestaPrenotazionePacchetto rPren= new PRequestPacchettoManager();
    /** Creates new form RPrenotazioneP */
    public RPrenotazioneP() {
        Main.gui.setVisible(false);
        initComponents();
        String[] s=rPren.showList();
         for (int i = 0; i < s.length; i++) {
            jCmBVillaggi.insertItemAt(s[i], i);
        }
    }


    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCmBVillaggi = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jFTFNsett = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jBDettagli = new javax.swing.JButton();
        jBPrenotare = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Seleziona il Pacchetto");

        jFTFNsett.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
        jFTFNsett.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFTFNsettActionPerformed(evt);
            }
        });
        jFTFNsett.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jFTFNsettKeyReleased(evt);
            }
        });

        jLabel2.setText("Numero Settimana");

        jBDettagli.setText("Dettagli");
        jBDettagli.setEnabled(false);
        jBDettagli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBDettagliActionPerformed(evt);
            }
        });

        jBPrenotare.setText("Prenotare");
        jBPrenotare.setEnabled(false);
        jBPrenotare.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPrenotareActionPerformed(evt);
            }
        });

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { " " };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jCmBVillaggi, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(jBDettagli))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(jBPrenotare)))
                        .addGap(29, 29, 29)
                        .addComponent(jFTFNsett, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(51, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1))
                    .addComponent(jCmBVillaggi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel2)
                        .addGap(50, 50, 50)
                        .addComponent(jBDettagli)
                        .addGap(18, 18, 18)
                        .addComponent(jBPrenotare))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jFTFNsett, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(38, Short.MAX_VALUE))
        );

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

    public static int getNsett(){
        return Integer.parseInt(jFTFNsett.getText());
    }
    public static String getNomeVillaggio(){
        return (String) jCmBVillaggi.getSelectedItem();
    }
    private void jBDettagliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBDettagliActionPerformed
        String[] showDetails = rPren.showDetails((String) jCmBVillaggi.getSelectedItem(), Integer.parseInt(jFTFNsett.getText()));
        jList1.setListData(showDetails);
    }//GEN-LAST:event_jBDettagliActionPerformed

    private void jFTFNsettActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFTFNsettActionPerformed
          try{
        if(rPren.isAvaible(Integer.parseInt(jFTFNsett.getText()), (String) jCmBVillaggi.getSelectedItem())){
            jBDettagli.setEnabled(true);
            jBPrenotare.setEnabled(true);
        }
        }catch(IllegalArgumentException e){
            JOptionPane.showMessageDialog(rootPane,"Settimana Passata","",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jFTFNsettActionPerformed

    private void jBPrenotareActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPrenotareActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DatiClienteP().setVisible(true);
            }
        });
    }//GEN-LAST:event_jBPrenotareActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
     Main.gui.setVisible(true);        
    }//GEN-LAST:event_formWindowClosing

    private void jFTFNsettKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jFTFNsettKeyReleased
        int nsett = Integer.parseInt(jFTFNsett.getText());
        if(nsett<1 || nsett>52) jFTFNsett.setText("");
    }//GEN-LAST:event_jFTFNsettKeyReleased

    /**
    * @param args the command line arguments
    */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBDettagli;
    private javax.swing.JButton jBPrenotare;
    public static javax.swing.JComboBox jCmBVillaggi;
    public static javax.swing.JFormattedTextField jFTFNsett;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
