4 import java.awt.Component;
5 import java.awt.Dimension;
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;
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;
28 import com.toedter.calendar.JCalendar;
30 import businessLogic.BLFacade;
31 import configuration.UtilDate;
32 import domain.AdminUser;
34 import exceptions.EventFinished;
35 import exceptions.QuestionAlreadyExist;
37 public class CreateQuestionGUI extends JFrame {
38 private static final long serialVersionUID = 1L;
40 private JComboBox<Event> jComboBoxEvents = new JComboBox<Event>();
41 DefaultComboBoxModel<Event> modelEvents = new DefaultComboBoxModel<Event>();
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");
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;
58 private JScrollPane scrollPaneEvents = new JScrollPane();
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();
67 private Vector<Date> datesWithEventsCurrentMonth = new Vector<Date>();
69 private AdminUser userlog = null;
71 public CreateQuestionGUI(Vector<domain.Event> v, AdminUser au) {
72 getContentPane().setBackground(Color.WHITE);
78 } catch (Exception e) {
83 private void jbInit(Vector<domain.Event> v) throws Exception {
85 this.getContentPane().setLayout(null);
86 this.setSize(new Dimension(604, 440));
87 // this.setTitle(ResourceBundle.getBundle("Etiquetas").getString("CreateQuestion"));
88 this.setTitle("CreateQuestion");
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);
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);
109 jButtonCreate.setBounds(new Rectangle(256, 342, 174, 30));
110 jButtonCreate.setEnabled(false);
112 jButtonCreate.addActionListener(new ActionListener() {
114 public void actionPerformed(ActionEvent e) {
115 jButtonCreate_actionPerformed(e);
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() {
124 public void actionPerformed(ActionEvent e) {
125 jButtonClose_actionPerformed(e);
129 jLabelMsg.setBounds(new Rectangle(275, 182, 305, 20));
130 jLabelMsg.setForeground(Color.red);
131 // jLabelMsg.setSize(new Dimension(305, 20));
133 jLabelError.setBounds(new Rectangle(175, 240, 305, 20));
134 jLabelError.setForeground(Color.red);
136 this.getContentPane().add(jLabelMsg, null);
137 this.getContentPane().add(jLabelError, null);
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);
145 this.getContentPane().add(jLabelMinBet, null);
146 this.getContentPane().add(jLabelListOfEvents, null);
147 this.getContentPane().add(jComboBoxEvents, null);
149 this.getContentPane().add(jCalendar, null);
151 BLFacade facade = LoginGUI.getBusinessLogic();
152 datesWithEventsCurrentMonth = facade.getEventsMonth(jCalendar.getDate());
153 paintDaysWithEvents(jCalendar, datesWithEventsCurrentMonth);
155 jLabelEventDate.setBounds(new Rectangle(40, 15, 140, 25));
156 jLabelEventDate.setBounds(40, 16, 140, 25);
157 getContentPane().add(jLabelEventDate);
159 // Code for JCalendar
160 this.jCalendar.addPropertyChangeListener(new PropertyChangeListener() {
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());
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);
185 jCalendar.setCalendar(calendarAct);
187 BLFacade facade = LoginGUI.getBusinessLogic();
189 datesWithEventsCurrentMonth = facade.getEventsMonth(jCalendar.getDate());
192 paintDaysWithEvents(jCalendar, datesWithEventsCurrentMonth);
194 // Date firstDay = UtilDate.trim(new
195 // Date(jCalendar.getCalendar().getTime().getTime()));
196 Date firstDay = UtilDate.trim(calendarAct.getTime());
199 BLFacade facade = LoginGUI.getBusinessLogic();
201 Vector<domain.Event> events = facade.getEvents(firstDay);
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()));
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);
214 for (domain.Event ev : events) {
216 modelEvents.addElement(ev);
219 jComboBoxEvents.repaint();
221 if (events.size() == 0)
222 jButtonCreate.setEnabled(false);
224 jButtonCreate.setEnabled(true);
226 } catch (Exception e1) {
228 jLabelError.setText(e1.getMessage());
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
240 Calendar calendar = jCalendar.getCalendar();
242 int month = calendar.get(Calendar.MONTH);
243 int today = calendar.get(Calendar.DAY_OF_MONTH);
244 int year = calendar.get(Calendar.YEAR);
246 calendar.set(Calendar.DAY_OF_MONTH, 1);
247 int offset = calendar.get(Calendar.DAY_OF_WEEK);
249 if (Locale.getDefault().equals(new Locale("es")))
254 for (Date d : datesWithEventsCurrentMonth) {
257 System.out.println(d);
259 // Obtain the component of the day in the panel of the DayChooser of the
261 // The component is located after the decorator buttons of "Sun", "Mon",... or
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);
272 calendar.set(Calendar.DAY_OF_MONTH, today);
273 calendar.set(Calendar.MONTH, month);
274 calendar.set(Calendar.YEAR, year);
278 private void jButtonCreate_actionPerformed(ActionEvent e) {
279 domain.Event event = ((domain.Event) jComboBoxEvents.getSelectedItem());
282 jLabelError.setText("");
283 jLabelMsg.setText("");
285 // Displays an exception if the query field is empty
286 String inputQuery = jTextFieldQuery.getText();
288 if (inputQuery.length() > 0) {
290 // It could be to trigger an exception if the introduced string is not a number
291 float inputPrice = Float.parseFloat(jTextFieldPrice.getText());
294 // jLabelError.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorNumber"));
295 jLabelError.setText("ErrorNumber");
298 // Obtain the business logic from a StartWindow class (local or remote)
299 BLFacade facade = LoginGUI.getBusinessLogic();
301 facade.createQuestion(event, inputQuery, inputPrice);
303 // jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("QueryCreated"));
304 jLabelMsg.setText("QueryCreated");
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) {
321 e1.printStackTrace();
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);