]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/weather/WeatherTableModel.java
35444b6dc9387786e5b870cc34f0c4f9da8000ea
[GpsPrune.git] / tim / prune / function / weather / WeatherTableModel.java
1 package tim.prune.function.weather;
2
3 import javax.swing.table.AbstractTableModel;
4
5 import tim.prune.I18nManager;
6
7
8 /**
9  * Table model for results of weather forecast
10  */
11 public class WeatherTableModel extends AbstractTableModel
12 {
13         /** Weather results */
14         private WeatherResults _results;
15
16         /** Row indices */
17         public static final int ROW_DAY  = 0;
18         public static final int ROW_DESC = 1;
19         public static final int ROW_WIND = 2;
20         public static final int ROW_ICON = 3;
21         public static final int ROW_TEMP = 4;
22         public static final int ROW_HUMID = 5;
23
24         /** String for degrees Celsius */
25         private static final String UNITS_DEGC = I18nManager.getText("units.degreescelsius.short");
26         /** String for degrees Fahrenheit */
27         private static final String UNITS_DEGF = I18nManager.getText("units.degreesfahrenheit.short");
28
29         /**
30          * @return column count
31          */
32         public int getColumnCount()
33         {
34                 if (_results == null) {return 0;}
35                 return _results.getNumForecasts();
36         }
37
38         /**
39          * @param inColNum column number
40          * @return column label for given column
41          */
42         public String getColumnName(int inColNum)
43         {
44                 if (_results != null && inColNum >= 0 && inColNum < getColumnCount())
45                 {
46                         SingleForecast forecast = _results.getForecast(inColNum);
47                         if (!forecast.hasTimes() || forecast.getTimeFrom().startsWith("00")) {
48                                 return forecast.getDate();
49                         }
50                         return forecast.getTimeFrom();
51                 }
52                 return "";
53         }
54
55         /**
56          * @return number of rows
57          */
58         public int getRowCount()
59         {
60                 return 6;
61         }
62
63         /** @return true if there are no columns */
64         public boolean isEmpty()
65         {
66                 return getColumnCount() == 0;
67         }
68
69         /**
70          * @param inRowNum row number
71          * @param inColNum column number
72          * @return cell entry at given row and column
73          */
74         public Object getValueAt(int inRowNum, int inColNum)
75         {
76                 if (inColNum < 0 || inColNum >= getColumnCount()) {return "";}
77                 SingleForecast forecast = _results.getForecast(inColNum);
78                 if (forecast != null)
79                 {
80                         switch (inRowNum)
81                         {
82                                 case ROW_DAY:  {
83                                         final String dayDesc = forecast.getDayDesc() == null ? "now" : forecast.getDayDesc();
84                                         return buildDisplayString(null, I18nManager.getText("dialog.weather.day." + dayDesc));
85                                 }
86                                 case ROW_DESC: return buildDisplayString(null, forecast.getDescription());
87                                 case ROW_WIND: return buildDisplayString(I18nManager.getText("dialog.weather.wind"), forecast.getWindDescription());
88                                 case ROW_ICON: return forecast.getImageName();
89                                 case ROW_TEMP: return buildDisplayString(I18nManager.getText("dialog.weather.temp"), forecast.getTemps()
90                                         + (_results.isCelsius() ? UNITS_DEGC : UNITS_DEGF));
91                                 case ROW_HUMID: return buildDisplayString(I18nManager.getText("dialog.weather.humidity"), forecast.getHumidity());
92                         }
93                 }
94                 return "";
95         }
96
97         /**
98          * Build a html string from the given title and value
99          */
100         private static final String buildDisplayString(String inTitle, String inValue)
101         {
102                 if (inValue == null) {return null;}
103                 return "<html>" + (inTitle == null ? "" : (inTitle + ":&nbsp;"))
104                         + "<big>" + inValue.replaceAll(" ", "&nbsp;") + "</big></html>";
105         }
106
107         /**
108          * Set the results
109          * @param inResults weather results including all forecasts
110          */
111         public void setResults(WeatherResults inResults)
112         {
113                 _results = inResults;
114                 fireTableStructureChanged();
115         }
116
117         /**
118          * Clear the list of forecasts
119          */
120         public void clear()
121         {
122                 setResults(null);
123         }
124 }