Given code uploaded
[RRRRHHHH_Code] / ruralHouses / src / gui / SearchOffersGUI.java
1 package gui;
2
3 import businessLogic.ApplicationFacadeInterface;
4
5 import com.toedter.calendar.JCalendar;
6
7 import domain.Offer;
8 import domain.RuralHouse;
9
10 import javax.swing.*;
11 import java.awt.*;
12 import java.awt.event.*;
13
14 import java.beans.*;
15
16 import java.sql.Date;
17 import java.text.DateFormat;
18 import java.util.*;
19
20 public class SearchOffersGUI extends JFrame {
21
22         private static final long serialVersionUID = 1L;
23
24         private JLabel jLabel1 = new JLabel();
25         private JComboBox jComboBox1;
26         private JLabel jLabel2 = new JLabel();
27         private JTextField jTextField2 = new JTextField();
28         private JLabel jLabel3 = new JLabel();
29         private JTextField jTextField3 = new JTextField();
30         private JButton jButton1 = new JButton();
31         private JButton jButton2 = new JButton();
32         // Code for JCalendar
33         private JCalendar jCalendar1 = new JCalendar();
34         private Calendar myCalendar = null;
35         private JLabel jLabel4 = new JLabel();
36         private JTextArea jTextArea1 = new JTextArea();
37         private JScrollPane scrollPane = new JScrollPane();
38
39         public SearchOffersGUI() {
40                 try {
41                         jbInit();
42                 }
43                 catch (Exception e) {
44                         e.printStackTrace();
45                 }
46         }
47
48         private void jbInit() throws Exception {
49                 ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
50
51                 Vector<RuralHouse> rhs=facade.getAllRuralHouses();
52                 jComboBox1 = new JComboBox(rhs);
53
54                 this.getContentPane().setLayout(null);
55                 this.setSize(new Dimension(440, 548));
56                 this.setTitle("Search available rural houses");
57                 jLabel1.setText("Rural house:");
58                 jComboBox1.setBounds(new Rectangle(10, 10, 115, 25));
59                 jLabel1.setBounds(new Rectangle(40, 20, 105, 25));
60                 jComboBox1.setBounds(new Rectangle(115, 30, 115, 20));
61                 jLabel2.setText("First day:");
62                 jLabel2.setBounds(new Rectangle(40, 55, 75, 25));
63                 jTextField2.setBounds(new Rectangle(190, 210, 155, 25));
64                 jTextField2.setEditable(false);
65                 jLabel3.setText("Number of nights:");
66                 jLabel3.setBounds(new Rectangle(40, 250, 115, 25));
67                 jTextField3.setBounds(new Rectangle(190, 250, 155, 25));
68                 jTextField3.setText("0");
69                 jButton1.setText("Accept");
70                 jButton1.setBounds(new Rectangle(55, 455, 130, 30));
71                 jButton1.addActionListener(new ActionListener() {
72                         public void actionPerformed(ActionEvent e) {
73                                 jButton1_actionPerformed(e);
74                         }
75                 });
76                 jButton2.setText("Close");
77                 jButton2.setBounds(new Rectangle(230, 455, 130, 30));
78
79                 jTextField3.addFocusListener(new FocusListener() {
80                         public void focusGained(FocusEvent e) { }
81                         public void focusLost(FocusEvent e) {
82                                 jTextField3_focusLost();
83                         }
84                 });
85                 jButton2.addActionListener(new ActionListener() {
86                         public void actionPerformed(ActionEvent e) {
87                                 jButton2_actionPerformed(e);
88                         }
89                 });
90                 jLabel4.setBounds(new Rectangle(55, 300, 305, 30));
91                 jLabel4.setForeground(Color.red);
92                 jTextArea1.setEditable(false);
93                 jCalendar1.setBounds(new Rectangle(190, 60, 225, 150));
94                 scrollPane.setBounds(new Rectangle(45, 305, 320, 135));
95                 jTextArea1.setText("");
96                 scrollPane.getViewport().add(jTextArea1, null);
97
98                 this.getContentPane().add(scrollPane, null);
99                 this.getContentPane().add(jCalendar1, null);
100                 this.getContentPane().add(jLabel4, null);
101                 this.getContentPane().add(jButton2, null);
102                 this.getContentPane().add(jButton1, null);
103                 this.getContentPane().add(jTextField3, null);
104                 this.getContentPane().add(jLabel3, null);
105                 this.getContentPane().add(jTextField2, null);
106                 this.getContentPane().add(jLabel2, null);
107                 this.getContentPane().add(jComboBox1, null);
108                 this.getContentPane().add(jLabel1, null);
109
110                 // Code for JCalendar
111                 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
112                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
113                                 if (propertychangeevent.getPropertyName().equals("locale")) {
114                                         jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
115                                         DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
116                                         jTextField2.setText(dateformat.format(myCalendar.getTime()));
117                                 }
118                                 else if (propertychangeevent.getPropertyName().equals("calendar")) {
119                                         myCalendar = (Calendar) propertychangeevent.getNewValue();
120                                         DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
121                                         jTextField2.setText(dateformat1.format(myCalendar.getTime()));
122                                         jCalendar1.setCalendar(myCalendar);
123                                 }
124                         } 
125                 });
126
127         }
128
129         private void jButton2_actionPerformed(ActionEvent e) {
130                 this.setVisible(false);
131         }
132
133         private void jTextField3_focusLost() {
134                 try {
135                         new Integer (jTextField3.getText());
136                         jLabel4.setText("");
137                 }
138                 catch (NumberFormatException ex) {
139                         jLabel4.setText("Error: Please introduce a number");
140                 }
141         }
142
143         private void jButton1_actionPerformed(ActionEvent e) {          
144                 // House object
145                 RuralHouse rh=(RuralHouse)jComboBox1.getSelectedItem();
146                 // First day
147                 Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
148                 //Remove the hour:minute:second:ms from the date 
149                 firstDay=Date.valueOf(firstDay.toString());
150                 final long diams=1000*60*60*24;
151                 long nights= diams * Integer.parseInt(jTextField3.getText());
152                 // Last day
153                 Date lastDay= new Date((long)(firstDay.getTime()+nights));
154                 jTextArea1.setText("");
155                 try {
156                         ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
157                         Vector<Offer> v=facade.getOffers(rh, firstDay, lastDay);
158
159                         Enumeration<Offer> en=v.elements();
160                         Offer of;
161                         while (en.hasMoreElements()) {
162                                 of=en.nextElement();
163                                 firstDay= new Date(of.getFirstDay().getTime());
164                                 firstDay=Date.valueOf(firstDay.toString());
165                                 lastDay= new Date(of.getLastDay().getTime());
166                                 lastDay=Date.valueOf(lastDay.toString());
167                                 jTextArea1.append("Offer "+Integer.toString(of.getOfferNumber())+":from:"+firstDay+" to:"+lastDay+"\n");                        
168                         }
169
170                         //} catch (RemoteException e1) {
171                         //                      e1.printStackTrace();
172
173                 }
174                 catch (Exception e1) {
175                         e1.printStackTrace();
176                 }
177         }
178 }