Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.42 KB | None | 0 0
  1. import java.awt.Container;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.WindowAdapter;
  5. import java.awt.event.WindowEvent;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.text.ParseException;
  9. import java.util.Vector;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JTextField;
  16. import javax.swing.SpringLayout;
  17.  
  18. public class Windows implements ActionListener {
  19.  
  20.     private Vector<Person> persons = new Vector<Person>();
  21.  
  22.     private int sizex = 600;
  23.     private int sizey = 200;
  24.    
  25.     private JFrame jframeshowpersons;
  26.     private JFrame jframeaddoreditperson;
  27.     private JLabel[] jlabels;
  28.     private JLabel jlabelnumber;
  29.     private int number;
  30.     private JTextField[] jtextfields;
  31.     private JButton jbuttonadd;
  32.     private JButton jbuttonleft;
  33.     private JButton jbuttonright;
  34.     private JButton jbuttondelete;
  35.     private JButton jbuttonedit;
  36.     private JButton jbuttoncheck;
  37.     private JButton jbuttoncancel;
  38.     private boolean edit;
  39.  
  40.     private Windows() throws ParseException, IOException {
  41.         MyLogger.init();
  42.         persons = MyFileReader.readFromFile();
  43.         jframeshowpersons = new JFrame("Talking");
  44.         jframeshowpersons.setSize(sizex, sizey);
  45.         jframeshowpersons.setLocationRelativeTo(null);
  46.         jframeshowpersons.addWindowListener(new WindowAdapter() {
  47.             public void windowClosing(WindowEvent e) {
  48.                 try {
  49.                     MyFileWriter.writeToFile(persons);
  50.                 } catch (FileNotFoundException e1) {
  51.                     e1.printStackTrace();
  52.                 }
  53.                 System.exit(0);
  54.             }
  55.         });
  56.         Container containershowperson = jframeshowpersons.getContentPane();
  57.         SpringLayout springlayoutshowperson = new SpringLayout();
  58.         containershowperson.setLayout(springlayoutshowperson);
  59.         addLabels(containershowperson, springlayoutshowperson);
  60.         jlabels = addLabelsForVector(containershowperson, springlayoutshowperson);
  61.         setJLabelNumber(containershowperson, springlayoutshowperson);
  62.         jbuttonadd = addJButtonAdd(containershowperson, springlayoutshowperson);
  63.         jbuttonadd.addActionListener(this);
  64.         jbuttonleft = addJButtonLeft(containershowperson, springlayoutshowperson);
  65.         jbuttonleft.addActionListener(this);
  66.         jbuttonright = addJButtonRight(containershowperson, springlayoutshowperson);
  67.         jbuttonright.addActionListener(this);
  68.         jbuttondelete = addJButtonDelete(containershowperson, springlayoutshowperson);
  69.         jbuttondelete.addActionListener(this);
  70.         jbuttonedit = addJButtonEdit(containershowperson, springlayoutshowperson);
  71.         jbuttonedit.addActionListener(this);
  72.         jframeshowpersons.setResizable(false);
  73.         toLabelsFromVector(0);
  74.         jframeshowpersons.setVisible(true);
  75.  
  76.         jframeaddoreditperson = new JFrame("Talking");
  77.         jframeaddoreditperson.setSize(sizex, sizey);
  78.         jframeaddoreditperson.setLocationRelativeTo(null);
  79.         jframeaddoreditperson.addWindowListener(new WindowAdapter() {
  80.             public void windowClosing(WindowEvent e) {
  81.                 try {
  82.                     MyFileWriter.writeToFile(persons);
  83.                 } catch (FileNotFoundException e1) {
  84.                     e1.printStackTrace();
  85.                 }
  86.                 System.exit(0);
  87.             }
  88.         });
  89.         Container containeraddperson = jframeaddoreditperson.getContentPane();
  90.         SpringLayout springlayoutaddperson = new SpringLayout();
  91.         containeraddperson.setLayout(springlayoutaddperson);
  92.         addLabels(containeraddperson, springlayoutaddperson);
  93.         jtextfields = addTextFields(containeraddperson, springlayoutaddperson);
  94.         jbuttoncheck = addJButtonCheck(containeraddperson, springlayoutaddperson);
  95.         jbuttoncheck.addActionListener(this);
  96.         jbuttoncancel = addJButtonCancel(containeraddperson, springlayoutaddperson);
  97.         jbuttoncancel.addActionListener(this);
  98.         jframeaddoreditperson.setResizable(false);
  99.         jframeaddoreditperson.setVisible(false);
  100.  
  101.     }
  102.  
  103.     private static void addLabels(Container container, SpringLayout springlayout) {
  104.         JLabel[] jlabels = new JLabel[4];
  105.         jlabels[0] = new JLabel("Imię:");
  106.         jlabels[1] = new JLabel("Nazwisko:");
  107.         jlabels[2] = new JLabel("Wiek:");
  108.         jlabels[3] = new JLabel("Data urodzenia(dd-MM-yyyy):");
  109.  
  110.         for (int i = 0; i < 4; i++) {
  111.             container.add(jlabels[i]);
  112.             springlayout.putConstraint(SpringLayout.WEST, jlabels[i], 5, SpringLayout.WEST, container);
  113.             springlayout.putConstraint(SpringLayout.NORTH, jlabels[i], (5 + i * 25), SpringLayout.NORTH, container);
  114.         }
  115.     }
  116.  
  117.     private static JLabel[] addLabelsForVector(Container container, SpringLayout springlayout) {
  118.         JLabel[] jlabels = new JLabel[4];
  119.  
  120.         for (int i = 0; i < 4; i++) {
  121.             jlabels[i] = new JLabel("-----");
  122.             container.add(jlabels[i]);
  123.             springlayout.putConstraint(SpringLayout.WEST, jlabels[i], 240, SpringLayout.WEST, container);
  124.             springlayout.putConstraint(SpringLayout.NORTH, jlabels[i], (5 + i * 25), SpringLayout.NORTH, container);
  125.         }
  126.  
  127.         return jlabels;
  128.     }
  129.  
  130.     private void setJLabelNumber(Container container, SpringLayout springlayout) {
  131.         jlabelnumber = new JLabel("-", JLabel.LEFT);
  132.         container.add(jlabelnumber);
  133.         springlayout.putConstraint(SpringLayout.WEST, jlabelnumber, 62, SpringLayout.WEST, container);
  134.         springlayout.putConstraint(SpringLayout.NORTH, jlabelnumber, 105, SpringLayout.NORTH, container);
  135.     }
  136.  
  137.     private static JTextField[] addTextFields(Container container, SpringLayout springlayout) {
  138.         JTextField[] jtextfields = new JTextField[4];
  139.  
  140.         for (int i = 0; i < 4; i++) {
  141.             jtextfields[i] = new JTextField(null, 25);
  142.             container.add(jtextfields[i]);
  143.             springlayout.putConstraint(SpringLayout.EAST, jtextfields[i], -5, SpringLayout.EAST, container);
  144.             springlayout.putConstraint(SpringLayout.NORTH, jtextfields[i], 5 + i * 25, SpringLayout.NORTH, container);
  145.         }
  146.  
  147.         return jtextfields;
  148.     }
  149.  
  150.     private static JButton addJButtonAdd(Container container, SpringLayout springlayout) {
  151.         JButton jbutton = new JButton("Dodaj nową osobę");
  152.         jbutton.setActionCommand("add");
  153.         container.add(jbutton);
  154.         springlayout.putConstraint(SpringLayout.EAST, jbutton, -5, SpringLayout.EAST, container);
  155.         springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
  156.  
  157.         return jbutton;
  158.     }
  159.  
  160.     private static JButton addJButtonLeft(Container container, SpringLayout springlayout) {
  161.         JButton jbutton = new JButton("<<");
  162.         jbutton.setActionCommand("left");
  163.         container.add(jbutton);
  164.         springlayout.putConstraint(SpringLayout.WEST, jbutton, 5, SpringLayout.WEST, container);
  165.         springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
  166.  
  167.         return jbutton;
  168.     }
  169.  
  170.     private static JButton addJButtonRight(Container container, SpringLayout springlayout) {
  171.         JButton jbutton = new JButton(">>");
  172.         jbutton.setActionCommand("right");
  173.         container.add(jbutton);
  174.         springlayout.putConstraint(SpringLayout.WEST, jbutton, 75, SpringLayout.WEST, container);
  175.         springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
  176.  
  177.         return jbutton;
  178.     }
  179.  
  180.     private static JButton addJButtonDelete(Container container, SpringLayout springlayout) {
  181.         JButton jbutton = new JButton("Usuń wpis");
  182.         jbutton.setActionCommand("delete");
  183.         container.add(jbutton);
  184.         springlayout.putConstraint(SpringLayout.EAST, jbutton, -295, SpringLayout.EAST, container);
  185.         springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
  186.  
  187.         return jbutton;
  188.     }
  189.  
  190.     private static JButton addJButtonEdit(Container container, SpringLayout springlayout) {
  191.         JButton jbutton = new JButton("Edytuj wpis");
  192.         jbutton.setActionCommand("edit");
  193.         container.add(jbutton);
  194.         springlayout.putConstraint(SpringLayout.EAST, jbutton, -175, SpringLayout.EAST, container);
  195.         springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
  196.  
  197.         return jbutton;
  198.     }
  199.  
  200.     private static JButton addJButtonCheck(Container container, SpringLayout springlayout) {
  201.         JButton jbutton = new JButton("Dodaj");
  202.         jbutton.setActionCommand("check");
  203.         container.add(jbutton);
  204.         springlayout.putConstraint(SpringLayout.EAST, jbutton, -5, SpringLayout.EAST, container);
  205.         springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
  206.  
  207.         return jbutton;
  208.     }
  209.  
  210.     private static JButton addJButtonCancel(Container container, SpringLayout springlayout) {
  211.         JButton jbutton = new JButton("Anuluj");
  212.         jbutton.setActionCommand("cancel");
  213.         container.add(jbutton);
  214.         springlayout.putConstraint(SpringLayout.WEST, jbutton, 5, SpringLayout.WEST, container);
  215.         springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
  216.  
  217.         return jbutton;
  218.     }
  219.  
  220.     @Override
  221.     public void actionPerformed(ActionEvent e) {
  222.         if ("add".equals(e.getActionCommand())) {
  223.             clickJButtonAdd();
  224.         } else if ("check".equals(e.getActionCommand())) {
  225.             clickJButtonCheck();
  226.         } else if ("left".equals(e.getActionCommand())) {
  227.             clickJButtonLeft();
  228.         } else if ("right".equals(e.getActionCommand())) {
  229.             clickJButtonRight();
  230.         } else if ("delete".equals(e.getActionCommand())) {
  231.             clickButtonDelete();
  232.         } else if ("edit".equals(e.getActionCommand())) {
  233.             clickButtonEdit();
  234.         } else if ("cancel".equals(e.getActionCommand())) {
  235.             clickButtonCancel();
  236.         }
  237.  
  238.     }
  239.  
  240.     private void clickJButtonAdd() {
  241.         edit = false;
  242.         jbuttoncheck.setText("Dodaj");
  243.         for (int i = 0; i < 4; i++) {
  244.             jtextfields[i].setText("");
  245.         }
  246.         jframeshowpersons.setVisible(false);
  247.         jframeaddoreditperson.setVisible(true);
  248.     }
  249.  
  250.     private void clickJButtonCheck() {
  251.         Person newperson = new Person();
  252.  
  253.         int counter = 0;
  254.  
  255.         if (newperson.validateFirstName(jtextfields[0].getText())) {
  256.             newperson.setFirstname(jtextfields[0].getText());
  257.             counter++;
  258.         } else {
  259.             JOptionPane.showMessageDialog(jframeaddoreditperson,
  260.                     "Imię musi składać się wyłącznie z liter i zawierać co najmniej 2 litery.", "Uwaga!",
  261.                     JOptionPane.WARNING_MESSAGE);
  262.         }
  263.  
  264.         if (newperson.validateLastName(jtextfields[1].getText())) {
  265.             newperson.setLastname(jtextfields[1].getText());
  266.             counter++;
  267.         } else {
  268.             JOptionPane.showMessageDialog(jframeaddoreditperson,
  269.                     "Nazwisko musi składać się wyłącznie z liter i zawierać co najmniej 2 litery.", "Uwaga!",
  270.                     JOptionPane.WARNING_MESSAGE);
  271.         }
  272.  
  273.         if (newperson.validateAge(jtextfields[2].getText())) {
  274.             newperson.setAge(jtextfields[2].getText());
  275.             counter++;
  276.         } else {
  277.             JOptionPane.showMessageDialog(jframeaddoreditperson, "Wiek powinien być dodatnią liczbą całkowitą.",
  278.                     "Uwaga!", JOptionPane.WARNING_MESSAGE);
  279.         }
  280.  
  281.         if (newperson.validateBirthdayDate(jtextfields[3].getText())) {
  282.             try {
  283.                 newperson.setDate(jtextfields[3].getText());
  284.                 counter++;
  285.             } catch (ParseException e1) {
  286.                 e1.printStackTrace();
  287.             }
  288.         } else {
  289.             JOptionPane.showMessageDialog(jframeaddoreditperson, "Niepoprawna data.", "Uwaga!",
  290.                     JOptionPane.WARNING_MESSAGE);
  291.         }
  292.  
  293.         try {
  294.             if (newperson.validateAgeAndBirthdayDate()) {
  295.                 counter++;
  296.             } else {
  297.                 JOptionPane.showMessageDialog(jframeaddoreditperson,
  298.                         "Podano nieprawdziwe dane (wiek bądź datę urodzenia).", "Uwaga!", JOptionPane.WARNING_MESSAGE);
  299.             }
  300.         } catch (ParseException e1) {
  301.             e1.printStackTrace();
  302.         }
  303.  
  304.         if (counter == 5) {
  305.             JOptionPane.showMessageDialog(jframeaddoreditperson, "Podano prawidłowe dane!", "Uwaga!",
  306.                     JOptionPane.DEFAULT_OPTION);
  307.             if (edit) {
  308.                 MyLogger.infoEditPerson(persons.get(number).getFirstname(), persons.get(number).getLastname(),
  309.                         persons.get(number).getAge(), persons.get(number).getDate(), newperson.getFirstname(),
  310.                         newperson.getLastname(), newperson.getAge(), newperson.getDate());
  311.                 persons.set(number, newperson);
  312.             } else {
  313.                 persons.add(newperson);
  314.                 MyLogger.infoAddNewPerson(newperson.getFirstname(), newperson.getLastname(), newperson.getAge(),
  315.                         newperson.getDate());
  316.                 number = persons.size() - 1;
  317.             }
  318.             toLabelsFromVector(number);
  319.             jframeshowpersons.setVisible(true);
  320.             jframeaddoreditperson.setVisible(false);
  321.         } else {
  322.             if (edit) {
  323.                 MyLogger.warningEditPerson(persons.get(number).getFirstname(), persons.get(number).getLastname(),
  324.                         persons.get(number).getAge(), persons.get(number).getDate(), jtextfields[0].getText(),
  325.                         jtextfields[1].getText(), jtextfields[2].getText(), jtextfields[3].getText());
  326.             } else {
  327.                 MyLogger.warningAddNewPerson(jtextfields[0].getText(), jtextfields[1].getText(),
  328.                         jtextfields[2].getText(), jtextfields[3].getText());
  329.             }
  330.         }
  331.     }
  332.  
  333.     private void clickJButtonLeft() {
  334.         if (!persons.isEmpty()) {
  335.             if (number == 0) {
  336.                 number = persons.size() - 1;
  337.             } else {
  338.                 number--;
  339.             }
  340.         }
  341.         toLabelsFromVector(number);
  342.     }
  343.  
  344.     private void clickJButtonRight() {
  345.         if (!persons.isEmpty()) {
  346.             if (number == persons.size() - 1) {
  347.                 number = 0;
  348.             } else {
  349.                 number++;
  350.             }
  351.         }
  352.         toLabelsFromVector(number);
  353.     }
  354.  
  355.     private void clickButtonDelete() {
  356.         if (!persons.isEmpty()) {
  357.             MyLogger.deletePerson(persons.get(number).getFirstname(), persons.get(number).getLastname(),
  358.                     persons.get(number).getAge(), persons.get(number).getDate());
  359.             persons.remove(number);
  360.             if (persons.isEmpty()) {
  361.                 number = 0;
  362.             } else {
  363.                 number %= persons.size();
  364.             }
  365.         }
  366.         toLabelsFromVector(number);
  367.     }
  368.  
  369.     private void clickButtonEdit() {
  370.         if (!persons.isEmpty()) {
  371.             edit = true;
  372.             jbuttoncheck.setText("Edytuj");
  373.             jtextfields[0].setText(persons.elementAt(number).getFirstname());
  374.             jtextfields[1].setText(persons.elementAt(number).getLastname());
  375.             jtextfields[2].setText("" + persons.elementAt(number).getAge());
  376.             jtextfields[3].setText(persons.elementAt(number).getDate());
  377.             jframeshowpersons.setVisible(false);
  378.             jframeaddoreditperson.setVisible(true);
  379.         } else {
  380.             JOptionPane.showMessageDialog(jframeshowpersons, "Brak osób w bazie.", "Uwaga!",
  381.                     JOptionPane.WARNING_MESSAGE);
  382.         }
  383.  
  384.     }
  385.  
  386.     private void clickButtonCancel() {
  387.         jframeshowpersons.setVisible(true);
  388.         jframeaddoreditperson.setVisible(false);
  389.     }
  390.  
  391.     private void toLabelsFromVector(int number) {
  392.         if (persons.isEmpty()) {
  393.             jlabelnumber.setText("-");
  394.             jlabels[0].setText("-----");
  395.             jlabels[1].setText("-----");
  396.             jlabels[2].setText("-----");
  397.             jlabels[3].setText("-----");
  398.         } else {
  399.             jlabelnumber.setText("" + number);
  400.             jlabels[0].setText(persons.elementAt(number).getFirstname());
  401.             jlabels[1].setText(persons.elementAt(number).getLastname());
  402.             jlabels[2].setText("" + persons.elementAt(number).getAge());
  403.             jlabels[3].setText(persons.elementAt(number).getDate());
  404.         }
  405.     }
  406.  
  407.     public static void main(String[] args) throws ParseException, IOException {
  408.         new Windows();
  409.     }
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement