From cda6c01a2abc7f60a191d97e8b070218879f7f92 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 26 Feb 2013 20:36:47 +1300 Subject: Add units preference (fix #7) --- src/org/traccar/web/client/FormatterUtil.java | 33 ++++++++++++++++++++--- src/org/traccar/web/client/model/StateReader.java | 4 +-- src/org/traccar/web/client/view/ArchiveView.java | 13 ++++++--- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/org/traccar/web/client/FormatterUtil.java b/src/org/traccar/web/client/FormatterUtil.java index f5c7d3e1..36c24dcb 100644 --- a/src/org/traccar/web/client/FormatterUtil.java +++ b/src/org/traccar/web/client/FormatterUtil.java @@ -1,8 +1,8 @@ package org.traccar.web.client; -import java.util.Date; - +import com.google.gwt.i18n.client.CurrencyList; import com.google.gwt.i18n.client.DateTimeFormat; +import com.google.gwt.i18n.client.NumberFormat; public class FormatterUtil { @@ -10,8 +10,33 @@ public class FormatterUtil { return DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss"); } - public String formatTime(Date time) { - return getTimeFormat().format(time); + private class SpeedNumberFormat extends NumberFormat { + + private final String unit; + private final double factor; + + public SpeedNumberFormat(String unit, double factor) { + super("0.##", CurrencyList.get().getDefault(), true); + this.unit = unit; + this.factor = factor; + } + + @Override + public String format(double number) { + return super.format(number * factor) + " " + unit; + } + + } + + public NumberFormat getSpeedFormat() { + switch (ApplicationContext.getInstance().getUserSettings().getSpeedUnit()) { + case kilometersPerHour: + return new SpeedNumberFormat("km/h", 1.852); + case milesPerHour: + return new SpeedNumberFormat("mph", 1.150779); + default: + return new SpeedNumberFormat("kn", 1); + } } } diff --git a/src/org/traccar/web/client/model/StateReader.java b/src/org/traccar/web/client/model/StateReader.java index 733bde62..667f474b 100644 --- a/src/org/traccar/web/client/model/StateReader.java +++ b/src/org/traccar/web/client/model/StateReader.java @@ -20,11 +20,11 @@ public class StateReader { List state = new LinkedList(); state.add(new StateItem("valid", toString(position.getValid()))); - state.add(new StateItem("time", ApplicationContext.getInstance().getFormatterUtil().formatTime(position.getTime()))); + state.add(new StateItem("time", ApplicationContext.getInstance().getFormatterUtil().getTimeFormat().format(position.getTime()))); state.add(new StateItem("latitude", toString(position.getLatitude()))); state.add(new StateItem("longitude", toString(position.getLongitude()))); state.add(new StateItem("altitude", toString(position.getAltitude()))); - state.add(new StateItem("speed", toString(position.getSpeed()))); + state.add(new StateItem("speed", ApplicationContext.getInstance().getFormatterUtil().getSpeedFormat().format(position.getSpeed()))); state.add(new StateItem("course", toString(position.getCourse()))); state.add(new StateItem("power", toString(position.getPower()))); state.add(new StateItem("address", position.getAddress())); diff --git a/src/org/traccar/web/client/view/ArchiveView.java b/src/org/traccar/web/client/view/ArchiveView.java index 04ece4af..28291d0d 100644 --- a/src/org/traccar/web/client/view/ArchiveView.java +++ b/src/org/traccar/web/client/view/ArchiveView.java @@ -32,6 +32,7 @@ import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.uibinder.client.UiHandler; import com.google.gwt.user.client.ui.Widget; +import com.sencha.gxt.cell.core.client.NumberCell; import com.sencha.gxt.core.client.Style.SelectionMode; import com.sencha.gxt.data.shared.ListStore; import com.sencha.gxt.data.shared.event.StoreHandlers; @@ -108,14 +109,18 @@ public class ArchiveView implements SelectionChangedEvent.SelectionChangedHandle columnConfigList.add(new ColumnConfig(positionProperties.valid(), 0, "Valid")); - ColumnConfig columnConfig = new ColumnConfig(positionProperties.time(), 0, "Time"); - columnConfig.setCell(new DateCell(ApplicationContext.getInstance().getFormatterUtil().getTimeFormat())); - columnConfigList.add(columnConfig); + ColumnConfig columnConfigDate = new ColumnConfig(positionProperties.time(), 0, "Time"); + columnConfigDate.setCell(new DateCell(ApplicationContext.getInstance().getFormatterUtil().getTimeFormat())); + columnConfigList.add(columnConfigDate); columnConfigList.add(new ColumnConfig(positionProperties.latitude(), 0, "Latitude")); columnConfigList.add(new ColumnConfig(positionProperties.longitude(), 0, "Longitude")); columnConfigList.add(new ColumnConfig(positionProperties.altitude(), 0, "Altitude")); - columnConfigList.add(new ColumnConfig(positionProperties.speed(), 0, "Speed")); + + ColumnConfig columnConfigDouble = new ColumnConfig(positionProperties.speed(), 0, "Speed"); + columnConfigDouble.setCell(new NumberCell(ApplicationContext.getInstance().getFormatterUtil().getSpeedFormat())); + columnConfigList.add(columnConfigDouble); + columnConfigList.add(new ColumnConfig(positionProperties.course(), 0, "Course")); columnConfigList.add(new ColumnConfig(positionProperties.power(), 0, "Power")); -- cgit v1.2.3