Advertisement
elsemTim

main

Nov 17th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 39.91 KB | None | 0 0
  1. //package clientServerApp;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.*;
  6. import javax.swing.table.TableModel;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Properties;
  14.  
  15.  
  16. public class FirebirdTWJJ {
  17.  
  18.     /**
  19.      * @param args
  20.      */
  21.     //global var
  22.     static DBConnect dbConn;
  23.  
  24.     static JFrame frame;
  25.     /// Depart controls
  26.     static JTable depGrid;
  27.  
  28.     static JDialog addDepartDialog;
  29.     static JDialog editDepartDialog;
  30.  
  31.     static JButton addDepButton;
  32.     static JButton delDepButton;
  33.     static JButton editDepButton;
  34.  
  35.     static JScrollPane departTableScroolPage;
  36.     /// End Depart controls
  37.  
  38.     /// Position controls
  39.     static JTable posGrid;
  40.  
  41.     static JDialog addPositDialog;
  42.     static JDialog editPositDialog;
  43.  
  44.     static JButton addPosButton;
  45.     static JButton delPosButton;
  46.     static JButton editPosButton;
  47.  
  48.     static JScrollPane positTableScroolPage;
  49.     /// End Position controls
  50.  
  51.     /// Employee controls
  52.     static JTable empGrid;
  53.  
  54.     static JDialog addEmplDialog;
  55.     static JDialog editEmplDialog;
  56.  
  57.     static JButton addEmpButton;
  58.     static JButton delEmpButton;
  59.     static JButton editEmpButton;
  60.  
  61.     static JScrollPane emplTableScroolPage;
  62.     /// End Employee controls
  63.  
  64.     static JRadioButton empRB;
  65.     static JRadioButton posRB;
  66.     static JRadioButton depRB;
  67.  
  68.  
  69.  
  70.     //helpers
  71.     static void InitDB(Properties p) throws Exception{
  72.         dbConn = DBConnect.getInstance(p);
  73.     }
  74.  
  75.     static DepartmentTable GetDepartData() throws SQLException {
  76.         List<Department> dep = null;
  77.         try {
  78.             dep = dbConn.SelectDep();
  79.         } catch (SQLException e) {
  80.             e.printStackTrace();
  81.             throw e;
  82.         }
  83.         DepartmentTable depTable = new DepartmentTable();
  84.         String[] tmp = new String[depTable.getColumnCount()];
  85.         for (int i = 0; i < dep.size(); i++) {
  86.             Department d = dep.get(i);
  87.             tmp[0] = "" + d.GetID();
  88.             tmp[1] = d.GetNameDepartment();
  89.             tmp[2] = d.GetMail();
  90.             tmp[3] = d.GetPhone();
  91.             depTable.AddRow(tmp);
  92.         }
  93.         return depTable;
  94.     }
  95.  
  96.     static PositionTable GetPositionData() throws SQLException {
  97.         List<Position> pos = null;
  98.         try {
  99.             pos = dbConn.SelectPos();
  100.         } catch (SQLException e) {
  101.             e.printStackTrace();
  102.             throw e;
  103.         }
  104.         PositionTable posTable = new PositionTable();
  105.         String[] tmp = new String[posTable.getColumnCount()];
  106.         for (int i = 0; i < pos.size(); i++) {
  107.             Position p = pos.get(i);
  108.             tmp[0] = "" + p.GetID();
  109.             tmp[1] = p.GetNamePosition();
  110.             tmp[2] = ""+p.GetSalary();
  111.             posTable.AddRow(tmp);
  112.         }
  113.         return posTable;
  114.     }
  115.  
  116.     static EmployeeTable GetEmployeeData() throws SQLException {
  117.         List<Employee> emp = null;
  118.         try {
  119.             emp = dbConn.SelectEmp();
  120.         } catch (SQLException e) {
  121.             e.printStackTrace();
  122.             throw e;
  123.         }
  124.         EmployeeTable empTable = new EmployeeTable();
  125.         String[] tmp = new String[empTable.getColumnCount()];
  126.         for (int i = 0; i < emp.size(); i++) {
  127.             Employee e = emp.get(i);
  128.             tmp[0] = "" + e.getID();
  129.             tmp[1] = e.GetFirstName();
  130.             tmp[2] = e.GetLastName();
  131.             tmp[3] = e.GetNameDep();
  132.             tmp[4] = e.GetNamePos();
  133.             tmp[5] =""+e.getSalary();
  134.             empTable.AddRow(tmp);
  135.         }
  136.         return empTable;
  137.     }
  138.  
  139.     static List<ComboItem> GetDepItems () {
  140.         try {
  141.             return dbConn.GetDepItems();
  142.         } catch (SQLException e) {
  143.             e.printStackTrace();
  144.         }
  145.         return new ArrayList<ComboItem> ();
  146.     }
  147.  
  148.     static List<ComboItem> GetPosItems () {
  149.         try {
  150.             return dbConn.GetPosItems();
  151.         } catch (SQLException e) {
  152.             e.printStackTrace();
  153.         }
  154.         return new ArrayList<ComboItem> ();
  155.     }
  156.  
  157.     //создаем новый Dialog при нажатие на кнопку Add для Departmena
  158.     static void InitAddDepartDialog(){
  159.         addDepartDialog = new JDialog(frame,"Добавить отдел",true);
  160.         addDepartDialog.setLocationRelativeTo(frame);
  161.         addDepartDialog.setSize(new Dimension(500,300));
  162.         addDepartDialog.setLayout(new GridBagLayout());
  163.         JTextField nameField=new JTextField(20);
  164.         JTextField phoneField=new JTextField(20);
  165.         JTextField mailField=new JTextField(20);
  166.         JButton okButton = new JButton("OK");
  167.         okButton.addActionListener(new ActionListener() {
  168.             @Override
  169.             public void actionPerformed(ActionEvent e) {
  170.                 String m=mailField.getText().trim();
  171.                 String p=phoneField.getText().trim();
  172.                 String n=nameField.getText().trim();
  173.                 if (m.length()>0 && p.length()>0 && n.length()>0) {
  174.                     try {
  175.                         dbConn.InsertDep(n, m, p);
  176.  
  177.                     } catch (SQLException e1) {
  178.                         e1.printStackTrace();
  179.                     }
  180.  
  181.                     try {
  182.                         addDepartDialog.setVisible(false);
  183.                         depGrid.setModel(GetDepartData());
  184.                         //DefaultTableModel model = (DefaultTableModel) depGrid.getModel();
  185.                         //model.fireTableDataChanged();
  186.                     } catch (SQLException e1) {
  187.                         e1.printStackTrace();
  188.                     }
  189.                 }
  190.                 addDepartDialog.setVisible(false);
  191.             }
  192.         });
  193.         addDepartDialog.add( new JLabel("Имя отдела: "), new GridBagConstraints(0,0,1,1,1,1,
  194.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  195.                 new Insets(2,2,2,2),0,0));
  196.  
  197.         addDepartDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
  198.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  199.                 new Insets(2,2,2,2),0,0));
  200.  
  201.         addDepartDialog.add( new JLabel("Почта: "), new GridBagConstraints(0,1,1,1,1,1,
  202.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  203.                 new Insets(2,2,2,2),0,0));
  204.  
  205.         addDepartDialog.add(mailField, new GridBagConstraints(1,1,1,1,1,1,
  206.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  207.                 new Insets(2,2,2,2),0,0));
  208.  
  209.         addDepartDialog.add( new JLabel("Телефон: "), new GridBagConstraints(0,2,1,1,1,1,
  210.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  211.                 new Insets(2,2,2,2),0,0));
  212.  
  213.         addDepartDialog.add(phoneField, new GridBagConstraints(1,2,1,1,1,1,
  214.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  215.                 new Insets(2,2,2,2),0,0));
  216.  
  217.         addDepartDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
  218.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  219.                 new Insets(2,2,2,2),0,0));
  220.  
  221.         addDepartDialog.pack();
  222.     }
  223.  
  224.     //создаем новый Dialog при нажатие на кнопку Add для Positiona
  225.     static void InitAddPositionDialog(){
  226.         addPositDialog = new JDialog(frame,"Добавить должность",true);
  227.         addPositDialog.setLocationRelativeTo(frame);
  228.         addPositDialog.setSize(new Dimension(500,300));
  229.         addPositDialog.setLayout(new GridBagLayout());
  230.         JTextField nameField=new JTextField(20);
  231.         JTextField salaryField=new JTextField(20);
  232.         JButton okButton = new JButton("OK");
  233.         okButton.addActionListener(new ActionListener() {
  234.             @Override
  235.             public void actionPerformed(ActionEvent e) {
  236.  
  237.                 String posn=nameField.getText().trim();
  238.                 String ss=salaryField.getText().trim();
  239.  
  240.                 if (ss.length()>0 && posn.length()>0) {
  241.                     try {
  242.                         int sal = Integer.parseInt(ss);
  243.                         dbConn.InsertPos(sal,posn);
  244.  
  245.                     } catch (SQLException e1) {
  246.                         e1.printStackTrace();
  247.                     }
  248.  
  249.                     try {
  250.                         addPositDialog.setVisible(false);
  251.                         posGrid.setModel(GetPositionData());
  252.                     } catch (SQLException e1) {
  253.                         e1.printStackTrace();
  254.                     }
  255.                 }
  256.                 addPositDialog.setVisible(false);
  257.             }
  258.         });
  259.         addPositDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,0,1,1,1,1,
  260.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  261.                 new Insets(2,2,2,2),0,0));
  262.  
  263.         addPositDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
  264.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  265.                 new Insets(2,2,2,2),0,0));
  266.  
  267.         addPositDialog.add( new JLabel("Зарплата: "), new GridBagConstraints(0,1,1,1,1,1,
  268.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  269.                 new Insets(2,2,2,2),0,0));
  270.  
  271.         addPositDialog.add(salaryField, new GridBagConstraints(1,1,1,1,1,1,
  272.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  273.                 new Insets(2,2,2,2),0,0));
  274.  
  275.         addPositDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
  276.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  277.                 new Insets(2,2,2,2),0,0));
  278.  
  279.         addPositDialog.pack();
  280.     }
  281.  
  282.     //создаем новый Dialog при нажатие на кнопку Add для Employee
  283.     static void InitAddEmployeeDialog(){
  284.         addEmplDialog = new JDialog(frame,"Добавить сотрудника",true);
  285.         addEmplDialog.setLocationRelativeTo(frame);
  286.         addEmplDialog.setSize(new Dimension(500,300));
  287.         addEmplDialog.setLayout(new GridBagLayout());
  288.  
  289.         JTextField nameField=new JTextField(20);
  290.         JTextField lnameField=new JTextField(20);
  291.         JComboBox<ComboItem> pos= new JComboBox<ComboItem>();
  292.         List <ComboItem> lp = GetPosItems();
  293.         for (int i=0;i<lp.size();i++)
  294.         {
  295.             pos.addItem(lp.get(i));
  296.         }
  297.         JComboBox<ComboItem> dep= new JComboBox<ComboItem>();
  298.         List <ComboItem> ld = GetDepItems();
  299.         for (int i=0;i<ld.size();i++)
  300.         {
  301.             dep.addItem(ld.get(i));
  302.         }
  303.         JButton okButton = new JButton("OK");
  304.         okButton.addActionListener(new ActionListener() {
  305.             @Override
  306.             public void actionPerformed(ActionEvent e) {
  307.  
  308.                 String n=nameField.getText().trim();
  309.                 String ln=lnameField.getText().trim();
  310.                 int did= ((ComboItem)dep.getSelectedItem()).getValue();
  311.                 int pid= ((ComboItem)pos.getSelectedItem()).getValue();
  312.                 if ( ln.length()>0 && n.length()>0) {
  313.                     try {
  314.                         dbConn.InsertEmp(n,ln, did,pid);
  315.  
  316.                     } catch (SQLException e1) {
  317.                         e1.printStackTrace();
  318.                     }
  319.  
  320.                     try {
  321.                         addEmplDialog.setVisible(false);
  322.                         empGrid.setModel(GetEmployeeData());
  323.                     } catch (SQLException e1) {
  324.                         e1.printStackTrace();
  325.                     }
  326.                 }
  327.                 addEmplDialog.setVisible(false);
  328.  
  329.             }
  330.         });
  331.         addEmplDialog.add( new JLabel("Имя: "), new GridBagConstraints(0,0,1,1,1,1,
  332.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  333.                 new Insets(2,2,2,2),0,0));
  334.  
  335.         addEmplDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
  336.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  337.                 new Insets(2,2,2,2),0,0));
  338.  
  339.         addEmplDialog.add( new JLabel("Фамилия: "), new GridBagConstraints(0,1,1,1,1,1,
  340.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  341.                 new Insets(2,2,2,2),0,0));
  342.  
  343.         addEmplDialog.add(lnameField, new GridBagConstraints(1,1,1,1,1,1,
  344.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  345.                 new Insets(2,2,2,2),0,0));
  346.  
  347.         addEmplDialog.add( new JLabel("Отдел: "), new GridBagConstraints(0,2,1,1,1,1,
  348.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  349.                 new Insets(2,2,2,2),0,0));
  350.  
  351.         addEmplDialog.add(dep, new GridBagConstraints(1,2,1,1,1,1,
  352.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  353.                 new Insets(2,2,2,2),0,0));
  354.  
  355.         addEmplDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,3,1,1,1,1,
  356.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  357.                 new Insets(2,2,2,2),0,0));
  358.  
  359.         addEmplDialog.add(pos, new GridBagConstraints(1,3,1,1,1,1,
  360.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  361.                 new Insets(2,2,2,2),0,0));
  362.  
  363.         addEmplDialog.add(okButton, new GridBagConstraints(1,4,1,1,1,1,
  364.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  365.                 new Insets(2,2,2,2),0,0));
  366.  
  367.         addEmplDialog.pack();
  368.     }
  369.  
  370.     static void InitEditDepartDialog(){
  371.         editDepartDialog = new JDialog(frame,"Изменить отдел",true);
  372.         editDepartDialog.setLocationRelativeTo(frame);
  373.         editDepartDialog.setSize(new Dimension(500,300));
  374.         editDepartDialog.setLayout(new GridBagLayout());
  375.         JTextField nameField=new JTextField(20);
  376.         JTextField phoneField=new JTextField(20);
  377.         JTextField mailField=new JTextField(20);
  378.         JTextField idField=new JTextField(20);
  379.         idField.setVisible(false);
  380.         JButton okButton = new JButton("OK");
  381.         okButton.addActionListener(new ActionListener() {
  382.             @Override
  383.             public void actionPerformed(ActionEvent e) {
  384.                 String m=mailField.getText().trim();
  385.                 String p=phoneField.getText().trim();
  386.                 String n=nameField.getText().trim();
  387.                 String sid=idField.getText().trim();
  388.                 int id = Integer.parseInt(sid);
  389.                 if (m.length()>0 && p.length()>0 && n.length()>0) {
  390.                     try {
  391.                         dbConn.UpdateDep(id, n, m, p);
  392.  
  393.                     } catch (SQLException e1) {
  394.                         e1.printStackTrace();
  395.                     }
  396.  
  397.                     try {
  398.                         editDepartDialog.setVisible(false);
  399.                         depGrid.setModel(GetDepartData());
  400.                         //DefaultTableModel model = (DefaultTableModel) depGrid.getModel();
  401.                         //model.fireTableDataChanged();
  402.                     } catch (SQLException e1) {
  403.                         e1.printStackTrace();
  404.                     }
  405.                 }
  406.                 editDepartDialog.setVisible(false);
  407.             }
  408.         });
  409.         editDepartDialog.add( new JLabel("Имя отдела: "), new GridBagConstraints(0,0,1,1,1,1,
  410.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  411.                 new Insets(2,2,2,2),0,0));
  412.  
  413.         editDepartDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
  414.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  415.                 new Insets(2,2,2,2),0,0));
  416.  
  417.         editDepartDialog.add( new JLabel("Почта: "), new GridBagConstraints(0,1,1,1,1,1,
  418.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  419.                 new Insets(2,2,2,2),0,0));
  420.  
  421.         editDepartDialog.add(mailField, new GridBagConstraints(1,1,1,1,1,1,
  422.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  423.                 new Insets(2,2,2,2),0,0));
  424.  
  425.         editDepartDialog.add( new JLabel("Телефон: "), new GridBagConstraints(0,2,1,1,1,1,
  426.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  427.                 new Insets(2,2,2,2),0,0));
  428.  
  429.         editDepartDialog.add(phoneField, new GridBagConstraints(1,2,1,1,1,1,
  430.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  431.                 new Insets(2,2,2,2),0,0));
  432.  
  433.         editDepartDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
  434.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  435.                 new Insets(2,2,2,2),0,0));
  436.  
  437.         editDepartDialog.add(idField, new GridBagConstraints(1,4,1,1,1,1,
  438.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  439.                 new Insets(2,2,2,2),0,0));
  440.  
  441.  
  442.         editDepartDialog.pack();
  443.     }
  444.  
  445.     static void InitEditPositDialog(){
  446.         editPositDialog = new JDialog(frame,"Изменить должность",true);
  447.         editPositDialog.setLocationRelativeTo(frame);
  448.         editPositDialog.setSize(new Dimension(500,300));
  449.         editPositDialog.setLayout(new GridBagLayout());
  450.         JTextField nameField=new JTextField(20);
  451.         JTextField salaryField=new JTextField(20);
  452.         JTextField idField=new JTextField(20);
  453.         idField.setVisible(false);
  454.         JButton okButton = new JButton("OK");
  455.         okButton.addActionListener(new ActionListener() {
  456.             @Override
  457.             public void actionPerformed(ActionEvent e) {
  458.                 String n=nameField.getText().trim();
  459.                 String s=salaryField.getText().trim();
  460.                 String sid=idField.getText().trim();
  461.                 int id = Integer.parseInt(sid);
  462.                 if (s.length()>0 && n.length()>0) {
  463.                     try {
  464.                         dbConn.UpdatePos(Integer.parseInt(s),n,id);
  465.  
  466.                     } catch (SQLException e1) {
  467.                         e1.printStackTrace();
  468.                     }
  469.  
  470.                     try {
  471.                         editPositDialog.setVisible(false);
  472.                         posGrid.setModel(GetPositionData());
  473.                     } catch (SQLException e1) {
  474.                         e1.printStackTrace();
  475.                     }
  476.                 }
  477.                 editPositDialog.setVisible(false);
  478.             }
  479.         });
  480.  
  481.         editPositDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,0,1,1,1,1,
  482.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  483.                 new Insets(2,2,2,2),0,0));
  484.  
  485.         editPositDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
  486.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  487.                 new Insets(2,2,2,2),0,0));
  488.  
  489.         editPositDialog.add( new JLabel("Зарплата: "), new GridBagConstraints(0,1,1,1,1,1,
  490.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  491.                 new Insets(2,2,2,2),0,0));
  492.  
  493.         editPositDialog.add(salaryField, new GridBagConstraints(1,1,1,1,1,1,
  494.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  495.                 new Insets(2,2,2,2),0,0));
  496.  
  497.  
  498.  
  499.         editPositDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
  500.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  501.                 new Insets(2,2,2,2),0,0));
  502.  
  503.         editPositDialog.add(idField, new GridBagConstraints(1,2,1,1,1,1,
  504.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  505.                 new Insets(2,2,2,2),0,0));
  506.  
  507.         editPositDialog.pack();
  508.     }
  509.  
  510.     static void InitEditEmployeeDialog(){
  511.         addEmplDialog = new JDialog(frame,"Добавить сотрудника",true);
  512.         addEmplDialog.setLocationRelativeTo(frame);
  513.         addEmplDialog.setSize(new Dimension(500,300));
  514.         addEmplDialog.setLayout(new GridBagLayout());
  515.  
  516.         JTextField nameField=new JTextField(20);
  517.         JTextField lnameField=new JTextField(20);
  518.         JTextField idField=new JTextField(20);
  519.         JComboBox<ComboItem> pos= new JComboBox<ComboItem>();
  520.         List <ComboItem> lp = GetPosItems();
  521.         for (int i=0;i<lp.size();i++)
  522.         {
  523.             pos.addItem(lp.get(i));
  524.         }
  525.         JComboBox<ComboItem> dep= new JComboBox<ComboItem>();
  526.         List <ComboItem> ld = GetDepItems();
  527.         for (int i=0;i<ld.size();i++)
  528.         {
  529.             dep.addItem(ld.get(i));
  530.         }
  531.         JButton okButton = new JButton("OK");
  532.         okButton.addActionListener(new ActionListener() {
  533.             @Override
  534.             public void actionPerformed(ActionEvent e) {
  535.  
  536.                 String n=nameField.getText().trim();
  537.                 String ln=lnameField.getText().trim();
  538.                 int did= ((ComboItem)dep.getSelectedItem()).getValue();
  539.                 int pid= ((ComboItem)pos.getSelectedItem()).getValue();
  540.                 String sid=idField.getText().trim();
  541.                 int id = Integer.parseInt(sid);
  542.                 if ( ln.length()>0 && n.length()>0) {
  543.                     try {
  544.                         dbConn.UpdateEmp(n,ln, did,pid,id);
  545.  
  546.                     } catch (SQLException e1) {
  547.                         e1.printStackTrace();
  548.                     }
  549.  
  550.                     try {
  551.                         addEmplDialog.setVisible(false);
  552.                         empGrid.setModel(GetEmployeeData());
  553.                     } catch (SQLException e1) {
  554.                         e1.printStackTrace();
  555.                     }
  556.                 }
  557.                 addEmplDialog.setVisible(false);
  558.  
  559.             }
  560.         });
  561.         addEmplDialog.add( new JLabel("Имя: "), new GridBagConstraints(0,0,1,1,1,1,
  562.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  563.                 new Insets(2,2,2,2),0,0));
  564.  
  565.         addEmplDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
  566.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  567.                 new Insets(2,2,2,2),0,0));
  568.  
  569.         addEmplDialog.add( new JLabel("Фамилия: "), new GridBagConstraints(0,1,1,1,1,1,
  570.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  571.                 new Insets(2,2,2,2),0,0));
  572.  
  573.         addEmplDialog.add(lnameField, new GridBagConstraints(1,1,1,1,1,1,
  574.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  575.                 new Insets(2,2,2,2),0,0));
  576.  
  577.         addEmplDialog.add( new JLabel("Отдел: "), new GridBagConstraints(0,2,1,1,1,1,
  578.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  579.                 new Insets(2,2,2,2),0,0));
  580.  
  581.         addEmplDialog.add(dep, new GridBagConstraints(1,2,1,1,1,1,
  582.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  583.                 new Insets(2,2,2,2),0,0));
  584.  
  585.         addEmplDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,3,1,1,1,1,
  586.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  587.                 new Insets(2,2,2,2),0,0));
  588.  
  589.         addEmplDialog.add(pos, new GridBagConstraints(1,3,1,1,1,1,
  590.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  591.                 new Insets(2,2,2,2),0,0));
  592.  
  593.         addEmplDialog.add(okButton, new GridBagConstraints(1,4,1,1,1,1,
  594.                 GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
  595.                 new Insets(2,2,2,2),0,0));
  596.  
  597.         addEmplDialog.pack();
  598.     }
  599.  
  600.     static void InitDialogs(){
  601.         InitAddDepartDialog();
  602.         InitEditDepartDialog();
  603.         InitAddPositionDialog();
  604.         InitEditPositDialog();
  605.         InitAddEmployeeDialog();
  606.         InitEditEmployeeDialog();
  607.     }
  608. ///////////Работа со скрытием элементов
  609. //dep
  610.     static void ShowDepartment() throws SQLException {
  611.         HidePosition();
  612.         HideEmployee();
  613.         frame.add(departTableScroolPage, new GridBagConstraints(0, 0, 3, 1, 0, 0,
  614.                 GridBagConstraints.NORTH,
  615.                 GridBagConstraints.BOTH,
  616.                 new Insets(1, 1, 1, 1), 0, 0));
  617.         depGrid.setModel(GetDepartData());
  618.         depGrid.setVisible(true);
  619.         addDepButton.setVisible(true);
  620.         delDepButton.setVisible(true);
  621.         editDepButton.setVisible(true);
  622.         departTableScroolPage.setVisible(true);
  623.     }
  624.  
  625.     static void HideDepartment() throws SQLException{
  626.         departTableScroolPage.setVisible(false);
  627.         frame.remove(departTableScroolPage);
  628.         depGrid.setVisible(false);
  629.         addDepButton.setVisible(false);
  630.         delDepButton.setVisible(false);
  631.         editDepButton.setVisible(false);
  632.     }
  633. //pos
  634.     static void ShowPosition() throws SQLException{
  635.         HideDepartment();
  636.         HideEmployee();
  637.         frame.add(positTableScroolPage, new GridBagConstraints(0, 0, 3, 1, 0, 0,
  638.                 GridBagConstraints.NORTH,
  639.                 GridBagConstraints.BOTH,
  640.                 new Insets(1, 1, 1, 1), 0, 0));
  641.         posGrid.setModel(GetPositionData());
  642.         posGrid.setVisible(true);
  643.         addPosButton.setVisible(true);
  644.         delPosButton.setVisible(true);
  645.         editPosButton.setVisible(true);
  646.         positTableScroolPage.setVisible(true);
  647.     }
  648.  
  649.     static void HidePosition() throws SQLException{
  650.         positTableScroolPage.setVisible(false);
  651.         frame.remove(positTableScroolPage);
  652.         posGrid.setVisible(false);
  653.         addPosButton.setVisible(false);
  654.         delPosButton.setVisible(false);
  655.         editPosButton.setVisible(false);
  656.     }
  657. //emp
  658.     static void ShowEmployee() throws SQLException{
  659.         HidePosition();
  660.         HideDepartment();
  661.         frame.add(emplTableScroolPage, new GridBagConstraints(0, 0, 3, 1, 0, 0,
  662.                 GridBagConstraints.NORTH,
  663.                 GridBagConstraints.BOTH,
  664.                 new Insets(1, 1, 1, 1), 0, 0));
  665.         empGrid.setModel(GetEmployeeData());
  666.         empGrid.setVisible(true);
  667.         addEmpButton.setVisible(true);
  668.         delEmpButton.setVisible(true);
  669.         editEmpButton.setVisible(true);
  670.         emplTableScroolPage.setVisible(true);
  671.     }
  672.  
  673.     static void HideEmployee() throws SQLException{
  674.         emplTableScroolPage.setVisible(false);
  675.         frame.remove(emplTableScroolPage);
  676.         empGrid.setVisible(false);
  677.         addEmpButton.setVisible(false);
  678.         delEmpButton.setVisible(false);
  679.         editEmpButton.setVisible(false);
  680.     }
  681.  
  682. ///////
  683.     static void ShowFrame()
  684.     {
  685.         frame.pack();
  686.         frame.setVisible(true);
  687.     }
  688.     static void InitFrame(){
  689.         frame = new JFrame("Учет сотрудников предприятия");
  690.         frame.setSize(new Dimension(1000, 1000));
  691.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  692.         frame.setLocationRelativeTo(null);
  693.         frame.setLayout(new GridBagLayout());
  694.  
  695.         ButtonGroup group = new ButtonGroup();
  696.         //выполняем инизиализацию и отрисовку для
  697.         //Department
  698.          depRB = new JRadioButton("Отдел",true);
  699.  
  700.         depRB.addActionListener(new ActionListener() {
  701.             @Override
  702.             public void actionPerformed(ActionEvent e) {
  703.                 try {
  704.                     ShowDepartment();
  705.                 }
  706.                 catch (SQLException ex)
  707.                 {
  708.                     JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
  709.                             JOptionPane.ERROR_MESSAGE);
  710.                     return;
  711.                 }
  712.             }
  713.         });
  714.         group.add(depRB);
  715.         //////
  716.  
  717.         //выполняем инизиализацию и отрисовку для
  718.         //Position
  719.          posRB = new JRadioButton("Должность",false);
  720.  
  721.         posRB.addActionListener(new ActionListener() {
  722.             @Override
  723.             public void actionPerformed(ActionEvent e) {
  724.                 try {
  725.                     ShowPosition();
  726.                 }
  727.                 catch (SQLException ex) {
  728.  
  729.                 JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
  730.                         JOptionPane.ERROR_MESSAGE);
  731.                 return;
  732.                 }
  733.             }
  734.         });
  735.         group.add(posRB);
  736.         //////
  737.  
  738.         //выполняем инизиализацию и отрисовку для
  739.         //Employee
  740.          empRB = new JRadioButton("Люди",false);
  741.         //
  742.         empRB.addActionListener(new ActionListener() {
  743.             @Override
  744.             public void actionPerformed(ActionEvent e) {
  745.                 try {
  746.                     ShowEmployee();
  747.                 }
  748.                 catch (SQLException ex) {
  749.  
  750.                     JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
  751.                             JOptionPane.ERROR_MESSAGE);
  752.                     return;
  753.                 }
  754.             }
  755.         });
  756.         group.add(empRB);
  757.         //////
  758.         frame.add(depRB, new GridBagConstraints(0, 2, 1, 1, 1, 1,
  759.                 GridBagConstraints.NORTH,
  760.                 GridBagConstraints.BOTH,
  761.                 new Insets(1, 4, 1, 1), 0, 0));
  762.         frame.add(posRB, new GridBagConstraints(1, 2, 1, 1, 1, 1,
  763.                 GridBagConstraints.NORTH,
  764.                 GridBagConstraints.BOTH,
  765.                 new Insets(1, 4, 1, 1), 0, 0));
  766.  
  767.         frame.add(empRB, new GridBagConstraints(2, 2, 1, 1, 1, 1,
  768.                 GridBagConstraints.NORTH,
  769.                 GridBagConstraints.BOTH,
  770.                 new Insets(1, 4, 1, 1), 0, 0));
  771.  
  772.     }
  773.  
  774.     static void InitDepartGrid() throws SQLException{
  775.         //указываем модель таблицы
  776.  
  777.         try {
  778.             depGrid = new JTable(GetDepartData());
  779.         } catch (SQLException e) {
  780.             e.printStackTrace();
  781.             throw e;
  782.         }
  783.         depGrid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  784.         departTableScroolPage = new JScrollPane(depGrid);
  785.         departTableScroolPage.setPreferredSize(new Dimension(400, 400));
  786.         //addbut
  787.         addDepButton = new JButton("Добавить");
  788.         addDepButton.addActionListener(new ActionListener() {
  789.             @Override
  790.             public void actionPerformed(ActionEvent e) {
  791.                 addDepartDialog.setVisible(true);
  792.             }
  793.         });
  794.  
  795.         //delbut
  796.         delDepButton = new JButton("Удалить");
  797.         delDepButton.addActionListener(new ActionListener() {
  798.             @Override
  799.             public void actionPerformed(ActionEvent e) {
  800.                 int n=depGrid.getSelectedRow();
  801.                 if (n>=0){
  802.                     TableModel m=depGrid.getModel();
  803.                     String s=(String)m.getValueAt(n,0);
  804.                     int id= Integer.parseInt(s);
  805.                     try {
  806.                         dbConn.RemoveDep(id);
  807.                     } catch (SQLException e1) {
  808.                         e1.printStackTrace();
  809.                     }
  810.  
  811.                     try {
  812.                         depGrid.setModel(GetDepartData());
  813.                         } catch (SQLException e1) {
  814.                         e1.printStackTrace();
  815.                     }
  816.                 }
  817.  
  818.             }
  819.         });
  820.  
  821.         //editbut
  822.         editDepButton = new JButton("Изменить");
  823.         editDepButton.addActionListener(new ActionListener() {
  824.             @Override
  825.             public void actionPerformed(ActionEvent e) {
  826.                 int n=depGrid.getSelectedRow();
  827.                 if (n>=0){
  828.                     TableModel m=depGrid.getModel();
  829.                     Component [] comp = editDepartDialog.getContentPane().getComponents();
  830.                     ((JTextField)comp[1]).setText((String)m.getValueAt(n,1));
  831.                     ((JTextField)comp[3]).setText((String)m.getValueAt(n,2));
  832.                     ((JTextField)comp[5]).setText((String)m.getValueAt(n,3));
  833.                     ((JTextField)comp[7]).setText((String)m.getValueAt(n,0));
  834.  
  835.                     editDepartDialog.setVisible(true);
  836.                 }
  837.  
  838.             }
  839.         });
  840.  
  841.         frame.add(addDepButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
  842.                 GridBagConstraints.NORTH,
  843.                 GridBagConstraints.BOTH,
  844.                 new Insets(1, 1, 1, 1), 0, 0));
  845.  
  846.         frame.add(delDepButton, new GridBagConstraints(1, 1, 1, 1, 1, 1,
  847.                 GridBagConstraints.NORTH,
  848.                 GridBagConstraints.BOTH,
  849.                 new Insets(1, 1, 1, 1), 0, 0));
  850.  
  851.         frame.add(editDepButton, new GridBagConstraints(2, 1, 1, 1, 1, 1,
  852.                 GridBagConstraints.NORTH,
  853.                 GridBagConstraints.BOTH,
  854.                 new Insets(1, 1, 1, 1), 0, 0));
  855.     }
  856.  
  857.     static void InitPositionGrid() throws SQLException{
  858.         //указываем модель таблицы
  859.  
  860.         try {
  861.             posGrid = new JTable(GetPositionData());
  862.         } catch (SQLException e) {
  863.             e.printStackTrace();
  864.             throw e;
  865.         }
  866.         posGrid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  867.         positTableScroolPage = new JScrollPane(posGrid);
  868.         positTableScroolPage.setPreferredSize(new Dimension(400, 400));
  869.         //addbut
  870.         addPosButton = new JButton("Добавить");
  871.         addPosButton.addActionListener(new ActionListener() {
  872.             @Override
  873.             public void actionPerformed(ActionEvent e) {
  874.                 addPositDialog.setVisible(true);
  875.             }
  876.         });
  877.  
  878.         //delbut
  879.         delPosButton = new JButton("Удалить");
  880.         delPosButton.addActionListener(new ActionListener() {
  881.             @Override
  882.             public void actionPerformed(ActionEvent e) {
  883.                 int n=posGrid.getSelectedRow();
  884.                 if (n>=0){
  885.                     TableModel m=posGrid.getModel();
  886.                     String s=(String)m.getValueAt(n,0);
  887.                     int id= Integer.parseInt(s);
  888.                     try {
  889.                         dbConn.RemovePos(id);
  890.                     } catch (SQLException e1) {
  891.                         e1.printStackTrace();
  892.                     }
  893.  
  894.                     try {
  895.                         posGrid.setModel(GetPositionData());
  896.                     } catch (SQLException e1) {
  897.                         e1.printStackTrace();
  898.                     }
  899.                 }
  900.  
  901.             }
  902.         });
  903.  
  904.         //editbut
  905.         editPosButton = new JButton("Изменить");
  906.         editPosButton.addActionListener(new ActionListener() {
  907.             @Override
  908.             public void actionPerformed(ActionEvent e) {
  909.                 int n=posGrid.getSelectedRow();
  910.                 if (n>=0){
  911.                     TableModel m=posGrid.getModel();
  912.                     Component [] comp = editPositDialog.getContentPane().getComponents();
  913.                     ((JTextField)comp[1]).setText((String)m.getValueAt(n,1));
  914.                     ((JTextField)comp[3]).setText((String)m.getValueAt(n,2));
  915.                     ((JTextField)comp[5]).setText((String)m.getValueAt(n,0));
  916.                     editPositDialog.setVisible(true);
  917.                 }
  918.  
  919.             }
  920.         });
  921.  
  922.  
  923.         frame.add(addPosButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
  924.                 GridBagConstraints.NORTH,
  925.                 GridBagConstraints.BOTH,
  926.                 new Insets(1, 1, 1, 1), 0, 0));
  927.  
  928.         frame.add(delPosButton, new GridBagConstraints(1, 1, 1, 1, 1, 1,
  929.                 GridBagConstraints.NORTH,
  930.                 GridBagConstraints.BOTH,
  931.                 new Insets(1, 1, 1, 1), 0, 0));
  932.  
  933.         frame.add(editPosButton, new GridBagConstraints(2, 1, 1, 1, 1, 1,
  934.                 GridBagConstraints.NORTH,
  935.                 GridBagConstraints.BOTH,
  936.                 new Insets(1, 1, 1, 1), 0, 0));
  937.         ShowFrame();
  938.     }
  939.  
  940.     static void InitEmplnGrid() throws SQLException{
  941.         //указываем модель таблицы
  942.  
  943.         try {
  944.             empGrid = new JTable(GetEmployeeData());
  945.         } catch (SQLException e) {
  946.             e.printStackTrace();
  947.             throw e;
  948.         }
  949.         empGrid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  950.         emplTableScroolPage = new JScrollPane(empGrid);
  951.         emplTableScroolPage.setPreferredSize(new Dimension(400, 400));
  952.         //addbut
  953.         addEmpButton = new JButton("Добавить");
  954.         addEmpButton.addActionListener(new ActionListener() {
  955.             @Override
  956.             public void actionPerformed(ActionEvent e) {
  957.                 addEmplDialog.setVisible(true);
  958.             }
  959.         });
  960.  
  961.         //delbut
  962.         delEmpButton = new JButton("Удалить");
  963.         delEmpButton.addActionListener(new ActionListener() {
  964.             @Override
  965.             public void actionPerformed(ActionEvent e) {
  966.                 int n=empGrid.getSelectedRow();
  967.                 if (n>=0){
  968.                     TableModel m=empGrid.getModel();
  969.                     String s=(String)m.getValueAt(n,0);
  970.                     int id= Integer.parseInt(s);
  971.                     try {
  972.                         dbConn.RemoveEmp(id);
  973.                     } catch (SQLException e1) {
  974.                         e1.printStackTrace();
  975.                     }
  976.  
  977.                     try {
  978.                         empGrid.setModel(GetEmployeeData());
  979.                     } catch (SQLException e1) {
  980.                         e1.printStackTrace();
  981.                     }
  982.                 }
  983.  
  984.             }
  985.         });
  986.  
  987.         //editbut
  988.         editEmpButton = new JButton("Изменить");
  989.         editEmpButton.addActionListener(new ActionListener() {
  990.             @Override
  991.             public void actionPerformed(ActionEvent e) {
  992.                 int n=empGrid.getSelectedRow();
  993.                 if (n>=0){
  994.                     TableModel m=empGrid.getModel();
  995.                     Component [] comp = editEmplDialog.getContentPane().getComponents();
  996.                     ((JTextField)comp[1]).setText((String)m.getValueAt(n,1));
  997.                     ((JTextField)comp[3]).setText((String)m.getValueAt(n,2));
  998.                     ((JTextField)comp[5]).setText((String)m.getValueAt(n,0));
  999.                     editEmplDialog.setVisible(true);
  1000.                 }
  1001.  
  1002.             }
  1003.         });
  1004.  
  1005.  
  1006.         frame.add(addEmpButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
  1007.                 GridBagConstraints.NORTH,
  1008.                 GridBagConstraints.BOTH,
  1009.                 new Insets(1, 1, 1, 1), 0, 0));
  1010.  
  1011.         frame.add(delEmpButton, new GridBagConstraints(1, 1, 1, 1, 1, 1,
  1012.                 GridBagConstraints.NORTH,
  1013.                 GridBagConstraints.BOTH,
  1014.                 new Insets(1, 1, 1, 1), 0, 0));
  1015.  
  1016.         frame.add(editEmpButton, new GridBagConstraints(2, 1, 1, 1, 1, 1,
  1017.                 GridBagConstraints.NORTH,
  1018.                 GridBagConstraints.BOTH,
  1019.                 new Insets(1, 1, 1, 1), 0, 0));
  1020.         ShowFrame();
  1021.     }
  1022.  
  1023.     public static void main(String[] args) {
  1024.         Properties props = new Properties();
  1025.         try {
  1026.             props.load(new FileInputStream(new File(".\\src\\config.ini")));
  1027.         } catch (IOException e) {
  1028.             JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
  1029.                     JOptionPane.ERROR_MESSAGE);
  1030.             return;
  1031.         }
  1032.         try {
  1033.             InitDB(props);
  1034.         } catch (Exception e) {
  1035.             e.printStackTrace();
  1036.             JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
  1037.                     JOptionPane.ERROR_MESSAGE);
  1038.             return;
  1039.         }
  1040.         InitFrame();
  1041.  
  1042.         try {
  1043.  
  1044.             InitDialogs();
  1045.             InitDepartGrid();
  1046.             InitPositionGrid();
  1047.             InitEmplnGrid();
  1048.  
  1049.  
  1050.             ShowDepartment();
  1051.             ShowFrame();
  1052.  
  1053.         } catch (SQLException e) {
  1054.             e.printStackTrace();
  1055.             JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
  1056.                     JOptionPane.ERROR_MESSAGE);
  1057.             return;
  1058.         };
  1059.     }
  1060. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement