Change AddCottage to support edit of cottage and add edit action on list
This commit is contained in:
parent
766dc9294c
commit
49c7b8e23a
7 changed files with 210 additions and 102 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="GUI.AddCottageGUI">
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="GUI.EditCottageGUI">
|
||||
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="10" left="10" bottom="10" right="10"/>
|
||||
<constraints>
|
133
src/GUI/EditCottageGUI.java
Normal file
133
src/GUI/EditCottageGUI.java
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -16,7 +16,7 @@
|
|||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="9538f" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="9538f" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
|
@ -26,7 +26,7 @@
|
|||
<children>
|
||||
<component id="5723f" class="javax.swing.JButton" binding="exitButton">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Exit"/>
|
||||
|
@ -34,7 +34,7 @@
|
|||
</component>
|
||||
<component id="51ebf" class="javax.swing.JButton" binding="deleteButton">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<enabled value="false"/>
|
||||
|
@ -49,6 +49,15 @@
|
|||
<text value="Add"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="784d0" class="javax.swing.JButton" binding="editButton" default-binding="true">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<enabled value="false"/>
|
||||
<text value="Edit"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
<scrollpane id="c326d">
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
Reference in a new issue