X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Ffunction%2Fweather%2FWeatherTableModel.java;fp=src%2Ftim%2Fprune%2Ffunction%2Fweather%2FWeatherTableModel.java;h=35444b6dc9387786e5b870cc34f0c4f9da8000ea;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/function/weather/WeatherTableModel.java b/src/tim/prune/function/weather/WeatherTableModel.java new file mode 100644 index 0000000..35444b6 --- /dev/null +++ b/src/tim/prune/function/weather/WeatherTableModel.java @@ -0,0 +1,124 @@ +package tim.prune.function.weather; + +import javax.swing.table.AbstractTableModel; + +import tim.prune.I18nManager; + + +/** + * Table model for results of weather forecast + */ +public class WeatherTableModel extends AbstractTableModel +{ + /** Weather results */ + private WeatherResults _results; + + /** Row indices */ + public static final int ROW_DAY = 0; + public static final int ROW_DESC = 1; + public static final int ROW_WIND = 2; + public static final int ROW_ICON = 3; + public static final int ROW_TEMP = 4; + public static final int ROW_HUMID = 5; + + /** String for degrees Celsius */ + private static final String UNITS_DEGC = I18nManager.getText("units.degreescelsius.short"); + /** String for degrees Fahrenheit */ + private static final String UNITS_DEGF = I18nManager.getText("units.degreesfahrenheit.short"); + + /** + * @return column count + */ + public int getColumnCount() + { + if (_results == null) {return 0;} + return _results.getNumForecasts(); + } + + /** + * @param inColNum column number + * @return column label for given column + */ + public String getColumnName(int inColNum) + { + if (_results != null && inColNum >= 0 && inColNum < getColumnCount()) + { + SingleForecast forecast = _results.getForecast(inColNum); + if (!forecast.hasTimes() || forecast.getTimeFrom().startsWith("00")) { + return forecast.getDate(); + } + return forecast.getTimeFrom(); + } + return ""; + } + + /** + * @return number of rows + */ + public int getRowCount() + { + return 6; + } + + /** @return true if there are no columns */ + public boolean isEmpty() + { + return getColumnCount() == 0; + } + + /** + * @param inRowNum row number + * @param inColNum column number + * @return cell entry at given row and column + */ + public Object getValueAt(int inRowNum, int inColNum) + { + if (inColNum < 0 || inColNum >= getColumnCount()) {return "";} + SingleForecast forecast = _results.getForecast(inColNum); + if (forecast != null) + { + switch (inRowNum) + { + case ROW_DAY: { + final String dayDesc = forecast.getDayDesc() == null ? "now" : forecast.getDayDesc(); + return buildDisplayString(null, I18nManager.getText("dialog.weather.day." + dayDesc)); + } + case ROW_DESC: return buildDisplayString(null, forecast.getDescription()); + case ROW_WIND: return buildDisplayString(I18nManager.getText("dialog.weather.wind"), forecast.getWindDescription()); + case ROW_ICON: return forecast.getImageName(); + case ROW_TEMP: return buildDisplayString(I18nManager.getText("dialog.weather.temp"), forecast.getTemps() + + (_results.isCelsius() ? UNITS_DEGC : UNITS_DEGF)); + case ROW_HUMID: return buildDisplayString(I18nManager.getText("dialog.weather.humidity"), forecast.getHumidity()); + } + } + return ""; + } + + /** + * Build a html string from the given title and value + */ + private static final String buildDisplayString(String inTitle, String inValue) + { + if (inValue == null) {return null;} + return "" + (inTitle == null ? "" : (inTitle + ": ")) + + "" + inValue.replaceAll(" ", " ") + ""; + } + + /** + * Set the results + * @param inResults weather results including all forecasts + */ + public void setResults(WeatherResults inResults) + { + _results = inResults; + fireTableStructureChanged(); + } + + /** + * Clear the list of forecasts + */ + public void clear() + { + setResults(null); + } +}