KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > weather > component > UIWeatherView


1 /**
2  * Wed, Jun 08, 2004 @ 17:03
3  * @author: Fran�ois MORON
4  * @email: francois.moron@rd.francetelecom.com
5  **/

6
7 package org.exoplatform.portlets.weather.component;
8
9 import java.text.DecimalFormat JavaDoc;
10 import java.util.Calendar JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.ResourceBundle JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14
15 import javax.faces.context.FacesContext;
16 import org.exoplatform.faces.core.component.UIExoComponentBase;
17
18 import com.capeclear.www.GlobalWeather_xsd.Direction;
19 import com.capeclear.www.GlobalWeather_xsd.Layer;
20 import com.capeclear.www.GlobalWeather_xsd.Sky;
21 import com.capeclear.www.GlobalWeather_xsd.WeatherReport;
22 import com.capeclear.www.GlobalWeather_xsd.Wind;
23
24
25 public class UIWeatherView extends UIExoComponentBase
26 {
27     private boolean stationFound_;
28     private WeatherReport weatherReport_;
29     private ResourceBundle JavaDoc resources_;
30     private String JavaDoc contextPath_;
31
32     private UIWeatherDetail uiWeatherDetail;
33     private UIWeatherForm uiWeatherForm;
34     
35     public UIWeatherView() {
36         super();
37         setId("UIWeatherView");
38         setRendererType("ChildrenRenderer");
39
40         List JavaDoc children = getChildren();
41
42         uiWeatherDetail = new UIWeatherDetail();
43         uiWeatherForm = new UIWeatherForm();
44
45         weatherReport_ = null;
46         stationFound_ = false;
47         resources_ = null;
48         contextPath_ = null;
49         updateTree();
50         children.add(uiWeatherDetail);
51         children.add(uiWeatherForm);
52     }
53
54     public String JavaDoc getSearchStationCode() {
55         return uiWeatherForm.getSearchStationCode();
56     }
57
58     public String JavaDoc getSearchStationName() {
59         return uiWeatherForm.getSearchStationName();
60     }
61     
62     public UIWeatherForm getUIWeatherForm() {
63         return uiWeatherForm;
64     }
65     
66     public void setSearchStationCode(String JavaDoc pSearchStationCode) {
67         uiWeatherForm.setSearchStationCode(pSearchStationCode);
68     }
69
70     public void setSearchStationName(String JavaDoc pSearchStationName) {
71         uiWeatherForm.setSearchStationName(pSearchStationName);
72     }
73
74     public void setStationFound(boolean pStationFound) {
75         stationFound_ = pStationFound;
76         updateTree();
77     }
78     
79     public void setWeatherReport(WeatherReport pWeatherReport) {
80         weatherReport_ = pWeatherReport;
81         updateTree();
82     }
83     
84     public void setResources(ResourceBundle JavaDoc pResources) {
85         resources_ = pResources;
86         updateTree();
87     }
88     
89     public void setContextPath(String JavaDoc pContextPath) {
90         contextPath_ = pContextPath;
91         updateTree();
92     }
93
94     public void decode(FacesContext context) {
95     }
96
97     private void updateTree() {
98         if (weatherReport_ != null) {
99             // station detail
100
Calendar JavaDoc timeStamp = weatherReport_.getTimestamp();
101             uiWeatherDetail.setStationDetail(timeStamp.getTime().toLocaleString());
102             // sky icon
103
Sky sky = weatherReport_.getSky();
104             if (sky != null) {
105                 String JavaDoc typeSky = null;
106                 String JavaDoc iconFile = null;
107                 Layer [] layers = sky.getLayers();
108                 if (layers != null) {
109                     if (layers.length > 0) {
110                         typeSky = layers[0].getType().getValue();
111                         if (typeSky != null)
112                         {
113                             if (typeSky.equals("CLOUD")) {
114                                 int extent = layers[0].getExtent();
115                                 if ((extent == 1) || (extent == 2) || (extent == 3) || (extent == 4)) {
116                                     typeSky+="_"+String.valueOf(extent);
117                                 }
118                             }
119                             try {
120                                 iconFile = resources_.getString(typeSky+"_DAY_ICON");
121                             }
122                             catch(Exception JavaDoc ex) {
123                             }
124                             try {
125                                 uiWeatherDetail.setIconSrc(contextPath_+resources_.getString("ICONS_SOURCE_PATH")+iconFile);
126                             }
127                             catch(Exception JavaDoc ex) {
128                             }
129                         }
130                     }
131                 }
132             }
133             // temperature
134
uiWeatherDetail.setTemperature(String.valueOf(weatherReport_.getTemperature().getAmbient()));
135             // wind
136
Wind wind = weatherReport_.getWind();
137             if (wind != null) {
138                 String JavaDoc stringWind = null;
139                 try {
140                     DecimalFormat JavaDoc df = new DecimalFormat JavaDoc("# ###.###");
141                     stringWind = df.format(wind.getPrevailing_speed()) + " m/s";
142                 } catch (Exception JavaDoc ex) {
143                 }
144                 Direction direction = wind.getPrevailing_direction();
145                 if (direction != null) {
146                     StringTokenizer JavaDoc strTk = new StringTokenizer JavaDoc(direction.getString(),"(");
147                     if (stringWind != null) stringWind += " (" + strTk.nextToken().toString().trim()+")";
148                     else stringWind = strTk.nextToken().toString();
149                 }
150                 uiWeatherDetail.setWind(stringWind);
151             }
152             uiWeatherDetail.setRendered(true);
153         }
154         else {
155             uiWeatherDetail.setRendered(false);
156         }
157         uiWeatherForm.setRendered(true);
158     }
159 }
160
Popular Tags