Esempio n. 1
0
  private boolean setFocusToSelection() {
    if (dayPanel != null) {
      if (cdt.hasSelection()) {
        Calendar first = cdt.getCalendarInstance((Date) dayButtons[0].getData(CDT.Key.Date));
        first.set(Calendar.MILLISECOND, 0);
        first.set(Calendar.SECOND, 0);
        first.set(Calendar.MINUTE, 0);
        first.set(Calendar.HOUR_OF_DAY, 0);

        Calendar last =
            cdt.getCalendarInstance((Date) dayButtons[dayButtons.length - 1].getData(CDT.Key.Date));
        last.set(Calendar.MILLISECOND, 0);
        last.set(Calendar.SECOND, 0);
        last.set(Calendar.MINUTE, 0);
        last.set(Calendar.HOUR_OF_DAY, 0);
        last.add(Calendar.DATE, 1);
        last.add(Calendar.MILLISECOND, -1);

        Date selection = cdt.getSelection();
        Calendar scal = cdt.getCalendarInstance(selection);
        for (int j = 0; j < dayButtons.length; j++) {
          Calendar tmpcal = cdt.getCalendarInstance((Date) dayButtons[j].getData(CDT.Key.Date));
          if ((scal.get(Calendar.DATE) == tmpcal.get(Calendar.DATE))
              && (scal.get(Calendar.MONTH) == tmpcal.get(Calendar.MONTH))
              && (scal.get(Calendar.YEAR) == tmpcal.get(Calendar.YEAR))) {
            return dayButtons[j].setFocus();
          }
        }
      } else {
        dayButtons[0].setFocus();
      }
    }
    return true;
  }
Esempio n. 2
0
 /**
  * set / update the text of the displayLabels. these are the Week column headers above the days on
  * the Calendar part of the <code>CDateTime</code>.
  */
 private void updateDaysOfWeek() {
   if (dayPanel != null) {
     Calendar tmpcal = cdt.getCalendarInstance();
     tmpcal.set(Calendar.DAY_OF_WEEK, tmpcal.getFirstDayOfWeek());
     Locale locale = cdt.getLocale();
     boolean ltr =
         (ComponentOrientation.getOrientation(locale).isLeftToRight()
             && !locale.getLanguage().equals("zh")); // $NON-NLS-1$
     BreakIterator iterator = BreakIterator.getCharacterInstance(locale);
     for (int x = 0; x < dayLabels.length; x++) {
       String str = getFormattedDate("E", tmpcal.getTime()); // $NON-NLS-1$
       if (dayLabels[x].getData(CDT.Key.Compact, Boolean.class)) {
         iterator.setText(str);
         int start, end;
         if (ltr) {
           start = iterator.first();
           end = iterator.next();
         } else {
           end = iterator.last();
           start = iterator.previous();
         }
         dayLabels[x].setText(str.substring(start, end));
       } else {
         dayLabels[x].setText(str);
       }
       tmpcal.add(Calendar.DAY_OF_WEEK, 1);
     }
   }
 }
Esempio n. 3
0
 /**
  * Modifies the given Calendar field by the given amount for every dayButton.<br>
  * calendar.add(CalendarField, amount)
  *
  * @param field Calendar Field
  * @param amount adjustment to be added
  */
 private void adjustDays(int field, int amount) {
   Calendar tmpcal = cdt.getCalendarInstance();
   for (int day = 0; day < dayButtons.length; day++) {
     tmpcal.setTime(dayButtons[day].getData(CDT.Key.Date, Date.class));
     tmpcal.add(field, amount);
     dayButtons[day].setData(CDT.Key.Date, tmpcal.getTime());
   }
 }
Esempio n. 4
0
 private void updateYears() {
   if (yearPanel != null) {
     Calendar tmpcal = cdt.getCalendarInstance();
     tmpcal.add(Calendar.YEAR, -7);
     for (int i = 0; i < yearButtons.length; i++) {
       yearButtons[i].setText(getFormattedDate("yyyy", tmpcal.getTime())); // $NON-NLS-1$
       tmpcal.add(Calendar.YEAR, 1);
     }
   }
 }
