Home » tomahawk-1.1.6-src » org.apache.myfaces » custom » schedule » renderer » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *   http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   package org.apache.myfaces.custom.schedule.renderer;
   21   
   22   import java.io.IOException;
   23   import java.io.Serializable;
   24   import java.text.DateFormat;
   25   import java.util.Date;
   26   import java.util.Map;
   27   
   28   import javax.faces.component.UIComponent;
   29   import javax.faces.context.FacesContext;
   30   import javax.faces.context.ResponseWriter;
   31   import javax.faces.el.ValueBinding;
   32   
   33   import org.apache.myfaces.custom.schedule.HtmlSchedule;
   34   import org.apache.myfaces.custom.schedule.model.ScheduleDay;
   35   import org.apache.myfaces.custom.schedule.model.ScheduleEntry;
   36   import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
   37   
   38   /**
   39    * The default implementation of the ScheduleEntryRenderer
   40    * 
   41    * @author Jurgen Lust (latest modification by $Author$)
   42    * @version $Revision$
   43    */
   44   public class DefaultScheduleEntryRenderer implements ScheduleEntryRenderer,
   45           Serializable
   46   {
   47       private static final long serialVersionUID = 4987926168243581739L;
   48   
   49       /**
   50        * @see org.apache.myfaces.custom.schedule.renderer.ScheduleEntryRenderer#renderContent(javax.faces.context.FacesContext, javax.faces.context.ResponseWriter, org.apache.myfaces.custom.schedule.HtmlSchedule, org.apache.myfaces.custom.schedule.model.ScheduleDay, org.apache.myfaces.custom.schedule.model.ScheduleEntry, boolean, boolean)
   51        */
   52       public void renderContent(FacesContext context, ResponseWriter writer,
   53                                 HtmlSchedule schedule, ScheduleDay day, ScheduleEntry entry,
   54                                 boolean compact, boolean selected) throws IOException
   55       {
   56           if (compact)
   57           {
   58               renderCompactContent(context, writer, schedule, day, entry, selected);
   59           } else
   60           {
   61               if (selected)
   62               {
   63                   StringBuffer entryStyle = new StringBuffer();
   64                   entryStyle.append("height: 100%; width: 100%;");
   65                   //the left border of a selected entry should have the same
   66                   //color as the entry border
   67                   String entryColor = getColor(context, schedule, entry, selected);
   68                   if (entryColor != null) {
   69                       entryStyle.append("border-color: ");
   70                       entryStyle.append(entryColor);
   71                       entryStyle.append(";");
   72                   }
   73                   // draw the contents of the selected entry
   74                   writer.startElement(HTML.DIV_ELEM, null);
   75                   writer.writeAttribute(HTML.CLASS_ATTR, getStyleClass(schedule,
   76                                                                        "text"), null);
   77                   writer.writeAttribute(HTML.STYLE_ATTR,entryStyle.toString(), null);
   78                   
   79                   renderDetailedContentText(context, writer, schedule, day, entry, selected);
   80                   
   81                   writer.endElement(HTML.DIV_ELEM);
   82               } else
   83               {
   84                   renderDetailedContentText(context, writer, schedule, day, entry, selected);
   85               }
   86           }
   87   
   88       }
   89   
   90       protected void renderCompactContent(FacesContext context, ResponseWriter writer, HtmlSchedule schedule, ScheduleDay day, ScheduleEntry entry, boolean selected) throws IOException
   91       {
   92           StringBuffer text = new StringBuffer();
   93           Date startTime = entry.getStartTime();
   94   
   95           if (day.getDayStart().after(entry.getStartTime()))
   96           {
   97               startTime = day.getDayStart();
   98           }
   99   
  100           Date endTime = entry.getEndTime();
  101   
  102           if (day.getDayEnd().before(entry.getEndTime()))
  103           {
  104               endTime = day.getDayEnd();
  105           }
  106   
  107           if (!entry.isAllDay())
  108           {
  109           	DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT);
  110           	text.append(format.format(startTime));
  111           	if (!startTime.equals(endTime)) {
  112           		text.append("-");
  113           		text.append(format.format(endTime));
  114           	}
  115           	text.append(": ");
  116           }
  117           text.append(entry.getTitle());
  118   
  119           writer.writeText(text.toString(), null);    	
  120       }
  121       
  122       protected void renderDetailedContentText(FacesContext context, ResponseWriter writer,
  123               HtmlSchedule schedule, ScheduleDay day, ScheduleEntry entry, boolean selected) throws IOException
  124       {
  125           // write the title of the entry
  126           if (entry.getTitle() != null)
  127           {
  128               writer.startElement(HTML.SPAN_ELEM, schedule);
  129               writer.writeAttribute(HTML.CLASS_ATTR, getStyleClass(
  130                       schedule, "title"), null);
  131               writer.writeText(entry.getTitle(), null);
  132               writer.endElement(HTML.SPAN_ELEM);
  133           }
  134           if (entry.getSubtitle() != null)
  135           {
  136               writer.startElement("br", schedule);
  137               writer.endElement("br");
  138               writer.startElement(HTML.SPAN_ELEM, schedule);
  139               writer.writeAttribute(HTML.CLASS_ATTR, getStyleClass(
  140                       schedule, "subtitle"), null);
  141               writer.writeText(entry.getSubtitle(), null);
  142               writer.endElement(HTML.SPAN_ELEM);
  143           }    	
  144       }
  145       
  146       /**
  147        * @see org.apache.myfaces.custom.schedule.renderer.ScheduleEntryRenderer#getColor(javax.faces.context.FacesContext, org.apache.myfaces.custom.schedule.HtmlSchedule, org.apache.myfaces.custom.schedule.model.ScheduleEntry, boolean)
  148        */
  149       public String getColor(FacesContext context, HtmlSchedule schedule,
  150                              ScheduleEntry entry, boolean selected)
  151       {
  152           return null;
  153       }
  154   
  155       /**
  156        * @see org.apache.myfaces.custom.schedule.renderer.ScheduleEntryRenderer#renderToolTip(javax.faces.context.FacesContext, javax.faces.context.ResponseWriter, org.apache.myfaces.custom.schedule.HtmlSchedule, org.apache.myfaces.custom.schedule.model.ScheduleEntry, boolean)
  157        */
  158       public void renderToolTip(FacesContext context, ResponseWriter writer,
  159                                 HtmlSchedule schedule, ScheduleEntry entry, boolean selected)
  160               throws IOException
  161       {
  162           StringBuffer buffer = new StringBuffer();
  163           buffer
  164                   .append("return makeTrue(domTT_activate(this, event, 'caption', '");
  165   
  166           if (entry.getTitle() != null)
  167           {
  168               buffer.append(escape(entry.getTitle()));
  169           }
  170   
  171           buffer.append("', 'content', '<i>");
  172   
  173           if (entry.getSubtitle() != null)
  174           {
  175               buffer.append(escape(entry.getSubtitle()));
  176           }
  177   
  178           buffer.append("</i>");
  179   
  180           if (entry.getDescription() != null)
  181           {
  182               buffer.append("<br/>");
  183               buffer.append(escape(entry.getDescription()));
  184           }
  185   
  186           buffer.append("', 'trail', true));");
  187           writer.writeAttribute("onmouseover", buffer.toString(), null);
  188       }
  189   
  190       private String escape(String text)
  191       {
  192           if (text == null)
  193           {
  194               return null;
  195           }
  196   
  197           return text.replaceAll("'", "\\\\\'").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
  198       }
  199   
  200       /**
  201        * <p>
  202        * Allow the developer to specify custom CSS classnames for the schedule
  203        * component.
  204        * </p>
  205        * 
  206        * @param component
  207        *            the component
  208        * @param className
  209        *            the default CSS classname
  210        * @return the custom classname
  211        */
  212       protected String getStyleClass(UIComponent component, String className)
  213       {
  214           // first check if the styleClass property is a value binding expression
  215           ValueBinding binding = component.getValueBinding(className);
  216           if (binding != null)
  217           {
  218               String value = (String) binding.getValue(FacesContext
  219                       .getCurrentInstance());
  220   
  221               if (value != null)
  222               {
  223                   return value;
  224               }
  225           }
  226           // it's not a value binding expression, so check for the string value
  227           // in the attributes
  228           Map attributes = component.getAttributes();
  229           String returnValue = (String) attributes.get(className);
  230           return returnValue == null ? className : returnValue;
  231       }
  232   
  233   }

Home » tomahawk-1.1.6-src » org.apache.myfaces » custom » schedule » renderer » [javadoc | source]