Query GUI finished pretty cool view
[RRRRHHHH_Code] / ruralHouses / src / gui / SetAvailability2GUI.java
1 package gui;
2 import java.beans.*;
3 import java.rmi.RMISecurityManager;
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 configuration.ConfigXML;
18 import domain.Offer;
19 import domain.RuralHouse;
20 import exceptions.OverlappingOfferExists;
21 import exceptions.BadDates;
22
23
24
25 public class SetAvailability2GUI extends JFrame  {
26 private static final long serialVersionUID = 1L;
27
28         
29   private JComboBox jComboBox1;
30   private JLabel jLabel1 = new JLabel();
31   private JLabel jLabel2 = new JLabel();
32   private JTextField jTextField1 = new JTextField();
33   private JLabel jLabel3 = new JLabel();
34   private JTextField jTextField2 = new JTextField();
35   private JLabel jLabel4 = new JLabel();
36   private JTextField jTextField3 = new JTextField();
37   private JButton jButton1 = new JButton();
38   
39   // Code for JCalendar
40   private JCalendar jCalendar1 = new JCalendar();
41   private JCalendar jCalendar2 = new JCalendar();
42   private Calendar calendarInicio = null;
43   private Calendar calendarFin = null;
44   private JButton jButton2 = new JButton();
45   private JLabel jLabel5 = new JLabel();
46   private final JLabel lblNewLabel = new JLabel("");
47   
48
49   public SetAvailability2GUI(Vector<RuralHouse> v)
50   {
51     try
52     {
53       jbInit(v);
54     }
55     catch(Exception e)
56     {
57       e.printStackTrace();
58     }
59   }
60
61   private void jbInit(Vector<RuralHouse> v) throws Exception
62   {
63     this.getContentPane().setLayout(null);
64     this.setSize(new Dimension(513, 433));
65     this.setTitle("Set availability");
66     
67     
68     jComboBox1 = new JComboBox(v);
69     jComboBox1.setBounds(new Rectangle(115, 30, 115, 20));
70     jLabel1.setText("List of houses:");
71     jLabel1.setBounds(new Rectangle(25, 30, 95, 20));
72     jLabel2.setText("First day :");
73     jLabel2.setBounds(new Rectangle(25, 75, 85, 25));
74     jTextField1.setBounds(new Rectangle(25, 265, 220, 25));
75     jTextField1.setEditable(false);
76     jLabel3.setText("Last day :");
77     jLabel3.setBounds(new Rectangle(260, 75, 75, 25));
78     jTextField2.setBounds(new Rectangle(260, 265, 220, 25));
79     jTextField2.setEditable(false);
80     jLabel4.setText("Price:");
81     jLabel4.setBounds(new Rectangle(260, 30, 75, 20));
82     jTextField3.setBounds(new Rectangle(350, 30, 115, 20));
83     jTextField3.setText("0");
84     jButton1.setText("Accept");
85     jButton1.setBounds(new Rectangle(100, 360, 130, 30));
86     jTextField3.addFocusListener(new FocusListener()
87       {
88           public void focusGained(FocusEvent e)
89           {
90           }
91   
92           public void focusLost(FocusEvent e)
93           {
94             jTextField3_focusLost();
95           }
96       });
97     jButton1.addActionListener(new ActionListener()
98       {
99         public void actionPerformed(ActionEvent e)
100         {
101           jButton1_actionPerformed(e);
102         }
103       });
104     jButton2.setText("Cancel");
105     jButton2.setBounds(new Rectangle(270, 360, 130, 30));
106     jButton2.addActionListener(new ActionListener()
107       {
108         public void actionPerformed(ActionEvent e)
109         {
110           jButton2_actionPerformed(e);
111         }
112       });
113     jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
114     jLabel5.setForeground(Color.red);
115     jLabel5.setSize(new Dimension(305, 20));
116     jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
117     jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
118     
119     // Code for  JCalendar
120     this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
121     {
122       public void propertyChange(PropertyChangeEvent propertychangeevent)
123       {
124         if (propertychangeevent.getPropertyName().equals("locale"))
125         {
126           jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
127           DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
128           jTextField1.setText(dateformat.format(calendarInicio.getTime()));
129         }
130         else if (propertychangeevent.getPropertyName().equals("calendar"))
131         {
132           calendarInicio = (Calendar) propertychangeevent.getNewValue();
133           DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
134           jTextField1.setText(dateformat1.format(calendarInicio.getTime()));
135           jCalendar1.setCalendar(calendarInicio);
136         }
137       } 
138     });
139     
140     this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener()
141     {
142       public void propertyChange(PropertyChangeEvent propertychangeevent)
143       {
144         if (propertychangeevent.getPropertyName().equals("locale"))
145         {
146           jCalendar2.setLocale((Locale) propertychangeevent.getNewValue());
147           DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar2.getLocale());
148           jTextField2.setText(dateformat.format(calendarFin.getTime()));
149         }
150         else if (propertychangeevent.getPropertyName().equals("calendar"))
151         {
152           calendarFin = (Calendar) propertychangeevent.getNewValue();
153           DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar2.getLocale());
154           jTextField2.setText(dateformat1.format(calendarFin.getTime()));
155           jCalendar2.setCalendar(calendarFin);
156         }
157       } 
158     });
159     
160     
161     this.getContentPane().add(jCalendar2, null);
162     this.getContentPane().add(jCalendar1, null);
163     this.getContentPane().add(jLabel5, null);
164     this.getContentPane().add(jButton2, null);
165     this.getContentPane().add(jButton1, null);
166     this.getContentPane().add(jTextField3, null);
167     this.getContentPane().add(jLabel4, null);
168     this.getContentPane().add(jTextField2, null);
169     this.getContentPane().add(jLabel3, null);
170     this.getContentPane().add(jTextField1, null);
171     this.getContentPane().add(jLabel2, null);
172     this.getContentPane().add(jLabel1, null);
173     this.getContentPane().add(jComboBox1, null);
174     lblNewLabel.setBounds(115, 301, 298, 38);
175     
176     getContentPane().add(lblNewLabel);
177   }
178
179   private void jButton1_actionPerformed(ActionEvent e)
180   {
181                 RuralHouse ruralHouse=((RuralHouse)jComboBox1.getSelectedItem());
182
183                 // The next instruction creates a java.sql.Date object from the date selected in the JCalendar object
184                 Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
185                 // The next instruction removes the hour, minute, second and ms from the date
186                 // This has to be made because the date will be stored in db4o as a java.util.Date object 
187                 // that would store those data, and that would give problems when comparing dates later
188                 firstDay=Date.valueOf(firstDay.toString());
189                 
190                 
191                 Date lastDay=new Date(jCalendar2.getCalendar().getTime().getTime());
192             //Remove the hour:minute:second:ms from the date 
193                 lastDay=Date.valueOf(lastDay.toString());
194                 
195                 
196                 try {
197
198                         //It could be to trigger an exception if the introduced string is not a number
199                         float price= Float.parseFloat(jTextField3.getText());
200
201                         //Obtain the business logic from a StartWindow class (local or remote)
202                         OfferManager offerM = new OfferManager();
203
204                         Offer o = offerM.createOffer(ruralHouse, firstDay, lastDay, price); 
205                         System.out.println("Offer created: "+o.toString());
206
207                         jLabel5.setText("Offer created");
208
209                 } catch (java.lang.NumberFormatException e1) {
210                         jLabel5.setText(jTextField3.getText()+ " is not a valid price");
211                 } catch (OverlappingOfferExists e1) {
212                         jLabel5.setText("There exists an overlapping offer");
213                 }
214                 catch (BadDates e1) {
215                         jLabel5.setText("Last day is before first day in the offer");
216                 } catch (NullPointerException e1){
217                         jLabel5.setText("Bad dates or there exists an overlapping offer");
218                 } catch (Exception e1) {
219                 
220                         e1.printStackTrace();
221                 } 
222   }
223   private void jButton2_actionPerformed(ActionEvent e)
224   {
225     this.setVisible(false);
226   }
227   
228  private void jTextField3_focusLost()
229  {
230    try
231   {
232     new Integer (jTextField3.getText());
233     jLabel5.setText("");
234   }
235   catch (NumberFormatException ex)
236   {
237     jLabel5.setText("Error: Introduce a number");
238   }
239  }
240 }