Google天气API在RCP中的应用

 

一、先看效果图,再看代码:

 

Google天气API在RCP中的应用(一) - 严军 - 严军的博客 

二、实现过程

1Google API

http://www.google.com/ig/api?hl=zh-cn&weather=Beijing,最后面的Beijing就是要获取天气的城市北京的拼音名,如果不是直辖市,可以在城市名后加上省名的拼音,如http://www.google.com/ig/api?hl=zh-cn&weather=Linhai,Zhejiang是浙江临海市,如果按城市名无法获取数据,也可以座标的方式来获取,如http://www.google.com/ig/api?hl=zh-cn&weather=,,,28800000,121130000也是浙江临海市的所在位置,具体的坐标查询方法,请参考Google,这里只讲实现方法。

我们以北京为例,把上面查询北京市天气的URL复制到浏览器中并回车,可以看到如下结果:

 

<?xml version="1.0"?>

<xml_api_reply version="1">

    <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1"

        row="0" section="0">

       <forecast_information>

           <city data="Beijing" />

           <postal_code data="Beijing" />

           <latitude_e6 data="" />

           <longitude_e6 data="" />

           <forecast_date data="2009-05-09" />

           <current_date_time data="2009-05-09 16:33:23 +0000" />

           <unit_system data="SI" />

       </forecast_information>

       <current_conditions>

           <condition data="" />

           <temp_f data="80" />

           <temp_c data="26" />

           <humidity data="湿度: 38%" />

           <icon data="/ig/images/weather/sunny.png" />

           <wind_condition data="风向: 西北、风速:11 (公里/小时)" />

       </current_conditions>

       <forecast_conditions>

           <day_of_week data="周六" />

           <low data="13" />

           <high data="23" />

           <icon data="/ig/images/weather/chance_of_rain.png" />

           <condition data="可能有雨" />

       </forecast_conditions>

       <forecast_conditions>

           <day_of_week data="周日" />

           <low data="13" />

           <high data="24" />

           <icon data="/ig/images/weather/mostly_sunny.png" />

           <condition data="以晴为主" />

       </forecast_conditions>

       <forecast_conditions>

           <day_of_week data="周一" />

           <low data="15" />

           <high data="27" />

           <icon data="/ig/images/weather/mostly_sunny.png" />

           <condition data="以晴为主" />

       </forecast_conditions>

       <forecast_conditions>

           <day_of_week data="周二" />

           <low data="13" />

           <high data="26" />

           <icon data="/ig/images/weather/mostly_sunny.png" />

           <condition data="以晴为主" />

       </forecast_conditions>

    </weather>

</xml_api_reply>

 

 

这是一个XML文档,天气数据就在里面,我们只需要从其中提取有用的数据再显示出来就行了。

 

 

2)提取天气数据:

 

GoogleWeatherConstants常量类,定义了XML标签名。

 

package com.hailite.weather.views;

 

import com.hailite.weather.Messages;

 

/**

 * Google气象APIXML节点节点属性描述

 * @author 严军

 * @date 2009-4-10 下午02:15:44

 */

public interface GoogleWeatherConstants {

    public static String image_url = Messages.GoogleWeatherConstants_ImageURL;

 

    public static final String xml_api_reply = "xml_api_reply"; //$NON-NLS-1$

    public static final String weather = "weather"; //$NON-NLS-1$

   

    public static final String module_id = "module_id"; //$NON-NLS-1$

    public static final String tab_id = "tab_id"; //$NON-NLS-1$

    public static final String mobile_row = "mobile_row"; //$NON-NLS-1$

    public static final String mobile_zipped = "mobile_zipped"; //$NON-NLS-1$

    public static final String row = "row"; //$NON-NLS-1$

    public static final String section = "section"; //$NON-NLS-1$

   

    public static final String forecast_information = "forecast_information"; //$NON-NLS-1$

    public static final String city = "city"; //$NON-NLS-1$

    public static final String postal_code = "postal_code"; //$NON-NLS-1$

    public static final String latitude_e6 = "latitude_e6"; //$NON-NLS-1$

    public static final String longitude_e6 = "longitude_e6"; //$NON-NLS-1$

    public static final String forecast_date = "forecast_date"; //$NON-NLS-1$

