Sunday, February 6, 2011

View Code using Button Combo List

Simple View Code :

package gpro.sfp;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.ButtonGroup;
import javax.swing.DefaultButtonModel;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class SwitchFileParserView extends JFrame {
            public SwitchFileParserView() {
                        super("SwitchFileParser");
            }

            public void initializeComponent() {
                        initComponents();
                        this.setLocation(400, 300);
                        this.setSize(508, 250);
                        this.setResizable(false);
                        this.add(sFPPanel);

            }

            private void initComponents() {

                        listModel = new DefaultListModel();
                        descriptionModel = newDefaultListModel();
                        clientList = new javax.swing.JList(listModel);
                        clientDescription = newJList(descriptionModel);
                        clientDescription.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                        listScrollPane = newJScrollPane(clientList);
                        descriptionScrollPane = newJScrollPane(clientDescription);
                        clientDescription.addListSelectionListener(newListSelectionListener() {
                                    public voidvalueChanged(ListSelectionEvent arg0) {
                                                clientDescriptionActionPerformed(arg0);
                                    }
                        });

                        cancelButton = newjavax.swing.JButton();
                        cancelButton.setText("Cancel");
                        cancelButton.addActionListener(newjava.awt.event.ActionListener() {
                                    public voidactionPerformed(java.awt.event.ActionEvent evt) {
                                                cancelButtonActionPerformed(evt);
                                    }
                        });

                        switchToNewModel = newjavax.swing.JRadioButton();
                        switchToOldModel = newjavax.swing.JRadioButton();
                        updateModel = newjavax.swing.JButton();
                        buttonGroup = newjavax.swing.ButtonGroup();

                        buttonGroup.add(switchToNewModel);
                        switchToNewModel.setText("NewModel");
                        switchToNewModel.addActionListener(newjava.awt.event.ActionListener() {
                                    public voidactionPerformed(java.awt.event.ActionEvent evt) {
                                                switchToNewModelActionPerformed(evt);
                                    }
                        });

                        buttonGroup.add(switchToOldModel);
                        switchToOldModel.setText("OldModel");
                        switchToOldModel.addActionListener(newjava.awt.event.ActionListener() {
                                    public voidactionPerformed(java.awt.event.ActionEvent evt) {
                                                switchToOldModelActionPerformed(evt);
                                    }
                        });

                        updateModel.setText("Update");
                        updateModel.addActionListener(newjava.awt.event.ActionListener() {
                                    public voidactionPerformed(java.awt.event.ActionEvent evt) {
                                                updateModelActionPerformed(evt);
                                    }
                        });
                        updateStatus = newjavax.swing.JLabel();
                        updateStatus.setText("UpdateStatus : No Action Performed");

                        clientList.setEnabled(false);

                        javax.swing.GroupLayout layout = newjavax.swing.GroupLayout(sFPPanel);
                        sFPPanel.setLayout(layout);
                        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                                                layout.createSequentialGroup().addContainerGap().addComponent(updateStatus,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,427, Short.MAX_VALUE).addGap(72, 72, 72)).addGroup(
                                                layout.createSequentialGroup().addGap(32,32, 32).addComponent(switchToNewModel).addGap(41, 41, 41)
                                                                        .addComponent(switchToOldModel).addContainerGap(208,Short.MAX_VALUE)).addGroup(
                                                layout.createSequentialGroup().addContainerGap().addComponent(descriptionScrollPane,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,128, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(37,
                                                                        37,37).addComponent(listScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 313,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(21,Short.MAX_VALUE)).addGroup(
                                                layout.createSequentialGroup().addGap(100,100, 100).addComponent(updateModel).addGap(59, 59, 59)
                                                                        .addComponent(cancelButton).addContainerGap(218,Short.MAX_VALUE)));
                        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                                                layout.createSequentialGroup().addContainerGap().addGroup(
                                                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                                                                                                descriptionScrollPane,javax.swing.GroupLayout.PREFERRED_SIZE, 114,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(listScrollPane,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,114, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
                                                                                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                                                                                        switchToNewModel).addComponent(switchToOldModel)).addGap(14,14, 14).addGroup(
                                                                                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                                                                                        updateModel).addComponent(cancelButton)).addPreferredGap(
                                                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(updateStatus)
                                                                        .addContainerGap(24,Short.MAX_VALUE)));

                        updateStatus.setVisible(false);
                        this.addWindowListener(newWindowAdapter() {
                                    public voidwindowClosing(WindowEvent e) {
                                                close();
                                    }
                        });
            }

            public void close() {
                        this.dispose();
            }

            private voidclientDescriptionActionPerformed(ListSelectionEvent ie) {
                        if (ie.getValueIsAdjusting() ==false) {
                                    int index =clientDescription.getSelectedIndex();
                                    selectedClientDescription= clientDescription.getModel().getElementAt(index).toString();
                        }
                        sFPC.setModelAndListOnView(selectedClientDescription);
            }

            private void cancelButtonActionPerformed(java.awt.event.ActionEventevt) {
                        close();
            }

            private voidswitchToNewModelActionPerformed(java.awt.event.ActionEvent evt) {
            }

            private voidswitchToOldModelActionPerformed(java.awt.event.ActionEvent evt) {
            }

            private voidupdateModelActionPerformed(java.awt.event.ActionEvent evt) {
                        if (selectedClientDescription.trim()!= null && selectedClientDescription.trim() != "") {
                                    JRadioButton rb =this.getSelection(buttonGroup);
                                    if (rb.getText() != null&& rb.getText().trim().length() != 0) {
                                                if(rb.getText().trim().equals("New Model")) {
                                                            if(clientState == 1) {
                                                                        alreadyNewModel();
                                                            }else {
                                                                        sFPC.updateModelInDatabaseAndBean(selectedClientDescription.trim(),1);
                                                            }
                                                }
                                                if (rb.getText().trim().equals("OldModel")) {
                                                            if(clientState == 0) {
                                                                        alreadyOldModel();
                                                            }else {
                                                                        sFPC.updateModelInDatabaseAndBean(selectedClientDescription.trim(),0);
                                                            }
                                                }
                                    }
                        } else {
                                    errorClientNotSelectedDialog();
                        }
            }

            void errorClientNotSelectedDialog() {
                        JOptionPane.showMessageDialog(this,"Please select the client", "Info",JOptionPane.INFORMATION_MESSAGE);
            }

            void alreadyNewModel() {
                        JOptionPane.showMessageDialog(this,"Client already on New Model", "Info",JOptionPane.INFORMATION_MESSAGE);
            }

            void alreadyOldModel() {
                        JOptionPane.showMessageDialog(this,"Client already on Old Model", "Info",JOptionPane.INFORMATION_MESSAGE);
            }

            public boolean isSelected(JRadioButton btn) {
                        DefaultButtonModel model =(DefaultButtonModel) btn.getModel();
                        returnmodel.getGroup().isSelected(model);
            }

            public JRadioButton getSelection(ButtonGroupgroup) {
                        for (Enumeration e =group.getElements(); e.hasMoreElements();) {
                                    JRadioButton b =(JRadioButton) e.nextElement();
                                    if (b.getModel() ==group.getSelection()) {
                                                return b;
                                    }
                        }
                        return null;
            }

            public voidsetOtherComponents(SwitchFileParserView sFPView) {
                        sFPC.setInstance(sFPView);
                        sFPC.getAndPopulateClientList();
            }

            public static void main(String args[]) {
                        sFPV = new SwitchFileParserView();
                        sFPV.initializeComponent();
                        sFPV.setOtherComponents(sFPV);
                        sFPV.setVisible(true);
            }

            public static SwitchFileParserView getInstance(){
                        if (sFPV == null) {
                                    sFPV = newSwitchFileParserView();
                        }
                        return sFPV;
            }

            public javax.swing.ButtonGroup               buttonGroup;
            public JList                                                                    clientDescription;
            public javax.swing.JButton                                  cancelButton;
            public javax.swing.JList                          clientList;
            public javax.swing.JRadioButton              switchToNewModel;
            public javax.swing.JRadioButton              switchToOldModel;
            private javax.swing.JButton                                 updateModel;
            public javax.swing.JLabel                                    updateStatus;
            public SwitchFileParserController            sFPC                                                                =new SwitchFileParserController();
            public static SwitchFileParserView          sFPV                                                                =null;
            public JPanel                                                                 sFPPanel                                                          =new JPanel();
            public ArrayList<String>                         listofclients                                            =new ArrayList();
            public ListSelectionModel                                   listSelectionModel;
            public String                                                                  selectedClientDescription           ="";
            public static int                                                  clientState                                                         =0;
            public DefaultListModel                                       descriptionModel;
            public DefaultListModel                                       listModel;
            public JScrollPane                                                         listScrollPane;
            public JScrollPane                                                         descriptionScrollPane;
            static final long                                                  serialVersionUID                                    =-1L;

}

No comments:

Post a Comment