-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Taylor Smock <[email protected]>
- Loading branch information
Showing
10 changed files
with
272 additions
and
7 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/main/java/org/openstreetmap/josm/plugins/routing2/PreferenceHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.plugins.routing2; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.io.UncheckedIOException; | ||
import java.util.Base64; | ||
import java.util.Objects; | ||
|
||
import jakarta.annotation.Nonnull; | ||
import jakarta.annotation.Nullable; | ||
import org.openstreetmap.josm.spi.preferences.Config; | ||
import org.openstreetmap.josm.tools.Logging; | ||
|
||
/** | ||
* A class for reading/writing preferences | ||
*/ | ||
public final class PreferenceHelper { | ||
private PreferenceHelper() { | ||
// Hide the constructor | ||
} | ||
|
||
/** | ||
* Read a record from preferences | ||
* @param preference The preference to read the record from | ||
* @param def The default value | ||
* @return The record from preferences, or the default | ||
* @param <R> The record type | ||
*/ | ||
@Nonnull | ||
public static <R extends Record> R readRecord(@Nonnull String preference, @Nonnull R def) { | ||
Objects.requireNonNull(def, "default value can not be null"); | ||
Objects.requireNonNull(preference, "preference can not be null"); | ||
final String pref = Config.getPref().get(preference); | ||
if (pref != null && !pref.isEmpty()) { | ||
try (ByteArrayInputStream bais = new ByteArrayInputStream(Base64.getDecoder().decode(pref)); | ||
ObjectInputStream ois = new ObjectInputStream(bais)) { | ||
Object read = ois.readObject(); | ||
if (def.getClass().isInstance(read)) { | ||
return (R) def.getClass().cast(read); | ||
} | ||
} catch (IOException e) { | ||
// Should never happen with ByteArrayInputStream | ||
throw new UncheckedIOException(e); | ||
} catch (ClassNotFoundException e) { | ||
Logging.error(e); | ||
Config.getPref().put(preference, null); // Reset the preference, just in case. | ||
} | ||
} | ||
return def; | ||
} | ||
|
||
/** | ||
* Write a record to preferences | ||
* @param preference The preference to write to | ||
* @param rec The record to write | ||
* @param <R> The record type | ||
*/ | ||
public static <R extends Record> void writeRecord(@Nonnull String preference, @Nullable R rec) { | ||
Objects.requireNonNull(preference, "preference can not be null"); | ||
if (rec == null) { | ||
Config.getPref().put(preference, null); | ||
} else { | ||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
ObjectOutputStream oos = new ObjectOutputStream(baos)) { | ||
oos.writeObject(rec); | ||
oos.flush(); | ||
Config.getPref().put(preference, Base64.getEncoder().encodeToString(baos.toByteArray())); | ||
} catch (IOException e) { | ||
// This shouldn't happen with a ByteArrayOutputStream | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/org/openstreetmap/josm/plugins/routing2/RoutingPreferences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.plugins.routing2; | ||
|
||
import static org.openstreetmap.josm.tools.I18n.tr; | ||
|
||
import java.util.EnumSet; | ||
|
||
import javax.swing.ImageIcon; | ||
import javax.swing.JLabel; | ||
import javax.swing.JPanel; | ||
import javax.swing.JTabbedPane; | ||
|
||
import org.openstreetmap.josm.gui.preferences.ExtensibleTabPreferenceSetting; | ||
import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane; | ||
import org.openstreetmap.josm.gui.widgets.JosmComboBox; | ||
import org.openstreetmap.josm.gui.widgets.JosmComboBoxModel; | ||
import org.openstreetmap.josm.plugins.routing2.lib.valhalla.TransportationType; | ||
import org.openstreetmap.josm.plugins.routing2.lib.valhalla.ValhallaConfig; | ||
import org.openstreetmap.josm.tools.ImageProvider; | ||
|
||
/** | ||
* Preferences for the routing engine(s) | ||
*/ | ||
public class RoutingPreferences extends ExtensibleTabPreferenceSetting { | ||
private ValhallaConfig valhallaConfig; | ||
|
||
RoutingPreferences() { | ||
super("dialogs/routing", tr("Routing2"), tr("Various settings that influence the routing plugin."), false); | ||
this.valhallaConfig = PreferenceHelper.readRecord("routing2.valhalla.config", ValhallaConfig.DEFAULT); | ||
} | ||
|
||
@Override | ||
public boolean ok() { | ||
PreferenceHelper.writeRecord("routing2.valhalla.config", | ||
ValhallaConfig.DEFAULT.equals(valhallaConfig) ? null : valhallaConfig); | ||
return false; | ||
} | ||
|
||
@Override | ||
public ImageIcon getIcon(ImageProvider.ImageSizes size) { | ||
return super.getIcon(size); | ||
} | ||
|
||
@Override | ||
public void addGui(PreferenceTabbedPane gui) { | ||
final JTabbedPane tabbedPane = getTabPane(); | ||
tabbedPane.add(tr("Valhalla"), buildValhallaPane()); | ||
super.addGui(gui); | ||
} | ||
|
||
private JPanel buildValhallaPane() { | ||
final JPanel jPanel = new JPanel(); | ||
final JosmComboBoxModel<TransportationType> model = new JosmComboBoxModel<>(); | ||
final JosmComboBox<TransportationType> valhalla = new JosmComboBox<>(model); | ||
model.addAllElements(EnumSet.allOf(TransportationType.class)); | ||
valhalla.setSelectedItem(this.valhallaConfig.transportationType()); | ||
jPanel.add(new JLabel(tr("Transportation type"))); | ||
jPanel.add(valhalla); | ||
valhalla.addItemListener(e -> { | ||
if (e.getItem()instanceof TransportationType type | ||
&& !type.equals(this.valhallaConfig.transportationType())) { | ||
this.valhallaConfig = new ValhallaConfig(type, this.valhallaConfig.systemOfMeasurement(), | ||
this.valhallaConfig.alternates()); | ||
} | ||
}); | ||
return jPanel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/org/openstreetmap/josm/plugins/routing2/lib/valhalla/TransportationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.plugins.routing2.lib.valhalla; | ||
|
||
/** | ||
* The transportation type | ||
*/ | ||
public enum TransportationType { | ||
/** Default */ | ||
AUTO, | ||
/** Bicycle */ | ||
BICYCLE, | ||
/** Bus */ | ||
BUS, | ||
/** Bike share */ | ||
BIKE_SHARE, | ||
/** Truck */ | ||
TRUCK, | ||
/** Taxi */ | ||
TAXI, | ||
/** Scooter */ | ||
MOTOR_SCOOTER, | ||
/** Motorcycle */ | ||
MOTORCYCLE, | ||
/** Multimodel (e.g. ped + public transit) */ | ||
MULTIMODEL, | ||
/** Pedestrian */ | ||
PEDESTRIAN, | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/openstreetmap/josm/plugins/routing2/lib/valhalla/ValhallaConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.plugins.routing2.lib.valhalla; | ||
|
||
import java.io.Serializable; | ||
|
||
import jakarta.annotation.Nonnull; | ||
import jakarta.annotation.Nullable; | ||
import org.openstreetmap.josm.data.SystemOfMeasurement; | ||
|
||
/** | ||
* A configuration record for valhalla | ||
* @param transportationType The transportation type to use | ||
* @param systemOfMeasurement The system of measurement to use, or the default for the download area | ||
* @param alternates The number of alternate routes to generate | ||
*/ | ||
public record ValhallaConfig(@Nonnull TransportationType transportationType, @Nullable SystemOfMeasurement systemOfMeasurement, int alternates) implements Serializable { | ||
/** The default configuration */ | ||
public static ValhallaConfig DEFAULT = new ValhallaConfig(TransportationType.AUTO, null, 3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.