    public static final String current_date_time = "current_date_time"; //$NON-NLS-1$

    public static final String unit_system = "unit_system"; //$NON-NLS-1$

   

    public static final String current_conditions = "current_conditions"; //$NON-NLS-1$

    public static final String condition = "condition"; //$NON-NLS-1$

    public static final String temp_f = "temp_f"; //$NON-NLS-1$

    public static final String temp_c = "temp_c"; //$NON-NLS-1$

    public static final String humidity = "humidity"; //$NON-NLS-1$

    public static final String icon = "icon"; //$NON-NLS-1$

    public static final String wind_condition = "wind_condition"; //$NON-NLS-1$

 

    public static final String forecast_conditions = "forecast_conditions"; //$NON-NLS-1$

    public static final String day_of_week = "day_of_week"; //$NON-NLS-1$

    public static final String low = "low"; //$NON-NLS-1$

    public static final String high = "high"; //$NON-NLS-1$

   

    public static final String data = "data"; //$NON-NLS-1$

   

    public static final String temp = Messages.GoogleWeatherConstants_C;

}

 

IConditionSearcher接口,从XML文档中提取XML节点信息并缓存到JAVA Bean中。

 

package com.hailite.weather.views;

 

/**

 * @author 严军

 * @date 2009-4-10 下午02:36:58

 */

public interface IConditionSearcher {

    void searching(Object source);

}

 

 

定义了三个JAVA Bean,如下:

package com.hailite.weather.views;

 

public abstract class WeatherCondition {

 

    private String icon;

    private String condition;

 

    public WeatherCondition() {

       super();

    }

 

    public String getIcon() {

       return icon;

    }

 

    public void setIcon(String icon) {

       this.icon = icon;

    }

 

    public String getCondition() {

       return condition;

    }

 

    public void setCondition(String condition) {

       this.condition = condition;

    }

 

}

 

当前天气情况的子类:

package com.hailite.weather.views;

 

/**

 * 当前天气情况

 *

 * @author 严军

 * @date 2009-4-10 下午02:24:47

 */

public class CurrentCondition extends WeatherCondition {

    private String temp_f;

    private String temp_c;

    private String humidity;

    private String wind_condition;

 

    public String getTemp_f() {

       return temp_f;

    }

 

    public void setTemp_f(String temp_f) {

       this.temp_f = temp_f;

    }

 

    public String getTemp_c() {

       return temp_c;

    }

 

    public void setTemp_c(String temp_c) {

       this.temp_c = temp_c;

    }

 

    public String getHumidity() {

       return humidity;

    }

 

    public void setHumidity(String humidity) {

       this.humidity = humidity;

    }

 

    public String getWind_condition() {

       return wind_condition;

    }

 

    public void setWind_condition(String wind_condition) {

       this.wind_condition = wind_condition;

    }

 

}

 

未来天气情况的子类:

package com.hailite.weather.views;

 

/**

 * 预报天气情况

 *

 * @author 严军

 * @date 2009-4-10 下午02:24:47

 */

public class ForecastCondition extends WeatherCondition {

    private int index;

    private int low;

    private int high;

    private String day_of_week;

 

    public int getIndex() {

       return index;

    }

 

    public void setIndex(int index) {

       this.index = index;

    }

 

    public int getLow() {

       return low;

    }

 

    public void setLow(int low) {

       this.low = low;

    }

 

    public int getHigh() {

       return high;

    }

 

    public void setHigh(int high) {

       this.high = high;

    }

 

    public String getDay_of_week() {

       return day_of_week;

    }

 

    public void setDay_of_week(String day_of_week) {

       this.day_of_week = day_of_week;

    }

}

 

收集当前天气和未来天气情况的类,缓存到一个List中,提供给UI调用:

package com.hailite.weather.views;

 

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

 

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

 

import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

 

import com.hailite.weather.Messages;

 

/**

 * @author 严军

 * @date 2009-4-6 下午02:18:41

 */

public class GoogleWeather implements GoogleWeatherConstants {

    private List<WeatherCondition> conditions = new ArrayList<WeatherCondition>();

 

    private void initWeather() throws IOException, ParserConfigurationException,

