View Javadoc

1   /*
2    * Copyright 2004-2005 Andreas Andreou
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.tapfx.components;
17  
18  import java.text.DateFormat;
19  import java.text.NumberFormat;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.tapestry.BaseComponent;
28  import org.apache.tapestry.IAsset;
29  import org.apache.tapestry.IRequestCycle;
30  
31  /***
32   * A utility base class.
33   * <p/>
34   * Should this be a third class? Yes.
35   * But either way you'd be stuck with an extra dependency...
36   * @author Joshua Long (josh@joshlong.com)
37   * @author andyhot
38   * @version $Id: TapFXBaseComponent.java,v 1.2 2005/11/04 23:32:54 andyhot Exp $
39   */
40  public class TapFXBaseComponent extends BaseComponent
41  {
42      private DateFormat timeFormatter = DateFormat.getDateTimeInstance();
43  
44      public DateFormat getTimeFormatter()
45      {
46          return timeFormatter;
47      }
48  
49      public void setTimeFormatter(DateFormat timeFormatter)
50      {
51          this.timeFormatter = timeFormatter;
52      }
53  
54      private NumberFormat percentFormatter =
55              NumberFormat.getPercentInstance();
56      private NumberFormat numberFormatter =
57              NumberFormat.getNumberInstance();
58  
59      public DateFormat getDateFormatter()
60      {
61          return dateFormatter;
62      }
63  
64      public void setDateFormatter(DateFormat dateFormatter)
65      {
66          this.dateFormatter = dateFormatter;
67      }
68  
69      private DateFormat dateFormatter =
70              DateFormat.getDateInstance(DateFormat.SHORT);
71  
72  
73      public NumberFormat getNumberFormatter()
74      {
75          return numberFormatter;
76      }
77  
78      public void setNumberFormatter(NumberFormat numberFormatter)
79      {
80          this.numberFormatter = numberFormatter;
81      }
82  
83      public NumberFormat getPercentFormatter()
84      {
85          return percentFormatter;
86      }
87  
88      public void setPercentFormatter(NumberFormat percentFormatter)
89      {
90          this.percentFormatter = percentFormatter;
91      }
92  
93  
94      protected Map getSymbolsForAssets(Map assets, IRequestCycle cycle)
95      {
96          Map img_sys = new HashMap(assets.size());
97          Set keys = assets.keySet();
98          Iterator it = keys.iterator();
99          while (it.hasNext())
100         {
101             String key = (String) it.next();
102             IAsset ass = (IAsset) assets.get(key);
103             String URL = ass.buildURL(cycle);
104             img_sys.put(key, URL);
105         }
106         return img_sys;
107     }
108 
109 
110     protected void prepareForRender(IRequestCycle cycle)
111     {
112         super.prepareForRender(cycle);
113         setupComponent(cycle);
114     }
115 
116     protected void setupComponent(IRequestCycle cycle)
117     {
118     }
119 
120     private static final Log log =
121             LogFactory.getLog(TapFXBaseComponent.class);
122 
123     protected void error(Throwable thr)
124     {
125         /*
126          * This used to use the apache commons' excellent
127          * ExceptionUtils.getStackTrace(), but,
128          * to remove an extra dependancy,
129          * I changed it for you
130          */        
131         log.info(thr.getMessage());
132         thr.printStackTrace();
133     }
134 }