Esempio n. 5
0
 private void updateMonths() {
   if (monthPanel != null) {
     Calendar tmpcal = cdt.getCalendarInstance();
     for (int i = 0; i < 12; i++) {
       tmpcal.set(Calendar.MONTH, i);
       monthButtons[i].setText(getFormattedDate("MMM", tmpcal.getTime())); // $NON-NLS-1$
       monthButtons[i].setData("Month", new Integer(tmpcal.get(Calendar.MONTH))); // $NON-NLS-1$
     }
   }
 }
Esempio n. 6
0
  /**
   * set / update the text of the header - <code>monthLabel</code>, <code>yearLabel</code>, and the
   * <code>monthLabel</code> context menu.
   */
  private void updateHeader() {
    if (header != null) {
      Calendar selected = cdt.getCalendarInstance();

      if (monthButton != null) {
        setMonthLabelText();
      }

      if (monthItems != null) {
        Calendar tmpcal = cdt.getCalendarInstance();
        for (int i = 0; i < 12; i++) {
          tmpcal.set(Calendar.MONTH, i);
          monthItems[i].setText(getFormattedDate("MMMM", tmpcal.getTime())); // $NON-NLS-1$
          monthItems[i].setData("Month", new Integer(tmpcal.get(Calendar.MONTH))); // $NON-NLS-1$
          if (selected.get(Calendar.MONDAY) == tmpcal.get(Calendar.MONTH)) {
            monthItems[i].setImage(Resources.getIconBullet());
          } else {
            monthItems[i].setImage(null);
          }
        }
      }

      if (yearButton != null) {
        yearButton.setText(getFormattedDate("yyyy", cdt.getCalendarTime())); // $NON-NLS-1$
      }

      if (yearItems != null) {
        Calendar tmpcal = cdt.getCalendarInstance();
        tmpcal.add(Calendar.YEAR, -5);
        for (int i = 0; i < 11; i++) {
          yearItems[i].setText(getFormattedDate("yyyy", tmpcal.getTime())); // $NON-NLS-1$
          if (selected.get(Calendar.YEAR) == tmpcal.get(Calendar.YEAR)) {
            yearItems[i].setImage(Resources.getIconBullet());
          } else {
            yearItems[i].setImage(null);
          }
          tmpcal.add(Calendar.YEAR, 1);
        }
      }

      header.layout();
    }
  }
Esempio n. 7
0
  /**
   * Set the date for each dayButton by starting with the given <code>firstDate</code> and iterating
   * over all the dayButtons, adding 1 day to the date with each iteration.<br>
   * The date is stored in the dayButton with: setData(CDT.Key.Date, date).<br>
   * If <code>alignMonth</code> is true, then the actual first date used will be modified to be the
   * first date of the visible calendar which includes the given <code>firstDate</code>
   *
   * @param firstDate the first date of the dayButtons
   * @param alignMonth whether or not to align the month
   */
  private void setDays(Date firstDate, boolean alignMonth) {
    Calendar tmpcal = cdt.getCalendarInstance();
    tmpcal.setTime(firstDate);

    if (alignMonth) {
      tmpcal.set(Calendar.DATE, 1);
      int firstDay = tmpcal.get(Calendar.DAY_OF_WEEK) - tmpcal.getFirstDayOfWeek();
      if (firstDay < 0) {
        firstDay += 7;
      }
      tmpcal.add(Calendar.DATE, -firstDay);
    }

    for (int day = 0; day < dayButtons.length; day++) {
      dayButtons[day].setData(CDT.Key.Date, tmpcal.getTime());
      tmpcal.add(Calendar.DATE, 1);
    }
  }
Esempio n. 8
0
 /** set / update the text of the <code>footerButton</code>. */
 private void updateFooter() {
   if (footer != null) {
     Locale locale = cdt.getLocale();
     if (today != null) {
       if (cdt.builder.hasFooter(Footer.VERBOSE_TODAY)) {
         Calendar cal = cdt.getCalendarInstance(System.currentTimeMillis());
         Object[] margs = {
           cal.getTime(),
           Resources.getString("date_ordinal_" + cal.get(Calendar.DATE), locale) // $NON-NLS-1$
         };
         MessageFormat formatter =
             new MessageFormat(
                 Resources.getString("today_verbose.text", locale), locale); // $NON-NLS-1$
         today.setText(formatter.format(margs));
       } else {
         today.setText(Resources.getString("today.text", locale)); // $NON-NLS-1$
       }
     }
     if (clear != null) {
       clear.setText(Resources.getString("clear.text", locale)); // $NON-NLS-1$
     }
     footer.layout();
   }
 }
