3 import businessLogic.HouseManager;
5 import com.toedter.calendar.JCalendar;
8 import domain.RuralHouse;
13 import java.awt.event.*;
16 import java.text.DateFormat;
19 import javax.swing.table.DefaultTableModel;
25 public class QueryAvailabilityGUI extends JFrame {
26 private static final long serialVersionUID = 1L;
28 private JLabel jLabel1 = new JLabel();
29 private JLabel jLabel2 = new JLabel();
30 private JTextField jTextField2 = new JTextField();
31 private JLabel jLabel3 = new JLabel();
32 private JTextField jTextField3 = new JTextField();
33 private JButton jButton1 = new JButton();
34 private JButton jButton2 = new JButton();
37 private JCalendar jCalendar1 = new JCalendar();
38 private Calendar calendarMio = null;
39 private JLabel jLabel4 = new JLabel();
40 private JScrollPane scrollPane = new JScrollPane();
41 private JComboBox comboBox ;
43 private DefaultTableModel tableModel;
44 private final JLabel labelNoOffers = new JLabel("");
45 private String[] columnNames = new String[] {
46 "Offer#", "Rural House", "First Day", "Last Day", "Price"
51 public QueryAvailabilityGUI()
65 private void jbInit() throws Exception
67 HouseManager houseM = new HouseManager();
71 Vector<RuralHouse> rhs=houseM.getAllRuralHouses();
73 comboBox = new JComboBox(rhs);
74 //comboBox.setModel(new DefaultComboBoxModel(rhs));
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(27, 20, 128, 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()
94 public void actionPerformed(ActionEvent e)
96 jButton1_actionPerformed(e);
99 jButton2.setText("Close");
100 jButton2.setBounds(new Rectangle(230, 455, 130, 30));
103 jTextField3.addFocusListener(new FocusListener()
105 public void focusGained(FocusEvent e)
109 public void focusLost(FocusEvent e)
111 jTextField3_focusLost();
114 jButton2.addActionListener(new ActionListener()
116 public void actionPerformed(ActionEvent e)
118 jButton2_actionPerformed(e);
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));
126 this.getContentPane().add(scrollPane, null);
128 table = new JTable();
129 table.addMouseListener(new MouseAdapter() {
131 public void mouseClicked(MouseEvent e) {
132 int i=table.getSelectedRow();
133 String houseName = (String) 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());
137 BookRuralHouseGUI b=new BookRuralHouseGUI(houseName,firstDate,lastDate);
141 scrollPane.setViewportView(table);
142 tableModel = new DefaultTableModel(
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);
159 getContentPane().add(comboBox);
160 labelNoOffers.setBounds(73, 432, 265, 14);
162 getContentPane().add(labelNoOffers);
164 // Codigo para el JCalendar
165 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
167 public void propertyChange(PropertyChangeEvent propertychangeevent)
169 if (propertychangeevent.getPropertyName().equals("locale"))
171 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
172 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
173 jTextField2.setText(dateformat.format(calendarMio.getTime()));
175 else if (propertychangeevent.getPropertyName().equals("calendar"))
177 calendarMio = (Calendar) propertychangeevent.getNewValue();
178 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
179 jTextField2.setText(dateformat1.format(calendarMio.getTime()));
180 jCalendar1.setCalendar(calendarMio);
188 private void jButton2_actionPerformed(ActionEvent e)
190 this.setVisible(false);
194 private void jTextField3_focusLost()
198 new Integer (jTextField3.getText());
201 catch (NumberFormatException ex)
203 jLabel4.setText("Error: Introduce a number");
207 private void jButton1_actionPerformed(ActionEvent e)
210 RuralHouse rh=(RuralHouse)comboBox.getSelectedItem();
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());
218 Date lastDay= new Date((long)(firstDay.getTime()+nights));
223 Vector<Offer> v=rh.getOffers(firstDay, lastDay);
225 Enumeration<Offer> en=v.elements();
227 tableModel.setDataVector(null, columnNames);
228 if (!en.hasMoreElements())
229 labelNoOffers.setText("There are no offers at these dates");
231 labelNoOffers.setText("Select an offer if you want to book");
233 while (en.hasMoreElements()) {
235 System.out.println("Offer retrieved: "+of.toString());
236 Vector<Object> row = new Vector<Object>();
237 row.add(of.getOfferNumber());
238 row.add(of.getRuralHouse().getHouseName());
240 // Dates are stored in db4o as java.util.Date objects instead of java.sql.Date objects
241 // They have to be converted into java.sql.Date objects before
242 Date firstDaySqlDate = new Date(of.getFirstDay().getTime());
243 Date lastDaySqlDate = new Date(of.getLastDay().getTime());
244 row.add(firstDaySqlDate);
245 row.add(lastDaySqlDate);
246 row.add(of.getPrice());
249 tableModel.addRow(row);
254 } catch (Exception e1) {
256 labelNoOffers.setText(e1.getMessage());