3 Replies Latest reply on Sep 25, 2008 2:42 PM by shadowcreeper

    Can we select a day twice (one after another)

    rktest05

      Hi,

      This is what I'm trying to do: I want to display a calendar (monthly with popup=false) and the user can select a day. When the user selects a day, the background color will change do red. if he selects it again, the background color will change to red. So, it will alternate between green and red.

      I've got most of it working with the sample code from Organizer Calendar demo src code (with the dataModel). However, one isssue I'm running into is: if I select a day, I cannot reselect it immediately. However, I can select another day and come back to reselect the old one. Basically, I cannot select a day twice one after another. How do I go about doing this?

      How do I enable such that user can select and select a day?

      Here is my code:

      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
      <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
      <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>


      .ecol1 { vertical-align: top; padding-right : 25px }
      .ecol2 { vertical-align: top; border-left: #ACBECE 1px solid; padding-left : 10px }
      .rich-calendar-tool-btn{
      font-family: Arial, Verdana;
      }
      .bk { background-color: red }
      .bookedbk { background-color: red }
      .availbk { background-color: green }


      <f:view>

      --Part of my CalendarDataModelImpl as follows:---

      public class CalendarDataModelImpl implements CalendarDataModel {

      /* (non-Javadoc)
      * @see org.richfaces.component.CalendarDataModel#getData(java.util.Date[])
      */

      private CalendarDataModelItem[] items;

      private String currentDescription;
      private String currentShortDescription;
      private Date currentDate;
      private boolean currentDisabled;
      private Map<String, CalendarDataModelItem[]> monthMap = new HashMap<String, CalendarDataModelItem[]>();


      /* (non-Javadoc)
      * @see org.richfaces.model.CalendarDataModel#getData(java.util.Date[])
      */
      public CalendarDataModelItem[] getData(Date[] dateArray) {
      if (dateArray == null) {
      return null;
      }

      //check if the monthmap already exists in memory
      String month = dateArray[0].getMonth() + "" + dateArray[0].getYear();
      if (monthMap.containsKey(month)) {
      items = (CalendarDataModelItem[])monthMap.get(month);
      return items;
      }

      items = new CalendarDataModelItem[dateArray.length];
      for (int i = 0; i < dateArray.length; i++) {
      items = createDataModelItem(dateArray);
      }

      //put the month info in memory
      monthMap.put(month, items);

      return items;
      }

      /**
      * @param date
      * @return CalendarDataModelItem for date
      */
      protected CalendarDataModelItem createDataModelItem(Date date) {
      CalendarDataModelItemImpl item = new CalendarDataModelItemImpl();
      Calendar c = Calendar.getInstance();
      c.setTime(date);
      item.setDay(c.get(Calendar.DAY_OF_MONTH));
      item.setEnabled(true);
      item.setStyleClass("availbk");
      return item;
      }

      /* (non-Javadoc)
      * @see org.richfaces.model.CalendarDataModel#getToolTip(java.util.Date)
      */
      public Object getToolTip(Date date) {

      // TODO Auto-generated method stub
      return null;
      }

        • 1. Re: Can we select a day twice (one after another)
          rktest05

          Sorry, I meant when the user selects, the background color will change to red. When he selects again it will turn to green.

          "rktest05" wrote:
          Hi,

          This is what I'm trying to do: I want to display a calendar (monthly with popup=false) and the user can select a day. When the user selects a day, the background color will change do red. if he selects it again, the background color will change to green. So, it will alternate between green and red.

          I've got most of it working with the sample code from Organizer Calendar demo src code (with the dataModel). However, one isssue I'm running into is: if I select a day, I cannot reselect it immediately. However, I can select another day and come back to reselect the old one. Basically, I cannot select a day twice one after another. How do I go about doing this?

          How do I enable such that user can select and select a day?

          Here is my code:

          <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
          <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
          <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
          <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>

          <style type="text/css">
          .ecol1 { vertical-align: top; padding-right : 25px }
          .ecol2 { vertical-align: top; border-left: #ACBECE 1px solid; padding-left : 10px }
          .rich-calendar-tool-btn{
          font-family: Arial, Verdana;
          }
          .bk { background-color: red }
          .bookedbk { background-color: red }
          .availbk { background-color: green }
          </style>

          <f:view>

          --Part of my CalendarDataModelImpl as follows:---

          public class CalendarDataModelImpl implements CalendarDataModel {

          /* (non-Javadoc)
          * @see org.richfaces.component.CalendarDataModel#getData(java.util.Date[])
          */

          private CalendarDataModelItem[] items;

          private String currentDescription;
          private String currentShortDescription;
          private Date currentDate;
          private boolean currentDisabled;
          private Map<String, CalendarDataModelItem[]> monthMap = new HashMap<String, CalendarDataModelItem[]>();


          /* (non-Javadoc)
          * @see org.richfaces.model.CalendarDataModel#getData(java.util.Date[])
          */
          public CalendarDataModelItem[] getData(Date[] dateArray) {
          if (dateArray == null) {
          return null;
          }

          //check if the monthmap already exists in memory
          String month = dateArray[0].getMonth() + "" + dateArray[0].getYear();
          if (monthMap.containsKey(month)) {
          items = (CalendarDataModelItem[])monthMap.get(month);
          return items;
          }

          items = new CalendarDataModelItem[dateArray.length];
          for (int i = 0; i < dateArray.length; i++) {
          items = createDataModelItem(dateArray);
          }

          //put the month info in memory
          monthMap.put(month, items);

          return items;
          }

          /**
          * @param date
          * @return CalendarDataModelItem for date
          */
          protected CalendarDataModelItem createDataModelItem(Date date) {
          CalendarDataModelItemImpl item = new CalendarDataModelItemImpl();
          Calendar c = Calendar.getInstance();
          c.setTime(date);
          item.setDay(c.get(Calendar.DAY_OF_MONTH));
          item.setEnabled(true);
          item.setStyleClass("availbk");
          return item;
          }

          /* (non-Javadoc)
          * @see org.richfaces.model.CalendarDataModel#getToolTip(java.util.Date)
          */
          public Object getToolTip(Date date) {

          // TODO Auto-generated method stub
          return null;
          }


          • 2. Re: Can we select a day twice (one after another)
            shadowcreeper

            I do this with a CalendarDataModel which supplies a styleClass for the selected date and the following code (note the oncomplete which deselects the selected date):

             <a4j:support event="ondateselected"
             action="#{controller.toggleSelectedDate}"
             oncomplete="#{rich:component( id )}.resetSelectedDate();refreshCalendars();">
             <a4j:actionparam name="selectedDateText"
             value="#{rich:component( id )}.getSelectedDateString('#{controller.dateFormat}')"
             assignTo="#{controller.selectedDateText}"
             noEscape="true"/>
             </a4j:support>
            


            • 3. Re: Can we select a day twice (one after another)
              shadowcreeper

              And refreshCalendars():

              function refreshCalendars ()
              {
               reRenderCalendar( #{rich:component( 'calendar1' )} );
               reRenderCalendar( #{rich:component( 'calendar2' )} );
              }
              


              And reRenderCalendar():
              function reRenderCalendar ( calendar )
              {
               /*
               * TODO: Get rid of this hack once the calendar.render() bug gets fixed...
               * we use the following hack because calendar.render() does not work and
               * calendar.changeCurrentDate() does nothing if month and year do not change
               */
               calendar.onUpdate();
              }