View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2WebGui library.
13   *
14   *  The Initial Owner of the Original Code is Power Of Two S.R.L.
15   *  Portions created by Power Of Two S.R.L. are Copyright (C) Power Of Two S.R.L.
16   *  All Rights Reserved.
17   *
18   * Contributor(s):
19   */
20  
21  package com.pow2.webgui;
22  
23  
24  import javax.servlet.jsp.tagext.BodyTagSupport;
25  
26  import org.apache.log4j.Category;
27  
28  import com.pow2.webgui.contentprovider.BaseContentProvider;
29  import com.pow2.webgui.taglib.TagUtil;
30  
31  
32  /***
33   *  Abstract Widget class.
34   *  <br>
35   *  Extends also BodyTagSupport - in this manner every widget tag
36   *  subclassing this class can access the basic widget properties.
37   *
38   * @author  Luca Fossato
39   * @created  12 October 2001
40   */
41  public abstract class Widget extends BodyTagSupport
42  {
43    /*** Log4j category */
44    protected static Category cat = Category.getInstance(Widget.class);
45  
46    /*** widget color (#rrggbb format) */
47    protected String color;
48  
49    /*** widget content */
50    protected StringBuffer content;
51  
52    /*** content provider class */
53    protected BaseContentProvider contentProvider;
54  
55    /*** content provider name */
56    protected String contentProviderName;
57  
58    /*** widget drawer key */
59    protected String drawer;
60  
61    /*** widget URI link */
62    protected String followUp;
63  
64    /*** widget height */
65    protected String height;
66  
67    /*** widget identifier */
68    protected String id;
69  
70    /*** widget name */
71    protected String name;
72  
73    /***
74     *  parent Widget.
75     *  <br>
76     *  Why <code>parentW</code> and not <code>parent</code> ?
77     *  <br>
78     *  Note that <code>parent</code> attribute overrides the
79     *  homonymous tag class attribute, generating an erroneous behaviour.
80     */
81    protected Widget parentW;
82  
83    /*** widget title */
84    protected String title;
85  
86    /*** title type; it can be: "text" (default) or "image". */
87    protected String titleType;
88  
89    /*** widgetDrawer strategy instance */
90    protected WidgetDrawer widgetDrawer;
91  
92    /*** widgetInfo instance */
93    protected WidgetInfo widgetInfo;
94  
95    /*** widget width */
96    protected String width;
97  
98  
99    /***
100    *  Default protected Constructor.
101    *  <br>
102    *  Use the WidgetFactory to instance a new Widget (sub)class.
103    */
104   protected Widget()
105   {
106     // Widget extends BodyTagSupport...
107     super();
108 
109     // default attribute values...
110     color               = "";
111     content             = new StringBuffer();
112     contentProvider     = null;
113     contentProviderName = "";
114     drawer              = "";
115     followUp            = "";
116     height              = "";
117     id                  = "";
118     name                = "";
119     parentW             = null;
120     title               = "";
121     titleType           = "text";
122     width               = "";
123     widgetDrawer        = null;
124     widgetInfo          = null;
125   }
126 
127 
128   /***
129    *  Sets the color attribute of the Widget object.
130    *  <br>
131    *  Use the notation <code>rrggbb</code> where:<br>
132    *  <code>rr</code> is the red   component (0-ff)<br>
133    *  <code>gg</code> is the green component (0-ff)<br>
134    *  <code>bb</code> is the blue  component (0-ff)<br>
135    *
136    * @param  newColor The new color value
137    */
138   public void setColor(String newColor)
139   {
140     color = newColor;
141   }
142 
143 
144   /***
145    *  Sets the content attribute of the Widget object
146    *
147    * @param  newContent The new content value.
148    */
149   public void setContent(StringBuffer newContent)
150   {
151     content = newContent;
152   }
153 
154 
155   /***
156    *  Sets the default contentProvider attribute.
157    *
158    * @param  contentProviderName the contentProvider name
159    * @exception  Exception if any error occurs
160    */
161   public void setContentProvider(String contentProviderName) throws Exception
162   {
163     if (widgetInfo == null)
164       throw new Exception("cannot set the default widget contentProvider; widgetInfo attributes is null");
165 
166     contentProvider = widgetInfo.getContentProvider(contentProviderName, this);
167 
168     TagUtil.setContentProviderData(contentProvider, pageContext);
169 
170     // REFACTORING !! Use a Prefs object;
171     //contentProvider.setTemplate      ((String)    pageContext.getAttribute("com.pow2.webgui.contentprovider.template"));
172     //contentProvider.setCollection    ((Collection)pageContext.getAttribute("com.pow2.webgui.contentprovider.collection"));
173     //contentProvider.setPropertiesMap ((HashMap)   pageContext.getAttribute("com.pow2.webgui.contentprovider.propertiesMap"));
174   }
175 
176 
177   /***
178    *  Sets the contentProviderName attribute.
179    *
180    * @param  contentProviderName the contentProvider name
181    */
182   public void setContentProviderName(String contentProviderName)
183   {
184     this.contentProviderName = contentProviderName;
185   }
186 
187 
188   /***
189    *  Sets the drawer attribute of the Widget object
190    *
191    * @param  newDrawer The new drawer value
192    */
193   public void setDrawer(String newDrawer)
194   {
195     drawer = newDrawer;
196   }
197 
198 
199   /***
200    *  Sets the followUp attribute of the Widget object.
201    *
202    * @param  newFollowUp The new followUp value
203    */
204   public void setFollowUp(String newFollowUp)
205   {
206     followUp = newFollowUp;
207   }
208 
209 
210   /***
211    *  Sets the height attribute of the Widget object
212    *
213    * @param  height The new height value
214    */
215   public void setHeight(String height)
216   {
217     this.height = height;
218   }
219 
220 
221   /***
222    *  Sets the id attribute of the Widget object
223    *
224    * @param  newId The new id value
225    */
226   public void setId(String newId)
227   {
228     id = newId;
229   }
230 
231 
232   /***
233    *  Sets the name attribute of the Widget object
234    *
235    * @param  newName The new name value
236    */
237   public void setName(String newName)
238   {
239     name = newName;
240   }
241 
242 
243   /***
244    *  Sets the parentW attribute of the Widget object
245    *
246    * @param  newParentW The new parentW value
247    */
248   public void setParentW(Widget newParentW)
249   {
250     parentW = newParentW;
251   }
252 
253 
254   /***
255    *  Sets the title attribute of the Widget object
256    *
257    * @param  newTitle The new title value.
258    */
259   public void setTitle(String newTitle)
260   {
261     title = newTitle;
262   }
263 
264 
265   /***
266    *  Sets the titleType attribute of the Widget object
267    *  <br>
268    *  Supported types are:
269    *  <ul>
270    *    <li>text</li>
271    *    <li>image</li>
272    *  </ul>
273    *
274    * @param  newTitleType The new titleType value.
275    */
276   public void setTitleType(String newTitleType)
277   {
278     // default value is "text".
279     if (!"text".equals(newTitleType) && !"image".equals(newTitleType))
280       newTitleType = "text";
281 
282     titleType = newTitleType;
283   }
284 
285 
286   /***
287    *  Sets the default widgetDrawer attribute, using the input <code>drawer</code>
288    *  parameter value.
289    *
290    * @param  drawer the drawer name
291    * @exception  Exception if any error occurs
292    */
293   public void setWidgetDrawer(String drawer) throws Exception
294   {
295     if (widgetInfo == null)
296       throw new Exception("cannot set the default widget drawer; widgetInfo attributes is null");
297 
298     widgetDrawer = widgetInfo.getWidgetDrawer(drawer, this);
299   }
300 
301 
302   /***
303    *  Sets the widgetInfo attribute of the Widget object.
304    *  <br>
305    *  Note: this method has got the final modifier.
306    *
307    * @param  widgetInfo The new widgetInfo value
308    */
309   public final void setWidgetInfo(WidgetInfo widgetInfo)
310   {
311     this.widgetInfo = widgetInfo;
312   }
313 
314 
315   /***
316    *  Sets the width attribute of the Widget object
317    *
318    * @param  width The new width value
319    */
320   public void setWidth(String width)
321   {
322     this.width = width;
323   }
324 
325 
326   /***
327    *  Gets the color attribute of the Widget object
328    *
329    * @return  The color value
330    */
331   public String getColor()
332   {
333     return color;
334   }
335 
336 
337   /***
338    *  Gets the content attribute of the Widget object
339    *
340    * @return  The content value
341    */
342   public StringBuffer getContent()
343   {
344     // if the contentProviderInstance property is set, use that
345     // class to render the widget content;
346     if (contentProvider != null)
347     {
348       try
349       {
350         content = contentProvider.getContent();
351       } catch (Exception e)
352       {
353         cat.error("::getContent - contentProvider ["
354             + contentProvider.getClass() + "] fails", e);
355       }
356     }
357 
358     return content;
359   }
360 
361 
362   /***
363    *  Gets the contentProvider attribute of the Widget object
364    *
365    * @return  The contentProvider value
366    */
367   public BaseContentProvider getContentProvider()
368   {
369     return contentProvider;
370   }
371 
372 
373   /***
374    *  Gets the contentProviderName attribute of the Widget object.
375    *
376    * @return  The contentProviderName value
377    */
378   public String getContentProviderName()
379   {
380     return contentProviderName;
381   }
382 
383 
384   /***
385    *  Gets the drawer attribute of the Widget object
386    *
387    * @return  The drawer value
388    */
389   public String getDrawer()
390   {
391     return drawer;
392   }
393 
394 
395   /***
396    *  Gets the followUp attribute of the Widget object.
397    *
398    * @return  The followUp value
399    */
400   public String getFollowUp()
401   {
402     return followUp;
403   }
404 
405 
406   /***
407    *  Gets the height attribute of the Widget object
408    *
409    * @return  The height value
410    */
411   public String getHeight()
412   {
413     return height;
414   }
415 
416 
417   /***
418    *  Gets the id attribute of the Widget object
419    *
420    * @return  The id value
421    */
422   public String getId()
423   {
424     return id;
425   }
426 
427 
428   /***
429    *  Gets the name attribute of the Widget object
430    *
431    * @return  The name value
432    */
433   public String getName()
434   {
435     return name;
436   }
437 
438 
439   /***
440    *  Gets the parentW attribute of the Widget object
441    *
442    * @return  The parentW value
443    */
444   public Widget getParentW()
445   {
446     return parentW;
447   }
448 
449 
450   /***
451    *  Gets the title attribute of the Widget object
452    *
453    * @return  The title value
454    */
455   public String getTitle()
456   {
457     return title;
458   }
459 
460 
461   /***
462    *  Gets the titleType attribute of the Window object
463    *
464    * @return  The titleType value.
465    */
466   public String getTitleType()
467   {
468     return titleType;
469   }
470 
471 
472   /***
473    *  Gets the widgetDrawer attribute of the Widget object
474    *
475    * @return  The widgetDrawer value
476    */
477   public WidgetDrawer getWidgetDrawer()
478   {
479     return widgetDrawer;
480   }
481 
482 
483   /***
484    *  Gets the widgetInfo attribute of the Widget object
485    *
486    * @return  The widgetInfo value
487    */
488   public WidgetInfo getWidgetInfo()
489   {
490     return widgetInfo;
491   }
492 
493 
494   /***
495    *  Gets the width attribute of the Widget object
496    *
497    * @return  The width value
498    */
499   public String getWidth()
500   {
501     return width;
502   }
503 
504 
505   /***
506    *  Gets the string representation of this widget.
507    *  <br>
508    *  Use the selected widgetDrawer strategy.
509    *
510    * @return  the string representation of this widget
511    * @exception  Exception if any error occurs
512    */
513   public StringBuffer draw() throws Exception
514   {
515     if (widgetDrawer != null)
516       return widgetDrawer.draw();
517 
518     throw new Exception("this widget [" + this.getClass() + "] doesn't have a widgetDrawer");
519   }
520 
521 
522   /***
523    *  Update this class attributes using the input Widget attributes values.
524    *
525    * @param  w the input widget subclass; must be a tag library class
526    *         that extends the Widget class
527    */
528   public void updateAttributes(Widget w)
529   {
530     id                  = w.getId();
531     name                = w.getName();
532     drawer              = w.getDrawer();
533     title               = w.getTitle();
534     titleType           = w.getTitleType();
535     followUp            = w.getFollowUp();
536     color               = w.getColor();
537     width               = w.getWidth();
538     height              = w.getHeight();
539     content             = w.getContent();
540     contentProvider     = w.getContentProvider();
541     contentProviderName = w.getContentProviderName();
542 
543     // pagecontext can be used as a Properties object;
544     pageContext = w.pageContext;
545   }
546 }