ISBets21MAUBRY
3cf4057e97448d401b49261033b8ab566105944e
ISBets21MAUBRY / eclipse-workspace / ISBets21MAUBRY / src / main / java / gui / CreateQuestionGUI.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
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); } } |
Commits for ISBets21MAUBRY/eclipse-workspace/ISBets21MAUBRY/src/main/java/gui/CreateQuestionGUI.java
Revision | Author | Commited | Message |
---|---|---|---|
3cf405 ... | porkipig | Sun 16 May, 2021 19:27:29 +0000 | Iteración 3(Versión sin idiomas) |