Skip to content

Commit

Permalink
Allow setting the parent component when using DialogUtil, add utility…
Browse files Browse the repository at this point in the history
… for icons
  • Loading branch information
floscher committed Nov 15, 2020
1 parent 92ce3b5 commit 0d0235a
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.openstreetmap.josm.plugins.pt_assistant.validation.PTAssistantValidatorTest;
import org.openstreetmap.josm.plugins.ptl.DistanceBetweenStops;
import org.openstreetmap.josm.plugins.ptl.PublicTransportLayer;
import org.openstreetmap.josm.tools.ImageProvider;

/**
* This is the main class of the PTAssistant plugin.
Expand All @@ -54,7 +53,6 @@
*
*/
public class PTAssistantPlugin extends Plugin {
public static final ImageProvider ICON = new ImageProvider("bus");

/**
* last fix that was can be re-applied to all similar route segments, can be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.customizepublictransportstop.OSMTags;
import org.openstreetmap.josm.plugins.pt_assistant.PTAssistantPlugin;
import org.openstreetmap.josm.plugins.pt_assistant.utils.PTIcons;
import org.openstreetmap.josm.plugins.pt_assistant.utils.StopUtils;
import org.openstreetmap.josm.tools.I18n;
import org.openstreetmap.josm.tools.MultiMap;
Expand Down Expand Up @@ -104,7 +104,7 @@ public void actionPerformed(ActionEvent e) {
.ifPresent(node -> modify(node, stopPositionNode.get()));
} else {
new Notification(I18n.tr("This action can only be performed if exactly one stop position node is selected."))
.setIcon(PTAssistantPlugin.ICON.get())
.setIcon(PTIcons.BUS.get())
.show();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ public void actionPerformed(ActionEvent e) {
editor.apply();

if (rel.hasIncompleteMembers()) {
if (DialogUtils.showYesNoQuestion(tr("Not all members are downloaded"), tr(
"The relation has incomplete members.\nDo you want to download them and continue with the sorting?"))) {
if (
DialogUtils.showYesNoQuestion(
MainApplication.getMainFrame(),
tr("Not all members are downloaded"),
tr("The relation has incomplete members.\nDo you want to download them and continue with the sorting?")
)
) {

List<Relation> incomplete = Collections.singletonList(rel);
Future<?> future = MainApplication.worker.submit(new DownloadRelationMemberTask(incomplete,
Expand All @@ -128,22 +133,34 @@ private void continueAfterDownload(Relation rel) {
boolean ask_to_create_return_route_and_routeMaster = false;

if (!RouteUtils.isVersionTwoPTRoute(newRel)) {
if (DialogUtils.showYesNoQuestion(tr("This is not a PT v2 relation"),
if (
DialogUtils.showYesNoQuestion(
MainApplication.getMainFrame(),
tr("This is not a PT v2 relation"),
tr("This relation is not PT version 2. Sorting its stops wouldn't make sense.\n\n"
+ "Would you like to set ''public_transport:version=2''?\n\n"
+ "There will still be some extra work needed,\n\n"
+ "but PT_Assistant can help prepare the \n" +
"''route'' and ''route_master'' relations."))) {
"''route'' and ''route_master'' relations."
)
)
) {
RouteUtils.setPTRouteVersion(newRel, "2");
ask_to_create_return_route_and_routeMaster = true;
if (DialogUtils.showYesNoQuestion(tr("Remove oneway and split roundabout ways?"),
tr("To prepare for conversion to PT v2\n\n" +
"it may help to remove oneway and split roundabout ways?\n\n" +
"Would you like to do this automatically?\n\n" +
"When asked to remove stops that cannot be served,\n" +
"answer ''no'' and use the button to sort the stops\n" +
"once more after adding the ways once again\n" +
"by using the routing helper"))) {
if (
DialogUtils.showYesNoQuestion(
MainApplication.getMainFrame(),
tr("Remove oneway and split roundabout ways?"),
tr("To prepare for conversion to PT v2\n\n" +
"it may help to remove oneway and split roundabout ways?\n\n" +
"Would you like to do this automatically?\n\n" +
"When asked to remove stops that cannot be served,\n" +
"answer ''no'' and use the button to sort the stops\n" +
"once more after adding the ways once again\n" +
"by using the routing helper"
)
)
) {
RouteUtils.removeOnewayAndSplitRoundaboutWays(newRel);
RouteUtils.addWayMembersAdjacentToPlatforms(newRel);
}
Expand Down Expand Up @@ -181,10 +198,18 @@ private void continueAfterDownload(Relation rel) {
final String from = route_manager.get("from");
final Optional<String> firstStopName = Optional.ofNullable(route_manager.getFirstStop()).map(PTStop::getName)
.filter(name -> !name.isEmpty()).map(name -> {
if ((from.isEmpty() && DialogUtils.showYesNoQuestion(tr("Set from tag?"),
tr("''from'' tag not set. Set it to\n{0} ?", name)))
|| (!name.equals(from) && DialogUtils.showYesNoQuestion(tr("Change from tag?"),
tr("''from''={0}.\nChange it to\n''{1}''\n instead?", from, name)))) {
if (
(from.isEmpty() && DialogUtils.showYesNoQuestion(
MainApplication.getMainFrame(),
tr("Set from tag?"),
tr("''from'' tag not set. Set it to\n{0} ?", name)
)) ||
(!name.equals(from) && DialogUtils.showYesNoQuestion(
MainApplication.getMainFrame(),
tr("Change from tag?"),
tr("''from''={0}.\nChange it to\n''{1}''\n instead?", from, name)
))
) {
route_manager.set("from", name);
}
return name;
Expand All @@ -193,19 +218,32 @@ private void continueAfterDownload(Relation rel) {
final Optional<String> lastStopName = Optional.ofNullable(route_manager.getLastStop()).map(PTStop::getName)
.filter(name -> !name.isEmpty()).map(name -> {
final String to = route_manager.get("to");
if ((to.isEmpty() && DialogUtils.showYesNoQuestion(tr("Set to tag?"),
tr("''to'' tag not set. Set it to\n{0} ?", name)))
|| (!name.equals(to) && DialogUtils.showYesNoQuestion(tr("Change to tag?"),
tr("''to''={0}.\nChange it to\n''{1}''\n instead?", to, name)))) {
if (
(to.isEmpty() && DialogUtils.showYesNoQuestion(
MainApplication.getMainFrame(),
tr("Set to tag?"),
tr("''to'' tag not set. Set it to\n{0} ?", name)
)) ||
(!name.equals(to) && DialogUtils.showYesNoQuestion(
MainApplication.getMainFrame(),
tr("Change to tag?"),
tr("''to''={0}.\nChange it to\n''{1}''\n instead?", to, name)
))
) {
route_manager.set("to", name);
}
return name;
});

String proposedRelationName = route_manager.getComposedName();
if (!Objects.equals(proposedRelationName, route_manager.get("name"))) {
if (DialogUtils.showYesNoQuestion(tr("Change name tag?"),
tr("Change name to\n''{0}''\n?", proposedRelationName))) {
if (
DialogUtils.showYesNoQuestion(
MainApplication.getMainFrame(),
tr("Change name tag?"),
tr("Change name to\n''{0}''\n?", proposedRelationName)
)
) {
route_manager.set("name", proposedRelationName);
}
route_manager.writeTagsToRelation();
Expand All @@ -214,8 +252,14 @@ private void continueAfterDownload(Relation rel) {
OsmDataLayer layer = MainApplication.getLayerManager().getEditLayer();
Relation routeMaster;

if (ask_to_create_return_route_and_routeMaster && DialogUtils.showYesNoQuestion(tr("Opposite itinerary?"),
tr("Create ''route'' relation for opposite direction of travel?"))) {
if (
ask_to_create_return_route_and_routeMaster &&
DialogUtils.showYesNoQuestion(
MainApplication.getMainFrame(),
tr("Opposite itinerary?"),
tr("Create ''route'' relation for opposite direction of travel?")
)
) {

// Reverse order of members in new route relation
final PTRouteDataManager return_route_manager = new PTRouteDataManager(otherDirRel);
Expand Down Expand Up @@ -254,8 +298,13 @@ private void continueAfterDownload(Relation rel) {

if (rmr.isPresent()) {
routeMaster = rmr.get();
} else if (DialogUtils.showYesNoQuestion(tr("Create a route_master?"),
tr("Create ''route_master'' relation and add these route relations to it?"))) {
} else if (
DialogUtils.showYesNoQuestion(
MainApplication.getMainFrame(),
tr("Create a route_master?"),
tr("Create ''route_master'' relation and add these route relations to it?")
)
) {
routeMaster = new Relation();
routeMaster.put(OSMTags.KEY_RELATION_TYPE, OSMTags.VALUE_TYPE_ROUTE_MASTER);
if (rel.hasKey(OSMTags.KEY_ROUTE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ private ColorPalette() {
public static final Color PINK = new Color(255, 175, 175, 180);
public static final Color WHITE = new Color(255, 255, 255, 180);

public static final Color REF_LABEL_COLOR = new Color(0x80FFFFFF, true);

public static Color[] FIVE_COLORS = { GREEN, RED, BLUE, YELLOW, CYAN };
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.pt_assistant.utils;

import java.awt.Component;

import javax.swing.JOptionPane;

import org.openstreetmap.josm.gui.MainApplication;
Expand Down Expand Up @@ -28,9 +30,9 @@ public static void showOkWarning(final String title, final String message) {
* @param question the question that is displayed in the dialog
* @return {@code true}, iff the user clicked yes. In all other cases, {@code false} is returned.
*/
public static boolean showYesNoQuestion(final String title, final String question) {
public static boolean showYesNoQuestion(final Component parentComponent, final String title, final String question) {
return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
MainApplication.getMainFrame(),
parentComponent,
question,
title,
JOptionPane.YES_NO_OPTION,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.openstreetmap.josm.plugins.pt_assistant.utils;

import org.openstreetmap.josm.tools.ImageProvider;

public final class PTIcons {
private PTIcons() {
// Private constructor to avoid instantiation
}

public static final ImageProvider BUS = new ImageProvider("bus");
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.openstreetmap.josm.gui.mappaint.StyleKeys;
import org.openstreetmap.josm.gui.mappaint.styleelement.TextLabel;
import org.openstreetmap.josm.gui.mappaint.styleelement.placement.OnLineStrategy;
import org.openstreetmap.josm.plugins.pt_assistant.utils.ColorPalette;
import org.openstreetmap.josm.tools.ColorHelper;
import org.openstreetmap.josm.tools.ImageProvider;
import org.openstreetmap.josm.tools.Logging;
Expand Down Expand Up @@ -98,12 +99,11 @@ protected void drawRefLabels(StyledMapRenderer renderer, MultiMap<Pair<Node, Nod
Cascade c = env.mc.getOrCreateCascade("default");
c.put(StyleKeys.FONT_FAMILY, "SansSerif");
c.put(StyleKeys.FONT_SIZE, 16);
Color color = new Color(0x80FFFFFF, true);

for (Pair<Node, Node> nodePair : segmentRefs.keySet()) {
final String label = String.join(", ", new TreeSet<>(segmentRefs.get(nodePair)));
c.put(StyleKeys.TEXT, label);
final TextLabel text = TextLabel.create(env, color, false);
final TextLabel text = TextLabel.create(env, ColorPalette.REF_LABEL_COLOR, false);
final Way way = new Way();
way.addNode(nodePair.a);
way.addNode(nodePair.b);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.openstreetmap.josm.plugins.pt_assistant.utils;

import org.junit.Rule;
import org.junit.Test;
import org.openstreetmap.josm.plugins.pt_assistant.TestUtil;
import org.openstreetmap.josm.testutils.JOSMTestRules;

public class UtilityClassesTest {

@Rule
public JOSMTestRules rules = new JOSMTestRules();

@Test
public void testAllUtilityClasses() {
TestUtil.testUtilityClass(BoundsUtils.class);
Expand All @@ -14,6 +19,7 @@ public void testAllUtilityClasses() {
TestUtil.testUtilityClass(NodeUtils.class);
TestUtil.testUtilityClass(NotificationUtils.class);
TestUtil.testUtilityClass(PrimitiveUtils.class);
TestUtil.testUtilityClass(PTIcons.class);
TestUtil.testUtilityClass(PTProperties.class);
TestUtil.testUtilityClass(RouteUtils.class);
// StopToWayAssigner does not match the criteria
Expand Down

0 comments on commit 0d0235a

Please sign in to comment.