Versión del proyecto de Apuestas tras la iteración 1
[ISBets21MAUBRY] / eclipse-workspace / ISBets21BRYMAUJONUNA / src / main / java / gui / CreateQuestionGUI.java
1 package gui;
2
3 import java.text.DateFormat;
4 import java.util.*;
5
6 import javax.swing.*;
7
8 import com.toedter.calendar.JCalendar;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14
15 import businessLogic.BLFacade;
16 import configuration.UtilDate;
17 import domain.Event;
18 import exceptions.EventFinished;
19 import exceptions.QuestionAlreadyExist;
20
21 public class CreateQuestionGUI extends JFrame {
22         private static final long serialVersionUID = 1L;
23
24         private JComboBox<Event> jComboBoxEvents = new JComboBox<Event>();
25         DefaultComboBoxModel<Event> modelEvents = new DefaultComboBoxModel<Event>();
26
27         private JLabel jLabelListOfEvents = new JLabel(ResourceBundle.getBundle("Etiquetas").getString("ListEvents"));
28         private JLabel jLabelQuery = new JLabel(ResourceBundle.getBundle("Etiquetas").getString("Query"));
29         private JLabel jLabelMinBet = new JLabel(ResourceBundle.getBundle("Etiquetas").getString("MinimumBetPrice"));
30         private JLabel jLabelEventDate = new JLabel(ResourceBundle.getBundle("Etiquetas").getString("EventDate"));
31
32         private JTextField jTextFieldQuery = new JTextField();
33         private JTextField jTextFieldPrice = new JTextField();
34         private JCalendar jCalendar = new JCalendar();
35         private Calendar calendarAct = null;
36         private Calendar calendarAnt = null;
37
38         private JScrollPane scrollPaneEvents = new JScrollPane();
39
40         private JButton jButtonCreate = new JButton(ResourceBundle.getBundle("Etiquetas").getString("CreateQuery"));
41         private JButton jButtonClose = new JButton(ResourceBundle.getBundle("Etiquetas").getString("Close"));
42         private JLabel jLabelMsg = new JLabel();
43         private JLabel jLabelError = new JLabel();
44         
45         private Vector<Date> datesWithEventsCurrentMonth = new Vector<Date>();
46
47         public CreateQuestionGUI(Vector<domain.Event> v) {
48                 try {
49                         jbInit(v);
50                 } catch (Exception e) {
51                         e.printStackTrace();
52                 }
53         }
54
55         private void jbInit(Vector<domain.Event> v) throws Exception {
56
57                 this.getContentPane().setLayout(null);
58                 this.setSize(new Dimension(604, 370));
59                 this.setTitle(ResourceBundle.getBundle("Etiquetas").getString("CreateQuery"));
60
61                 jComboBoxEvents.setModel(modelEvents);
62                 jComboBoxEvents.setBounds(new Rectangle(275, 47, 250, 20));
63                 jLabelListOfEvents.setBounds(new Rectangle(290, 18, 277, 20));
64                 jLabelQuery.setBounds(new Rectangle(25, 211, 75, 20));
65                 jTextFieldQuery.setBounds(new Rectangle(100, 211, 429, 20));
66                 jLabelMinBet.setBounds(new Rectangle(25, 243, 75, 20));
67                 jTextFieldPrice.setBounds(new Rectangle(100, 243, 60, 20));
68
69                 jCalendar.setBounds(new Rectangle(40, 50, 225, 150));
70                 scrollPaneEvents.setBounds(new Rectangle(25, 44, 346, 116));
71
72                 jButtonCreate.setBounds(new Rectangle(100, 275, 130, 30));
73                 jButtonCreate.setEnabled(false);
74
75                 jButtonCreate.addActionListener(new ActionListener() {
76                         public void actionPerformed(ActionEvent e) {
77                                 jButtonCreate_actionPerformed(e);
78                         }
79                 });
80                 jButtonClose.setBounds(new Rectangle(275, 275, 130, 30));
81                 jButtonClose.addActionListener(new ActionListener() {
82                         public void actionPerformed(ActionEvent e) {
83                                 jButtonClose_actionPerformed(e);
84                         }
85                 });
86
87                 jLabelMsg.setBounds(new Rectangle(275, 182, 305, 20));
88                 jLabelMsg.setForeground(Color.red);
89                 // jLabelMsg.setSize(new Dimension(305, 20));
90
91                 jLabelError.setBounds(new Rectangle(175, 240, 305, 20));
92                 jLabelError.setForeground(Color.red);
93
94                 this.getContentPane().add(jLabelMsg, null);
95                 this.getContentPane().add(jLabelError, null);
96
97                 this.getContentPane().add(jButtonClose, null);
98                 this.getContentPane().add(jButtonCreate, null);
99                 this.getContentPane().add(jTextFieldQuery, null);
100                 this.getContentPane().add(jLabelQuery, null);
101                 this.getContentPane().add(jTextFieldPrice, null);
102
103                 this.getContentPane().add(jLabelMinBet, null);
104                 this.getContentPane().add(jLabelListOfEvents, null);
105                 this.getContentPane().add(jComboBoxEvents, null);
106
107                 this.getContentPane().add(jCalendar, null);
108                 
109                 
110                 BLFacade facade = LoginGUI.getBusinessLogic();
111                 datesWithEventsCurrentMonth=facade.getEventsMonth(jCalendar.getDate());
112                 paintDaysWithEvents(jCalendar,datesWithEventsCurrentMonth);
113                 
114                 
115
116                 jLabelEventDate.setBounds(new Rectangle(40, 15, 140, 25));
117                 jLabelEventDate.setBounds(40, 16, 140, 25);
118                 getContentPane().add(jLabelEventDate);
119
120                 
121                 // Code for JCalendar
122                 this.jCalendar.addPropertyChangeListener(new PropertyChangeListener() {
123                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
124 //                              this.jCalendar.addPropertyChangeListener(new PropertyChangeListener() {
125 //                                      public void propertyChange(PropertyChangeEvent propertychangeevent) {
126                                 if (propertychangeevent.getPropertyName().equals("locale")) {
127                                         jCalendar.setLocale((Locale) propertychangeevent.getNewValue());
128                                 } else if (propertychangeevent.getPropertyName().equals("calendar")) {
129                                         calendarAnt = (Calendar) propertychangeevent.getOldValue();
130                                         calendarAct = (Calendar) propertychangeevent.getNewValue();
131                                         System.out.println("calendarAnt: "+calendarAnt.getTime());
132                                         System.out.println("calendarAct: "+calendarAct.getTime());
133                                         DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar.getLocale());
134                                         
135                                         int monthAnt = calendarAnt.get(Calendar.MONTH);
136                                         int monthAct = calendarAct.get(Calendar.MONTH);
137                                         if (monthAct!=monthAnt) {
138                                                 if (monthAct==monthAnt+2) { 
139                                                         // Si en JCalendar está 30 de enero y se avanza al mes siguiente, devolvería 2 de marzo (se toma como equivalente a 30 de febrero)
140                                                         // Con este código se dejará como 1 de febrero en el JCalendar
141                                                         calendarAct.set(Calendar.MONTH, monthAnt+1);
142                                                         calendarAct.set(Calendar.DAY_OF_MONTH, 1);
143                                                 }
144                                                 
145                                                 jCalendar.setCalendar(calendarAct);
146                                                 
147                                                 BLFacade facade = LoginGUI.getBusinessLogic();
148
149                                                 datesWithEventsCurrentMonth=facade.getEventsMonth(jCalendar.getDate());
150                                         }
151
152
153
154                                         paintDaysWithEvents(jCalendar,datesWithEventsCurrentMonth);
155
156                                         //      Date firstDay = UtilDate.trim(new Date(jCalendar.getCalendar().getTime().getTime()));
157                                         Date firstDay = UtilDate.trim(calendarAct.getTime());
158
159                                         try {
160                                                 BLFacade facade = LoginGUI.getBusinessLogic();
161
162                                                 Vector<domain.Event> events = facade.getEvents(firstDay);
163
164                                                 if (events.isEmpty())
165                                                         jLabelListOfEvents.setText(ResourceBundle.getBundle("Etiquetas").getString("NoEvents")
166                                                                         + ": " + dateformat1.format(calendarAct.getTime()));
167                                                 else
168                                                         jLabelListOfEvents.setText(ResourceBundle.getBundle("Etiquetas").getString("Events") + ": "
169                                                                         + dateformat1.format(calendarAct.getTime()));
170                                                 jComboBoxEvents.removeAllItems();
171                                                 System.out.println("Events " + events);
172
173                                                 for (domain.Event ev : events)
174                                                         modelEvents.addElement(ev);
175                                                 jComboBoxEvents.repaint();
176
177                                                 if (events.size() == 0)
178                                                         jButtonCreate.setEnabled(false);
179                                                 else
180                                                         jButtonCreate.setEnabled(true);
181
182                                         } catch (Exception e1) {
183
184                                                 jLabelError.setText(e1.getMessage());
185                                         }
186
187                                 }
188                         }
189                 });
190         }
191
192         
193 public static void paintDaysWithEvents(JCalendar jCalendar,Vector<Date> datesWithEventsCurrentMonth) {
194                 // For each day with events in current month, the background color for that day is changed.
195
196                 
197                 Calendar calendar = jCalendar.getCalendar();
198                 
199                 int month = calendar.get(Calendar.MONTH);
200                 int today=calendar.get(Calendar.DAY_OF_MONTH);
201                 int year=calendar.get(Calendar.YEAR);
202                 
203                 calendar.set(Calendar.DAY_OF_MONTH, 1);
204                 int offset = calendar.get(Calendar.DAY_OF_WEEK);
205
206                 if (Locale.getDefault().equals(new Locale("es")))
207                         offset += 4;
208                 else
209                         offset += 5;
210                 
211                 
212                 for (Date d:datesWithEventsCurrentMonth){
213
214                         calendar.setTime(d);
215                         System.out.println(d);
216                         
217
218                         
219                         // Obtain the component of the day in the panel of the DayChooser of the
220                         // JCalendar.
221                         // The component is located after the decorator buttons of "Sun", "Mon",... or
222                         // "Lun", "Mar"...,
223                         // the empty days before day 1 of month, and all the days previous to each day.
224                         // That number of components is calculated with "offset" and is different in
225                         // English and Spanish
226 //                                        Component o=(Component) jCalendar.getDayChooser().getDayPanel().getComponent(i+offset);; 
227                         Component o = (Component) jCalendar.getDayChooser().getDayPanel()
228                                         .getComponent(calendar.get(Calendar.DAY_OF_MONTH) + offset);
229                         o.setBackground(Color.CYAN);
230                 }
231                 
232                         calendar.set(Calendar.DAY_OF_MONTH, today);
233                         calendar.set(Calendar.MONTH, month);
234                         calendar.set(Calendar.YEAR, year);
235
236                 
237         }
238         
239          
240         private void jButtonCreate_actionPerformed(ActionEvent e) {
241                 domain.Event event = ((domain.Event) jComboBoxEvents.getSelectedItem());
242
243                 try {
244                         jLabelError.setText("");
245                         jLabelMsg.setText("");
246
247                         // Displays an exception if the query field is empty
248                         String inputQuery = jTextFieldQuery.getText();
249
250                         if (inputQuery.length() > 0) {
251
252                                 // It could be to trigger an exception if the introduced string is not a number
253                                 float inputPrice = Float.parseFloat(jTextFieldPrice.getText());
254
255                                 if (inputPrice <= 0)
256                                         jLabelError.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorNumber"));
257                                 else {
258
259                                         // Obtain the business logic from a StartWindow class (local or remote)
260                                         BLFacade facade = LoginGUI.getBusinessLogic();
261
262                                         facade.createQuestion(event, inputQuery, inputPrice);
263
264                                         jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("QueryCreated"));
265                                 }
266                         } else
267                                 jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorQuery"));
268                 } catch (EventFinished e1) {
269                         jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorEventHasFinished") + ": "
270                                         + event.getDescription());
271                 } catch (QuestionAlreadyExist e1) {
272                         jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorQueryAlreadyExist"));
273                 } catch (java.lang.NumberFormatException e1) {
274                         jLabelError.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorNumber"));
275                 } catch (Exception e1) {
276
277                         e1.printStackTrace();
278
279                 }
280         }
281
282         private void jButtonClose_actionPerformed(ActionEvent e) {
283                 Frame gui = new MainAdminGUI();
284                 gui.setAlwaysOnTop(true);
285                 gui.setVisible(true);
286                 this.setVisible(false);
287         }
288 }