b176ef24f677250d061768172cc297cc8f4034ca
[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.OfferManager;
14
15 import com.toedter.calendar.JCalendar;
16
17 import domain.Offer;
18 import domain.RuralHouse;
19 import exceptions.OverlappingOfferExists;
20 import exceptions.BadDates;
21
22 public class QueryAvailabilityGUI2 extends JFrame {
23         private static final long serialVersionUID = 1L;
24
25         private JComboBox jComboBox1;
26         private JLabel jLabel2 = new JLabel();
27         private JTextField jTextField1 = new JTextField();
28         private JLabel jLabel3 = new JLabel();
29         private JTextField jTextField2 = new JTextField();
30         private JLabel jLabel4 = new JLabel();
31         private JTextField jTextField3 = new JTextField();
32         private JButton jButton1 = new JButton();
33
34         // Code for JCalendar
35         private JCalendar jCalendar1 = new JCalendar();
36         private JCalendar jCalendar2 = new JCalendar();
37         private Calendar calendarInicio = null;
38         private Calendar calendarFin = null;
39         private JButton jButton2 = new JButton();
40         private JLabel jLabel5 = new JLabel();
41         private final JLabel lblNewLabel = new JLabel("");
42         private final JRadioButton ruralHouseName = new JRadioButton(
43                         "Use RuralHouse name");
44         private final ButtonGroup buttonGroup = new ButtonGroup();
45         private final JMenuBar menuBar = new JMenuBar();
46         private final JMenu mnMoreOptions = new JMenu("Search Options");
47         private final JMenuItem PopUpM = new JMenuItem("More Filters");
48         private final JPopupMenu popupMenu = new JPopupMenu();
49         private final JLabel lblNewLabel_1 = new JLabel("Number of bedrooms:");
50         private JTextField nBedrooms;
51         private JTextField nKitchens;
52         private final JLabel lblExtraFeatures = new JLabel("EXTRA FEATURES");
53         private final JLabel lblNumberOfBaths = new JLabel("Number of baths:");
54         private final JTextField nBaths = new JTextField();
55         private final JLabel lblParkings = new JLabel("Parking slots:");
56         private final JTextField nParkings = new JTextField();
57
58         public QueryAvailabilityGUI2(Vector<RuralHouse> v) {
59                 nParkings.setText("");
60                 nParkings.setColumns(10);
61                 nBaths.setColumns(10);
62                 try {
63                         jbInit(v);
64                 } catch (Exception e) {
65                         e.printStackTrace();
66                 }
67         }
68
69         private void jbInit(Vector<RuralHouse> v) throws Exception {
70                 this.getContentPane().setLayout(null);
71                 this.setSize(new Dimension(550, 500));
72                 this.setTitle("Set availability");
73
74                 jComboBox1 = new JComboBox(v);
75                 jComboBox1.setBounds(new Rectangle(115, 30, 115, 20));
76                 jLabel2.setText("First day :");
77                 jLabel2.setBounds(new Rectangle(20, 134, 85, 25));
78                 jTextField1.setBounds(new Rectangle(20, 350, 220, 25));
79                 jTextField1.setEditable(false);
80                 jLabel3.setText("Last day :");
81                 jLabel3.setBounds(new Rectangle(281, 134, 75, 25));
82                 jTextField2.setBounds(new Rectangle(281, 350, 220, 20));
83                 jTextField2.setEditable(false);
84                 jLabel4.setText("Rural House name:");
85                 jLabel4.setBounds(new Rectangle(275, 30, 128, 20));
86                 jTextField3.setEnabled(false);
87                 jTextField3.setBounds(new Rectangle(413, 30, 115, 20));
88                 jButton1.setText("Accept");
89                 jButton1.setBounds(new Rectangle(60, 377, 165, 30));
90                 jButton1.addActionListener(new ActionListener() {
91                         public void actionPerformed(ActionEvent e) {
92                                 jButton1_actionPerformed(e);
93                         }
94                 });
95                 jButton2.setText("Cancel");
96                 jButton2.setBounds(new Rectangle(305, 377, 162, 30));
97                 jButton2.addActionListener(new ActionListener() {
98                         public void actionPerformed(ActionEvent e) {
99                                 jButton2_actionPerformed(e);
100                         }
101                 });
102                 jLabel5.setBounds(new Rectangle(207, 600, 305, 20));
103                 jLabel5.setForeground(Color.red);
104                 jLabel5.setSize(new Dimension(305, 20));
105                 jCalendar1.setBounds(new Rectangle(20, 184, 220, 165));
106                 jCalendar2.setBounds(new Rectangle(281, 184, 220, 165));
107
108                 // Code for JCalendar
109                 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
110                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
111                                 if (propertychangeevent.getPropertyName().equals("locale")) {
112                                         jCalendar1.setLocale((Locale) propertychangeevent
113                                                         .getNewValue());
114                                         DateFormat dateformat = DateFormat.getDateInstance(1,
115                                                         jCalendar1.getLocale());
116                                         jTextField1.setText(dateformat.format(calendarInicio
117                                                         .getTime()));
118                                 } else if (propertychangeevent.getPropertyName().equals(
119                                                 "calendar")) {
120                                         calendarInicio = (Calendar) propertychangeevent
121                                                         .getNewValue();
122                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
123                                                         jCalendar1.getLocale());
124                                         jTextField1.setText(dateformat1.format(calendarInicio
125                                                         .getTime()));
126                                         jCalendar1.setCalendar(calendarInicio);
127                                 }
128                         }
129                 });
130
131                 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
132                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
133                                 if (propertychangeevent.getPropertyName().equals("locale")) {
134                                         jCalendar2.setLocale((Locale) propertychangeevent
135                                                         .getNewValue());
136                                         DateFormat dateformat = DateFormat.getDateInstance(1,
137                                                         jCalendar2.getLocale());
138                                         jTextField2.setText(dateformat.format(calendarFin.getTime()));
139                                 } else if (propertychangeevent.getPropertyName().equals(
140                                                 "calendar")) {
141                                         calendarFin = (Calendar) propertychangeevent.getNewValue();
142                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
143                                                         jCalendar2.getLocale());
144                                         jTextField2.setText(dateformat1.format(calendarFin
145                                                         .getTime()));
146                                         jCalendar2.setCalendar(calendarFin);
147                                 }
148                         }
149                 });
150                 popupMenu.setBounds(75, 125, 58, 16);
151                 
152                 addPopup(getContentPane(), popupMenu);
153                 lblExtraFeatures.setHorizontalAlignment(SwingConstants.CENTER);
154                 
155                 popupMenu.add(lblExtraFeatures);
156                 
157                 popupMenu.add(lblNewLabel_1);
158                 
159                 nBedrooms = new JTextField();
160                 popupMenu.add(nBedrooms);
161                 nBedrooms.setColumns(10);
162                 
163                 JLabel lblNumberOfKitchens = new JLabel("Number of kitchens:");
164                 popupMenu.add(lblNumberOfKitchens);
165                 
166                 nKitchens = new JTextField();
167                 popupMenu.add(nKitchens);
168                 nKitchens.setColumns(10);
169                 
170                 popupMenu.add(lblNumberOfBaths);
171                 
172                 popupMenu.add(nBaths);
173                 
174                 popupMenu.add(lblParkings);
175                 
176                 popupMenu.add(nParkings);
177
178                 this.getContentPane().add(jCalendar2, null);
179                 this.getContentPane().add(jCalendar1, null);
180                 this.getContentPane().add(jLabel5, null);
181                 this.getContentPane().add(jButton2, null);
182                 this.getContentPane().add(jButton1, null);
183                 this.getContentPane().add(jTextField3, null);
184                 this.getContentPane().add(jLabel4, null);
185                 this.getContentPane().add(jTextField2, null);
186                 this.getContentPane().add(jLabel3, null);
187                 this.getContentPane().add(jTextField1, null);
188                 this.getContentPane().add(jLabel2, null);
189                 this.getContentPane().add(jComboBox1, null);
190                 lblNewLabel.setBounds(115, 301, 298, 38);
191
192                 getContentPane().add(lblNewLabel);
193
194                 JRadioButton district = new JRadioButton("Use district name");
195                 district.setSelected(true);
196                 buttonGroup.add(district);
197                 district.setBounds(20, 69, 128, 20);
198                 getContentPane().add(district);
199                 buttonGroup.add(ruralHouseName);
200                 ruralHouseName.setBounds(276, 60, 177, 38);
201                 district.addItemListener(new ItemListener() {
202                         @Override
203                         public void itemStateChanged(ItemEvent e) {
204                                 int state = e.getStateChange();
205                                 if (state == ItemEvent.SELECTED) {
206                                         jComboBox1.setEnabled(true);
207                                 } else if (state == ItemEvent.DESELECTED) {
208                                         jComboBox1.setEnabled(false);
209                                 }
210                         }
211                 });
212                 
213                 ruralHouseName.addItemListener(new ItemListener() {
214                         @Override
215                         public void itemStateChanged(ItemEvent e) {
216                                 int state = e.getStateChange();
217                                 if (state == ItemEvent.SELECTED) {
218                                         jTextField3.setEnabled(true);
219                                 } else if (state == ItemEvent.DESELECTED) {
220                                         jTextField3.setEnabled(false);
221                                 }
222                         }
223                 });
224                 getContentPane().add(ruralHouseName);
225                 
226                 JLabel lblNewLabel_2 = new JLabel("District name:");
227                 lblNewLabel_2.setBounds(20, 30, 85, 17);
228                 getContentPane().add(lblNewLabel_2);
229                 
230                 setJMenuBar(menuBar);
231                 
232                 menuBar.add(mnMoreOptions);
233                 PopUpM.addActionListener(new ActionListener() {
234                         public void actionPerformed(ActionEvent arg0) {
235                                 popupMenu.show(QueryAvailabilityGUI2.this,popupMenu.getX(), popupMenu.getY());
236                         }
237                 });
238                 
239                 mnMoreOptions.add(PopUpM);
240         }
241
242         private void jButton1_actionPerformed(ActionEvent e) {
243                 
244         }
245
246         private void jButton2_actionPerformed(ActionEvent e) {
247                 this.setVisible(false);
248         }
249         private static void addPopup(Component component, final JPopupMenu popup) {
250                 component.addMouseListener(new MouseAdapter() {
251                         public void mousePressed(MouseEvent e) {
252                                 if (e.isPopupTrigger()) {
253                                         showMenu(e);
254                                 }
255                         }
256                         public void mouseReleased(MouseEvent e) {
257                                 if (e.isPopupTrigger()) {
258                                         showMenu(e);
259                                 }
260                         }
261                         private void showMenu(MouseEvent e) {
262                                 popup.show(e.getComponent(), e.getX(), e.getY());
263                         }
264                 });
265         }
266 }