           SAXException {

       URL url = new URL(Messages.GoogleWeather_WeatherURL);

       System.out.println(Messages.GoogleWeather_WeatherURL);

              //"http://www.google.com/ig/api?hl=zh-cn&weather=,,,28800000,121130000"

              //"http://www.google.com/ig/api?hl=zh-cn&weather=Linhai,Zhejiang"); //$NON-NLS-1$

 

       File f = new File("weather.xml");//$NON-NLS-1$

 

       FileOutputStream out = new FileOutputStream(f);

 

       String temp = null;

       StringBuffer sb = new StringBuffer();

       //sb.append("<?xml version=/"1.0/" encoding=/"UTF-8/"?>"); //$NON-NLS-1$

       BufferedReader in = new BufferedReader(new InputStreamReader(url

              .openStream(), Messages.GoogleWeather_InEncoding));// 读取网页全部内容

       while ((temp = in.readLine()) != null) {

           sb.append(temp + "/n"); //$NON-NLS-1$

       }

       sb.insert(19, Messages.GoogleWeather_Encoding);

       System.out.println(sb);

       in.close();

       out.write(sb.toString().getBytes());

       out.close();

       out.flush();

 

       InputStream is = new FileInputStream(f);

       Document doc = DocumentBuilderFactory.newInstance()

              .newDocumentBuilder().parse(is);

       NodeList nl = doc.getElementsByTagName(weather);

 

       nextNode(nl);

    }

 

    public List<WeatherCondition> getConditions() throws IOException, ParserConfigurationException, SAXException {

       initWeather();

       return conditions;

    }

 

    private void nextNode(NodeList nl) {

       for (int i = 0; i < nl.getLength(); i++) {

           Node node = nl.item(i);

           new CurrentConditionSearcher().searching(node);

           new ForecastConditionSearcher().searching(node);

           if (node.getChildNodes().getLength() > 0) {

              nextNode(node.getChildNodes());

           }

       }

    }

 

    private static String getNodeData(Node node, String attrName) {

       NamedNodeMap nnm = node.getAttributes();

       if (nnm != null) {

           for (int j = 0; j < nnm.getLength(); j++) {

              Node nd = nnm.item(j);

              if (attrName.equals(nd.getNodeName())) {

                  return nd.getNodeValue();

              }

           }

       }

       return null;

    }

 

    private class CurrentConditionSearcher implements IConditionSearcher {

       @Override

       public void searching(Object source) {

           if (source instanceof Node) {

              Node node = (Node) source;

              if (current_conditions.equals(node.getNodeName())) {

                  CurrentCondition cc = new CurrentCondition();

                  NodeList nl = node.getChildNodes();

                  for (int i = 0; i < nl.getLength(); i++) {

                     Node nd = nl.item(i);

                     if (condition.equals(nd.getNodeName())) {

                         cc.setCondition(getNodeData(nd, data));

                         continue;

                     }

                     if (temp_f.equals(nd.getNodeName())) {

                         cc.setTemp_f(getNodeData(nd, data) + temp);

                         continue;

                     }

                      if (temp_c.equals(nd.getNodeName())) {

                         cc.setTemp_c(getNodeData(nd, data) + temp);

                         continue;

                     }

                     if (humidity.equals(nd.getNodeName())) {

                         cc.setHumidity(getNodeData(nd, data));

                         continue;

                     }

                     if (icon.equals(nd.getNodeName())) {

                         cc.setIcon(getNodeData(nd, data));

                         continue;

                     }

                     if (wind_condition.equals(nd.getNodeName())) {

                         cc.setWind_condition(getNodeData(nd, data));

                         continue;

                     }

                  }

                  conditions.add(cc);

              }

           }

       }

 

    }

 

    private class ForecastConditionSearcher implements IConditionSearcher {

       @Override

       public void searching(Object source) {

           Node node = (Node) source;

           if (forecast_conditions.equals(node.getNodeName())) {

              ForecastCondition cc = new ForecastCondition();

              NodeList nl = node.getChildNodes();

              for (int i = 0; i < nl.getLength(); i++) {

                  Node nd = nl.item(i);

                  if (condition.equals(nd.getNodeName())) {

                     cc.setCondition(getNodeData(nd, data));

                     continue;

                  }

                  if (day_of_week.equals(nd.getNodeName())) {

                     cc.setDay_of_week(getNodeData(nd, data));

                     continue;

                  }

                  if (low.equals(nd.getNodeName())) {

                     cc.setLow(Integer.valueOf(getNodeData(nd, data)));

                     continue;

                  }

                  if (high.equals(nd.getNodeName())) {

                     cc.setHigh(Integer.valueOf(getNodeData(nd, data)));

                     continue;

                  }

                  if (icon.equals(nd.getNodeName())) {

                     cc.setIcon(getNodeData(nd, data));

                     continue;

                  }

              }

              cc.setIndex(conditions.size());

               conditions.add(cc);

           }

       }

    }

}

3UI的设计:

UI主要有两类,一类是显示当前天气情况,另一类是显示未来几天的天气情况,具体细节略过,看代码:

显示天气情况的UI的抽象类ConditionComposite,是一个Composite

 

package com.hailite.weather.views;

 

import java.net.URL;

 

import org.eclipse.jface.resource.ImageDescriptor;

import org.eclipse.swt.SWT;

import org.eclipse.swt.graphics.Color;

import org.eclipse.swt.graphics.Font;

import org.eclipse.swt.graphics.Image;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Display;

 

import com.hailite.weather.Messages;

 

/**

 * @author 严军

 * @date 2009-4-10 下午04:21:43

 */

public abstract class ConditionComposite extends Composite implements

       GoogleWeatherConstants {

    static final Font f30 = new Font(Display.getDefault() ,Messages.CurrentConditionComposite_0, 30, SWT.BOLD);

    static final Font f25 = new Font(Display.getDefault() ,Messages.CurrentConditionComposite_1, 25, SWT.NONE);

    static final Font f14 = new Font(Display.getDefault() ,"", 14, SWT.NONE); //$NON-NLS-1$

    static final Font f12 = new Font(Display.getDefault() ,"", 12, SWT.NONE); //$NON-NLS-1$

 

    static final Color c1 = new Color(Display.getDefault() , 0, 0, 255);

    static final Color c2 = new Color(Display.getDefault() , 128, 0, 0);

    static final Color c3 = new Color(Display.getDefault() , 0, 128, 0);

   

    public ConditionComposite(Composite parent, int style,

           WeatherCondition condition) {

       super(parent, style);

    }

 

    protected abstract void initWeather(WeatherCondition condition);

 

    protected Image getImageForUrl(String name) {

       Image img = null;

       if (name.startsWith("/")) { //$NON-NLS-1$

           name = name.substring(1, name.length());

       }

       try {

           URL url = new URL(image_url + name); // 创建URL

           img = ImageDescriptor.createFromURL(url).createImage();

       } catch (Exception e) {

           return img;

       }

       return img;

    }

 

}

 

显示当前天气情况的UICurrentConditionComposite,继承自ConditionComposite

 

package com.hailite.weather.views;

 

import org.eclipse.swt.SWT;

import org.eclipse.swt.custom.CLabel;

import org.eclipse.swt.graphics.Image;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Label;

import org.eclipse.ui.forms.widgets.FormToolkit;

 

import com.hailite.weather.Messages;

 

/**

 * 当前天气

 * @author 严军

 * @date 2009-4-10 下午04:11:53

 */

public class CurrentConditionComposite extends ConditionComposite {

