cfad48a88e6a440998842d12fbbe76b88977d973
[RRRRHHHH_Code] / ruralHouses / src / gui / QueryAvailabilityGUI.java
1 package gui;
2
3 import businessLogic.ApplicationFacadeInterface;
4
5 import com.toedter.calendar.JCalendar;
6
7 import configuration.ConfigXML;
8 import domain.Offer;
9 import domain.RuralHouse;
10
11 import javax.swing.*;
12
13 import java.awt.*;
14 import java.awt.event.*;
15 import java.beans.*;
16 import java.sql.Date;
17 import java.text.DateFormat;
18 import java.util.*;
19
20 import javax.swing.table.DefaultTableModel;
21
22
23
24
25
26 public class QueryAvailabilityGUI extends JFrame {
27 private static final long serialVersionUID = 1L;
28
29   private JLabel jLabel1 = new JLabel();
30   private JLabel jLabel2 = new JLabel();
31   private JTextField jTextField2 = new JTextField();
32   private JLabel jLabel3 = new JLabel();
33   private JTextField jTextField3 = new JTextField();
34   private JButton jButton1 = new JButton();
35   private JButton jButton2 = new JButton();
36   
37   // Code for JCalendar
38   private JCalendar jCalendar1 = new JCalendar();
39   private Calendar calendarMio = null;
40   private JLabel jLabel4 = new JLabel();
41   private JScrollPane scrollPane = new JScrollPane();
42   private JComboBox comboBox ;
43   private JTable table;
44   private DefaultTableModel tableModel;
45   private final JLabel labelNoOffers = new JLabel("");
46   private String[] columnNames = new String[] {
47                 "Offer#", "Rural House", "First Day", "Last Day", "Price"
48         };
49
50
51   private static configuration.ConfigXML c;
52         
53   public QueryAvailabilityGUI()
54   {
55         
56     try
57     {
58       jbInit();
59     }
60     catch(Exception e)
61     {
62       e.printStackTrace();
63     }
64
65   }
66
67   private void jbInit() throws Exception
68   {
69         ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
70                 
71         Vector<RuralHouse> rhs=facade.getAllRuralHouses();
72         
73         comboBox = new JComboBox(rhs);
74         //comboBox.setModel(new DefaultComboBoxModel(rhs));
75
76         
77     this.getContentPane().setLayout(null);
78     this.setSize(new Dimension(433, 548));
79     this.setTitle("Query availability");
80     jLabel1.setText("Rural house code:");
81     jLabel1.setBounds(new Rectangle(40, 20, 105, 25));
82     jLabel2.setText("First day:");
83     jLabel2.setBounds(new Rectangle(40, 55, 75, 25));
84     jTextField2.setBounds(new Rectangle(190, 210, 155, 25));
85     jTextField2.setEditable(false);
86     jLabel3.setText("Number of nights:");
87     jLabel3.setBounds(new Rectangle(40, 250, 115, 25));
88     jTextField3.setBounds(new Rectangle(190, 250, 155, 25));
89     jTextField3.setText("0");
90     jButton1.setText("Accept");
91     jButton1.setBounds(new Rectangle(55, 455, 130, 30));
92     jButton1.addActionListener(new ActionListener()
93               {
94                 public void actionPerformed(ActionEvent e)
95                 {
96                   jButton1_actionPerformed(e);
97                 }
98               });
99     jButton2.setText("Close");
100     jButton2.setBounds(new Rectangle(230, 455, 130, 30));
101     
102    
103     jTextField3.addFocusListener(new FocusListener()
104       {
105           public void focusGained(FocusEvent e)
106           {
107           }
108   
109           public void focusLost(FocusEvent e)
110           {
111             jTextField3_focusLost();
112           }
113       });
114     jButton2.addActionListener(new ActionListener()
115       {
116         public void actionPerformed(ActionEvent e)
117         {
118           jButton2_actionPerformed(e);
119         }
120       });
121     jLabel4.setBounds(new Rectangle(55, 300, 305, 30));
122     jLabel4.setForeground(Color.red);
123     jCalendar1.setBounds(new Rectangle(190, 60, 225, 150));
124     scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
125     
126     this.getContentPane().add(scrollPane, null);
127     
128     table = new JTable();
129     table.addMouseListener(new MouseAdapter() {
130         @Override
131         public void mouseClicked(MouseEvent e) {
132                 int i=table.getSelectedRow();
133                 int houseNumber = (int) tableModel.getValueAt(i,1);
134                 Date firstDate=new Date(((java.util.Date)tableModel.getValueAt(i,2)).getTime());
135                 Date lastDate=new Date(((java.util.Date)tableModel.getValueAt(i,3)).getTime());
136         
137                         BookRuralHouseGUI b=new BookRuralHouseGUI(houseNumber,firstDate,lastDate);
138                         b.setVisible(true);}
139     });
140
141     scrollPane.setViewportView(table);
142     tableModel = new DefaultTableModel(
143                         null,
144                 columnNames);
145                 
146     table.setModel(tableModel);
147     this.getContentPane().add(jCalendar1, null);
148     this.getContentPane().add(jLabel4, null);
149     this.getContentPane().add(jButton2, null);
150     this.getContentPane().add(jButton1, null);
151     this.getContentPane().add(jTextField3, null);
152     this.getContentPane().add(jLabel3, null);
153     this.getContentPane().add(jTextField2, null);
154     this.getContentPane().add(jLabel2, null);
155     this.getContentPane().add(jLabel1, null);
156     comboBox.setBounds(new Rectangle(245, 22, 115, 20));
157     comboBox.setBounds(189, 22, 115, 20);
158     
159     getContentPane().add(comboBox);
160     labelNoOffers.setBounds(73, 432, 265, 14);
161     
162     getContentPane().add(labelNoOffers);
163     
164     // Codigo para el JCalendar
165     this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
166     {
167       public void propertyChange(PropertyChangeEvent propertychangeevent)
168       {
169         if (propertychangeevent.getPropertyName().equals("locale"))
170         {
171           jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
172           DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
173           jTextField2.setText(dateformat.format(calendarMio.getTime()));
174         }
175         else if (propertychangeevent.getPropertyName().equals("calendar"))
176         {
177           calendarMio = (Calendar) propertychangeevent.getNewValue();
178           DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
179           jTextField2.setText(dateformat1.format(calendarMio.getTime()));
180           jCalendar1.setCalendar(calendarMio);
181         }
182       } 
183     });
184     
185
186   }
187
188   private void jButton2_actionPerformed(ActionEvent e)
189   {
190     this.setVisible(false);
191   }
192   
193   
194  private void jTextField3_focusLost()
195  {
196    try
197   {
198     new Integer (jTextField3.getText());
199     jLabel4.setText("");
200   }
201   catch (NumberFormatException ex)
202   {
203     jLabel4.setText("Error: Introduce a number");
204   }
205  }
206
207  private void jButton1_actionPerformed(ActionEvent e)
208  {              
209                 // House object
210                 RuralHouse rh=(RuralHouse)comboBox.getSelectedItem();
211                 // First day
212                 Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
213             //Remove the hour:minute:second:ms from the date 
214                 firstDay=Date.valueOf(firstDay.toString());
215                 final long diams=1000*60*60*24;
216                 long nights= diams * Integer.parseInt(jTextField3.getText());
217                 // Last day
218         Date lastDay= new Date((long)(firstDay.getTime()+nights));
219         
220         try {
221                 
222                 ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
223
224                 Vector<Offer> v=rh.getOffers(firstDay, lastDay);
225   
226                         Enumeration<Offer> en=v.elements();
227                         Offer of;
228                         tableModel.setDataVector(null, columnNames);
229                         if (!en.hasMoreElements())
230                                 labelNoOffers.setText("There are no offers at these dates");
231                         else {
232                                 labelNoOffers.setText("Select an offer if you want to book");
233
234                                 while (en.hasMoreElements()) {
235                                         of=en.nextElement();
236                                         System.out.println("Offer retrieved: "+of.toString());
237                                         Vector row = new Vector();
238                                         row.add(of.getOfferNumber());
239                                         row.add(of.getRuralHouse().getHouseNumber());
240
241                                         // Dates are stored in db4o as java.util.Date objects instead of java.sql.Date objects
242                                         // They have to be converted into java.sql.Date objects before
243                                         Date firstDaySqlDate = new Date(of.getFirstDay().getTime());
244                                         Date lastDaySqlDate = new Date(of.getLastDay().getTime());
245                                         row.add(firstDaySqlDate);
246                                         row.add(lastDaySqlDate);
247                                         row.add(of.getPrice());
248
249                                         
250                                         tableModel.addRow(row);                                         
251                                 }
252                         }
253
254                 
255        } catch (Exception e1) {
256
257            labelNoOffers.setText(e1.getMessage());
258         }}      
259 }