@@ -64,45 +64,33 @@ public static String extractComment(BaseNavigationPosition position) {
public static String formatDistance(Double distance) {
if (isEmpty(distance) || distance <= 0.0)
return "";
Unit unitPreference = RouteConverter.getInstance().getUnitPreference();
switch (unitPreference) {
Unit unit = RouteConverter.getInstance().getUnitModel().getCurrent();
double distanceInMeters = convertMetersToUnit(distance, unit);
if (abs(distanceInMeters) < maximumDistanceDisplayedInMeters)
return format("%d %s", round(distanceInMeters), unit.getElevation());
double distanceInKilometers = convertKilometersToUnit(distance / 1000.0, unit);
if (abs(distanceInMeters) < maximumDistanceDisplayedInHundredMeters)
return format("%s %s", roundFraction(distanceInKilometers, 1), unit.getDistance());
return format("%d %s", round(distanceInKilometers), unit.getDistance());
}

private static double convertMetersToUnit(Double value, Unit unit) {
switch (unit) {
case METRIC:
return formatMetricDistance(distance);
return value;
case STATUTE:
return formatStatuteDistance(distance);
return meterToFeets(value);
default:
throw new IllegalArgumentException(format("Unit %s is not supported", unitPreference));
throw new IllegalArgumentException(format("Unit %s is not supported", unit));
}
}

private static String formatMetricDistance(Double distance) {
if (abs(distance) < maximumDistanceDisplayedInMeters)
return round(distance) + " m";
if (abs(distance) < maximumDistanceDisplayedInHundredMeters)
return roundFraction(distance / 1000.0, 1) + " Km";
return round(distance / 1000.0) + " Km";
}

private static String formatStatuteDistance(Double distance) {
if (abs(distance) < maximumDistanceDisplayedInMeters)
return round(meterToFeets(distance)) + " ft";
if (abs(distance) < maximumDistanceDisplayedInHundredMeters)
return roundFraction(kilometerToMiles(distance / 1000.0), 1) + " mi";
return round(kilometerToMiles(distance / 1000.0)) + " mi";
}

public static String formatElevation(Double elevation) {
if (isEmpty(elevation))
return "";
Unit unitPreference = RouteConverter.getInstance().getUnitPreference();
switch (unitPreference) {
case METRIC:
return round(elevation) + " m";
case STATUTE:
return round(meterToFeets(elevation)) + " ft";
default:
throw new IllegalArgumentException(format("Unit %s is not supported", unitPreference));
}
Unit unit = RouteConverter.getInstance().getUnitModel().getCurrent();
double distanceInUnit = convertMetersToUnit(elevation, unit);
return format("%d %s", round(distanceInUnit), unit.getElevation());
}

public static String extractElevation(BaseNavigationPosition position) {
@@ -122,27 +110,26 @@ public static String formatLongitudeOrLatitude(Double longitudeOrLatitude) {
return result;
}

private static String formatSpeed(Double speed) {
if (isEmpty(speed))
return "";
Unit unitPreference = RouteConverter.getInstance().getUnitPreference();
switch (unitPreference) {
private static double convertKilometersToUnit(Double value, Unit unit) {
switch (unit) {
case METRIC:
return formatSpeedWithFraction(speed) + " Km/h";
return value;
case STATUTE:
return formatSpeedWithFraction(kilometerToMiles(speed)) + " mi/h";
return kilometerToMiles(value);
default:
throw new IllegalArgumentException(format("Unit %s is not supported", unitPreference));
throw new IllegalArgumentException(format("Unit %s is not supported", unit));
}
}

private static String formatSpeedWithFraction(Double speed) {
String speedStr;
if (abs(speed) < 10.0)
speedStr = Double.toString(roundFraction(speed, 1));
private static String formatSpeed(Double speed) {
if (isEmpty(speed))
return "";
Unit unit = RouteConverter.getInstance().getUnitModel().getCurrent();
double speedInUnit = convertKilometersToUnit(speed, unit);
if (abs(speedInUnit) < 10.0)
return format("%s %s/h", roundFraction(speedInUnit, 1), unit.getDistance());
else
speedStr = Long.toString(round(speed));
return speedStr;
return format("%d %s/h", round(speedInUnit), unit.getDistance());
}

public static String extractSpeed(BaseNavigationPosition position) {
@@ -199,7 +199,7 @@ public void edit(Object aValue, int rowIndex, int columnIndex, boolean fireEvent
}

private Double parseElevation(Object objectValue, String stringValue) {
Unit unitPreference = RouteConverter.getInstance().getUnitPreference();
Unit unitPreference = RouteConverter.getInstance().getUnitModel().getCurrent();
switch (unitPreference) {
case METRIC:
return parseDouble(objectValue, stringValue, "m");
@@ -212,7 +212,7 @@ private Double parseElevation(Object objectValue, String stringValue) {
}

private Double parseSpeed(Object objectValue, String stringValue) {
Unit unitPreference = RouteConverter.getInstance().getUnitPreference();
Unit unitPreference = RouteConverter.getInstance().getUnitModel().getCurrent();
switch (unitPreference) {
case METRIC:
return parseDouble(objectValue, stringValue, "Km/h");
@@ -40,12 +40,12 @@

public class RecentUrlsModel {
private static final Logger log = Logger.getLogger(RecentUrlsModel.class.getName());
private static final Preferences preferences = Preferences.userNodeForPackage(RecentUrlsModel.class);
private static final String RECENT_URLS_PREFERENCE = "recentUrls";
private static final String RECENT_PREFERENCE = "recent";
private static final String MAXIMUM_RECENT_URL_COUNT_PREFERENCE = "maximumRecentUrlCount";
private static final char FIRST_CHAR = 'a';

private final Preferences preferences = Preferences.userNodeForPackage(RecentUrlsModel.class);
private EventListenerList listenerList = new EventListenerList();

private int getMaximumCount() {
@@ -0,0 +1,64 @@
/*
This file is part of RouteConverter.
RouteConverter is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
RouteConverter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RouteConverter; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Copyright (C) 2007 Christian Pesch. All Rights Reserved.
*/

package slash.navigation.converter.gui.models;

import slash.navigation.util.Unit;

import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import java.util.prefs.Preferences;

import static slash.navigation.util.Unit.METRIC;

/**
* A model for {@link Unit}.
*
* @author Christian Pesch
*/

public class UnitModel {
private static final String UNIT_PREFERENCE = "unit";
private static final Preferences preferences = Preferences.userNodeForPackage(UnitModel.class);

private EventListenerList listenerList = new EventListenerList();

public Unit getCurrent() {
return Unit.valueOf(preferences.get(UNIT_PREFERENCE, METRIC.toString()));
}

public void setCurrent(Unit unit) {
preferences.put(UNIT_PREFERENCE, unit.toString());
fireChanged();
}

protected void fireChanged() {
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChangeListener.class) {
((ChangeListener) listeners[i + 1]).stateChanged(null);
}
}
}

public void addChangeListener(ChangeListener l) {
listenerList.add(ChangeListener.class, l);
}
}
@@ -65,6 +65,8 @@
import java.util.logging.Logger;
import java.util.prefs.Preferences;

import static java.lang.Integer.MAX_VALUE;
import static javax.swing.event.TableModelEvent.ALL_COLUMNS;
import static slash.common.io.Files.*;
import static slash.navigation.base.NavigationFileParser.getNumberOfFilesToWriteFor;
import static slash.navigation.base.NavigationFormats.getReadFormatsPreferredByExtension;
@@ -214,6 +216,11 @@ public void process(DocumentEvent e) {
RouteConverter.getInstance().getFrame().setTitle(title);
}
});
r.getUnitModel().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
getPositionsModel().fireTableRowsUpdated(0, MAX_VALUE, ALL_COLUMNS);
}
});

tablePositions.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
@@ -25,6 +25,8 @@
import slash.navigation.converter.gui.elevationview.ElevationView;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

/**
@@ -45,9 +47,16 @@ private void initialize() {
final RouteConverter r = RouteConverter.getInstance();

elevationView = new ElevationView(r.getPositionsModel(), r.getPositionsSelectionModel());
elevationView.setUnit(r.getUnitModel().getCurrent());
elevationPanel = new JPanel(new BorderLayout());
elevationPanel.add(elevationView.getComponent(), BorderLayout.CENTER);
elevationPanel.setTransferHandler(new PanelDropHandler());

r.getUnitModel().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
elevationView.setUnit(r.getUnitModel().getCurrent());
}
});
}

public Component getRootComponent() {
@@ -181,10 +181,10 @@ public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() != SELECTED)
return;
Unit unit = Unit.class.cast(e.getItem());
r.setUnitPreference(unit);
r.getUnitModel().setCurrent(unit);
}
});
comboBoxUnit.setSelectedItem(r.getUnitPreference());
comboBoxUnit.setSelectedItem(r.getUnitModel().getCurrent());

comboBoxTimeZone.setModel(new DefaultComboBoxModel(getTimeZoneIds()));
comboBoxTimeZone.addItemListener(new ItemListener() {
@@ -250,8 +250,8 @@ like to download the later version?
no-update-available=\
There is no later version of RouteConverter available.
distance-axis=Distance [Km]
elevation-axis=Elevation [m]
distance-axis=Distance [{0}]
elevation-axis=Elevation [{0}]
overall-ascend=Overall ascend:
overall-descend=Overall descend:
@@ -253,8 +253,8 @@ Přejete si stáhnout novší verzi?
no-update-available=\
Momentálně není dostupná novší verze RouteConvertera.

distance-axis=Vzdálenost [Km]
elevation-axis=Nadmořská výška [m]
distance-axis=Vzdálenost [{0}]
elevation-axis=Nadmořská výška [{0}]
overall-ascend=Celkový výstup:
overall-descend=Celkový sestup:

@@ -251,8 +251,8 @@ Postoji novije verzija {1}. Da li\n\
no-update-available=\
Trenutno ne postoji novija verzija RouteConverter-a.

distance-axis=Razdaljina [Km]
elevation-axis=Nadmorska visina [m]
distance-axis=Razdaljina [{0}]
elevation-axis=Nadmorska visina [{0}]
overall-ascend=Ukupan uspon:
overall-descend=Ukupan pad:

@@ -253,8 +253,8 @@ Prajete si stiahnúť novšiu verziu?
no-update-available=\
Momentálne nie je dostupná novšia verzia RouteConvertera.

distance-axis=Vzdialenosť [Km]
elevation-axis=Nadmorská výška [m]
distance-axis=Vzdialenosť [{0}]
elevation-axis=Nadmorská výška [{0}]
overall-ascend=Celkový výstup:
overall-descend=Celkový zostup:

@@ -251,8 +251,8 @@ Postoji novije verzija {1}. Da li\n\
no-update-available=\
Trenutno ne postoji novija verzija RouteConverter-a.

distance-axis=Razdaljina [Km]
elevation-axis=Nadmorska visina [m]
distance-axis=Razdaljina [{0}]
elevation-axis=Nadmorska visina [{0}]
overall-ascend=Ukupan uspon:
overall-descend=Ukupan pad:

@@ -262,8 +262,8 @@ RouteConverter当前版本为: 版本 {0}\n\
no-update-available=\
没有最新的RouteConverter版本。

distance-axis=距离[Km]
elevation-axis=海拔[m]
distance-axis=距离[{0}]
elevation-axis=海拔[{0}]
overall-ascend=总体上升:
overall-descend=总体下降:

@@ -287,8 +287,8 @@ like to download the later version?
no-update-available=\
There is no later version of RouteConverter available.
distance-axis=Distance [Km]
elevation-axis=Elevation [m]
distance-axis=Distance [{0}]
elevation-axis=Elevation [{0}]
overall-ascend=Overall ascend:
overall-descend=Overall descend:
@@ -257,8 +257,8 @@ neuere Version heruntergeladen werden?
no-update-available=\
Es gibt keine neuere Version von RouteConverter.

distance-axis=Länge [Km]
elevation-axis=Höhe [m]
distance-axis=Länge [{0}]
elevation-axis=Höhe [{0}]
overall-ascend=Gesamtsteigung:
overall-descend=Gesamtgefälle:

@@ -252,8 +252,8 @@ descargar la
no-update-available=\
No hay disponible ninguna versión más reciente de RouteConverter.

distance-axis=Distancia [Km]
elevation-axis=Altitud [m]
distance-axis=Distancia [{0}]
elevation-axis=Altitud [{0}]
overall-ascend=Ascendente:
overall-descend=Descendente:

@@ -251,8 +251,8 @@ t
no-update-available=\
Il n'y a pas de nouvelle version de RouteConverter.
distance-axis=Distance [Km]
elevation-axis=Altitude [m]
distance-axis=Distance [{0}]
elevation-axis=Altitude [{0}]
overall-ascend=Montée:
overall-descend=Descente:
@@ -252,8 +252,8 @@ de nieuwere versie te downloaden?
no-update-available=\
U heeft de meest recente versie van Routeconverter.
distance-axis=Afstand [Km]
elevation-axis=Elevatie [m]
distance-axis=Afstand [{0}]
elevation-axis=Elevatie [{0}]
overall-ascend=Totale steiging:
overall-descend=Totale daling: