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