--- /dev/null
+package gui;
+
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.Frame;
+import java.awt.Rectangle;
+import java.awt.SystemColor;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.text.DateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+import java.util.Vector;
+
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.JButton;
+import javax.swing.JComboBox;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextField;
+
+import com.toedter.calendar.JCalendar;
+
+import businessLogic.BLFacade;
+import configuration.UtilDate;
+import domain.AdminUser;
+import domain.Event;
+import exceptions.EventFinished;
+import exceptions.QuestionAlreadyExist;
+
+public class CreateQuestionGUI extends JFrame {
+ private static final long serialVersionUID = 1L;
+
+ private JComboBox<Event> jComboBoxEvents = new JComboBox<Event>();
+ DefaultComboBoxModel<Event> modelEvents = new DefaultComboBoxModel<Event>();
+
+// private JLabel jLabelListOfEvents = new JLabel(ResourceBundle.getBundle("Etiquetas").getString("ListEvents"));
+// private JLabel jLabelQuery = new JLabel(ResourceBundle.getBundle("Etiquetas").getString("Query"));
+// private JLabel jLabelMinBet = new JLabel(ResourceBundle.getBundle("Etiquetas").getString("MinimumBetPrice"));
+// private JLabel jLabelEventDate = new JLabel(ResourceBundle.getBundle("Etiquetas").getString("EventDate"));
+ private JLabel jLabelListOfEvents = new JLabel("ListEvents");
+ private JLabel jLabelQuery = new JLabel("Query");
+ private JLabel jLabelMinBet = new JLabel("MinimumBet");
+ private JLabel jLabelEventDate = new JLabel("EventDate");
+
+ private JTextField jTextFieldQuery = new JTextField();
+ private JTextField jTextFieldPrice = new JTextField();
+ private JCalendar jCalendar = new JCalendar();
+ private Calendar calendarAct = null;
+ private Calendar calendarAnt = null;
+
+ private JScrollPane scrollPaneEvents = new JScrollPane();
+
+// private JButton jButtonCreate = new JButton(ResourceBundle.getBundle("Etiquetas").getString("CreateQuestion"));
+// private JButton jButtonClose = new JButton(ResourceBundle.getBundle("Etiquetas").getString("Close"));
+ private JButton jButtonCreate = new JButton("CreateQuestion");
+ private JButton jButtonClose = new JButton("Close");
+ private JLabel jLabelMsg = new JLabel();
+ private JLabel jLabelError = new JLabel();
+
+ private Vector<Date> datesWithEventsCurrentMonth = new Vector<Date>();
+
+ private AdminUser userlog = null;
+
+ public CreateQuestionGUI(Vector<domain.Event> v, AdminUser au) {
+ getContentPane().setBackground(Color.WHITE);
+
+ userlog = au;
+
+ try {
+ jbInit(v);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void jbInit(Vector<domain.Event> v) throws Exception {
+
+ this.getContentPane().setLayout(null);
+ this.setSize(new Dimension(604, 440));
+// this.setTitle(ResourceBundle.getBundle("Etiquetas").getString("CreateQuestion"));
+ this.setTitle("CreateQuestion");
+
+ jComboBoxEvents.setModel(modelEvents);
+ jComboBoxEvents.setBounds(new Rectangle(275, 47, 250, 20));
+ jLabelListOfEvents.setBounds(new Rectangle(290, 18, 277, 20));
+ jLabelQuery.setBounds(new Rectangle(25, 235, 75, 20));
+ jTextFieldQuery.setBounds(new Rectangle(100, 230, 437, 30));
+ jTextFieldQuery.setForeground(Color.GRAY);
+ jTextFieldQuery.setFont(new Font("Arial", Font.PLAIN, 16));
+ jLabelMinBet.setBounds(new Rectangle(25, 283, 75, 20));
+ jTextFieldPrice.setBounds(new Rectangle(110, 282, 60, 20));
+ jTextFieldPrice.setForeground(Color.GRAY);
+ jTextFieldPrice.setFont(new Font("Arial", Font.PLAIN, 15));
+ jCalendar.getDayChooser().getDayPanel().setBackground(Color.WHITE);
+
+ jCalendar.setBounds(new Rectangle(40, 50, 225, 150));
+ scrollPaneEvents.setBounds(new Rectangle(25, 44, 346, 116));
+ jButtonCreate.setForeground(Color.WHITE);
+ jButtonCreate.setFont(new Font("Dialog", Font.BOLD, 14));
+ jButtonCreate.setBackground(SystemColor.textHighlight);
+
+ jButtonCreate.setBounds(new Rectangle(256, 342, 174, 30));
+ jButtonCreate.setEnabled(false);
+
+ jButtonCreate.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ jButtonCreate_actionPerformed(e);
+ }
+ });
+ jButtonClose.setForeground(Color.WHITE);
+ jButtonClose.setFont(new Font("Dialog", Font.BOLD, 14));
+ jButtonClose.setBackground(SystemColor.controlShadow);
+ jButtonClose.setBounds(new Rectangle(25, 342, 130, 30));
+ jButtonClose.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ jButtonClose_actionPerformed(e);
+ }
+ });
+
+ jLabelMsg.setBounds(new Rectangle(275, 182, 305, 20));
+ jLabelMsg.setForeground(Color.red);
+ // jLabelMsg.setSize(new Dimension(305, 20));
+
+ jLabelError.setBounds(new Rectangle(175, 240, 305, 20));
+ jLabelError.setForeground(Color.red);
+
+ this.getContentPane().add(jLabelMsg, null);
+ this.getContentPane().add(jLabelError, null);
+
+ this.getContentPane().add(jButtonClose, null);
+ this.getContentPane().add(jButtonCreate, null);
+ this.getContentPane().add(jTextFieldQuery, null);
+ this.getContentPane().add(jLabelQuery, null);
+ this.getContentPane().add(jTextFieldPrice, null);
+
+ this.getContentPane().add(jLabelMinBet, null);
+ this.getContentPane().add(jLabelListOfEvents, null);
+ this.getContentPane().add(jComboBoxEvents, null);
+
+ this.getContentPane().add(jCalendar, null);
+
+ BLFacade facade = LoginGUI.getBusinessLogic();
+ datesWithEventsCurrentMonth = facade.getEventsMonth(jCalendar.getDate());
+ paintDaysWithEvents(jCalendar, datesWithEventsCurrentMonth);
+
+ jLabelEventDate.setBounds(new Rectangle(40, 15, 140, 25));
+ jLabelEventDate.setBounds(40, 16, 140, 25);
+ getContentPane().add(jLabelEventDate);
+
+ // Code for JCalendar
+ this.jCalendar.addPropertyChangeListener(new PropertyChangeListener() {
+ @Override
+ public void propertyChange(PropertyChangeEvent propertychangeevent) {
+// this.jCalendar.addPropertyChangeListener(new PropertyChangeListener() {
+// public void propertyChange(PropertyChangeEvent propertychangeevent) {
+ if (propertychangeevent.getPropertyName().equals("locale")) {
+ jCalendar.setLocale((Locale) propertychangeevent.getNewValue());
+ } else if (propertychangeevent.getPropertyName().equals("calendar")) {
+ calendarAnt = (Calendar) propertychangeevent.getOldValue();
+ calendarAct = (Calendar) propertychangeevent.getNewValue();
+ System.out.println("calendarAnt: " + calendarAnt.getTime());
+ System.out.println("calendarAct: " + calendarAct.getTime());
+ DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar.getLocale());
+
+ int monthAnt = calendarAnt.get(Calendar.MONTH);
+ int monthAct = calendarAct.get(Calendar.MONTH);
+ if (monthAct != monthAnt) {
+ if (monthAct == monthAnt + 2) {
+ // 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)
+ // Con este código se dejará como 1 de febrero en el JCalendar
+ calendarAct.set(Calendar.MONTH, monthAnt + 1);
+ calendarAct.set(Calendar.DAY_OF_MONTH, 1);
+ }
+
+ jCalendar.setCalendar(calendarAct);
+
+ BLFacade facade = LoginGUI.getBusinessLogic();
+
+ datesWithEventsCurrentMonth = facade.getEventsMonth(jCalendar.getDate());
+ }
+
+ paintDaysWithEvents(jCalendar, datesWithEventsCurrentMonth);
+
+ // Date firstDay = UtilDate.trim(new
+ // Date(jCalendar.getCalendar().getTime().getTime()));
+ Date firstDay = UtilDate.trim(calendarAct.getTime());
+
+ try {
+ BLFacade facade = LoginGUI.getBusinessLogic();
+
+ Vector<domain.Event> events = facade.getEvents(firstDay);
+
+ if (events.isEmpty())
+// jLabelListOfEvents.setText(ResourceBundle.getBundle("Etiquetas").getString("NoEvents")
+// + ": " + dateformat1.format(calendarAct.getTime()));
+ jLabelListOfEvents.setText("NoEvents" + ": " + dateformat1.format(calendarAct.getTime()));
+ else
+// jLabelListOfEvents.setText(ResourceBundle.getBundle("Etiquetas").getString("Events") + ": "
+// + dateformat1.format(calendarAct.getTime()));
+ jLabelListOfEvents.setText("Events" + ": " + dateformat1.format(calendarAct.getTime()));
+ jComboBoxEvents.removeAllItems();
+ System.out.println("Events " + events);
+
+ for (domain.Event ev : events) {
+ if (!ev.getClosed())
+ modelEvents.addElement(ev);
+ }
+
+ jComboBoxEvents.repaint();
+
+ if (events.size() == 0)
+ jButtonCreate.setEnabled(false);
+ else
+ jButtonCreate.setEnabled(true);
+
+ } catch (Exception e1) {
+
+ jLabelError.setText(e1.getMessage());
+ }
+
+ }
+ }
+ });
+ }
+
+ public static void paintDaysWithEvents(JCalendar jCalendar, Vector<Date> datesWithEventsCurrentMonth) {
+ // For each day with events in current month, the background color for that day
+ // is changed.
+
+ Calendar calendar = jCalendar.getCalendar();
+
+ int month = calendar.get(Calendar.MONTH);
+ int today = calendar.get(Calendar.DAY_OF_MONTH);
+ int year = calendar.get(Calendar.YEAR);
+
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
+ int offset = calendar.get(Calendar.DAY_OF_WEEK);
+
+ if (Locale.getDefault().equals(new Locale("es")))
+ offset += 4;
+ else
+ offset += 5;
+
+ for (Date d : datesWithEventsCurrentMonth) {
+
+ calendar.setTime(d);
+ System.out.println(d);
+
+ // Obtain the component of the day in the panel of the DayChooser of the
+ // JCalendar.
+ // The component is located after the decorator buttons of "Sun", "Mon",... or
+ // "Lun", "Mar"...,
+ // the empty days before day 1 of month, and all the days previous to each day.
+ // That number of components is calculated with "offset" and is different in
+ // English and Spanish
+// Component o=(Component) jCalendar.getDayChooser().getDayPanel().getComponent(i+offset);;
+ Component o = jCalendar.getDayChooser().getDayPanel()
+ .getComponent(calendar.get(Calendar.DAY_OF_MONTH) + offset);
+ o.setBackground(Color.CYAN);
+ }
+
+ calendar.set(Calendar.DAY_OF_MONTH, today);
+ calendar.set(Calendar.MONTH, month);
+ calendar.set(Calendar.YEAR, year);
+
+ }
+
+ private void jButtonCreate_actionPerformed(ActionEvent e) {
+ domain.Event event = ((domain.Event) jComboBoxEvents.getSelectedItem());
+
+ try {
+ jLabelError.setText("");
+ jLabelMsg.setText("");
+
+ // Displays an exception if the query field is empty
+ String inputQuery = jTextFieldQuery.getText();
+
+ if (inputQuery.length() > 0) {
+
+ // It could be to trigger an exception if the introduced string is not a number
+ float inputPrice = Float.parseFloat(jTextFieldPrice.getText());
+
+ if (inputPrice <= 0)
+// jLabelError.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorNumber"));
+ jLabelError.setText("ErrorNumber");
+ else {
+
+ // Obtain the business logic from a StartWindow class (local or remote)
+ BLFacade facade = LoginGUI.getBusinessLogic();
+
+ facade.createQuestion(event, inputQuery, inputPrice);
+
+// jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("QueryCreated"));
+ jLabelMsg.setText("QueryCreated");
+ }
+ } else
+// jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorQuery"));
+ jLabelMsg.setText("ErrorQuery");
+ } catch (EventFinished e1) {
+// jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorEventHasFinished") + ": "
+// + event.getDescription());
+ jLabelMsg.setText("ErrorEventHasFinished" + ": " + event.getDescription());
+ } catch (QuestionAlreadyExist e1) {
+// jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorQueryAlreadyExist"));
+ jLabelMsg.setText("ErrorQueryAlreadyExist");
+ } catch (java.lang.NumberFormatException e1) {
+// jLabelError.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorNumber"));
+ jLabelError.setText("ErrorNumber");
+ } catch (Exception e1) {
+
+ e1.printStackTrace();
+
+ }
+ }
+
+ private void jButtonClose_actionPerformed(ActionEvent e) {
+ Frame gui = new MainAdminGUI(userlog);
+ gui.setAlwaysOnTop(true);
+ gui.setVisible(true);
+ this.setVisible(false);
+ }
+}
\ No newline at end of file