]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/weather/WeatherTableModel.java
Version 16, February 2014
[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("Wind", forecast.getWindDescription());
88                                 case ROW_ICON: return forecast.getImageName();
89                                 case ROW_TEMP: return buildDisplayString("Temp", forecast.getTemps()
90                                         + (_results.isCelsius() ? UNITS_DEGC : UNITS_DEGF));
91                                 case ROW_HUMID: return buildDisplayString("Humidity", forecast.getHumidity());
92                         }
93                         // TODO: Use language-specific texts for wind, temp and humidity
94                 }
95                 return "";
96         }
97
98         /**
99          * Build a html string from the given title and value
100          */
101         private static final String buildDisplayString(String inTitle, String inValue)
102         {
103                 if (inValue == null) {return null;}
104                 return "<html>" + (inTitle == null ? "" : (inTitle + ":&nbsp;"))
105                         + "<big>" + inValue.replaceAll(" ", "&nbsp;") + "</big></html>";
106         }
107
108         /**
109          * Set the results
110          * @param inResults weather results including all forecasts
111          */
112         public void setResults(WeatherResults inResults)
113         {
114                 _results = inResults;
115                 fireTableStructureChanged();
116         }
117
118         /**
119          * Clear the list of forecasts
120          */
121         public void clear()
122         {
123                 setResults(null);
124         }
125 }