error correction. minor error
[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
11 import java.awt.*;
12 import java.awt.event.*;
13
14 import businessLogic.HouseManager;
15 import businessLogic.OfferManager;
16
17 import com.toedter.calendar.JCalendar;
18
19 import domain.Districs;
20 import domain.Offer;
21 import domain.RuralHouse;
22 import exceptions.OverlappingOfferExists;
23 import exceptions.BadDates;
24
25 public class QueryAvailabilityGUI2 extends JFrame {
26         private static final long serialVersionUID = 1L;
27
28         private JComboBox jComboBox1;
29         private JLabel jLabel2 = new JLabel();
30         private JTextField jTextField1 = new JTextField();
31         private JLabel jLabel3 = new JLabel();
32         private JTextField jTextField2 = new JTextField();
33         private JLabel jLabel4 = new JLabel();
34         private JTextField jTextField3 = new JTextField();
35         private JButton jButton1 = new JButton();
36
37         // Code for JCalendar
38         private JCalendar jCalendar1 = new JCalendar();
39         private JCalendar jCalendar2 = new JCalendar();
40         private Calendar calendarInicio = null;
41         private Calendar calendarFin = null;
42         private JButton jButton2 = new JButton();
43         private JLabel jLabel5 = new JLabel();
44         private final JLabel lblNewLabel = new JLabel("");
45         private final JRadioButton ruralHouseName = new JRadioButton(
46                         "Use RuralHouse name");
47         private final JRadioButton district = new JRadioButton("Use district name");
48         private final ButtonGroup buttonGroup = new ButtonGroup();
49         private final JMenuBar menuBar = new JMenuBar();
50         private final JMenu mnMoreOptions = new JMenu("Search Options");
51         private final JMenuItem PopUpM = new JMenuItem("More Filters");
52         private final JPopupMenu popupMenu = new JPopupMenu();
53         private final JLabel lblNewLabel_1 = new JLabel("Number of bedrooms:");
54         private JTextField nBedrooms;
55         private JTextField nKitchens;
56         private final JLabel lblExtraFeatures = new JLabel("EXTRA FEATURES");
57         private final JLabel lblNumberOfBaths = new JLabel("Number of baths:");
58         private final JTextField nBaths = new JTextField();
59         private final JLabel lblParkings = new JLabel("Parking slots:");
60         private final JTextField nParkings = new JTextField();
61         private HouseManager houseMan = new HouseManager();
62         private final JLabel lblNumberOfLivings = new JLabel("Number of Livings:");
63         private final JTextField nLivings = new JTextField();
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.setBounds(new Rectangle(115, 30, 115, 20));
87                 jLabel2.setText("First day :");
88                 jLabel2.setBounds(new Rectangle(20, 134, 85, 25));
89                 jTextField1.setBounds(new Rectangle(20, 350, 220, 25));
90                 jTextField1.setEditable(false);
91                 jLabel3.setText("Last day :");
92                 jLabel3.setBounds(new Rectangle(281, 134, 75, 25));
93                 jTextField2.setBounds(new Rectangle(281, 350, 220, 20));
94                 jTextField2.setEditable(false);
95                 jLabel4.setText("Rural House name:");
96                 jLabel4.setBounds(new Rectangle(275, 30, 128, 20));
97                 jTextField3.setEnabled(false);
98                 jTextField3.setBounds(new Rectangle(413, 30, 115, 20));
99                 jButton1.setText("Accept");
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.setSelected(true);
212                 ;
213                 district.setBounds(20, 69, 128, 20);
214                 getContentPane().add(district);
215                 buttonGroup.add(ruralHouseName);
216                 buttonGroup.add(district);
217                 ruralHouseName.setBounds(276, 60, 177, 38);
218                 district.addItemListener(new ItemListener() {
219                         @Override
220                         public void itemStateChanged(ItemEvent e) {
221                                 int state = e.getStateChange();
222                                 if (state == ItemEvent.SELECTED) {
223                                         jComboBox1.setEnabled(true);
224                                 } else if (state == ItemEvent.DESELECTED) {
225                                         jComboBox1.setEnabled(false);
226                                 }
227                         }
228                 });
229
230                 ruralHouseName.addItemListener(new ItemListener() {
231                         @Override
232                         public void itemStateChanged(ItemEvent e) {
233                                 int state = e.getStateChange();
234                                 if (state == ItemEvent.SELECTED) {
235                                         jTextField3.setEnabled(true);
236                                 } else if (state == ItemEvent.DESELECTED) {
237                                         jTextField3.setEnabled(false);
238                                 }
239                         }
240                 });
241                 getContentPane().add(ruralHouseName);
242
243                 JLabel lblNewLabel_2 = new JLabel("District name:");
244                 lblNewLabel_2.setBounds(20, 30, 85, 17);
245                 getContentPane().add(lblNewLabel_2);
246
247                 setJMenuBar(menuBar);
248
249                 menuBar.add(mnMoreOptions);
250                 PopUpM.addActionListener(new ActionListener() {
251                         public void actionPerformed(ActionEvent arg0) {
252                                 popupMenu.show(QueryAvailabilityGUI2.this, popupMenu.getX(),
253                                                 popupMenu.getY());
254                         }
255                 });
256
257                 mnMoreOptions.add(PopUpM);
258         }
259
260         private void jButton1_actionPerformed(ActionEvent e) {
261                 if (this.district.isSelected()) {
262                         Vector<RuralHouse> houses = houseMan.getHouses(jComboBox1
263                                         .getSelectedItem().toString(), Integer.parseInt(nBedrooms
264                                         .getText()), Integer.parseInt(nKitchens.getText()), Integer
265                                         .parseInt(nBaths.getText()), Integer.parseInt(nParkings
266                                         .getText()), Integer.parseInt(nLivings.getText()));
267                         listOfHousesGUI list = new listOfHousesGUI(houses);
268                         list.setVisible(true);
269                 } else if (this.ruralHouseName.isSelected()) {
270                         // show features of specific house
271                         // TODO
272                 }
273         }
274
275         private void jButton2_actionPerformed(ActionEvent e) {
276                 this.setVisible(false);
277         }
278
279         private static void addPopup(Component component, final JPopupMenu popup) {
280                 component.addMouseListener(new MouseAdapter() {
281                         public void mousePressed(MouseEvent e) {
282                                 if (e.isPopupTrigger()) {
283                                         showMenu(e);
284                                 }
285                         }
286
287                         public void mouseReleased(MouseEvent e) {
288                                 if (e.isPopupTrigger()) {
289                                         showMenu(e);
290                                 }
291                         }
292
293                         private void showMenu(MouseEvent e) {
294                                 popup.show(e.getComponent(), e.getX(), e.getY());
295                         }
296                 });
297         }
298 }