Esempio n. 9
0
  /**
   * create the header for the Calendar part of this CDateTime<br>
   * there is no equivalent for the Clock part
   */
  private void createHeader() {
    header = new VPanel(this, SWT.NONE);
    header.setData(CDT.PickerPart, PickerPart.HeaderPanel);
    header.setPainter(cdt.getPainter());
    cdt.getPainter().update(header);
    VGridLayout layout = new VGridLayout();
    layout.makeColumnsEqualWidth = cdt.builder.getHeaderEqualColumns();
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 2;
    layout.marginWidth = 2;
    header.setLayout(layout);
    header.setLayoutData(new GridData(cdt.builder.getHeaderAlignment(), SWT.FILL, true, false));

    GridData data;

    for (Iterator<Header> iter = cdt.builder.getHeaders().iterator(); iter.hasNext(); ) {
      ((VGridLayout) header.getLayout()).numColumns++;
      Header h = iter.next();
      data = new GridData(h.alignment, SWT.FILL, h.grab, false);
      switch (h.type) {
        case Header.DATE_NOW:
          dateNow = new VButton(header, SWT.PUSH | SWT.NO_FOCUS);
          dateNow.setData(CDT.PickerPart, PickerPart.DateNow);
          dateNow.setPainter(cdt.getPainter());
          cdt.getPainter().update(dateNow);
          dateNow.setMargins(4, 0);
          dateNow.setPolygon(new int[] {7, 7});
          dateNow.setLayoutData(data);
          dateNow.addListener(
              SWT.Selection,
              new Listener() {
                public void handleEvent(Event event) {
                  cdt.setSelection(new Date());
                  if (event.widget == null) {
                    cdt.fireSelectionChanged();
                  }
                }
              });
          break;
        case Header.MONTH:
          monthButton = new VButton(header, SWT.TOGGLE | SWT.NO_FOCUS);
          monthButton.setData(CDT.PickerPart, PickerPart.MonthLabel);
          monthButton.setPainter(cdt.getPainter());
          cdt.getPainter().update(monthButton);
          monthButton.setAlignment(h.textAlignment, SWT.CENTER);
          monthButton.setLayoutData(data);
          if (h.readOnly) {
            monthButton.setPaintNative(false);
          } else {
            if (CDT.gtk) {
              monthButton.addListener(
                  SWT.MouseWheel,
                  new Listener() {
                    public void handleEvent(Event event) {
                      if (SWT.MouseWheel == event.type) {
                        Calendar tmpcal = cdt.getCalendarInstance();
                        tmpcal.add(Calendar.MONTH, (event.count > 0) ? 1 : -1);
                        cdt.setSelection(tmpcal.getTime());
                      }
                    }
                  });
            }
            monthButton.addListener(
                SWT.Selection,
                new Listener() {
                  public void handleEvent(Event event) {
                    handleHeaderSelection((VButton) event.data);
                  }
                });

            Menu monthMenu = monthButton.createMenu();
            monthItems = new MenuItem[12];
            for (int i = 0; i < 12; i++) {
              Calendar tmpcal = cdt.getCalendarInstance();
              tmpcal.set(Calendar.MONTH, i);
              monthItems[i] = new MenuItem(monthMenu, SWT.NONE);
              monthItems[i].setData(
                  "Month", new Integer(tmpcal.get(Calendar.MONTH))); // $NON-NLS-1$
              monthItems[i].addSelectionListener(
                  new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent e) {
                      MenuItem item = (MenuItem) e.widget;
                      Calendar tmpcal = cdt.getCalendarInstance();
                      tmpcal.set(
                          Calendar.MONTH,
                          ((Integer) item.getData("Month")).intValue()); // $NON-NLS-1$
                      cdt.setSelection(tmpcal.getTime());
                    }
                  });
            }
          }
          break;
        case Header.MONTH_NEXT:
          monthNext = new VButton(header, SWT.ARROW | SWT.RIGHT | SWT.NO_FOCUS);
          monthNext.setData(CDT.PickerPart, PickerPart.MonthNext);
          monthNext.setPainter(cdt.getPainter());
          cdt.getPainter().update(monthNext);
          monthNext.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
          monthNext.addListener(
              SWT.Selection,
              new Listener() {
                public void handleEvent(Event event) {
                  Calendar tmpcal = cdt.getCalendarInstance();
                  if (yearNext == null && yearButton != null && yearButton.getSelection()) {
                    tmpcal.add(Calendar.YEAR, 10);
                  } else {
                    tmpcal.add(Calendar.MONTH, 1);
                  }
                  cdt.setSelection(tmpcal.getTime());
                }
              });
          break;
        case Header.MONTH_PREV:
          monthPrev = new VButton(header, SWT.ARROW | SWT.LEFT | SWT.NO_FOCUS);
          monthPrev.setData(CDT.PickerPart, PickerPart.MonthPrev);
          monthPrev.setPainter(cdt.getPainter());
          cdt.getPainter().update(monthPrev);
          monthPrev.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
          monthPrev.addListener(
              SWT.Selection,
              new Listener() {
                public void handleEvent(Event event) {
                  Calendar tmpcal = cdt.getCalendarInstance();
                  if (yearPrev == null && yearButton != null && yearButton.getSelection()) {
                    tmpcal.add(Calendar.YEAR, -10);
                  } else {
                    tmpcal.add(Calendar.MONTH, -1);
                  }
                  cdt.setSelection(tmpcal.getTime());
                }
              });
          break;
        case Header.TIME:
          timeButton = new VButton(header, SWT.TOGGLE | SWT.NO_FOCUS);
          timeButton.setMargins(3, 5);
          timeButton.setImage(Resources.getIconClock());
          timeButton.setData(CDT.PickerPart, PickerPart.MonthPrev);
          timeButton.setPainter(cdt.getPainter());
          cdt.getPainter().update(timeButton);
          timeButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
          timeButton.addListener(
              SWT.Selection,
              new Listener() {
                public void handleEvent(Event event) {
                  handleHeaderSelection((VButton) event.data);
                }
              });
          break;
        case Header.YEAR:
          yearButton = new VButton(header, SWT.TOGGLE | SWT.NO_FOCUS);
          yearButton.setData(CDT.PickerPart, PickerPart.YearLabel);
          yearButton.setPainter(cdt.getPainter());
          cdt.getPainter().update(yearButton);
          yearButton.setAlignment(h.textAlignment, SWT.CENTER);
          yearButton.setLayoutData(data);
          if (h.readOnly) {
            yearButton.setPaintNative(false);
          } else {
            if (CDT.gtk) {
              yearButton.addListener(
                  SWT.MouseWheel,
                  new Listener() {
                    public void handleEvent(Event event) {
                      if (SWT.MouseWheel == event.type) {
                        Calendar tmpcal = cdt.getCalendarInstance();
                        tmpcal.add(Calendar.YEAR, (event.count > 0) ? 1 : -1);
                        cdt.setSelection(tmpcal.getTime());
                      }
                    }
                  });
            }
            yearButton.addListener(
                SWT.Selection,
                new Listener() {
                  public void handleEvent(Event event) {
                    handleHeaderSelection((VButton) event.data);
                  }
                });

            Menu yearMenu = yearButton.createMenu();
            yearItems = new MenuItem[11];
            for (int i = 0; i < 11; i++) {
              yearItems[i] = new MenuItem(yearMenu, SWT.NONE);
              yearItems[i].setData("Year", new Integer(i)); // $NON-NLS-1$
              yearItems[i].addSelectionListener(
                  new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent e) {
                      MenuItem item = (MenuItem) e.widget;
                      Calendar tmpcal = cdt.getCalendarInstance();
                      tmpcal.add(
                          Calendar.YEAR,
                          ((Integer) item.getData("Year")).intValue() - 5); // $NON-NLS-1$
                      cdt.setSelection(tmpcal.getTime());
                    }
                  });
            }
          }
          break;
        case Header.YEAR_NEXT:
          yearNext = new VButton(header, SWT.ARROW | SWT.RIGHT | SWT.NO_FOCUS);
          yearNext.setData(CDT.PickerPart, PickerPart.YearNext);
          yearNext.setPainter(cdt.getPainter());
          cdt.getPainter().update(yearNext);
          yearNext.setLayoutData(data);
          yearNext.addListener(
              SWT.Selection,
              new Listener() {
                public void handleEvent(Event event) {
                  Calendar tmpcal = cdt.getCalendarInstance();
                  if (yearButton != null && yearButton.getSelection()) {
                    tmpcal.add(Calendar.YEAR, 10);
                  } else if (cdt.field.length == 1
                      && cdt.getCalendarField(cdt.field[0]) == Calendar.YEAR) {
                    tmpcal.add(Calendar.YEAR, 10);
                  } else {
                    tmpcal.add(Calendar.YEAR, 1);
                  }
                  cdt.setSelection(tmpcal.getTime());
                }
              });
          break;
        case Header.YEAR_PREV:
          yearPrev = new VButton(header, SWT.ARROW | SWT.LEFT | SWT.NO_FOCUS);
          yearPrev.setData(CDT.PickerPart, PickerPart.YearPrev);
          yearPrev.setPainter(cdt.getPainter());
          cdt.getPainter().update(yearPrev);
          yearPrev.setLayoutData(data);
          yearPrev.addListener(
              SWT.Selection,
              new Listener() {
                public void handleEvent(Event event) {
                  Calendar tmpcal = cdt.getCalendarInstance();
                  if (yearButton != null && yearButton.getSelection()) {
                    tmpcal.add(Calendar.YEAR, -10);
                  } else if (cdt.field.length == 1
                      && cdt.getCalendarField(cdt.field[0]) == Calendar.YEAR) {
                    tmpcal.add(Calendar.YEAR, -10);
                  } else {
                    tmpcal.add(Calendar.YEAR, -1);
                  }
                  cdt.setSelection(tmpcal.getTime());
                }
              });
          break;
      }
    }
  }
