Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.text.ParseException;
- import java.util.Vector;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- import javax.swing.SpringLayout;
- public class Windows implements ActionListener {
- private Vector<Person> persons = new Vector<Person>();
- private int sizex = 600;
- private int sizey = 200;
- private JFrame jframeshowpersons;
- private JFrame jframeaddoreditperson;
- private JLabel[] jlabels;
- private JLabel jlabelnumber;
- private int number;
- private JTextField[] jtextfields;
- private JButton jbuttonadd;
- private JButton jbuttonleft;
- private JButton jbuttonright;
- private JButton jbuttondelete;
- private JButton jbuttonedit;
- private JButton jbuttoncheck;
- private JButton jbuttoncancel;
- private boolean edit;
- private Windows() throws ParseException, IOException {
- MyLogger.init();
- persons = MyFileReader.readFromFile();
- jframeshowpersons = new JFrame("Talking");
- jframeshowpersons.setSize(sizex, sizey);
- jframeshowpersons.setLocationRelativeTo(null);
- jframeshowpersons.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- try {
- MyFileWriter.writeToFile(persons);
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- System.exit(0);
- }
- });
- Container containershowperson = jframeshowpersons.getContentPane();
- SpringLayout springlayoutshowperson = new SpringLayout();
- containershowperson.setLayout(springlayoutshowperson);
- addLabels(containershowperson, springlayoutshowperson);
- jlabels = addLabelsForVector(containershowperson, springlayoutshowperson);
- setJLabelNumber(containershowperson, springlayoutshowperson);
- jbuttonadd = addJButtonAdd(containershowperson, springlayoutshowperson);
- jbuttonadd.addActionListener(this);
- jbuttonleft = addJButtonLeft(containershowperson, springlayoutshowperson);
- jbuttonleft.addActionListener(this);
- jbuttonright = addJButtonRight(containershowperson, springlayoutshowperson);
- jbuttonright.addActionListener(this);
- jbuttondelete = addJButtonDelete(containershowperson, springlayoutshowperson);
- jbuttondelete.addActionListener(this);
- jbuttonedit = addJButtonEdit(containershowperson, springlayoutshowperson);
- jbuttonedit.addActionListener(this);
- jframeshowpersons.setResizable(false);
- toLabelsFromVector(0);
- jframeshowpersons.setVisible(true);
- jframeaddoreditperson = new JFrame("Talking");
- jframeaddoreditperson.setSize(sizex, sizey);
- jframeaddoreditperson.setLocationRelativeTo(null);
- jframeaddoreditperson.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- try {
- MyFileWriter.writeToFile(persons);
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- System.exit(0);
- }
- });
- Container containeraddperson = jframeaddoreditperson.getContentPane();
- SpringLayout springlayoutaddperson = new SpringLayout();
- containeraddperson.setLayout(springlayoutaddperson);
- addLabels(containeraddperson, springlayoutaddperson);
- jtextfields = addTextFields(containeraddperson, springlayoutaddperson);
- jbuttoncheck = addJButtonCheck(containeraddperson, springlayoutaddperson);
- jbuttoncheck.addActionListener(this);
- jbuttoncancel = addJButtonCancel(containeraddperson, springlayoutaddperson);
- jbuttoncancel.addActionListener(this);
- jframeaddoreditperson.setResizable(false);
- jframeaddoreditperson.setVisible(false);
- }
- private static void addLabels(Container container, SpringLayout springlayout) {
- JLabel[] jlabels = new JLabel[4];
- jlabels[0] = new JLabel("Imię:");
- jlabels[1] = new JLabel("Nazwisko:");
- jlabels[2] = new JLabel("Wiek:");
- jlabels[3] = new JLabel("Data urodzenia(dd-MM-yyyy):");
- for (int i = 0; i < 4; i++) {
- container.add(jlabels[i]);
- springlayout.putConstraint(SpringLayout.WEST, jlabels[i], 5, SpringLayout.WEST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jlabels[i], (5 + i * 25), SpringLayout.NORTH, container);
- }
- }
- private static JLabel[] addLabelsForVector(Container container, SpringLayout springlayout) {
- JLabel[] jlabels = new JLabel[4];
- for (int i = 0; i < 4; i++) {
- jlabels[i] = new JLabel("-----");
- container.add(jlabels[i]);
- springlayout.putConstraint(SpringLayout.WEST, jlabels[i], 240, SpringLayout.WEST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jlabels[i], (5 + i * 25), SpringLayout.NORTH, container);
- }
- return jlabels;
- }
- private void setJLabelNumber(Container container, SpringLayout springlayout) {
- jlabelnumber = new JLabel("-", JLabel.LEFT);
- container.add(jlabelnumber);
- springlayout.putConstraint(SpringLayout.WEST, jlabelnumber, 62, SpringLayout.WEST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jlabelnumber, 105, SpringLayout.NORTH, container);
- }
- private static JTextField[] addTextFields(Container container, SpringLayout springlayout) {
- JTextField[] jtextfields = new JTextField[4];
- for (int i = 0; i < 4; i++) {
- jtextfields[i] = new JTextField(null, 25);
- container.add(jtextfields[i]);
- springlayout.putConstraint(SpringLayout.EAST, jtextfields[i], -5, SpringLayout.EAST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jtextfields[i], 5 + i * 25, SpringLayout.NORTH, container);
- }
- return jtextfields;
- }
- private static JButton addJButtonAdd(Container container, SpringLayout springlayout) {
- JButton jbutton = new JButton("Dodaj nową osobę");
- jbutton.setActionCommand("add");
- container.add(jbutton);
- springlayout.putConstraint(SpringLayout.EAST, jbutton, -5, SpringLayout.EAST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
- return jbutton;
- }
- private static JButton addJButtonLeft(Container container, SpringLayout springlayout) {
- JButton jbutton = new JButton("<<");
- jbutton.setActionCommand("left");
- container.add(jbutton);
- springlayout.putConstraint(SpringLayout.WEST, jbutton, 5, SpringLayout.WEST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
- return jbutton;
- }
- private static JButton addJButtonRight(Container container, SpringLayout springlayout) {
- JButton jbutton = new JButton(">>");
- jbutton.setActionCommand("right");
- container.add(jbutton);
- springlayout.putConstraint(SpringLayout.WEST, jbutton, 75, SpringLayout.WEST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
- return jbutton;
- }
- private static JButton addJButtonDelete(Container container, SpringLayout springlayout) {
- JButton jbutton = new JButton("Usuń wpis");
- jbutton.setActionCommand("delete");
- container.add(jbutton);
- springlayout.putConstraint(SpringLayout.EAST, jbutton, -295, SpringLayout.EAST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
- return jbutton;
- }
- private static JButton addJButtonEdit(Container container, SpringLayout springlayout) {
- JButton jbutton = new JButton("Edytuj wpis");
- jbutton.setActionCommand("edit");
- container.add(jbutton);
- springlayout.putConstraint(SpringLayout.EAST, jbutton, -175, SpringLayout.EAST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
- return jbutton;
- }
- private static JButton addJButtonCheck(Container container, SpringLayout springlayout) {
- JButton jbutton = new JButton("Dodaj");
- jbutton.setActionCommand("check");
- container.add(jbutton);
- springlayout.putConstraint(SpringLayout.EAST, jbutton, -5, SpringLayout.EAST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
- return jbutton;
- }
- private static JButton addJButtonCancel(Container container, SpringLayout springlayout) {
- JButton jbutton = new JButton("Anuluj");
- jbutton.setActionCommand("cancel");
- container.add(jbutton);
- springlayout.putConstraint(SpringLayout.WEST, jbutton, 5, SpringLayout.WEST, container);
- springlayout.putConstraint(SpringLayout.NORTH, jbutton, 105, SpringLayout.NORTH, container);
- return jbutton;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if ("add".equals(e.getActionCommand())) {
- clickJButtonAdd();
- } else if ("check".equals(e.getActionCommand())) {
- clickJButtonCheck();
- } else if ("left".equals(e.getActionCommand())) {
- clickJButtonLeft();
- } else if ("right".equals(e.getActionCommand())) {
- clickJButtonRight();
- } else if ("delete".equals(e.getActionCommand())) {
- clickButtonDelete();
- } else if ("edit".equals(e.getActionCommand())) {
- clickButtonEdit();
- } else if ("cancel".equals(e.getActionCommand())) {
- clickButtonCancel();
- }
- }
- private void clickJButtonAdd() {
- edit = false;
- jbuttoncheck.setText("Dodaj");
- for (int i = 0; i < 4; i++) {
- jtextfields[i].setText("");
- }
- jframeshowpersons.setVisible(false);
- jframeaddoreditperson.setVisible(true);
- }
- private void clickJButtonCheck() {
- Person newperson = new Person();
- int counter = 0;
- if (newperson.validateFirstName(jtextfields[0].getText())) {
- newperson.setFirstname(jtextfields[0].getText());
- counter++;
- } else {
- JOptionPane.showMessageDialog(jframeaddoreditperson,
- "Imię musi składać się wyłącznie z liter i zawierać co najmniej 2 litery.", "Uwaga!",
- JOptionPane.WARNING_MESSAGE);
- }
- if (newperson.validateLastName(jtextfields[1].getText())) {
- newperson.setLastname(jtextfields[1].getText());
- counter++;
- } else {
- JOptionPane.showMessageDialog(jframeaddoreditperson,
- "Nazwisko musi składać się wyłącznie z liter i zawierać co najmniej 2 litery.", "Uwaga!",
- JOptionPane.WARNING_MESSAGE);
- }
- if (newperson.validateAge(jtextfields[2].getText())) {
- newperson.setAge(jtextfields[2].getText());
- counter++;
- } else {
- JOptionPane.showMessageDialog(jframeaddoreditperson, "Wiek powinien być dodatnią liczbą całkowitą.",
- "Uwaga!", JOptionPane.WARNING_MESSAGE);
- }
- if (newperson.validateBirthdayDate(jtextfields[3].getText())) {
- try {
- newperson.setDate(jtextfields[3].getText());
- counter++;
- } catch (ParseException e1) {
- e1.printStackTrace();
- }
- } else {
- JOptionPane.showMessageDialog(jframeaddoreditperson, "Niepoprawna data.", "Uwaga!",
- JOptionPane.WARNING_MESSAGE);
- }
- try {
- if (newperson.validateAgeAndBirthdayDate()) {
- counter++;
- } else {
- JOptionPane.showMessageDialog(jframeaddoreditperson,
- "Podano nieprawdziwe dane (wiek bądź datę urodzenia).", "Uwaga!", JOptionPane.WARNING_MESSAGE);
- }
- } catch (ParseException e1) {
- e1.printStackTrace();
- }
- if (counter == 5) {
- JOptionPane.showMessageDialog(jframeaddoreditperson, "Podano prawidłowe dane!", "Uwaga!",
- JOptionPane.DEFAULT_OPTION);
- if (edit) {
- MyLogger.infoEditPerson(persons.get(number).getFirstname(), persons.get(number).getLastname(),
- persons.get(number).getAge(), persons.get(number).getDate(), newperson.getFirstname(),
- newperson.getLastname(), newperson.getAge(), newperson.getDate());
- persons.set(number, newperson);
- } else {
- persons.add(newperson);
- MyLogger.infoAddNewPerson(newperson.getFirstname(), newperson.getLastname(), newperson.getAge(),
- newperson.getDate());
- number = persons.size() - 1;
- }
- toLabelsFromVector(number);
- jframeshowpersons.setVisible(true);
- jframeaddoreditperson.setVisible(false);
- } else {
- if (edit) {
- MyLogger.warningEditPerson(persons.get(number).getFirstname(), persons.get(number).getLastname(),
- persons.get(number).getAge(), persons.get(number).getDate(), jtextfields[0].getText(),
- jtextfields[1].getText(), jtextfields[2].getText(), jtextfields[3].getText());
- } else {
- MyLogger.warningAddNewPerson(jtextfields[0].getText(), jtextfields[1].getText(),
- jtextfields[2].getText(), jtextfields[3].getText());
- }
- }
- }
- private void clickJButtonLeft() {
- if (!persons.isEmpty()) {
- if (number == 0) {
- number = persons.size() - 1;
- } else {
- number--;
- }
- }
- toLabelsFromVector(number);
- }
- private void clickJButtonRight() {
- if (!persons.isEmpty()) {
- if (number == persons.size() - 1) {
- number = 0;
- } else {
- number++;
- }
- }
- toLabelsFromVector(number);
- }
- private void clickButtonDelete() {
- if (!persons.isEmpty()) {
- MyLogger.deletePerson(persons.get(number).getFirstname(), persons.get(number).getLastname(),
- persons.get(number).getAge(), persons.get(number).getDate());
- persons.remove(number);
- if (persons.isEmpty()) {
- number = 0;
- } else {
- number %= persons.size();
- }
- }
- toLabelsFromVector(number);
- }
- private void clickButtonEdit() {
- if (!persons.isEmpty()) {
- edit = true;
- jbuttoncheck.setText("Edytuj");
- jtextfields[0].setText(persons.elementAt(number).getFirstname());
- jtextfields[1].setText(persons.elementAt(number).getLastname());
- jtextfields[2].setText("" + persons.elementAt(number).getAge());
- jtextfields[3].setText(persons.elementAt(number).getDate());
- jframeshowpersons.setVisible(false);
- jframeaddoreditperson.setVisible(true);
- } else {
- JOptionPane.showMessageDialog(jframeshowpersons, "Brak osób w bazie.", "Uwaga!",
- JOptionPane.WARNING_MESSAGE);
- }
- }
- private void clickButtonCancel() {
- jframeshowpersons.setVisible(true);
- jframeaddoreditperson.setVisible(false);
- }
- private void toLabelsFromVector(int number) {
- if (persons.isEmpty()) {
- jlabelnumber.setText("-");
- jlabels[0].setText("-----");
- jlabels[1].setText("-----");
- jlabels[2].setText("-----");
- jlabels[3].setText("-----");
- } else {
- jlabelnumber.setText("" + number);
- jlabels[0].setText(persons.elementAt(number).getFirstname());
- jlabels[1].setText(persons.elementAt(number).getLastname());
- jlabels[2].setText("" + persons.elementAt(number).getAge());
- jlabels[3].setText(persons.elementAt(number).getDate());
- }
- }
- public static void main(String[] args) throws ParseException, IOException {
- new Windows();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement