source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/DownloadRelationMemberTask.java@ 6248

Last change on this file since 6248 was 6248, checked in by Don-vip, 11 years ago

Rework console output:

  • new log level "error"
  • Replace nearly all calls to system.out and system.err to Main.(error|warn|info|debug)
  • Remove some unnecessary debug output
  • Some messages are modified (removal of "Info", "Warning", "Error" from the message itself -> notable i18n impact but limited to console error messages not seen by the majority of users, so that's ok)
  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.relation;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.Dialog;
8import java.io.IOException;
9import java.util.Collection;
10import java.util.HashSet;
11import java.util.Set;
12
13import javax.swing.SwingUtilities;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.Relation;
19import org.openstreetmap.josm.gui.DefaultNameFormatter;
20import org.openstreetmap.josm.gui.ExceptionDialogUtil;
21import org.openstreetmap.josm.gui.PleaseWaitRunnable;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
25import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
26import org.openstreetmap.josm.io.OsmTransferException;
27import org.xml.sax.SAXException;
28
29/**
30 * The asynchronous task for downloading relation members.
31 *
32 */
33public class DownloadRelationMemberTask extends PleaseWaitRunnable {
34 private boolean canceled;
35 private Exception lastException;
36 private final Set<Relation> parents = new HashSet<Relation>();
37 private Collection<OsmPrimitive> children;
38 private OsmDataLayer curLayer;
39 private MultiFetchServerObjectReader objectReader;
40
41 public DownloadRelationMemberTask(Relation parent, Collection<OsmPrimitive> children, OsmDataLayer curLayer, Dialog dialog) {
42 super(tr("Download relation members"), new PleaseWaitProgressMonitor(dialog), false /* don't ignore exception */);
43 if(parent != null)
44 this.parents.add(parent);
45 this.children = children;
46 this.curLayer = curLayer;
47 }
48
49 public DownloadRelationMemberTask(Relation parent, Collection<OsmPrimitive> children, OsmDataLayer curLayer) {
50 super(tr("Download relation members"), false /* don't ignore exception */);
51 if(parent != null)
52 this.parents.add(parent);
53 this.children = children;
54 this.curLayer = curLayer;
55 }
56
57 /**
58 * Creates a download task for downloading the child primitives {@code children} for all parent
59 * relations in {@code parents}.
60 *
61 * @param parents the collection of parent relations
62 * @param children the collection of child primitives to download
63 * @param curLayer the current OSM layer
64 */
65 public DownloadRelationMemberTask(Collection<Relation> parents, Collection<OsmPrimitive> children, OsmDataLayer curLayer) {
66 super(tr("Download relation members"), false /* don't ignore exception */);
67 this.parents.addAll(parents);
68 this.children = children;
69 this.curLayer = curLayer;
70 }
71
72 @Override
73 protected void cancel() {
74 canceled = true;
75 synchronized(this) {
76 if (objectReader != null) {
77 objectReader.cancel();
78 }
79 }
80 }
81
82 @Override
83 protected void finish() {
84 Main.map.repaint();
85 if (canceled)
86 return;
87 if (lastException != null) {
88 ExceptionDialogUtil.explainException(lastException);
89 }
90 }
91
92 protected String buildDownloadFeedbackMessage() {
93 if (parents.isEmpty()) {
94 return trn("Downloading {0} incomplete object",
95 "Downloading {0} incomplete objects",
96 children.size(),
97 children.size());
98 } else if (parents.size() == 1) {
99 Relation parent = parents.iterator().next();
100 return trn("Downloading {0} incomplete child of relation ''{1}''",
101 "Downloading {0} incomplete children of relation ''{1}''",
102 children.size(),
103 children.size(),
104 parent.getDisplayName(DefaultNameFormatter.getInstance()));
105 } else {
106 return trn("Downloading {0} incomplete child of {1} parent relations",
107 "Downloading {0} incomplete children of {1} parent relations",
108 children.size(),
109 children.size(),
110 parents.size());
111 }
112 }
113
114 @Override
115 protected void realRun() throws SAXException, IOException, OsmTransferException {
116 try {
117 synchronized (this) {
118 if (canceled) return;
119 objectReader = new MultiFetchServerObjectReader();
120 }
121 objectReader.append(children);
122 progressMonitor.indeterminateSubTask(
123 buildDownloadFeedbackMessage()
124 );
125 final DataSet dataSet = objectReader.parseOsm(progressMonitor
126 .createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
127 if (dataSet == null)
128 return;
129 dataSet.deleteInvisible();
130 synchronized (this) {
131 if (canceled) return;
132 objectReader = null;
133 }
134
135 SwingUtilities.invokeLater(
136 new Runnable() {
137 @Override
138 public void run() {
139 curLayer.mergeFrom(dataSet);
140 curLayer.onPostDownloadFromServer();
141 }
142 }
143 );
144
145 } catch (Exception e) {
146 if (canceled) {
147 Main.warn(tr("Ignoring exception because task was canceled. Exception: {0}", e.toString()));
148 return;
149 }
150 lastException = e;
151 }
152 }
153}
Note: See TracBrowser for help on using the repository browser.