import javax.swing.*;
import java.awt.event.*;
import org.netbeans.lib.awtextra.*;

/** This is a class to add a new Part to a set.  This class extends a JFrame
 */
public class JFAddNewPart extends JFrame implements ActionListener{
    
       // Variables declaration - do not modify
    /** This is Swing component used to fill in the description of the object
     */
    private JTextPane jtpDescrip;
    /** This is combobox componnent and is used to choose the Set at wich you
     * want to add this component
     *
     */
    private JComboBox jcbSets;
    /** A label
     */
    private JLabel jlblSet;
    /** A label
     */
    private JLabel jlblName;
    /** A label
     */
    private JLabel jlblDescrip;
    /** This a textfield component.  This component is used to fill in the url
     * of the Part
     */
    private JTextField jtxtURL;
    /** This is a TextField component and is used to give a name to an object
     */
    private JTextField jtxtName;
    /** A label
     *
     */
    private JLabel jlblURL;
    /** The Save button.  If this button is clicked the settings will be saved
     */
    private JButton jbtnSave;
    /** Quit button.  Used to close this frame or to cancel
     */
    private JButton jbtnQuit;
    /** a part object
     */
    private JButton jURL;
    private DBParts part;
    
    private JFSets jFSets;
    
   
    // End of variables declaration

    /** Creates new form JFAddNewPart
     */
    public JFAddNewPart() {
        super("Add a new part to a set");
        initComponents ();
        pack ();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     *
     */
    private void initComponents () {
        jtpDescrip = new JTextPane ();
        
        DBSets  dbSets = new DBSets();
        Sets[] sets = dbSets.getSets();
        jcbSets = new JComboBox (sets); 
        
              
        jlblSet = new JLabel ();
        jlblName = new JLabel ();
        jlblDescrip = new JLabel ();
        jtxtURL = new JTextField ();
        jtxtName = new JTextField ();        
        jbtnSave = new JButton ();
        jbtnQuit = new JButton ();
        jURL = new JButton();
        
        getContentPane ().setLayout (new AbsoluteLayout ());     
        getContentPane ().add (jtpDescrip, new AbsoluteConstraints (10, 210, 440, 80));
        getContentPane ().add (jcbSets, new AbsoluteConstraints (110, 70, 340, 30));
       
        jlblSet.setText ("Sets:");
        jlblSet.setFont (new java.awt.Font ("Dialog", 1, 14));
        getContentPane ().add (jlblSet, new AbsoluteConstraints (10, 70, -1, -1));
        jlblName.setText ("Name:");
        jlblName.setFont (new java.awt.Font ("Dialog", 1, 14));
        getContentPane ().add (jlblName, new AbsoluteConstraints (10, 30, -1, -1));
        jlblDescrip.setName ("jlblDescrip");
        jlblDescrip.setText ("Description: ");
        jlblDescrip.setFont (new java.awt.Font ("Dialog", 1, 14));
        getContentPane ().add (jlblDescrip, new AbsoluteConstraints (10, 180, -1, -1));
        getContentPane ().add (jtxtURL, new AbsoluteConstraints (110, 120, 340, 30));
        getContentPane ().add (jtxtName, new AbsoluteConstraints (110, 30, 340, 30));
        jbtnSave.setText ("Save");
        jbtnSave.setActionCommand("SAVE");
        jbtnSave.addActionListener(this);
        getContentPane ().add (jbtnSave, new AbsoluteConstraints (110, 350, -1, -1));
        jbtnQuit.setText ("Cancel");
        jbtnQuit.setActionCommand("CANCEL");
        jbtnQuit.addActionListener(this);
        getContentPane ().add (jbtnQuit, new AbsoluteConstraints (210, 350, -1, -1));
        jURL.setText("URL");
        jURL.setActionCommand("URL");
        jURL.addActionListener(this);
        getContentPane ().add (jURL, new AbsoluteConstraints (10, 120, -1, -1));
        this.setResizable(false);
    }    

      /** A method to start this form without starting the entire application
     * @param args the command line arguments
     */
    public static void main (String args[]) {
        new JFAddNewPart ().show ();
    }
    
    /** When an action is performed this method is called
     * @param p1 An actionEvent
     */
    public void actionPerformed(final java.awt.event.ActionEvent p1) {
        if(p1.getActionCommand().equals("SAVE")){
                CapturingPicture picture = new CapturingPicture(this.jtxtURL.getText(), this.jtxtName.getText(),ObservableSettings.SETS_PATH);
                part =  new DBParts();
                part.addNewPart(this.jtxtName.getText(),((Sets)this.jcbSets.getSelectedItem()).getId(),this.jtxtURL.getText(),this.jtpDescrip.getText());
                jFSets = new JFSets();
                jFSets.show();
                this.hide();
        }else{
            if(p1.getActionCommand().equals("CANCEL")){                
                jFSets = new JFSets();
                jFSets.show();
                this.hide();            
            }
            if(p1.getActionCommand().equals("URL")){
                jtxtURL.setText("http://w2000-jurgenv/sets/" + jtxtName.getText() + ".wrl");
            }
        }        
    }
    
    public void addActionListener(ActionListener l){
        jbtnSave.addActionListener(l);
    }
    
    public void show(){
        jtxtName.requestFocus();
        jtxtName.selectAll();
        super.show();
    }
 }