Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package clientServerApp;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.table.TableModel;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.List;
- import java.util.Properties;
- public class FirebirdTWJJ {
- /**
- * @param args
- */
- //global var
- static DBConnect dbConn;
- static JFrame frame;
- /// Depart controls
- static JTable depGrid;
- static JDialog addDepartDialog;
- static JDialog editDepartDialog;
- static JButton addDepButton;
- static JButton delDepButton;
- static JButton editDepButton;
- /// End Depart controls
- /// Position controls
- static JTable posGrid;
- static JDialog addPositDialog;
- static JDialog editPositDialog;
- static JButton addPosButton;
- static JButton delPosButton;
- static JButton editPosButton;
- /// End Position controls
- //helpers
- static void InitDB(Properties p) throws Exception{
- dbConn = DBConnect.getInstance(p);
- }
- static DepartmentTable GetDepartData() throws SQLException {
- List<Department> dep = null;
- try {
- dep = dbConn.SelectDep();
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- DepartmentTable depTable = new DepartmentTable();
- String[] tmp = new String[depTable.getColumnCount()];
- for (int i = 0; i < dep.size(); i++) {
- Department d = dep.get(i);
- tmp[0] = "" + d.GetID();
- tmp[1] = d.GetNameDepartment();
- tmp[2] = d.GetMail();
- tmp[3] = d.GetPhone();
- depTable.AddRow(tmp);
- }
- return depTable;
- }
- static PositionTable GetPositionData() throws SQLException {
- List<Position> pos = null;
- try {
- pos = dbConn.SelectPos();
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- PositionTable posTable = new PositionTable();
- String[] tmp = new String[posTable.getColumnCount()];
- for (int i = 0; i < pos.size(); i++) {
- Position p = pos.get(i);
- tmp[0] = "" + p.GetID();
- tmp[1] = p.GetNamePosition();
- tmp[2] = ""+p.GetSalary();
- posTable.AddRow(tmp);
- }
- return posTable;
- }
- //создаем новый Dialog при нажатие на кнопку Add для Departmena
- static void InitAddDepartDialog(){
- addDepartDialog = new JDialog(frame,"Добавить запись",true);
- addDepartDialog.setLocationRelativeTo(frame);
- addDepartDialog.setSize(new Dimension(500,300));
- addDepartDialog.setLayout(new GridBagLayout());
- JTextField nameField=new JTextField(20);
- JTextField phoneField=new JTextField(20);
- JTextField mailField=new JTextField(20);
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String m=mailField.getText().trim();
- String p=phoneField.getText().trim();
- String n=nameField.getText().trim();
- if (m.length()>0 && p.length()>0 && n.length()>0) {
- try {
- dbConn.InsertDep(n, m, p);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- addDepartDialog.setVisible(false);
- depGrid.setModel(GetDepartData());
- //DefaultTableModel model = (DefaultTableModel) depGrid.getModel();
- //model.fireTableDataChanged();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- addDepartDialog.setVisible(false);
- }
- });
- addDepartDialog.add( new JLabel("Имя отдела: "), new GridBagConstraints(0,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add( new JLabel("Почта: "), new GridBagConstraints(0,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add(mailField, new GridBagConstraints(1,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add( new JLabel("Телефон: "), new GridBagConstraints(0,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add(phoneField, new GridBagConstraints(1,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addDepartDialog.pack();
- }
- //создаем новый Dialog при нажатие на кнопку Add для Positiona
- static void InitAddPositionDialog(){
- addPositDialog = new JDialog(frame,"Добавить запись",true);
- addPositDialog.setLocationRelativeTo(frame);
- addPositDialog.setSize(new Dimension(500,300));
- addPositDialog.setLayout(new GridBagLayout());
- JTextField nameField=new JTextField(20);
- JTextField salaryField=new JTextField(20);
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String s=nameField.getText().trim();
- String n=salaryField.getText().trim();
- if ( s.length()>0 && n.length()>0) {
- try {
- dbConn.InsertPos(n,s);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- addPositDialog.setVisible(false);
- posGrid.setModel(GetPositionData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- addPositDialog.setVisible(false);
- }
- });
- addPositDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add( new JLabel("Зарплата: "), new GridBagConstraints(0,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add(salaryField, new GridBagConstraints(1,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.pack();
- }
- static void InitEditDepartDialog(){
- editDepartDialog = new JDialog(frame,"Добавить запись",true);
- editDepartDialog.setLocationRelativeTo(frame);
- editDepartDialog.setSize(new Dimension(500,300));
- editDepartDialog.setLayout(new GridBagLayout());
- JTextField nameField=new JTextField(20);
- JTextField phoneField=new JTextField(20);
- JTextField mailField=new JTextField(20);
- JTextField idField=new JTextField(20);
- idField.setVisible(false);
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String m=mailField.getText().trim();
- String p=phoneField.getText().trim();
- String n=nameField.getText().trim();
- String sid=idField.getText().trim();
- int id = Integer.parseInt(sid);
- if (m.length()>0 && p.length()>0 && n.length()>0) {
- try {
- dbConn.UpdateDep(id, n, m, p);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- editDepartDialog.setVisible(false);
- depGrid.setModel(GetDepartData());
- //DefaultTableModel model = (DefaultTableModel) depGrid.getModel();
- //model.fireTableDataChanged();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- editDepartDialog.setVisible(false);
- }
- });
- editDepartDialog.add( new JLabel("Имя отдела: "), new GridBagConstraints(0,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add( new JLabel("Почта: "), new GridBagConstraints(0,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add(mailField, new GridBagConstraints(1,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add( new JLabel("Телефон: "), new GridBagConstraints(0,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add(phoneField, new GridBagConstraints(1,2,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.add(idField, new GridBagConstraints(1,4,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editDepartDialog.pack();
- }
- static void InitEditPositDialog(){
- editPositDialog = new JDialog(frame,"Добавить запись",true);
- editPositDialog.setLocationRelativeTo(frame);
- editPositDialog.setSize(new Dimension(500,300));
- editPositDialog.setLayout(new GridBagLayout());
- JTextField nameField=new JTextField(20);
- JTextField salaryField=new JTextField(20);
- JTextField idField=new JTextField(20);
- idField.setVisible(false);
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String n=nameField.getText().trim();
- String s=salaryField.getText().trim();
- String sid=idField.getText().trim();
- int id = Integer.parseInt(sid);
- if (s.length()>0 && n.length()>0) {
- try {
- dbConn.UpdatePos(Integer.parseInt(s),n,id);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- editPositDialog.setVisible(false);
- posGrid.setModel(GetPositionData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- editPositDialog.setVisible(false);
- }
- });
- addPositDialog.add( new JLabel("Должность: "), new GridBagConstraints(0,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add(nameField, new GridBagConstraints(1,0,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add( new JLabel("Зарплата: "), new GridBagConstraints(0,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add(salaryField, new GridBagConstraints(1,1,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- addPositDialog.add(okButton, new GridBagConstraints(1,3,1,1,1,1,
- GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
- new Insets(2,2,2,2),0,0));
- editPositDialog.pack();
- }
- static void InitDialogs(){
- InitAddDepartDialog();
- InitEditDepartDialog();
- }
- ///////////Работа со скрытием элементов
- //dep
- static void ShowDepartment() throws SQLException {
- depGrid.setModel(GetDepartData());
- depGrid.setVisible(true);
- addDepButton.setVisible(true);
- delDepButton.setVisible(true);
- editDepButton.setVisible(true);
- }
- static void HideDepartment() throws SQLException{
- depGrid.setVisible(false);
- addDepButton.setVisible(false);
- delDepButton.setVisible(false);
- editDepartDialog.setVisible(false);
- }
- //pos
- static void ShowPosition() throws SQLException{
- posGrid.setModel(GetPositionData());
- posGrid.setVisible(true);
- addPosButton.setVisible(true);
- delPosButton.setVisible(true);
- editPosButton.setVisible(true);
- //HideDepartment();
- }
- static void HidePosition() throws SQLException{
- addPosButton.setVisible(false);
- delPosButton.setVisible(false);
- editPosButton.setVisible(false);
- posGrid.setVisible(false);
- }
- //emp
- static void ShowEmployee() throws SQLException{
- // HidePosition();
- //HideDepartment();
- //включаем видимость кнопок
- //
- }
- //static void HideEmployee() throws SQLException{
- //выключаем видимость кнопок
- //}
- ///////
- static void ShowFrame()
- {
- frame.setVisible(true);
- frame.pack();
- }
- static void InitFrame(){
- frame = new JFrame("Учет сотрудников предприятия");
- frame.setSize(new Dimension(1000, 1000));
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setLayout(new GridBagLayout());
- ButtonGroup group = new ButtonGroup();
- //выполняем инизиализацию и отрисовку для
- //Department
- JRadioButton dep = new JRadioButton("Отдел",true);
- dep.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- HidePosition();
- ShowDepartment();
- }
- catch (SQLException ex)
- {
- JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- }
- });
- group.add(dep);
- //////
- //выполняем инизиализацию и отрисовку для
- //Position
- JRadioButton pos = new JRadioButton("Должность",false);
- pos.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- InitAddPositionDialog();
- InitEditPositDialog();
- InitPositionGrid();
- HideDepartment();
- ShowPosition();
- }
- catch (SQLException ex) {
- JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- }
- });
- group.add(pos);
- //////
- //выполняем инизиализацию и отрисовку для
- //Employee
- JRadioButton emp = new JRadioButton("Люди",false);
- //
- emp.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- ShowEmployee();
- }
- catch (SQLException ex) {
- JOptionPane.showMessageDialog(new JFrame(), ex.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- }
- });
- group.add(emp);
- //////
- frame.add(dep, new GridBagConstraints(0, 2, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 4, 1, 1), 0, 0));
- frame.add(pos, new GridBagConstraints(1, 2, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 4, 1, 1), 0, 0));
- frame.add(emp, new GridBagConstraints(2, 2, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 4, 1, 1), 0, 0));
- }
- static void InitDepartGrid() throws SQLException{
- //указываем модель таблицы
- try {
- depGrid = new JTable(GetDepartData());
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- depGrid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- JScrollPane emplTableScroolPage = new JScrollPane(depGrid);
- emplTableScroolPage.setPreferredSize(new Dimension(400, 400));
- //addbut
- addDepButton = new JButton("Добавить");
- addDepButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- addDepartDialog.setVisible(true);
- }
- });
- //delbut
- delDepButton = new JButton("Удалить");
- delDepButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int n=depGrid.getSelectedRow();
- if (n>=0){
- TableModel m=depGrid.getModel();
- String s=(String)m.getValueAt(n,0);
- int id= Integer.parseInt(s);
- try {
- dbConn.RemoveDep(id);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- depGrid.setModel(GetDepartData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- }
- });
- //editbut
- editDepButton = new JButton("Изменить");
- editDepButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int n=depGrid.getSelectedRow();
- if (n>=0){
- TableModel m=depGrid.getModel();
- Component [] comp = editDepartDialog.getContentPane().getComponents();
- ((JTextField)comp[1]).setText((String)m.getValueAt(n,1));
- ((JTextField)comp[3]).setText((String)m.getValueAt(n,2));
- ((JTextField)comp[5]).setText((String)m.getValueAt(n,3));
- ((JTextField)comp[7]).setText((String)m.getValueAt(n,0));
- editDepartDialog.setVisible(true);
- }
- }
- });
- frame.add(emplTableScroolPage, new GridBagConstraints(0, 0, 3, 1, 0, 0,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(addDepButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(delDepButton, new GridBagConstraints(1, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(editDepButton, new GridBagConstraints(2, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- }
- static void InitPositionGrid() throws SQLException{
- //указываем модель таблицы
- try {
- posGrid = new JTable(GetPositionData());
- } catch (SQLException e) {
- e.printStackTrace();
- throw e;
- }
- posGrid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- JScrollPane emplTableScroolPage = new JScrollPane(posGrid);
- emplTableScroolPage.setPreferredSize(new Dimension(400, 400));
- //addbut
- addPosButton = new JButton("Добавить");
- addPosButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- addPositDialog.setVisible(true);
- }
- });
- //delbut
- delPosButton = new JButton("Удалить");
- delPosButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int n=posGrid.getSelectedRow();
- if (n>=0){
- TableModel m=posGrid.getModel();
- String s=(String)m.getValueAt(n,0);
- int id= Integer.parseInt(s);
- try {
- dbConn.RemovePos(id);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- try {
- posGrid.setModel(GetPositionData());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- }
- });
- //editbut
- editPosButton = new JButton("Изменить");
- editPosButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int n=posGrid.getSelectedRow();
- if (n>=0){
- TableModel m=posGrid.getModel();
- Component [] comp = editPositDialog.getContentPane().getComponents();
- ((JTextField)comp[1]).setText((String)m.getValueAt(n,1));
- ((JTextField)comp[3]).setText((String)m.getValueAt(n,2));
- ((JTextField)comp[5]).setText((String)m.getValueAt(n,3));
- editPositDialog.setVisible(true);
- }
- }
- });
- frame.add(emplTableScroolPage, new GridBagConstraints(0, 0, 3, 1, 0, 0,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(addPosButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(delPosButton, new GridBagConstraints(1, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- frame.add(editPosButton, new GridBagConstraints(2, 1, 1, 1, 1, 1,
- GridBagConstraints.NORTH,
- GridBagConstraints.BOTH,
- new Insets(1, 1, 1, 1), 0, 0));
- ShowFrame();
- }
- public static void main(String[] args) {
- Properties props = new Properties();
- try {
- props.load(new FileInputStream(new File(".\\src\\config.ini")));
- } catch (IOException e) {
- JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- try {
- InitDB(props);
- } catch (Exception e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- InitFrame();
- InitDialogs();
- try {
- InitDepartGrid();
- ShowFrame();
- //ShowDepartment();
- } catch (SQLException e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "ERROR",
- JOptionPane.ERROR_MESSAGE);
- return;
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment