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