Package event_manager.views

Source Code of event_manager.views.AddStaffToEvent

package event_manager.views;

import event_manager.models.Event;
import event_manager.models.Staff;
import java.awt.event.KeyEvent;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;

/**
*
* @author uzzaldevkota
*/
public class AddStaffToEvent extends AppDialog {

    private final Event event;
    private final DefaultListModel fromListModel;
    private final DefaultListModel toListModel;
   
    /**
     * Creates new form Show
     * @param parent
     * @param modal
     * @param e Event
     */
    public AddStaffToEvent(JFrame parent, boolean modal, Event e) {
        super(parent, modal);
        event = e;
        fromListModel = Staff.getListModel(event);
        toListModel = new DefaultListModel();
        initComponents();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    /**
     * 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() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        showPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        fromList = new javax.swing.JList(fromListModel);
        jScrollPane2 = new javax.swing.JScrollPane();
        toList = new javax.swing.JList(toListModel);
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        submitButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        showPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add Staff to Event (" + event.getName() + ")", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Abadi MT Condensed Extra Bold", 0, 18))); // NOI18N

        fromList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        fromList.setBounds(new java.awt.Rectangle(0, 0, 39, 140));
        fromList.setCellRenderer(Staff.getListCellRenderer());
        fromList.setPreferredSize(new java.awt.Dimension(39, 80));
        fromList.setSelectedIndex(0);
        fromList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fromListMouseClicked(evt);
            }
        });
        fromList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                fromListKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(fromList);

        toList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        toList.setBounds(new java.awt.Rectangle(0, 0, 39, 140));
        toList.setCellRenderer(Staff.getListCellRenderer());
        toList.setSelectedIndex(0);
        toList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                toListMouseClicked(evt);
            }
        });
        toList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                toListKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(toList);

        addButton.setText(">");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, fromList, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), addButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        removeButton.setText("<");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, toList, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), removeButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Available Staffs");

        jLabel2.setText("Staffs in Event");

        javax.swing.GroupLayout showPanelLayout = new javax.swing.GroupLayout(showPanel);
        showPanel.setLayout(showPanelLayout);
        showPanelLayout.setHorizontalGroup(
            showPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(showPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(showPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(showPanelLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(showPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(removeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(showPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(showPanelLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 80, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        showPanelLayout.setVerticalGroup(
            showPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, showPanelLayout.createSequentialGroup()
                .addGroup(showPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, showPanelLayout.createSequentialGroup()
                        .addGap(96, 96, 96)
                        .addComponent(addButton)
                        .addGap(18, 18, 18)
                        .addComponent(removeButton)
                        .addGap(0, 184, Short.MAX_VALUE))
                    .addGroup(showPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(showPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(showPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addComponent(jScrollPane2))))
                .addContainerGap())
        );

        submitButton.setText("Submit");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(showPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(submitButton)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(showPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(submitButton)
                .addContainerGap())
        );

        bindingGroup.bind();

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

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
        addStaffToEvent();
    }//GEN-LAST:event_submitButtonActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        addStaff();
    }//GEN-LAST:event_addButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        removeStaff();
    }//GEN-LAST:event_removeButtonActionPerformed

    private void fromListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fromListMouseClicked
        if(evt.getClickCount() > 1) {
            addStaff();
        }
    }//GEN-LAST:event_fromListMouseClicked

    private void toListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_toListMouseClicked
        if(evt.getClickCount() > 1) {
            removeStaff();
        }
    }//GEN-LAST:event_toListMouseClicked

    private void fromListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_fromListKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
            addStaff();
        }
    }//GEN-LAST:event_fromListKeyPressed

    private void toListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_toListKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
            removeStaff();
        }
    }//GEN-LAST:event_toListKeyPressed

    private void addStaff() {
        if(fromList.isSelectionEmpty()) return;
        Staff staff = (Staff) fromList.getSelectedValue();
        int index = fromList.getSelectedIndex();
        fromListModel.removeElementAt(index);
        toListModel.addElement(staff);
    }
   
    private void removeStaff() {
        if(toList.isSelectionEmpty()) return;
        Staff staff = (Staff) toList.getSelectedValue();
        int index = toList.getSelectedIndex();
        toListModel.removeElementAt(index);
        fromListModel.addElement(staff);
    }

    private void addStaffToEvent() {
        int staffSize = toListModel.size();
        if(staffSize < 1) return;
        Object[] staffIds = new Object[staffSize];
        for(int i = 0; i < staffSize; i++) {
            Staff staff = (Staff) toListModel.getElementAt(i);
            staffIds[i] = staff.getId();
        }
       event.addStaffs(staffIds);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JList fromList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton removeButton;
    private javax.swing.JPanel showPanel;
    private javax.swing.JButton submitButton;
    private javax.swing.JList toList;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of event_manager.views.AddStaffToEvent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.