Minor error correction and some bugs, alongside with modify offers quality of life...
[RRRRHHHH_Code] / ruralHouses / src / gui / QueryAvailabilityGUI2.java
1 package gui;
2
3 import java.beans.*;
4 import java.sql.Date;
5 import java.text.DateFormat;
6 import java.util.*;
7
8 import javax.swing.*;
9
10 import java.awt.*;
11 import java.awt.event.*;
12
13 import businessLogic.HouseManager;
14 import businessLogic.OfferManager;
15
16 import com.toedter.calendar.JCalendar;
17
18 import domain.Districs;
19 import domain.Offer;
20 import domain.RuralHouse;
21 import exceptions.OverlappingOfferExists;
22 import exceptions.BadDates;
23
24 public class QueryAvailabilityGUI2 extends JFrame {
25         private static final long serialVersionUID = 1L;
26
27         private JComboBox jComboBox1;
28         private JLabel jLabel2 = new JLabel();
29         private JTextField jTextField1 = new JTextField();
30         private JLabel jLabel3 = new JLabel();
31         private JTextField jTextField2 = new JTextField();
32         private JLabel jLabel4 = new JLabel();
33         private JTextField jTextField3 = new JTextField();
34         private JButton jButton1 = new JButton();
35
36         // Code for JCalendar
37         private JCalendar jCalendar1 = new JCalendar();
38         private JCalendar jCalendar2 = new JCalendar();
39         private Calendar calendarInicio = null;
40         private Calendar calendarFin = null;
41         private JButton jButton2 = new JButton();
42         private JLabel jLabel5 = new JLabel();
43         private final JLabel lblNewLabel = new JLabel("");
44         private final JRadioButton ruralHouseName = new JRadioButton(
45                         "Use RuralHouse name");
46         private final JRadioButton district = new JRadioButton("Use District Name");
47         private final ButtonGroup buttonGroup = new ButtonGroup();
48         private final JMenuBar menuBar = new JMenuBar();
49         private final JMenu mnMoreOptions = new JMenu("Search Options");
50         private final JMenuItem PopUpM = new JMenuItem("More Filters");
51         private final JPopupMenu popupMenu = new JPopupMenu();
52         private final JLabel lblNewLabel_1 = new JLabel("Number of bedrooms:");
53         private JTextField nBedrooms;
54         private JTextField nKitchens;
55         private final JLabel lblExtraFeatures = new JLabel("EXTRA FEATURES");
56         private final JLabel lblNumberOfBaths = new JLabel("Number of baths:");
57         private final JTextField nBaths = new JTextField();
58         private final JLabel lblParkings = new JLabel("Parking slots:");
59         private final JTextField nParkings = new JTextField();
60         private HouseManager houseMan = new HouseManager();
61         private final JLabel lblNumberOfLivings = new JLabel("Number of Livings:");
62         private final JTextField nLivings = new JTextField();
63         private final JLabel feedback = new JLabel("");
64
65         public QueryAvailabilityGUI2() {
66                 nLivings.setText("0");
67                 nLivings.setColumns(10);
68                 nParkings.setText("0");
69                 nParkings.setColumns(10);
70                 nBaths.setText("0");
71                 nBaths.setColumns(10);
72                 try {
73                         jbInit();
74                 } catch (Exception e) {
75                         e.printStackTrace();
76                 }
77         }
78
79         private void jbInit() throws Exception {
80                 this.getContentPane().setLayout(null);
81                 this.setSize(new Dimension(550, 500));
82                 this.setTitle("Query Availability");
83
84                 jComboBox1 = new JComboBox(new DefaultComboBoxModel(
85                                 Districs.longNames()));
86                 jComboBox1.setEnabled(false);
87                 jComboBox1.setBounds(new Rectangle(115, 30, 115, 20));
88                 jLabel2.setText("First day :");
89                 jLabel2.setBounds(new Rectangle(20, 134, 85, 25));
90                 jTextField1.setBounds(new Rectangle(20, 350, 220, 25));
91                 jTextField1.setEditable(false);
92                 jLabel3.setText("Last day :");
93                 jLabel3.setBounds(new Rectangle(281, 134, 75, 25));
94                 jTextField2.setBounds(new Rectangle(281, 350, 220, 20));
95                 jTextField2.setEditable(false);
96                 jLabel4.setText("Rural House name:");
97                 jLabel4.setBounds(new Rectangle(275, 30, 128, 20));
98                 jTextField3.setBounds(new Rectangle(413, 30, 115, 20));
99                 jButton1.setText("Search");
100                 jButton1.setBounds(new Rectangle(60, 377, 165, 30));
101                 jButton1.addActionListener(new ActionListener() {
102                         public void actionPerformed(ActionEvent e) {
103                                 jButton1_actionPerformed(e);
104                         }
105                 });
106                 jButton2.setText("Cancel");
107                 jButton2.setBounds(new Rectangle(305, 377, 162, 30));
108                 jButton2.addActionListener(new ActionListener() {
109                         public void actionPerformed(ActionEvent e) {
110                                 jButton2_actionPerformed(e);
111                         }
112                 });
113                 jLabel5.setBounds(new Rectangle(207, 600, 305, 20));
114                 jLabel5.setForeground(Color.red);
115                 jLabel5.setSize(new Dimension(305, 20));
116                 jCalendar1.setBounds(new Rectangle(20, 184, 220, 165));
117                 jCalendar2.setBounds(new Rectangle(281, 184, 220, 165));
118
119                 // Code for JCalendar
120                 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
121                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
122                                 if (propertychangeevent.getPropertyName().equals("locale")) {
123                                         jCalendar1.setLocale((Locale) propertychangeevent
124                                                         .getNewValue());
125                                         DateFormat dateformat = DateFormat.getDateInstance(1,
126                                                         jCalendar1.getLocale());
127                                         jTextField1.setText(dateformat.format(calendarInicio
128                                                         .getTime()));
129                                 } else if (propertychangeevent.getPropertyName().equals(
130                                                 "calendar")) {
131                                         calendarInicio = (Calendar) propertychangeevent
132                                                         .getNewValue();
133                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
134                                                         jCalendar1.getLocale());
135                                         jTextField1.setText(dateformat1.format(calendarInicio
136                                                         .getTime()));
137                                         jCalendar1.setCalendar(calendarInicio);
138                                 }
139                         }
140                 });
141
142                 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
143                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
144                                 if (propertychangeevent.getPropertyName().equals("locale")) {
145                                         jCalendar2.setLocale((Locale) propertychangeevent
146                                                         .getNewValue());
147                                         DateFormat dateformat = DateFormat.getDateInstance(1,
148                                                         jCalendar2.getLocale());
149                                         jTextField2.setText(dateformat.format(calendarFin.getTime()));
150                                 } else if (propertychangeevent.getPropertyName().equals(
151                                                 "calendar")) {
152                                         calendarFin = (Calendar) propertychangeevent.getNewValue();
153                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
154                                                         jCalendar2.getLocale());
155                                         jTextField2.setText(dateformat1.format(calendarFin
156                                                         .getTime()));
157                                         jCalendar2.setCalendar(calendarFin);
158                                 }
159                         }
160                 });
161                 popupMenu.setBounds(75, 125, 58, 16);
162
163                 addPopup(getContentPane(), popupMenu);
164                 lblExtraFeatures.setHorizontalAlignment(SwingConstants.CENTER);
165
166                 popupMenu.add(lblExtraFeatures);
167
168                 popupMenu.add(lblNewLabel_1);
169
170                 nBedrooms = new JTextField();
171                 nBedrooms.setText("0");
172                 popupMenu.add(nBedrooms);
173                 nBedrooms.setColumns(10);
174
175                 JLabel lblNumberOfKitchens = new JLabel("Number of kitchens:");
176                 popupMenu.add(lblNumberOfKitchens);
177
178                 nKitchens = new JTextField();
179                 nKitchens.setText("0");
180                 popupMenu.add(nKitchens);
181                 nKitchens.setColumns(10);
182
183                 popupMenu.add(lblNumberOfBaths);
184
185                 popupMenu.add(nBaths);
186
187                 popupMenu.add(lblParkings);
188
189                 popupMenu.add(nParkings);
190
191                 popupMenu.add(lblNumberOfLivings);
192
193                 popupMenu.add(nLivings);
194                 
195                 this.getContentPane().add(jCalendar2, null);
196                 this.getContentPane().add(jCalendar1, null);
197                 this.getContentPane().add(jLabel5, null);
198                 this.getContentPane().add(jButton2, null);
199                 this.getContentPane().add(jButton1, null);
200                 this.getContentPane().add(jTextField3, null);
201                 this.getContentPane().add(jLabel4, null);
202                 this.getContentPane().add(jTextField2, null);
203                 this.getContentPane().add(jLabel3, null);
204                 this.getContentPane().add(jTextField1, null);
205                 this.getContentPane().add(jLabel2, null);
206                 this.getContentPane().add(jComboBox1, null);
207                 lblNewLabel.setBounds(115, 301, 298, 38);
208
209                 getContentPane().add(lblNewLabel);
210                 ;
211                 district.setBounds(20, 69, 128, 20);
212                 getContentPane().add(district);
213                 buttonGroup.add(ruralHouseName);
214                 buttonGroup.add(district);
215                 ruralHouseName.setSelected(true);
216                 ruralHouseName.setBounds(276, 60, 177, 38);
217                 district.addItemListener(new ItemListener() {
218                         @Override
219                         public void itemStateChanged(ItemEvent e) {
220                                 int state = e.getStateChange();
221                                 if (state == ItemEvent.SELECTED) {
222                                         jComboBox1.setEnabled(true);
223                                         jCalendar1.setEnabled(false);
224                                         jCalendar2.setEnabled(false);
225                                 } else if (state == ItemEvent.DESELECTED) {
226                                         jComboBox1.setEnabled(false);
227
228                                 }
229                         }
230                 });
231
232                 ruralHouseName.addItemListener(new ItemListener() {
233                         @Override
234                         public void itemStateChanged(ItemEvent e) {
235                                 int state = e.getStateChange();
236                                 if (state == ItemEvent.SELECTED) {
237                                         jTextField3.setEnabled(true);
238                                         jCalendar1.setEnabled(true);
239                                         jCalendar2.setEnabled(true);
240                                 } else if (state == ItemEvent.DESELECTED) {
241                                         jTextField3.setEnabled(false);
242                                 }
243                         }
244                 });
245                 getContentPane().add(ruralHouseName);
246
247                 JLabel lblNewLabel_2 = new JLabel("District name:");
248                 lblNewLabel_2.setBounds(20, 30, 85, 17);
249                 getContentPane().add(lblNewLabel_2);
250                 feedback.setHorizontalAlignment(SwingConstants.CENTER);
251                 feedback.setForeground(Color.RED);
252                 feedback.setBounds(134, 413, 269, 17);
253
254                 getContentPane().add(feedback);
255
256                 setJMenuBar(menuBar);
257
258                 menuBar.add(mnMoreOptions);
259                 PopUpM.addActionListener(new ActionListener() {
260                         public void actionPerformed(ActionEvent arg0) {
261                                 popupMenu.show(QueryAvailabilityGUI2.this, popupMenu.getX(),
262                                                 popupMenu.getY());
263                         }
264                 });
265
266                 mnMoreOptions.add(PopUpM);
267         }
268
269         private void jButton1_actionPerformed(ActionEvent e) {
270                 if (this.district.isSelected()) {
271                         Vector<RuralHouse> houses = houseMan.getHouses(jComboBox1
272                                         .getSelectedItem().toString(), Integer.parseInt(nBedrooms
273                                         .getText()), Integer.parseInt(nKitchens.getText()), Integer
274                                         .parseInt(nBaths.getText()), Integer.parseInt(nParkings
275                                         .getText()), Integer.parseInt(nLivings.getText()));
276                         listOfHousesGUI list = new listOfHousesGUI(houses);
277                         list.setVisible(true);
278                 } else if (this.ruralHouseName.isSelected()) {
279                         RuralHouse rh = houseMan.getHouseByName(jTextField3.getText());
280                         // The next instruction creates a java.sql.Date object from the date selected in the JCalendar object
281                         Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
282                         // The next instruction removes the hour, minute, second and ms from the date
283                         // This has to be made because the date will be stored in db4o as a java.util.Date object 
284                         // that would store those data, and that would give problems when comparing dates later
285                         firstDay=Date.valueOf(firstDay.toString());
286                         
287                         
288                         Date lastDay=new Date(jCalendar2.getCalendar().getTime().getTime());
289                     //Remove the hour:minute:second:ms from the date 
290                         lastDay=Date.valueOf(lastDay.toString());
291                         if (rh != null) {
292                                 HouseFeaturesGUI hou = new HouseFeaturesGUI(rh,firstDay,lastDay);
293                                 hou.setVisible(true);
294                         } else {
295                                 feedback.setText("Not matching houses");
296                         }
297                 }
298         }
299
300         private void jButton2_actionPerformed(ActionEvent e) {
301                 this.setVisible(false);
302         }
303         
304
305         private static void addPopup(Component component, final JPopupMenu popup) {
306                 component.addMouseListener(new MouseAdapter() {
307                         public void mousePressed(MouseEvent e) {
308                                 if (e.isPopupTrigger()) {
309                                         showMenu(e);
310                                 }
311                         }
312
313                         public void mouseReleased(MouseEvent e) {
314                                 if (e.isPopupTrigger()) {
315                                         showMenu(e);
316                                 }
317                         }
318
319                         private void showMenu(MouseEvent e) {
320                                 popup.show(e.getComponent(), e.getX(), e.getY());
321                         }
322                 });
323         }
324 }