1 package tim.prune.function.weather;
3 import javax.swing.table.AbstractTableModel;
5 import tim.prune.I18nManager;
9 * Table model for results of weather forecast
11 public class WeatherTableModel extends AbstractTableModel
13 /** Weather results */
14 private WeatherResults _results;
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;
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");
30 * @return column count
32 public int getColumnCount()
34 if (_results == null) {return 0;}
35 return _results.getNumForecasts();
39 * @param inColNum column number
40 * @return column label for given column
42 public String getColumnName(int inColNum)
44 if (_results != null && inColNum >= 0 && inColNum < getColumnCount())
46 SingleForecast forecast = _results.getForecast(inColNum);
47 if (!forecast.hasTimes() || forecast.getTimeFrom().startsWith("00")) {
48 return forecast.getDate();
50 return forecast.getTimeFrom();
56 * @return number of rows
58 public int getRowCount()
63 /** @return true if there are no columns */
64 public boolean isEmpty()
66 return getColumnCount() == 0;
70 * @param inRowNum row number
71 * @param inColNum column number
72 * @return cell entry at given row and column
74 public Object getValueAt(int inRowNum, int inColNum)
76 if (inColNum < 0 || inColNum >= getColumnCount()) {return "";}
77 SingleForecast forecast = _results.getForecast(inColNum);
83 final String dayDesc = forecast.getDayDesc() == null ? "now" : forecast.getDayDesc();
84 return buildDisplayString(null, I18nManager.getText("dialog.weather.day." + dayDesc));
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());
98 * Build a html string from the given title and value
100 private static final String buildDisplayString(String inTitle, String inValue)
102 if (inValue == null) {return null;}
103 return "<html>" + (inTitle == null ? "" : (inTitle + ": "))
104 + "<big>" + inValue.replaceAll(" ", " ") + "</big></html>";
109 * @param inResults weather results including all forecasts
111 public void setResults(WeatherResults inResults)
113 _results = inResults;
114 fireTableStructureChanged();
118 * Clear the list of forecasts