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.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
16 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
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 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 | Main.worker.submit(new DownloadRelationTask(Collections.singletonList(rel), Main.getLayerManager().getEditLayer()));
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected void downloadIncomplete(Relation rel) {
|
---|
74 | if (rel.isNew()) return;
|
---|
75 | Set<OsmPrimitive> ret = new HashSet<>();
|
---|
76 | ret.addAll(rel.getIncompleteMembers());
|
---|
77 | if (ret.isEmpty()) return;
|
---|
78 | Main.worker.submit(new DownloadRelationMemberTask(Collections.singletonList(rel), ret, Main.getLayerManager().getEditLayer()));
|
---|
79 | }
|
---|
80 | }
|
---|