276dcfa4162e83b776c945ec124096c0b712c059
[RRRRHHHH_Code] / ruralHouses / src / gui / IntroduceOffer2GUI.java
1 package gui;
2
3 import java.beans.*;
4
5 import java.sql.Date;
6 import java.text.DateFormat;
7 import java.util.*;
8
9 import javax.swing.*;
10 import java.awt.*;
11 import java.awt.event.*;
12
13 import businessLogic.ApplicationFacadeInterface;
14
15 import com.toedter.calendar.JCalendar;
16
17 import domain.RuralHouse;
18
19 public class IntroduceOffer2GUI extends JFrame  {
20         
21         private static final long serialVersionUID = 1L;
22
23         private JComboBox jComboBox1;
24         private JLabel jLabel1 = new JLabel();
25         private JLabel jLabel2 = new JLabel();
26         private JTextField jTextField1 = new JTextField();
27         private JLabel jLabel3 = new JLabel();
28         private JTextField jTextField2 = new JTextField();
29         private JLabel jLabel4 = new JLabel();
30         private JTextField jTextField3 = new JTextField();
31         private JButton jButton1 = new JButton();
32         // Code for JCalendar
33         private JCalendar jCalendar1 = new JCalendar();
34         private JCalendar jCalendar2 = new JCalendar();
35         private Calendar calendarInicio = null;
36         private Calendar calendarFin = null;
37         private JButton jButton2 = new JButton();
38         private JLabel jLabel5 = new JLabel();
39
40         public IntroduceOffer2GUI(Vector<RuralHouse> v) {
41                 try     {
42                         jbInit(v);
43                 }
44                 catch (Exception e) {
45                         e.printStackTrace();
46                 }
47         }
48
49         private void jbInit(Vector<RuralHouse> v) throws Exception {
50                 this.getContentPane().setLayout(null);
51                 this.setSize(new Dimension(513, 433));
52                 this.setTitle("Set availability");
53
54                 jComboBox1 = new JComboBox(v);
55                 jComboBox1.setBounds(new Rectangle(115, 30, 115, 20));
56                 jLabel1.setText("List of houses:");
57                 jLabel1.setBounds(new Rectangle(25, 30, 95, 20));
58                 jLabel2.setText("First day :");
59                 jLabel2.setBounds(new Rectangle(25, 75, 85, 25));
60                 jTextField1.setBounds(new Rectangle(25, 265, 220, 25));
61                 jTextField1.setEditable(false);
62                 jLabel3.setText("Last day :");
63                 jLabel3.setBounds(new Rectangle(260, 75, 75, 25));
64                 jTextField2.setBounds(new Rectangle(260, 265, 220, 25));
65                 jTextField2.setEditable(false);
66                 jLabel4.setText("Price:");
67                 jLabel4.setBounds(new Rectangle(260, 30, 75, 20));
68                 jTextField3.setBounds(new Rectangle(350, 30, 115, 20));
69                 jTextField3.setText("0");
70                 jButton1.setText("Accept");
71                 jButton1.setBounds(new Rectangle(100, 360, 130, 30));
72                 jTextField3.addFocusListener(new FocusListener() {
73                         public void focusGained(FocusEvent e) {}
74                         public void focusLost(FocusEvent e) {
75                                 jTextField3_focusLost();
76                         }
77                 });
78                 jButton1.addActionListener(new ActionListener() {
79                         public void actionPerformed(ActionEvent e) {
80                                 jButton1_actionPerformed(e);
81                         }
82                 });
83                 jButton2.setText("Cancel");
84                 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
85                 jButton2.addActionListener(new ActionListener() {
86                         public void actionPerformed(ActionEvent e) {
87                                 jButton2_actionPerformed(e);
88                         }
89                 });
90                 jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
91                 jLabel5.setForeground(Color.red);
92                 jLabel5.setSize(new Dimension(305, 20));
93                 jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
94                 jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
95
96                 // Code for JCalendar
97                 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
98                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
99                                 if (propertychangeevent.getPropertyName().equals("locale")) {
100                                         jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
101                                         DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
102                                         jTextField1.setText(dateformat.format(calendarInicio.getTime()));
103                                 }
104                                 else if (propertychangeevent.getPropertyName().equals("calendar")) {
105                                         calendarInicio = (Calendar) propertychangeevent.getNewValue();
106                                         DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
107                                         jTextField1.setText(dateformat1.format(calendarInicio.getTime()));
108                                         jCalendar1.setCalendar(calendarInicio);
109                                 }
110                         } 
111                 });
112
113                 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
114                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
115                                 if (propertychangeevent.getPropertyName().equals("locale")) {
116                                         jCalendar2.setLocale((Locale) propertychangeevent.getNewValue());
117                                         DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar2.getLocale());
118                                         jTextField2.setText(dateformat.format(calendarFin.getTime()));
119                                 }
120                                 else if (propertychangeevent.getPropertyName().equals("calendar")) {
121                                         calendarFin = (Calendar) propertychangeevent.getNewValue();
122                                         DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar2.getLocale());
123                                         jTextField2.setText(dateformat1.format(calendarFin.getTime()));
124                                         jCalendar2.setCalendar(calendarFin);
125                                 }
126                         } 
127                 });
128
129                 this.getContentPane().add(jCalendar2, null);
130                 this.getContentPane().add(jCalendar1, null);
131                 this.getContentPane().add(jLabel5, null);
132                 this.getContentPane().add(jButton2, null);
133                 this.getContentPane().add(jButton1, null);
134                 this.getContentPane().add(jTextField3, null);
135                 this.getContentPane().add(jLabel4, null);
136                 this.getContentPane().add(jTextField2, null);
137                 this.getContentPane().add(jLabel3, null);
138                 this.getContentPane().add(jTextField1, null);
139                 this.getContentPane().add(jLabel2, null);
140                 this.getContentPane().add(jLabel1, null);
141                 this.getContentPane().add(jComboBox1, null);
142         }
143
144         private void jButton1_actionPerformed(ActionEvent e) {
145                 RuralHouse ruralHouse=((RuralHouse)jComboBox1.getSelectedItem());
146                 Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
147                 //Remove the hour:minute:second:ms from the date 
148                 firstDay=Date.valueOf(firstDay.toString());
149                 Date lastDay=new Date(jCalendar2.getCalendar().getTime().getTime());
150                 //Remove the hour:minute:second:ms from the date 
151                 lastDay=Date.valueOf(lastDay.toString());
152                 //It could be to trigger an exception if the introduced string is not a number
153                 float price= Float.parseFloat(jTextField3.getText());
154                 try {
155                         //Obtain the business logic from a StartWindow class (local or remote)
156                         ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
157
158                         facade.createOffer(ruralHouse, firstDay, lastDay, price); 
159
160                         this.setVisible(false);
161                 }
162                 catch (Exception e1) {
163                         e1.printStackTrace();
164                 }
165         }
166         
167         private void jButton2_actionPerformed(ActionEvent e) {
168                 this.setVisible(false);
169         }
170
171         private void jTextField3_focusLost() {
172                 try {
173                         new Integer (jTextField3.getText());
174                         jLabel5.setText("");
175                 }
176                 catch (NumberFormatException ex) {
177                         jLabel5.setText("Error: Please introduce a number");
178                 }
179         }
180 }