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