/fin_val/src/production/artem/finance/gui/OrgCatalog.java
Java | 529 lines | 339 code | 59 blank | 131 comment | 29 complexity | 6017d7eb7c69b5b72c201c113a990edc MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, IPL-1.0, Apache-2.0, LGPL-2.1, LGPL-2.0, BSD-3-Clause
- package artem.finance.gui;
-
- import javax.swing.JPanel;
-
- import java.awt.Dimension;
- import java.awt.Rectangle;
-
- import javax.swing.JButton;
- import javax.swing.JInternalFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.ListSelectionModel;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
- import javax.swing.table.DefaultTableColumnModel;
- import javax.swing.table.TableColumn;
-
- import java.rmi.RemoteException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Properties;
-
- import javax.swing.JLabel;
-
- import artem.finance.gui.db.BeansFactory;
- import artem.finance.server.persist.Organization;
- import artem.finance.server.persist.beans.OrganizationBean;
-
- /**
- *
- * @author Burtsev Ivan
- *
- */
-
- public class OrgCatalog extends JInternalFrame {
-
- private static final long serialVersionUID = 1L;
- private JPanel jContentPane = null;
- private JButton addButton = null;
- private JTextField orgNameTextField = null;
- private JTextField orgDescriptionTextField = null;
- private JButton delButton = null;
- private JScrollPane jScrollPane = null;
- private JTable jTable = null;
- private JButton cancelButton = null;
- private JLabel orgLabel = null;
- private JLabel orgDescriptionLabel = null;
- private JButton editButton = null;
- private BeansFactory factory;
- private Organization selectedOrganization;
- private Properties properties;
-
- /**
- * This is the default constructor
- */
- public OrgCatalog(Properties properties) {
- super("",
- false, //resizable
- true, //closable
- false, //maximizable
- true);//iconifiable
-
- this.properties = properties;
- this.factory = BeansFactory.getInstance();
- initialize();
- }
-
- /**
- * This method initializes this
- *
- * @return void
- */
- private void initialize() {
- this.setSize(490, 530);
- this.setContentPane(getJContentPane());
- this.setTitle(properties.getProperty("organizations"));
- }
-
- /**
- * This method initializes jContentPane
- *
- * @return javax.swing.JPanel
- */
- private JPanel getJContentPane()
- {
- if (jContentPane == null)
- {
- orgDescriptionLabel = new JLabel();
- orgDescriptionLabel.setBounds(new Rectangle(195, 499, 225, 16));
- orgDescriptionLabel.setText(properties.getProperty("orgFullName"));
- orgLabel = new JLabel();
- orgLabel.setBounds(new Rectangle(15, 499, 131, 16));
- orgLabel.setText(properties.getProperty("organization"));
- jContentPane = new JPanel();
- jContentPane.setLayout(null);
- jContentPane.setSize(new Dimension(690, 230));
- jContentPane.add(getAddButton(), null);
- jContentPane.add(getOrgNameTextField(), null);
- jContentPane.add(getOrgDescriptionTextField(), null);
- jContentPane.add(getDelButton(), null);
- jContentPane.add(getJScrollPane(), null);
- jContentPane.setOpaque(true);
- jContentPane.add(getCancelButton(), null);
- jContentPane.add(orgLabel, null);
- jContentPane.add(orgDescriptionLabel, null);
- jContentPane.add(getEditButton(), null);
-
- }
- return jContentPane;
- }
-
- /**
- * This method initializes addButton
- *
- * @return javax.swing.JButton
- */
- private JButton getAddButton()
- {
- if (addButton == null)
- {
- addButton = new JButton();
- addButton.setBounds(new Rectangle(15, 466, 140, 26));
- addButton.setText(properties.getProperty("add"));
- addButton.setEnabled(false);
- addButton.setVisible(false);
- addButton.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent e)
- {
- //add new field or update current
- if(orgNameTextField.getText().equals("") || orgNameTextField.getText() == null){
- JOptionPane.showMessageDialog(getJTable(), properties.getProperty("orgNameMissing"));
- return;
- }
-
- if(orgDescriptionTextField.getText().equals("") || orgDescriptionTextField.getText() == null){
- JOptionPane.showMessageDialog(getJTable(), properties.getProperty("orgFullNameMissing"));
- return;
- }
-
- if(selectedOrganization == null )
- {
- try{
- selectedOrganization = new Organization();
-
- selectedOrganization.setName(orgNameTextField.getText());
- selectedOrganization.setDescription(orgDescriptionTextField.getText());
-
- factory.getOrganizationServiceSLSB().saveOrUpdate( new OrganizationBean(selectedOrganization));
- JOptionPane.showMessageDialog(getJTable(), properties.getProperty("organization")+" "+selectedOrganization.getDescription()+properties.getProperty("savedSuccessfully1"));
- cleanTextFields();
- //update jTable
- getDatabaseTableModel().setData(getDataFromDb());
- getDatabaseTableModel().fireTableRowsInserted(getJTable().getRowCount()-1, getJTable().getRowCount()-1);
- //Select the inserted row
- getJTable().getSelectionModel().setLeadSelectionIndex(getJTable().getRowCount());
-
- getJTable().repaint();
-
- selectedOrganization = null;
- }catch(Exception ex){
- JOptionPane.showMessageDialog(getJTable(), properties.getProperty("organization")+" "+properties.getProperty("notSaved1")+ex.getMessage());
- selectedOrganization = null;
- }
- }else{
- try
- {
- selectedOrganization.setName(orgNameTextField.getText());
- selectedOrganization.setDescription(orgDescriptionTextField.getText());
- factory.getOrganizationServiceSLSB().saveOrUpdate( new OrganizationBean(selectedOrganization));
- JOptionPane.showMessageDialog(getJTable(), properties.getProperty("organization")+" "+selectedOrganization.getDescription()+properties.getProperty("updatedSuccessfully1"));
- cleanTextFields();
-
- getDatabaseTableModel().setData(getDataFromDb());
- int selected = getJTable().getSelectionModel().getLeadSelectionIndex();
- getDatabaseTableModel().fireTableRowsUpdated(selected, selected);//.fireTableRowsInserted(getJTable().getRowCount()-1, getJTable().getRowCount());
-
- getJTable().repaint();
-
- selectedOrganization = null;
- }catch(Exception ex){
- JOptionPane.showMessageDialog(getJTable(), properties.getProperty("organization")+" "+properties.getProperty("notUpdated1")+ex.getMessage());
- selectedOrganization = null;
- }
- }
-
- }
- });
- }
- return addButton;
- }
-
- private void cleanTextFields()
- {
- orgNameTextField.setText("");
- orgDescriptionTextField.setText("");
- }
-
- /**
- * This method initializes orgNameTextField
- *
- * @return javax.swing.JTextField
- */
- private JTextField getOrgNameTextField()
- {
- if (orgNameTextField == null)
- {
- orgNameTextField = new JTextField();
- orgNameTextField.setBounds(new Rectangle(15, 524, 165, 20));
- }
- return orgNameTextField;
- }
-
- /**
- * This method initializes orgDescriptionTextField
- *
- * @return javax.swing.JTextField
- */
- private JTextField getOrgDescriptionTextField()
- {
- if (orgDescriptionTextField == null)
- {
- orgDescriptionTextField = new JTextField();
- orgDescriptionTextField.setBounds(new Rectangle(196, 525, 275, 20));
- }
- return orgDescriptionTextField;
- }
-
- /**
- * This method initializes updButton
- *
- * @return javax.swing.JButton
- */
- private JButton getDelButton()
- {
- if (delButton == null)
- {
- delButton = new JButton();
- delButton.setBounds(new Rectangle(165, 466, 140, 26));
- delButton.setText(properties.getProperty("delete"));
- delButton.setEnabled(false);
- delButton.setVisible(false);
- delButton.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent e) {
-
- getOrgNameTextField().setText( "" );
- getOrgDescriptionTextField().setText( "" );
-
- if(selectedOrganization == null )
- {
- JOptionPane.showMessageDialog(getJTable(), properties.getProperty("orgNotSelectedToDelete"));
- }
- else
- {
- try
- {
- factory.getOrganizationServiceSLSB().delete(new OrganizationBean(selectedOrganization));
- JOptionPane.showMessageDialog(getJTable(), properties.getProperty("organization")+" "+selectedOrganization.getDescription()+properties.getProperty("deleted1"));
- selectedOrganization = null;
-
- //update jTable
- getDatabaseTableModel().setData(getDataFromDb());
- int selectedRow = getJTable().getSelectionModel().getLeadSelectionIndex();
- getDatabaseTableModel().fireTableRowsDeleted(selectedRow, selectedRow);
- }
- catch(Exception ex)
- {
- JOptionPane.showMessageDialog(getJTable(), selectedOrganization.getDescription()+" "+properties.getProperty("notDeleted"));
- }
- }
- }
- });
- }
- return delButton;
- }
-
- /**
- * This method initializes jScrollPane
- *
- * @return javax.swing.JScrollPane
- */
- private JScrollPane getJScrollPane()
- {
- if (jScrollPane == null)
- {
- jScrollPane = new JScrollPane();
- jScrollPane.setBounds(new Rectangle(13, 15, 455, 446));
- jScrollPane.setViewportView(getJTable());
- }
- return jScrollPane;
- }
-
- private List<Object> getDataFromDb()
- {
- List<Object> data = new ArrayList<Object>();
- List<OrganizationBean> organizations;
- try
- {
- organizations = factory.getOrganizationServiceSLSB().findAll();
-
- for(int i = 0; i< organizations.size(); i++){
- OrganizationBean organization = organizations.get(i);
- List<Object> raw = new ArrayList<Object>();
- raw.add(organization.getOrganization().getId());
- raw.add(organization.getOrganization().getName());
- raw.add(organization.getOrganization().getDescription());
- //raw.add(new JRadioButton());
- data.add(raw);
-
- }
- } catch (RemoteException e)
- {
- e.printStackTrace();
- }
- return data;
- }
-
- /**
- * Used for lazy data loading to the table. Fills the data only after first frame shoving
- */
- public void fillOrgsTable()
- {
- getDatabaseTableModel().setData(getDataFromDb());
- getDatabaseTableModel().fireTableDataChanged();
- }
-
- /**
- * This method initializes jTable
- *
- * @return javax.swing.JTable
- */
- private JTable getJTable()
- {
- if (jTable == null)
- {
-
- DatabaseTableModel tableModel = new DatabaseTableModel(true);
- final List<String> names = new ArrayList<String>();
- names.add("");
- names.add(properties.getProperty("organization"));
- names.add(properties.getProperty("desciption"));
-
- tableModel.setColumnNames(names);
- final List<Class> types = new ArrayList<Class>(0);
- types.add(Long.class);
- types.add(String.class);
- types.add(String.class);
- //types.add(JRadioButton.class);
- tableModel.setColumnTypes(types);
-
- //Gets all the organizations during loading!
- //tableModel.setData(getDataFromDb());
-
- jTable = new JTable(tableModel);
-
- DefaultTableColumnModel columns = (DefaultTableColumnModel) jTable.getColumnModel();
-
- TableColumn tc = columns.getColumn(0);
- tc.setPreferredWidth(0);
- tc = columns.getColumn(1);
- tc.setPreferredWidth(315);
- tc = columns.getColumn(2);
- tc.setPreferredWidth(815);
-
- jTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
- jTable.setBounds(new Rectangle(15, 15, 455, 420));
- jTable.setFillsViewportHeight(true);
- //User can select only one row.
- jTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
-
- @Override
- public void valueChanged(ListSelectionEvent e) {
- int selectedRow = getJTable().getSelectionModel().getLeadSelectionIndex();
-
- Long id = (Long)getJTable().getModel().getValueAt(selectedRow, 0);
- String name = (String)getJTable().getModel().getValueAt(selectedRow, 1);
- String description = (String)getJTable().getModel().getValueAt(selectedRow, 2);
-
- selectedOrganization = new Organization();
- selectedOrganization.setId(id);
- selectedOrganization.setName(name);
- selectedOrganization.setDescription(description);
-
- getOrgNameTextField().setText( name );
- getOrgDescriptionTextField().setText( description );
- }
-
- });
- }
- return jTable;
- }
-
- private DatabaseTableModel getDatabaseTableModel(){
- DatabaseTableModel model = (DatabaseTableModel)jTable.getModel();
- return model;
- }
-
- /**
- * This method initializes cancelButton
- *
- * @return javax.swing.JButton
- */
- private JButton getCancelButton() {
- if (cancelButton == null) {
- cancelButton = new JButton();
- cancelButton.setBounds(new Rectangle(315, 466, 145, 26));
- cancelButton.setText(properties.getProperty("cancel"));
- cancelButton.setEnabled(false);
- cancelButton.setVisible(false);
- cancelButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- editButton.setEnabled(true);
- editButton.setVisible(true);
- addButton.setVisible(false);
- addButton.setEnabled(false);
- addButton.setName(properties.getProperty("add"));
- delButton.setVisible(false);
- delButton.setEnabled(false);
- cancelButton.setVisible(false);
- cancelButton.setEnabled(false);
- setSize(490, 535);
- orgNameTextField.setText(null);
- orgDescriptionTextField.setText(null);
-
-
- //Clear highlighting the selected row in the table.
- int leadSelectionInd = getJTable().getSelectionModel().getLeadSelectionIndex();
- getJTable().getSelectionModel().removeSelectionInterval(leadSelectionInd, leadSelectionInd);
- }
- });
- }
- return cancelButton;
- }
-
-
-
-
- /**
- * This method initializes editButton1
- *
- * @return javax.swing.JButton
- */
- private JButton getEditButton() {
- if (editButton == null) {
- editButton = new JButton();
- editButton.setBounds(new Rectangle(15, 466, 140, 26));
- editButton.setText(properties.getProperty("edit"));
- editButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- editButton.setEnabled(false);
- editButton.setVisible(false);
- addButton.setVisible(true);
- addButton.setEnabled(true);
- delButton.setVisible(true);
- delButton.setEnabled(true);
- cancelButton.setVisible(true);
- cancelButton.setEnabled(true);
- setSize(490, 585);
- }
- });
- }
- return editButton;
- }
-
- // /**
- // * This class used to render RadioButtons in the cells of jTable
- // *
- // * @author Andrey Sirak
- // */
- //class RadioButtonRenderer implements TableCellRenderer {
- // public Component getTableCellRendererComponent(JTable table, Object value,
- // boolean isSelected, boolean hasFocus, int row, int column) {
- // if (value == null)
- // return null;
- // if (hasFocus) {
- // //?????? ?????? ? ???? ??? ?????????????? ? ???????? ?????? ???????
- // orgNameTextField.setText(table.getValueAt(row, 0).toString());
- // orgDescriptionTextField.setText(table.getValueAt(row, 1).toString());
- // delButton.setEnabled(true);
- // addButton.setName("????????");
- // };
- // return (Component) value;
- // }
- // }
- ///**
- // * This class used to edit RadioButtons in the cells of jTable
- // *
- // * @author Andrey Sirak
- // */
- // class RadioButtonEditor extends DefaultCellEditor implements ItemListener {
- // /**
- // *
- // */
- // private static final long serialVersionUID = 1L;
- // private JRadioButton button;
- //
- //
- // public RadioButtonEditor(JCheckBox checkBox) {
- // super(checkBox);
- // }
- //
- // public Component getTableCellEditorComponent(JTable table, Object value,
- // boolean isSelected, int row, int column) {
- // if (value == null)
- // return null;
- // button = (JRadioButton) value;
- // button.addItemListener(this);
- //
- // return (Component) value;
- // }
- //
- // public Object getCellEditorValue() {
- // button.removeItemListener(this);
- //
- // return button;
- // }
- //
- // public void itemStateChanged(ItemEvent e) {
- // if (button.isSelected()) button.setSelected(false);
- // else button.setSelected(true);
- // super.fireEditingStopped();
- //
- // }
- // }
- } // @jve:decl-index=0:visual-constraint="8,-87"