source: osm/applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/DownloadChosenRelationAction.java@ 30701

Last change on this file since 30701 was 30145, checked in by donvip, 11 years ago

[josm-plugins] global replacement of Main.map.mapview.getEditLayer() by Main.main.getEditLayer() (avoids NPEs)

File size: 2.8 KB
Line 
1package relcontext.actions;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4import java.awt.event.ActionEvent;
5import java.util.Collections;
6import java.util.HashSet;
7import java.util.Set;
8import javax.swing.AbstractAction;
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.osm.OsmPrimitive;
11import org.openstreetmap.josm.data.osm.Relation;
12import org.openstreetmap.josm.data.osm.RelationMember;
13import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
14import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
15import org.openstreetmap.josm.tools.ImageProvider;
16import relcontext.ChosenRelation;
17import relcontext.ChosenRelationListener;
18
19/**
20 * Downloads or updates chosen relation members, depending on completeness.
21 *
22 * @author Zverik
23 */
24public 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}
Note: See TracBrowser for help on using the repository browser.