View Javadoc

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;
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   * @since 1.1.7
42   * @author Jurgen Lust (latest modification by $Author$)
43   * @version $Revision$
44   */
45  public class DefaultScheduleEntryRenderer implements ScheduleEntryRenderer,
46          Serializable
47  {
48      private static final long serialVersionUID = 4987926168243581739L;
49  
50      /**
51       * @see org.apache.myfaces.custom.schedule.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)
52       */
53      public void renderContent(FacesContext context, ResponseWriter writer,
54                                HtmlSchedule schedule, ScheduleDay day, ScheduleEntry entry,
55                                boolean compact, boolean selected) throws IOException
56      {
57          if (compact)
58          {
59              renderCompactContent(context, writer, schedule, day, entry, selected);
60          } else
61          {
62              if (selected)
63              {
64                  StringBuffer entryStyle = new StringBuffer();
65                  entryStyle.append("height: 100%; width: 100%;");
66                  //the left border of a selected entry should have the same
67                  //color as the entry border
68                  String entryColor = getColor(context, schedule, entry, selected);
69                  if (entryColor != null) {
70                      entryStyle.append("border-color: ");
71                      entryStyle.append(entryColor);
72                      entryStyle.append(";");
73                  }
74                  // draw the contents of the selected entry
75                  writer.startElement(HTML.DIV_ELEM, null);
76                  writer.writeAttribute(HTML.CLASS_ATTR, getStyleClass(schedule,
77                                                                       "text"), null);
78                  writer.writeAttribute(HTML.STYLE_ATTR,entryStyle.toString(), null);
79                  
80                  renderDetailedContentText(context, writer, schedule, day, entry, selected);
81                  
82                  writer.endElement(HTML.DIV_ELEM);
83              } else
84              {
85                  renderDetailedContentText(context, writer, schedule, day, entry, selected);
86              }
87          }
88  
89      }
90  
91      protected void renderCompactContent(FacesContext context, ResponseWriter writer, HtmlSchedule schedule, ScheduleDay day, ScheduleEntry entry, boolean selected) throws IOException
92      {
93          StringBuffer text = new StringBuffer();
94          Date startTime = entry.getStartTime();
95  
96          if (day.getDayStart().after(entry.getStartTime()))
97          {
98              startTime = day.getDayStart();
99          }
100 
101         Date endTime = entry.getEndTime();
102 
103         if (day.getDayEnd().before(entry.getEndTime()))
104         {
105             endTime = day.getDayEnd();
106         }
107 
108         if (!entry.isAllDay())
109         {
110             DateFormat format = AbstractScheduleRenderer.getDateFormat(context, schedule, 
111                     HtmlSchedule.HOUR_NOTATION_24.equals(schedule.getHourNotation()) ? "HH:mm" : "h:mma");
112             
113             text.append(format.format(startTime));
114             if (!startTime.equals(endTime)) {
115                 text.append("-");
116                 text.append(format.format(endTime));
117             }
118             text.append(": ");
119         }
120         text.append(entry.getTitle());
121 
122         writer.writeText(text.toString(), null);        
123     }
124     
125     protected void renderDetailedContentText(FacesContext context, ResponseWriter writer,
126             HtmlSchedule schedule, ScheduleDay day, ScheduleEntry entry, boolean selected) throws IOException
127     {
128         // write the title of the entry
129         if (entry.getTitle() != null)
130         {
131             writer.startElement(HTML.SPAN_ELEM, schedule);
132             writer.writeAttribute(HTML.CLASS_ATTR, getStyleClass(
133                     schedule, "title"), null);
134             writer.writeText(entry.getTitle(), null);
135             writer.endElement(HTML.SPAN_ELEM);
136         }
137         if (entry.getSubtitle() != null)
138         {
139             writer.startElement("br", schedule);
140             writer.endElement("br");
141             writer.startElement(HTML.SPAN_ELEM, schedule);
142             writer.writeAttribute(HTML.CLASS_ATTR, getStyleClass(
143                     schedule, "subtitle"), null);
144             writer.writeText(entry.getSubtitle(), null);
145             writer.endElement(HTML.SPAN_ELEM);
146         }        
147     }
148     
149     /**
150      * @see org.apache.myfaces.custom.schedule.ScheduleEntryRenderer#getColor(javax.faces.context.FacesContext, org.apache.myfaces.custom.schedule.HtmlSchedule, org.apache.myfaces.custom.schedule.model.ScheduleEntry, boolean)
151      */
152     public String getColor(FacesContext context, HtmlSchedule schedule,
153                            ScheduleEntry entry, boolean selected)
154     {
155         return null;
156     }
157 
158     /**
159      * @see org.apache.myfaces.custom.schedule.ScheduleEntryRenderer#renderToolTip(javax.faces.context.FacesContext, javax.faces.context.ResponseWriter, org.apache.myfaces.custom.schedule.HtmlSchedule, org.apache.myfaces.custom.schedule.model.ScheduleEntry, boolean)
160      */
161     public void renderToolTip(FacesContext context, ResponseWriter writer,
162                               HtmlSchedule schedule, ScheduleEntry entry, boolean selected)
163             throws IOException
164     {
165         StringBuffer buffer = new StringBuffer();
166         buffer
167                 .append("return makeTrue(domTT_activate(this, event, 'caption', '");
168 
169         if (entry.getTitle() != null)
170         {
171             buffer.append(escape(entry.getTitle()));
172         }
173 
174         buffer.append("', 'content', '<i>");
175 
176         if (entry.getSubtitle() != null)
177         {
178             buffer.append(escape(entry.getSubtitle()));
179         }
180 
181         buffer.append("</i>");
182 
183         if (entry.getDescription() != null)
184         {
185             buffer.append("<br/>");
186             buffer.append(escape(entry.getDescription()));
187         }
188 
189         buffer.append("', 'trail', true));");
190         writer.writeAttribute("onmouseover", buffer.toString(), null);
191     }
192 
193     private String escape(String text)
194     {
195         if (text == null)
196         {
197             return null;
198         }
199 
200         return text.replaceAll("'", "\\\\\'").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
201     }
202 
203     /**
204      * @see org.apache.myfaces.custom.schedule.ScheduleEntryRenderer#getEntryClass(org.apache.myfaces.custom.schedule.HtmlSchedule, org.apache.myfaces.custom.schedule.model.ScheduleEntry)
205      */
206     public String getEntryClass(HtmlSchedule schedule, ScheduleEntry entry)
207     {
208         return getStyleClass(schedule, "entry");
209     }
210     
211     /**
212      * <p>
213      * Allow the developer to specify custom CSS classnames for the schedule
214      * component.
215      * </p>
216      * 
217      * @param component
218      *            the component
219      * @param className
220      *            the default CSS classname
221      * @return the custom classname
222      */
223     protected String getStyleClass(UIComponent component, String className)
224     {
225         // first check if the styleClass property is a value binding expression
226         ValueBinding binding = component.getValueBinding(className);
227         if (binding != null)
228         {
229             String value = (String) binding.getValue(FacesContext
230                     .getCurrentInstance());
231 
232             if (value != null)
233             {
234                 return value;
235             }
236         }
237         // it's not a value binding expression, so check for the string value
238         // in the attributes
239         Map attributes = component.getAttributes();
240         String returnValue = (String) attributes.get(className + "Class");
241         return returnValue == null ? className : returnValue;
242     }
243 
244 }