Esempio n. 10
0
  /** set / update the text and font color of the <code>dayButton</code>s. */
  private void updateDays() {
    if (dayPanel != null) {
      Calendar date = cdt.getCalendarInstance();
      Calendar active = cdt.getCalendarInstance();
      Calendar today = cdt.getCalendarInstance(System.currentTimeMillis());
      for (int day = 0; day < dayButtons.length; day++) {
        if ((dayButtons[day] != null)) {
          date.setTime((Date) dayButtons[day].getData(CDT.Key.Date));

          boolean isToday =
              (date.get(Calendar.YEAR) == today.get(Calendar.YEAR))
                  && (date.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR));
          boolean isActive =
              (date.get(Calendar.YEAR) == active.get(Calendar.YEAR))
                  && (date.get(Calendar.MONTH) == active.get(Calendar.MONTH));

          dayButtons[day].setText(getFormattedDate("d", date.getTime())); // $NON-NLS-1$
          dayButtons[day].setData(CDT.Key.Today, isToday);
          dayButtons[day].setData(CDT.Key.Active, isActive);

          cdt.getPainter().update(dayButtons[day]);
        }
      }

      int focusButton = -1;

      for (int i = 0; i < dayButtons.length; i++) {
        dayButtons[i].setSelection(false);
      }

      if (cdt.hasSelection()) {
        Calendar first = cdt.getCalendarInstance((Date) dayButtons[0].getData(CDT.Key.Date));
        first.set(Calendar.MILLISECOND, 0);
        first.set(Calendar.SECOND, 0);
        first.set(Calendar.MINUTE, 0);
        first.set(Calendar.HOUR_OF_DAY, 0);

        Calendar last =
            cdt.getCalendarInstance((Date) dayButtons[dayButtons.length - 1].getData(CDT.Key.Date));
        last.set(Calendar.MILLISECOND, 0);
        last.set(Calendar.SECOND, 0);
        last.set(Calendar.MINUTE, 0);
        last.set(Calendar.HOUR_OF_DAY, 0);
        last.add(Calendar.DATE, 1);
        last.add(Calendar.MILLISECOND, -1);

        Date selection = cdt.getSelection();
        Calendar scal = cdt.getCalendarInstance(selection);
        for (int j = 0; j < dayButtons.length; j++) {
          Calendar tmpcal = cdt.getCalendarInstance((Date) dayButtons[j].getData(CDT.Key.Date));
          if ((scal.get(Calendar.DATE) == tmpcal.get(Calendar.DATE))
              && (scal.get(Calendar.MONTH) == tmpcal.get(Calendar.MONTH))
              && (scal.get(Calendar.YEAR) == tmpcal.get(Calendar.YEAR))) {
            dayButtons[j].setSelection(true);
            focusButton = j;
            break;
          }
        }
        if (focusButton >= 0) {
          dayButtons[focusButton].setFocus();
        }
      }

      dayPanel.redraw();
    }
  }