From 49c7b8e23a5f14d1bc191f4f43e807104ef34e81 Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 15 Jun 2020 14:44:35 +0200 Subject: [PATCH] Change AddCottage to support edit of cottage and add edit action on list --- src/DB/Request.java | 26 ++++ src/GUI/AddCottageGUI.java | 91 ------------ ...AddCottageGUI.form => EditCottageGUI.form} | 2 +- src/GUI/EditCottageGUI.java | 133 ++++++++++++++++++ src/GUI/ListCottageGUI.form | 15 +- src/GUI/ListCottageGUI.java | 13 +- src/Objects/Cottage.java | 32 +++++ 7 files changed, 210 insertions(+), 102 deletions(-) delete mode 100644 src/GUI/AddCottageGUI.java rename src/GUI/{AddCottageGUI.form => EditCottageGUI.form} (99%) create mode 100644 src/GUI/EditCottageGUI.java diff --git a/src/DB/Request.java b/src/DB/Request.java index 82e4cf2..b52c404 100644 --- a/src/DB/Request.java +++ b/src/DB/Request.java @@ -114,4 +114,30 @@ public class Request { } return null; } + + public static boolean editCottage(Cottage cottage) { + Connection c = Connexion.getConnexion(); + boolean res = false; + try { + PreparedStatement ps = c.prepareStatement("UPDATE GITE SET NOMGITE=?, ADGITE=?, CPGITE=?, VILLEGITE=?, NBPLACES=?, NBCHAMBRES=?, SURFACE=?, PRIX=? WHERE IDGITE=?"); + ps.setString(1, cottage.getName()); + ps.setString(2, cottage.getAddress()); + ps.setString(3, cottage.getZip()); + ps.setString(4, cottage.getCity()); + ps.setInt(5, cottage.getNumberPlaces()); + ps.setInt(6, cottage.getNumberBedrooms()); + ps.setInt(7, cottage.getArea()); + ps.setInt(8, cottage.getPrice()); + ps.setInt(9, cottage.getId()); + + if (ps.executeUpdate() == 1) + res = true; + ps.close(); + c.close(); + } catch (SQLException e) { + System.err.println(e); + JOptionPane.showMessageDialog(null, "Error when update cottage !"); + } + return res; + } } diff --git a/src/GUI/AddCottageGUI.java b/src/GUI/AddCottageGUI.java deleted file mode 100644 index 88d9a9b..0000000 --- a/src/GUI/AddCottageGUI.java +++ /dev/null @@ -1,91 +0,0 @@ -package GUI; - -import DB.Request; -import Objects.Cottage; -import Objects.Owner; - -import javax.swing.*; -import javax.swing.text.NumberFormatter; -import java.awt.event.*; -import java.text.NumberFormat; - -public class AddCottageGUI extends JDialog { - private JPanel contentPane; - private JButton buttonOK; - private JButton buttonCancel; - private JTextField nameField; - private JTextField addressField; - private JTextField cityField; - private JFormattedTextField numberPlacesFormattedTextField; - private JFormattedTextField numberBedroomsFormattedTextField; - private JFormattedTextField areaFormattedTextField; - private JFormattedTextField priceFormattedTextField; - private JFormattedTextField zipFormattedTextField; - private Owner owner; - private DefaultListModel defaultListModel; - - public AddCottageGUI(Owner owner, ListModel model) { - this.owner = owner; - this.defaultListModel = (DefaultListModel) model; - setTitle("Add cottage"); - setLocationRelativeTo(null); - setContentPane(contentPane); - setModal(true); - getRootPane().setDefaultButton(buttonOK); - - buttonOK.addActionListener(actionEvent -> onOK()); - - buttonCancel.addActionListener(actionEvent -> onCancel()); - - // call onCancel() when cross is clicked - setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); - addWindowListener(new WindowAdapter() { - public void windowClosing(WindowEvent e) { - onCancel(); - } - }); - - // call onCancel() on ESCAPE - contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); - - pack(); - setVisible(true); - } - - private void onOK() { - // add your code here - Cottage cottage = Request.addCottage(nameField.getText(), - addressField.getText(), - zipFormattedTextField.getText(), - cityField.getText(), - new Integer(numberPlacesFormattedTextField.getText()), - new Integer(numberBedroomsFormattedTextField.getText()), - new Integer(areaFormattedTextField.getText()), - new Integer(priceFormattedTextField.getText()), - owner); - if (cottage != null) { - defaultListModel.add(defaultListModel.size(), cottage); - dispose(); - } - } - - private void onCancel() { - dispose(); - } - - private void createUIComponents() { - NumberFormat format = NumberFormat.getIntegerInstance(); - format.setGroupingUsed(false); - NumberFormatter formatter = new NumberFormatter(format); - formatter.setValueClass(Integer.class); - formatter.setMinimum(0); - formatter.setMaximum(Integer.MAX_VALUE); - formatter.setAllowsInvalid(false); - - zipFormattedTextField = new JFormattedTextField(formatter); - numberPlacesFormattedTextField = new JFormattedTextField(formatter); - numberBedroomsFormattedTextField = new JFormattedTextField(formatter); - areaFormattedTextField = new JFormattedTextField(formatter); - priceFormattedTextField = new JFormattedTextField(formatter); - } -} diff --git a/src/GUI/AddCottageGUI.form b/src/GUI/EditCottageGUI.form similarity index 99% rename from src/GUI/AddCottageGUI.form rename to src/GUI/EditCottageGUI.form index 805881d..4a28335 100644 --- a/src/GUI/AddCottageGUI.form +++ b/src/GUI/EditCottageGUI.form @@ -1,5 +1,5 @@ -
+ diff --git a/src/GUI/EditCottageGUI.java b/src/GUI/EditCottageGUI.java new file mode 100644 index 0000000..bcbbca7 --- /dev/null +++ b/src/GUI/EditCottageGUI.java @@ -0,0 +1,133 @@ +package GUI; + +import DB.Request; +import Objects.Cottage; +import Objects.Owner; + +import javax.swing.*; +import javax.swing.text.NumberFormatter; +import java.awt.event.*; +import java.text.NumberFormat; + +public class EditCottageGUI extends JDialog { + private JPanel contentPane; + private JButton buttonOK; + private JButton buttonCancel; + private JTextField nameField; + private JTextField addressField; + private JTextField cityField; + private JFormattedTextField numberPlacesFormattedTextField; + private JFormattedTextField numberBedroomsFormattedTextField; + private JFormattedTextField areaFormattedTextField; + private JFormattedTextField priceFormattedTextField; + private JFormattedTextField zipFormattedTextField; + private Owner owner; + private Cottage cottage; + private DefaultListModel defaultListModel; + + public EditCottageGUI(Owner owner, ListModel model) { + this.owner = owner; + this.defaultListModel = (DefaultListModel) model; + creation();; + } + + public EditCottageGUI(Owner owner, ListModel model, Cottage cottage) { + this.owner = owner; + this.defaultListModel = (DefaultListModel) model; + this.cottage = cottage; + creation(); + } + + private void creation() { + setTitle("Add cottage"); + setLocationRelativeTo(null); + setContentPane(contentPane); + setModal(true); + getRootPane().setDefaultButton(buttonOK); + + if (cottage != null) { + nameField.setText(cottage.getName()); + addressField.setText(cottage.getAddress()); + addressField.setEnabled(false); + zipFormattedTextField.setText(cottage.getZip()); + zipFormattedTextField.setEnabled(false); + cityField.setText(cottage.getCity()); + cityField.setEnabled(false); + numberPlacesFormattedTextField.setText(Integer.toString(cottage.getNumberPlaces())); + numberBedroomsFormattedTextField.setText(Integer.toString(cottage.getNumberBedrooms())); + areaFormattedTextField.setText(Integer.toString(cottage.getArea())); + priceFormattedTextField.setText(Integer.toString(cottage.getPrice())); + } + buttonOK.addActionListener(actionEvent -> onOK()); + + buttonCancel.addActionListener(actionEvent -> onCancel()); + + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + onCancel(); + } + }); + + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + + pack(); + setVisible(true); + } + + private void onOK() { + // add your code here + if (cottage == null) { + Cottage cottage = Request.addCottage(nameField.getText(), + addressField.getText(), + zipFormattedTextField.getText(), + cityField.getText(), + new Integer(numberPlacesFormattedTextField.getText()), + new Integer(numberBedroomsFormattedTextField.getText()), + new Integer(areaFormattedTextField.getText()), + new Integer(priceFormattedTextField.getText()), + owner); + if (cottage != null) { + defaultListModel.add(defaultListModel.size(), cottage); + dispose(); + } + } else { + int index = defaultListModel.indexOf(cottage); + if (!nameField.getText().equals(cottage.getName())) + cottage.setName(nameField.getText()); + if (new Integer(numberPlacesFormattedTextField.getText()) != cottage.getNumberPlaces()) + cottage.setNumberPlaces(new Integer(numberPlacesFormattedTextField.getText())); + if (new Integer(numberBedroomsFormattedTextField.getText()) != cottage.getNumberBedrooms()) + cottage.setNumberBedrooms(new Integer(numberBedroomsFormattedTextField.getText())); + if (new Integer(areaFormattedTextField.getText()) != cottage.getArea()) + cottage.setArea(new Integer(areaFormattedTextField.getText())); + if (new Integer(priceFormattedTextField.getText()) != cottage.getPrice()) + cottage.setPrice(new Integer(priceFormattedTextField.getText())); + if (Request.editCottage(cottage)) + defaultListModel.set(index, cottage); + dispose(); + } + } + + private void onCancel() { + dispose(); + } + + private void createUIComponents() { + NumberFormat format = NumberFormat.getIntegerInstance(); + format.setGroupingUsed(false); + NumberFormatter formatter = new NumberFormatter(format); + formatter.setValueClass(Integer.class); + formatter.setMinimum(0); + formatter.setMaximum(Integer.MAX_VALUE); + formatter.setAllowsInvalid(false); + + zipFormattedTextField = new JFormattedTextField(formatter); + numberPlacesFormattedTextField = new JFormattedTextField(formatter); + numberBedroomsFormattedTextField = new JFormattedTextField(formatter); + areaFormattedTextField = new JFormattedTextField(formatter); + priceFormattedTextField = new JFormattedTextField(formatter); + } +} diff --git a/src/GUI/ListCottageGUI.form b/src/GUI/ListCottageGUI.form index c396571..35efd5d 100644 --- a/src/GUI/ListCottageGUI.form +++ b/src/GUI/ListCottageGUI.form @@ -16,7 +16,7 @@ - + @@ -26,7 +26,7 @@ - + @@ -34,7 +34,7 @@ - + @@ -49,6 +49,15 @@ + + + + + + + + + diff --git a/src/GUI/ListCottageGUI.java b/src/GUI/ListCottageGUI.java index 6662969..2500695 100644 --- a/src/GUI/ListCottageGUI.java +++ b/src/GUI/ListCottageGUI.java @@ -5,8 +5,6 @@ import Objects.Cottage; import Objects.Owner; import javax.swing.*; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; import java.awt.event.*; public class ListCottageGUI extends JDialog { @@ -15,6 +13,7 @@ public class ListCottageGUI extends JDialog { private JList cottageList; private JButton deleteButton; private JButton addButton; + private JButton editButton; private Owner owner; public ListCottageGUI(Owner owner) { @@ -33,9 +32,13 @@ public class ListCottageGUI extends JDialog { cottageList.addListSelectionListener(listSelectionEvent -> { if (!deleteButton.isEnabled()) deleteButton.setEnabled(true); + if (!editButton.isEnabled()) + editButton.setEnabled(true); }); - addButton.addActionListener(actionEvent -> onAdd()); + addButton.addActionListener(actionEvent -> new EditCottageGUI(owner, cottageList.getModel())); + + editButton.addActionListener(actionEvent -> new EditCottageGUI(owner, cottageList.getModel(), (Cottage) cottageList.getSelectedValue())); // call onCancel() when cross is clicked setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); @@ -59,10 +62,6 @@ public class ListCottageGUI extends JDialog { ((DefaultListModel) cottageList.getModel()).remove(cottageList.getSelectedIndex()); } - private void onAdd() { - new AddCottageGUI(owner, cottageList.getModel()); - } - private void onExit() { // add your code here if necessary dispose(); diff --git a/src/Objects/Cottage.java b/src/Objects/Cottage.java index dffb624..1526994 100644 --- a/src/Objects/Cottage.java +++ b/src/Objects/Cottage.java @@ -65,6 +65,38 @@ public class Cottage { return owner; } + public void setName(String name) { + this.name = name; + } + + public void setAddress(String address) { + this.address = address; + } + + public void setZip(String zip) { + this.zip = zip; + } + + public void setCity(String city) { + this.city = city; + } + + public void setNumberPlaces(int numberPlaces) { + this.numberPlaces = numberPlaces; + } + + public void setNumberBedrooms(int numberBedrooms) { + this.numberBedrooms = numberBedrooms; + } + + public void setArea(int area) { + this.area = area; + } + + public void setPrice(int price) { + this.price = price; + } + @Override public String toString() { return "N°" + id + " " + name + " " + address + ", " + zip + ", " + city + ". places: " + numberPlaces + ", bedrooms: " + numberBedrooms + ", area: " + area + ", price:" + price + ", id owner: " + owner.getId();