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();
69 Vector<RuralHouse> rhs=houseM.getAllRuralHouses();
71 comboBox = new JComboBox(rhs);
72 //comboBox.setModel(new DefaultComboBoxModel(rhs));
75 this.getContentPane().setLayout(null);
76 this.setSize(new Dimension(433, 548));
77 this.setTitle("Query availability");
78 jLabel1.setText("Rural house code:");
79 jLabel1.setBounds(new Rectangle(27, 20, 128, 25));
80 jLabel2.setText("First day:");
81 jLabel2.setBounds(new Rectangle(40, 55, 75, 25));
82 jTextField2.setBounds(new Rectangle(190, 210, 155, 25));
83 jTextField2.setEditable(false);
84 jLabel3.setText("Number of nights:");
85 jLabel3.setBounds(new Rectangle(40, 250, 115, 25));
86 jTextField3.setBounds(new Rectangle(190, 250, 155, 25));
87 jTextField3.setText("0");
88 jButton1.setText("Accept");
89 jButton1.setBounds(new Rectangle(55, 455, 130, 30));
90 jButton1.addActionListener(new ActionListener()
92 public void actionPerformed(ActionEvent e)
94 jButton1_actionPerformed(e);
97 jButton2.setText("Close");
98 jButton2.setBounds(new Rectangle(230, 455, 130, 30));
101 jTextField3.addFocusListener(new FocusListener()
103 public void focusGained(FocusEvent e)
107 public void focusLost(FocusEvent e)
109 jTextField3_focusLost();
112 jButton2.addActionListener(new ActionListener()
114 public void actionPerformed(ActionEvent e)
116 jButton2_actionPerformed(e);
119 jLabel4.setBounds(new Rectangle(55, 300, 305, 30));
120 jLabel4.setForeground(Color.red);
121 jCalendar1.setBounds(new Rectangle(190, 60, 225, 150));
122 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
124 this.getContentPane().add(scrollPane, null);
126 table = new JTable();
127 table.addMouseListener(new MouseAdapter() {
129 public void mouseClicked(MouseEvent e) {
130 int i=table.getSelectedRow();
131 String houseName = (String) tableModel.getValueAt(i,1);
132 Date firstDate=new Date(((java.util.Date)tableModel.getValueAt(i,2)).getTime());
133 Date lastDate=new Date(((java.util.Date)tableModel.getValueAt(i,3)).getTime());
135 BookRuralHouseGUI b=new BookRuralHouseGUI(houseName,firstDate,lastDate);
139 scrollPane.setViewportView(table);
140 tableModel = new DefaultTableModel(
144 table.setModel(tableModel);
145 this.getContentPane().add(jCalendar1, null);
146 this.getContentPane().add(jLabel4, null);
147 this.getContentPane().add(jButton2, null);
148 this.getContentPane().add(jButton1, null);
149 this.getContentPane().add(jTextField3, null);
150 this.getContentPane().add(jLabel3, null);
151 this.getContentPane().add(jTextField2, null);
152 this.getContentPane().add(jLabel2, null);
153 this.getContentPane().add(jLabel1, null);
154 comboBox.setBounds(new Rectangle(245, 22, 115, 20));
155 comboBox.setBounds(189, 22, 115, 20);
157 getContentPane().add(comboBox);
158 labelNoOffers.setBounds(73, 432, 265, 14);
160 getContentPane().add(labelNoOffers);
162 // Codigo para el JCalendar
163 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
165 public void propertyChange(PropertyChangeEvent propertychangeevent)
167 if (propertychangeevent.getPropertyName().equals("locale"))
169 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
170 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
171 jTextField2.setText(dateformat.format(calendarMio.getTime()));
173 else if (propertychangeevent.getPropertyName().equals("calendar"))
175 calendarMio = (Calendar) propertychangeevent.getNewValue();
176 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
177 jTextField2.setText(dateformat1.format(calendarMio.getTime()));
178 jCalendar1.setCalendar(calendarMio);
186 private void jButton2_actionPerformed(ActionEvent e)
188 this.setVisible(false);
192 private void jTextField3_focusLost()
196 new Integer (jTextField3.getText());
199 catch (NumberFormatException ex)
201 jLabel4.setText("Error: Introduce a number");
205 private void jButton1_actionPerformed(ActionEvent e)
208 RuralHouse rh=(RuralHouse)comboBox.getSelectedItem();
210 Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
211 //Remove the hour:minute:second:ms from the date
212 firstDay=Date.valueOf(firstDay.toString());
213 final long diams=1000*60*60*24;
214 long nights= diams * Integer.parseInt(jTextField3.getText());
216 Date lastDay= new Date((long)(firstDay.getTime()+nights));
221 Vector<Offer> v=rh.getOffers(firstDay, lastDay);
223 Enumeration<Offer> en=v.elements();
225 tableModel.setDataVector(null, columnNames);
226 if (!en.hasMoreElements())
227 labelNoOffers.setText("There are no offers at these dates");
229 labelNoOffers.setText("Select an offer if you want to book");
231 while (en.hasMoreElements()) {
233 System.out.println("Offer retrieved: "+of.toString());
234 Vector<Object> row = new Vector<Object>();
235 row.add(of.getOfferNumber());
236 row.add(of.getRuralHouse().getHouseName());
238 // Dates are stored in db4o as java.util.Date objects instead of java.sql.Date objects
239 // They have to be converted into java.sql.Date objects before
240 Date firstDaySqlDate = new Date(of.getFirstDay().getTime());
241 Date lastDaySqlDate = new Date(of.getLastDay().getTime());
242 row.add(firstDaySqlDate);
243 row.add(lastDaySqlDate);
244 row.add(of.getPrice());
247 tableModel.addRow(row);
252 } catch (Exception e1) {
254 labelNoOffers.setText(e1.getMessage());