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

package com.caucho.netbeans.ide.ui;

import com.caucho.netbeans.ide.AddInstanceIterator;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public final class AddServerVisualPanel extends JPanel {
  private AddInstanceIterator _addServer;
  private AddServerLocationPanel _wizardPanel;

  AddServerVisualPanel(AddInstanceIterator addServer, 
                       AddServerLocationPanel wizardPanel)
  {
    initComponents();

    _addServer = addServer;
    _wizardPanel = wizardPanel;
    
    this.jResinHome.setText(addServer.getResinHome());
    this.jSpinner1.setValue(addServer.getPort());

    this.jResinHome.getDocument().addDocumentListener(new DocumentListener() {
	public void changedUpdate(DocumentEvent e)
	{
	  _wizardPanel.stateChanged(new ChangeEvent(this));
	}
	
	public void insertUpdate(DocumentEvent e)
	{
	  _wizardPanel.stateChanged(new ChangeEvent(this));
	}
	
	public void removeUpdate(DocumentEvent e)
	{
	  _wizardPanel.stateChanged(new ChangeEvent(this));
	}
      });
  }

  public String getResinHome()
  {
    return this.jResinHome.getText().trim();
  }
  
  public int getPort()
  {
    return Integer.valueOf(this.jSpinner1.getValue().toString());
  }

  @Override
  public String getName()
  {
    return "Set Resin Home";
  }

    /** 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() {

    jLabel1 = new javax.swing.JLabel();
    jResinHome = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jSpinner1 = new javax.swing.JSpinner();

    org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "Resin Home");

    jResinHome.setText("jResinHome");

    org.openide.awt.Mnemonics.setLocalizedText(jButton1, "Browse");
    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        jButton1MouseClicked(evt);
      }
    });

    org.openide.awt.Mnemonics.setLocalizedText(jLabel2, "Resin Home Location");

    org.openide.awt.Mnemonics.setLocalizedText(jLabel3, "Port");

    jSpinner1.setEditor(new javax.swing.JSpinner.NumberEditor(jSpinner1, ""));
    jSpinner1.addChangeListener(new javax.swing.event.ChangeListener() {
      public void stateChanged(javax.swing.event.ChangeEvent evt) {
        jSpinner1StateChanged(evt);
      }
    });
    jSpinner1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
      public void propertyChange(java.beans.PropertyChangeEvent evt) {
        jSpinner1PropertyChange(evt);
      }
    });

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .addContainerGap()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(jLabel2)
          .add(layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(jLabel1)
              .add(jLabel3))
            .add(28, 28, 28)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(layout.createSequentialGroup()
                .add(jResinHome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jButton1))
              .add(jSpinner1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 55, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
        .addContainerGap(53, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .addContainerGap()
        .add(jLabel2)
        .add(31, 31, 31)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jResinHome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
          .add(jLabel1)
          .add(jButton1))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jLabel3)
          .add(jSpinner1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addContainerGap(195, Short.MAX_VALUE))
    );
  }// </editor-fold>//GEN-END:initComponents

  private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
    String loc = browseResinHome();
    
    if (loc != null && ! "".equals(loc))
      this.jResinHome.setText(loc);
  }//GEN-LAST:event_jButton1MouseClicked

  private void jSpinner1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jSpinner1PropertyChange
    _wizardPanel.stateChanged(new ChangeEvent(this));
  }//GEN-LAST:event_jSpinner1PropertyChange

  private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner1StateChanged
    _wizardPanel.stateChanged(evt);
  }//GEN-LAST:event_jSpinner1StateChanged
    
  private String browseResinHome()
  {
    JFileChooser chooser = getResinHomeChooser();
    int ret = chooser.showDialog(SwingUtilities.getWindowAncestor(this),
				 "Choose Resin Home");
        
    if (ret == JFileChooser.APPROVE_OPTION)
      return chooser.getSelectedFile().getAbsolutePath();
    else
      return null;
  }
    
  private JFileChooser getResinHomeChooser()
  {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Select Resin Home");
    chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);

    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setApproveButtonMnemonic('C');
    chooser.setMultiSelectionEnabled(false);
    chooser.setApproveButtonToolTipText("Chooser");

    chooser.getAccessibleContext().setAccessibleName("Chooser");
    chooser.getAccessibleContext().setAccessibleDescription("Chooser");

    chooser.setSelectedFile(new File(getResinHome()));

    return chooser;
  }


  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JTextField jResinHome;
  private javax.swing.JSpinner jSpinner1;
  // End of variables declaration//GEN-END:variables
}

