package gui; import java.text.DateFormat; import java.util.*; import javax.swing.*; import com.toedter.calendar.JCalendar; import java.awt.*; import java.awt.event.*; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import businessLogic.BLFacade; import configuration.UtilDate; import domain.Event; import exceptions.EventFinished; import exceptions.QuestionAlreadyExist; public class CreateQuestionGUI extends JFrame { private static final long serialVersionUID = 1L; private JComboBox jComboBoxEvents = new JComboBox(); DefaultComboBoxModel modelEvents = new DefaultComboBoxModel(); 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 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("CreateQuery")); private JButton jButtonClose = new JButton(ResourceBundle.getBundle("Etiquetas").getString("Close")); private JLabel jLabelMsg = new JLabel(); private JLabel jLabelError = new JLabel(); private Vector datesWithEventsCurrentMonth = new Vector(); public CreateQuestionGUI(Vector v) { try { jbInit(v); } catch (Exception e) { e.printStackTrace(); } } private void jbInit(Vector v) throws Exception { this.getContentPane().setLayout(null); this.setSize(new Dimension(604, 370)); this.setTitle(ResourceBundle.getBundle("Etiquetas").getString("CreateQuery")); jComboBoxEvents.setModel(modelEvents); jComboBoxEvents.setBounds(new Rectangle(275, 47, 250, 20)); jLabelListOfEvents.setBounds(new Rectangle(290, 18, 277, 20)); jLabelQuery.setBounds(new Rectangle(25, 211, 75, 20)); jTextFieldQuery.setBounds(new Rectangle(100, 211, 429, 20)); jLabelMinBet.setBounds(new Rectangle(25, 243, 75, 20)); jTextFieldPrice.setBounds(new Rectangle(100, 243, 60, 20)); jCalendar.setBounds(new Rectangle(40, 50, 225, 150)); scrollPaneEvents.setBounds(new Rectangle(25, 44, 346, 116)); jButtonCreate.setBounds(new Rectangle(100, 275, 130, 30)); jButtonCreate.setEnabled(false); jButtonCreate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jButtonCreate_actionPerformed(e); } }); jButtonClose.setBounds(new Rectangle(275, 275, 130, 30)); jButtonClose.addActionListener(new ActionListener() { 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() { 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 events = facade.getEvents(firstDay); if (events.isEmpty()) jLabelListOfEvents.setText(ResourceBundle.getBundle("Etiquetas").getString("NoEvents") + ": " + dateformat1.format(calendarAct.getTime())); else jLabelListOfEvents.setText(ResourceBundle.getBundle("Etiquetas").getString("Events") + ": " + dateformat1.format(calendarAct.getTime())); jComboBoxEvents.removeAllItems(); System.out.println("Events " + events); for (domain.Event ev : events) 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 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 = (Component) 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")); 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")); } } else jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorQuery")); } catch (EventFinished e1) { jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorEventHasFinished") + ": " + event.getDescription()); } catch (QuestionAlreadyExist e1) { jLabelMsg.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorQueryAlreadyExist")); } catch (java.lang.NumberFormatException e1) { jLabelError.setText(ResourceBundle.getBundle("Etiquetas").getString("ErrorNumber")); } catch (Exception e1) { e1.printStackTrace(); } } private void jButtonClose_actionPerformed(ActionEvent e) { Frame gui = new MainAdminGUI(); gui.setAlwaysOnTop(true); gui.setVisible(true); this.setVisible(false); } }