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