private void initSpinner() {
    spinner.setBorder(BorderFactory.createEmptyBorder());
    Component textfield = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
    // textfield.removeFocusListener(focusListener);
    textfield.addFocusListener(focusListener);

    // spinner.removeChangeListener(changeListener);
    spinner.addChangeListener(changeListener);

    if (spinner.getModel() instanceof SpinnerNumberModel) isNumberModel = true;
  }
Пример #2
0
  /**
   * This is called every time we want a new booking to be added
   *
   * @param b - The booking we can alter
   * @param c - The remote to switch view
   */
  public final void newBooking(Booking b, Controller c) {
    this.remote = c;
    this.booking = b;
    removeAll();
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    nameField = addTextField("");
    nameField.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createMatteBorder(5, 5, 5, 5, Color.white),
            BorderFactory.createTitledBorder("Namn:")));

    phoneNrField = addTextField("");
    phoneNrField.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createMatteBorder(5, 5, 5, 5, Color.white),
            BorderFactory.createTitledBorder("Telefon:")));

    personsField = addTextField("");
    personsField.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createMatteBorder(5, 5, 5, 5, Color.white),
            BorderFactory.createTitledBorder("Antal personer:")));

    /* Time spinner */
    SpinnerDateModel model = new SpinnerDateModel();
    model.setCalendarField(Calendar.MINUTE);
    spinner = new JSpinner();

    spinner.setModel(model);
    spinner.setEditor(new JSpinner.DateEditor(spinner, "HH:mm"));
    spinner.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createMatteBorder(5, 5, 5, 5, Settings.Styles.applicationBgColor),
            BorderFactory.createTitledBorder("Tid:")));
    add(spinner);
    /* Time spinner */

    add(Box.createRigidArea(new Dimension(0, 10)));

    Border bbb = BorderFactory.createTitledBorder("Antal personer:");
    final DatePicker datePicker = new DatePicker(new Date());
    datePicker.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
    datePicker.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createMatteBorder(5, 5, 5, 5, Settings.Styles.applicationBgColor),
            BorderFactory.createTitledBorder("Datum:")));

    this.add(datePicker);
    durationField = addTextField("");
    durationField.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createMatteBorder(5, 5, 5, 5, Settings.Styles.applicationBgColor),
            BorderFactory.createTitledBorder("Varaktighet i timmar:")));

    add(Box.createRigidArea(new Dimension(0, 10)));
    addBookingBtn.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
    add(addBookingBtn);

    errorMessagesTextArea = new JTextArea();
    errorMessagesTextArea.setEditable(false);

    add(errorMessagesTextArea);
    errorMessagesTextArea.setVisible(false);

    addBookingBtn.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent ae) {
            errorMessagesTextArea.setText("");
            errorMessagesTextArea.setVisible(false);

            bookingTime = (Date) spinner.getValue();
            boolean invalidInput = false;

            if (nameField.getText().isEmpty()
                || personsField.getText().isEmpty()
                || durationField.getText().isEmpty()
                || phoneNrField.getText().isEmpty()) {
              errorMessagesTextArea.append(missingBookingInput + newLine);
              invalidInput = true;
            }

            if (!(isInteger(personsField.getText()) && isInteger(durationField.getText()))) {
              errorMessagesTextArea.append(invalidNumberInput + newLine);
              invalidInput = true;
            }

            // Invalid input if choosen date & time is earlier than the present date and time
            mergeDateWithTime(bookingTime, datePicker.getDate());
            if (bookingTime.getTime() < new Date().getTime()) {
              errorMessagesTextArea.append(invalidDateInput + newLine);
              invalidInput = true;
            }

            if (invalidInput) {
              errorMessagesTextArea.setVisible(true);
              return;
            }

            // Merge datePicker with timePicker
            booking.setName(nameField.getText());
            booking.setPersons(Integer.parseInt(personsField.getText()));
            booking.setPhoneNr(phoneNrField.getText());

            booking.setDate(bookingTime);
            booking.setDuration(Integer.parseInt(durationField.getText()));

            if (ae.getSource().getClass() != JButton.class) {
              return;
            }
            JButton btn = (JButton) ae.getSource();
            if (btn.getParent() == null) {
              return;
            }
            Container parent = btn.getParent().getParent();
            remote.setViewBookings();
            parent.revalidate();
          }
        });
  }