/*
 * Peercast4jYPSettingBox.java
 *
 * Created on 2008/03/18, 23:27
 */

package peercast4j;

import java.awt.event.KeyEvent;
import javax.swing.SwingUtilities;
import org.jdesktop.application.Action;


/**
 *
 * @author  ab
 */
public class Peercast4jYPSettingBox extends javax.swing.JDialog {
    
    /** Creates new form Peercast4jYPSettingBox */
    public Peercast4jYPSettingBox(java.awt.Frame parent) {
        super(parent);
        initComponents();
    }
    
    /** 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() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        ypSettingPopupMenu = new javax.swing.JPopupMenu();
        deleteMenuItem = new javax.swing.JMenuItem();
        ypSettingsTabbedPane = new javax.swing.JTabbedPane();
        addYPTextField = new javax.swing.JTextField();
        addYPButton = new javax.swing.JButton();

        ypSettingPopupMenu.setName("ypSettingPopupMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(peercast4j.Peercast4jApp.class).getContext().getActionMap(Peercast4jYPSettingBox.class, this);
        deleteMenuItem.setAction(actionMap.get("deleteTab")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(peercast4j.Peercast4jApp.class).getContext().getResourceMap(Peercast4jYPSettingBox.class);
        deleteMenuItem.setText(resourceMap.getString("deleteMenuItem.text")); // NOI18N
        deleteMenuItem.setName("deleteMenuItem"); // NOI18N
        ypSettingPopupMenu.add(deleteMenuItem);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        ypSettingsTabbedPane.setName("ypSettingsTabbedPane"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, ypSettingPopupMenu, org.jdesktop.beansbinding.ObjectProperty.create(), ypSettingsTabbedPane, org.jdesktop.beansbinding.BeanProperty.create("componentPopupMenu"));
        bindingGroup.addBinding(binding);

        addYPTextField.setText(resourceMap.getString("addYPTextField.text")); // NOI18N
        addYPTextField.setToolTipText(resourceMap.getString("addYPTextField.toolTipText")); // NOI18N
        addYPTextField.setName("addYPTextField"); // NOI18N
        addYPTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                addYPTextFieldKeyTyped(evt);
            }
        });

        addYPButton.setText(resourceMap.getString("addYPButton.text")); // NOI18N
        addYPButton.setEnabled(false);
        addYPButton.setName("addYPButton"); // NOI18N
        addYPButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addYPButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ypSettingsTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addYPTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addYPButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addYPButton)
                    .addComponent(addYPTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ypSettingsTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))
        );

        bindingGroup.bind();

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

    private void addYPTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_addYPTextFieldKeyTyped
        if (!addYPButton.isEnabled()) {
            addYPButton.setEnabled(true);
        }
}//GEN-LAST:event_addYPTextFieldKeyTyped

    private void addYPButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addYPButtonActionPerformed
        ypSettingsTabbedPane.setVisible(true);
        String s = addYPTextField.getText();
        addYPTextField.setText("");
        addYPButton.setEnabled(false);
        ypSettingsTabbedPane.add(new YPSettingPanel(s));
    }//GEN-LAST:event_addYPButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Peercast4jYPSettingBox dialog = new Peercast4jYPSettingBox(new javax.swing.JFrame());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void deleteTab() {
        int index = ypSettingsTabbedPane.getSelectedIndex();
        if (index >= 0) {
            ypSettingsTabbedPane.remove(index);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addYPButton;
    private javax.swing.JTextField addYPTextField;
    private javax.swing.JMenuItem deleteMenuItem;
    private javax.swing.JPopupMenu ypSettingPopupMenu;
    private javax.swing.JTabbedPane ypSettingsTabbedPane;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    
}
