| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package relcontext.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.event.ActionEvent;
|
|---|
| 7 | import java.util.Collections;
|
|---|
| 8 | import java.util.HashSet;
|
|---|
| 9 | import java.util.Set;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.AbstractAction;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 16 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 17 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
|
|---|
| 18 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
|
|---|
| 19 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 20 |
|
|---|
| 21 | import relcontext.ChosenRelation;
|
|---|
| 22 | import relcontext.ChosenRelationListener;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Downloads or updates chosen relation members, depending on completeness.
|
|---|
| 26 | *
|
|---|
| 27 | * @author Zverik
|
|---|
| 28 | */
|
|---|
| 29 | public class DownloadChosenRelationAction extends AbstractAction implements ChosenRelationListener {
|
|---|
| 30 | private final ChosenRelation rel;
|
|---|
| 31 |
|
|---|
| 32 | public DownloadChosenRelationAction(ChosenRelation rel) {
|
|---|
| 33 | putValue(SMALL_ICON, ImageProvider.get("relcontext", "download"));
|
|---|
| 34 | putValue(SHORT_DESCRIPTION, tr("Download incomplete members for the chosen relation"));
|
|---|
| 35 | this.rel = rel;
|
|---|
| 36 | rel.addChosenRelationListener(this);
|
|---|
| 37 | setEnabled(false);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | @Override
|
|---|
| 41 | public void actionPerformed(ActionEvent e) {
|
|---|
| 42 | Relation relation = rel.get();
|
|---|
| 43 | if (relation == null || relation.isNew()) return;
|
|---|
| 44 | int total = relation.getMembersCount();
|
|---|
| 45 | int incomplete = relation.getIncompleteMembers().size();
|
|---|
| 46 | if (incomplete <= 10 && incomplete * 3 < total) {
|
|---|
| 47 | downloadIncomplete(relation);
|
|---|
| 48 | } else {
|
|---|
| 49 | downloadMembers(relation);
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | @Override
|
|---|
| 54 | public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
|
|---|
| 55 | boolean incomplete = false;
|
|---|
| 56 | if (newRelation != null) {
|
|---|
| 57 | for (RelationMember m : newRelation.getMembers()) {
|
|---|
| 58 | if (m.getMember().isIncomplete()) {
|
|---|
| 59 | incomplete = true;
|
|---|
| 60 | break;
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | setEnabled(newRelation != null && incomplete);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | protected void downloadMembers(Relation rel) {
|
|---|
| 68 | if (!rel.isNew()) {
|
|---|
| 69 | MainApplication.worker.submit(
|
|---|
| 70 | new DownloadRelationTask(Collections.singletonList(rel), MainApplication.getLayerManager().getEditLayer()));
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | protected void downloadIncomplete(Relation rel) {
|
|---|
| 75 | if (rel.isNew()) return;
|
|---|
| 76 | Set<OsmPrimitive> ret = new HashSet<>(rel.getIncompleteMembers());
|
|---|
| 77 | if (ret.isEmpty()) return;
|
|---|
| 78 | MainApplication.worker.submit(
|
|---|
| 79 | new DownloadRelationMemberTask(Collections.singletonList(rel), ret, MainApplication.getLayerManager().getEditLayer()));
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|