    private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());

 

    private Label lblWindAndHumidity;

    private Label lblTemp;

    private Label lblCondition;

 

    private CLabel lblIcon;

 

    public CurrentConditionComposite(Composite parent, int style, CurrentCondition condition) {

       super(parent, style, condition);

       setLayout(new FillLayout());

       toolkit.adapt(this);

       toolkit.paintBordersFor(this);

 

       final Composite composite_3 = toolkit.createComposite(this, SWT.NONE);

       final GridLayout gridLayout = new GridLayout();

       gridLayout.numColumns = 2;

       composite_3.setLayout(gridLayout);

       toolkit.paintBordersFor(composite_3);

 

       final Composite composite = toolkit.createComposite(composite_3, SWT.NONE);

       final GridData gd_composite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2);

       gd_composite.widthHint = 95;

       composite.setLayoutData(gd_composite);

       composite.setLayout(new FillLayout(SWT.VERTICAL));

       toolkit.paintBordersFor(composite);

 

       lblIcon = new CLabel(composite, SWT.CENTER);

       toolkit.adapt(lblIcon, true, true);

 

       lblTemp = toolkit.createLabel(composite, "", SWT.CENTER); //$NON-NLS-1$

       lblTemp.setForeground(c1);

       lblTemp.setFont(f30);

 

       final Composite composite1 = toolkit.createComposite(composite_3, SWT.NONE);

       final GridData gd_composite1 = new GridData(SWT.FILL, SWT.CENTER, true, false);

       composite1.setLayoutData(gd_composite1);

       composite1.setLayout(new FillLayout());

       toolkit.paintBordersFor(composite1);

 

       lblCondition = toolkit.createLabel(composite1, "", SWT.NONE); //$NON-NLS-1$

       lblCondition.setForeground(c2);

       lblCondition.setFont(f25);

 

       final Composite composite2 = toolkit.createComposite(composite_3, SWT.NONE);

       final GridData gd_composite2 = new GridData(SWT.FILL, SWT.FILL, true, false);

       composite2.setLayoutData(gd_composite2);

       composite2.setLayout(new FillLayout(SWT.VERTICAL));

       toolkit.paintBordersFor(composite2);

 

       lblWindAndHumidity = toolkit.createLabel(composite2, "", SWT.WRAP); //$NON-NLS-1$

       lblWindAndHumidity.setForeground(c3);

       lblWindAndHumidity.setFont(f12);

 

       //

       initWeather(condition);

    }

   

    @Override

    protected void initWeather(WeatherCondition condition) {

       if (condition instanceof CurrentCondition) {

           CurrentCondition cc = (CurrentCondition) condition;

           lblCondition.setText(cc.getCondition() == null ? "" : cc.getCondition()); //$NON-NLS-1$

           if (cc.getIcon() != null && cc.getIcon().length() > 0) {

              Image img = getImageForUrl(cc.getIcon());

              if (img != null && img.isDisposed() == false) {

                  lblIcon.setImage(img);

              } else {

                  lblIcon.setFont(f25);

                  lblIcon.setText(cc.getCondition() == null ? "" : cc.getCondition()); //$NON-NLS-1$

              }

           }

           lblTemp.setText(cc.getTemp_c() == null ? "" : cc.getTemp_c()); //$NON-NLS-1$

           String wah = cc.getWind_condition() + Messages.CurrentConditionComposite_8 + cc.getHumidity();

           lblWindAndHumidity.setText(wah);

           lblIcon.setToolTipText(cc.getCondition() == null ? "" : cc.getCondition()); //$NON-NLS-1$

       }

    }

   

    @Override

    public void dispose() {

       if (!f12.isDisposed())

           f12.dispose();

       if (!f25.isDisposed())

           f25.dispose();

       if (!f30.isDisposed())

           f30.dispose();

       super.dispose();

    }

}

 

 

显示未来天气情况的UIForecastConditionComposite,继承自ConditionComposite

 

package com.hailite.weather.views;

 

import org.eclipse.swt.SWT;

import org.eclipse.swt.custom.CLabel;

import org.eclipse.swt.graphics.Image;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Label;

import org.eclipse.ui.forms.widgets.FormToolkit;

 

/**

 * 未来天气

 * @author 严军

 * @date 2009-4-10 下午04:11:53

 */

public class ForecastConditionComposite extends ConditionComposite {

    private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());

 

    private Label lblCondition;

 

    private CLabel lblIcon;

 

    private Label lblDay;

   

    private int index;

 

    public ForecastConditionComposite(Composite parent, int style, ForecastCondition fc) {

       super(parent, style, fc);

       setLayout(new FillLayout());

       toolkit.adapt(this);

       toolkit.paintBordersFor(this);

 

       final Composite composite_3 = toolkit.createComposite(this, SWT.NONE);

       composite_3.setLayout(new GridLayout());

       toolkit.paintBordersFor(composite_3);

 

       lblDay = toolkit.createLabel(composite_3, "", SWT.CENTER); //$NON-NLS-1$

       lblDay.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));

 

       lblIcon = new CLabel(composite_3, SWT.CENTER);

       lblIcon.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));

       toolkit.adapt(lblIcon, true, true);

 

       lblCondition = toolkit.createLabel(composite_3, "", SWT.CENTER); //$NON-NLS-1$

       lblCondition.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));

 

       //

       this.index = fc.getIndex();

       initWeather(fc);

    }

   

    @Override

    protected void initWeather(WeatherCondition condition) {

       if (condition instanceof ForecastCondition) {

           ForecastCondition cc = (ForecastCondition) condition;

           lblCondition.setText(cc.getLow() + " / " + cc.getHigh() + temp); //$NON-NLS-1$

           lblDay.setText(cc.getDay_of_week() == null ? "" : cc.getDay_of_week()); //$NON-NLS-1$

           if (cc.getIcon() != null && cc.getIcon().length() > 0) {

              Image img = getImageForUrl(cc.getIcon());

              if (img != null && img.isDisposed() == false) {

                  lblIcon.setImage(img);

              } else {

                  lblIcon.setFont(f25);

                  lblIcon.setText(cc.getCondition() == null ? "" : cc.getCondition()); //$NON-NLS-1$

              }

           }

           //lblIcon.setImage(cc.getIcon() == null ? null : getImageForUrl(cc.getIcon()));

           lblIcon.setToolTipText(cc.getCondition() == null ? "" : cc.getCondition()); //$NON-NLS-1$

       }

    }

   

    @Override

    public void dispose() {

       if (!f14.isDisposed())

           f14.dispose();

       if (!f25.isDisposed())

           f25.dispose();

       if (!f30.isDisposed())

           f30.dispose();

       super.dispose();

    }

 

    public int getIndex() {

       return index;

    }

}

 

4)显示天气预报:

 

新建一个ViewPart,名为WeatherView,把所有的天气情况显示在WeatherView,并创建一个定时器,每隔4个小时刷新一次天气情况,代码如下:

package com.hailite.weather.views;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import org.eclipse.swt.SWT;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Display;

import org.eclipse.ui.forms.widgets.FormToolkit;

import org.eclipse.ui.forms.widgets.Section;

import org.eclipse.ui.part.ViewPart;

 

import com.hailite.weather.Messages;

 

public class WeatherView extends ViewPart {

    public static final String ID = "com.hailite.weather.views.WeatherView"; //$NON-NLS-1$

    private Composite container;

    private Composite currentContainer;

    private Composite forecastContainer;

    private Map<Class<?>, List<ConditionComposite>> conditionComposites = new HashMap<Class<?>, List<ConditionComposite>>();

    private static final int timer = 1000 * 60 * 60 * 4;

 

    @Override

    public void createPartControl(Composite parent) {

       FormToolkit toolkit = new FormToolkit(Display.getCurrent());

       container = toolkit.createComposite(parent, SWT.NONE);

       container.setLayout(new FillLayout());

       toolkit.paintBordersFor(container);

 

       final Composite composite = toolkit.createComposite(container, SWT.NONE);

       composite.setLayout(new GridLayout());

       toolkit.paintBordersFor(composite);

 

       final Section section_2 = toolkit.createSection(composite, Section.TITLE_BAR);

       section_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

       section_2.setText(Messages.WeatherView_0);

 

       currentContainer = toolkit.createComposite(section_2, SWT.NONE);

       currentContainer.setLayout(new FillLayout());

       section_2.setClient(currentContainer);

       toolkit.paintBordersFor(currentContainer);

      

 

       final Section section_2_1 = toolkit.createSection(composite, Section.TITLE_BAR);

       section_2_1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true));

       section_2_1.setText(Messages.WeatherView_1);

 

       forecastContainer = toolkit.createComposite(section_2_1, SWT.NONE);

       forecastContainer.setLayout(new FillLayout());

       toolkit.paintBordersFor(forecastContainer);

       section_2_1.setClient(forecastContainer);

       //

       initWeather();

       //

       container.getDisplay().timerExec(timer, new Runnable(){

           @Override

           public void run() {

              refreshWeather();

              if (container == null || container.isDisposed()) {

                  return;

              }

              Display display = container.getDisplay();

              if (display == null || display.isDisposed()) {

                  return;

              }

              display.timerExec(timer, this);

           }

       });

    }

   

    private void refreshWeather() {

       try {

           List<WeatherCondition> list = new GoogleWeather().getConditions();

           if (list.size() > 0) {

              for (WeatherCondition wc : list) {

                  List<ConditionComposite> cccs = conditionComposites.get(wc.getClass());

                  if (cccs != null) {

                     for (ConditionComposite cc : cccs) {

                         if (cc != null && cc.isDisposed() == false) {

                            if (wc instanceof ForecastCondition && cc instanceof ForecastConditionComposite) {

                                if (((ForecastCondition)wc).getIndex() == ((ForecastConditionComposite)cc).getIndex()) {

                                   cc.initWeather(wc);

                                }

                            } else if (wc instanceof CurrentCondition && cc instanceof CurrentConditionComposite) {

                                cc.initWeather(wc);

                            }

                         }

                     }

                  }

              }

           }

       } catch (Exception e) {

           e.printStackTrace();

       }

   

    }

   

    private void initWeather() {

       try {

           List<WeatherCondition> list = new GoogleWeather().getConditions();

           List<ConditionComposite> fccs = new ArrayList<ConditionComposite>();

           List<ConditionComposite> cccs = new ArrayList<ConditionComposite>();

           if (list.size() > 0) {

              for (WeatherCondition wc : list) {

                  if (wc instanceof CurrentCondition) {

                     cccs.add(createCurrentCondition(currentContainer, (CurrentCondition) wc));

                     continue;

                  }

                  if (wc instanceof ForecastCondition) {

                     fccs.add(createForecastCondition(forecastContainer, (ForecastCondition) wc));

                     continue;

                  }

              }

           }

           conditionComposites.put(CurrentCondition.class, cccs);

           conditionComposites.put(ForecastCondition.class, fccs);

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

 

    private CurrentConditionComposite createCurrentCondition(Composite container, CurrentCondition cc) {

       return new CurrentConditionComposite(currentContainer, SWT.NONE, cc);

      

    }

   

    private ForecastConditionComposite createForecastCondition(Composite container, ForecastCondition fc) {

       return new ForecastConditionComposite(forecastContainer, SWT.NONE, fc);

    }

 

    @Override

    public void setFocus() {

       container.setFocus();

    }

 

}

 

5)国际化:

 

国际化类Messages

package com.hailite.weather;

 

import org.eclipse.osgi.util.NLS;

 

public class Messages extends NLS {

    private static final String BUNDLE_NAME = "com.hailite.weather.messages"; //$NON-NLS-1$

    public static String CurrentConditionComposite_0;

    public static String CurrentConditionComposite_1;

    public static String CurrentConditionComposite_8;

    public static String ForecastConditionComposite_0;

    public static String ForecastConditionComposite_1;

    public static String GoogleWeather_Encoding;

    public static String GoogleWeather_InEncoding;

    public static String GoogleWeather_WeatherURL;

    public static String GoogleWeatherConstants_C;

    public static String GoogleWeatherConstants_ImageURL;

    public static String WeatherView_0;

    public static String WeatherView_1;

    static {

       // initialize resource bundle

       NLS.initializeMessages(BUNDLE_NAME, Messages.class);

    }

 

    private Messages() {

    }

}

 

国际化文件messages.properties

#Created by JInto - www.guh-software.de

#Sat May 09 09:26:16 CST 2009

CurrentConditionComposite_0=/u9ED1/u4F53

CurrentConditionComposite_1=/u5B8B/u4F53

CurrentConditionComposite_8=/u3001

ForecastConditionComposite_0=/u9ED1/u4F53

ForecastConditionComposite_1=/u5B8B/u4F53

GoogleWeatherConstants_C=/u2103

GoogleWeatherConstants_ImageURL=http/://www.google.cn/

GoogleWeather_Encoding=/ encoding/="GBK"

GoogleWeather_InEncoding=GBK

GoogleWeather_WeatherURL=http/://www.google.com/ig/api?hl/=zh-cn&weather/=Beijing

WeatherView_0=/u5F53/u524D/u5929/u6C14

WeatherView_1=/u672A/u6765/u5929/u6C14

 

三、总结

相对来说,Google的天气API还算稳定,但也经常会得不到数据,而且得到的数据也不一定准确,与当地气象部门发布的气象信息存在较大的出入。所以,获得的气象数据只能作为参考。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值