source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTask.java@ 13891

Last change on this file since 13891 was 13261, checked in by Don-vip, 6 years ago

Open Location: new expert combobox to choose whether to zoom on downloaded data (like in download dialog) + remember window geometry (no i18n impact)

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import java.util.ArrayList;
5import java.util.List;
6
7import org.openstreetmap.josm.data.ProjectionBounds;
8import org.openstreetmap.josm.io.XmlWriter;
9
10/**
11 * Common abstract implementation of other download tasks.
12 * @param <T> The downloaded data type
13 * @since 2322
14 */
15public abstract class AbstractDownloadTask<T> implements DownloadTask {
16 private final List<Object> errorMessages;
17 private boolean canceled;
18 private boolean failed;
19 protected T downloadedData;
20 protected boolean zoomAfterDownload = true;
21
22 /**
23 * Constructs a new {@code AbstractDownloadTask}.
24 */
25 public AbstractDownloadTask() {
26 errorMessages = new ArrayList<>();
27 }
28
29 /**
30 * Determines if the download task has been canceled.
31 * @return {@code true} if the download task has been canceled
32 */
33 public boolean isCanceled() {
34 return canceled;
35 }
36
37 /**
38 * Marks this download task as canceled.
39 * @param canceled {@code true} to mark this download task as canceled
40 */
41 public void setCanceled(boolean canceled) {
42 this.canceled = canceled;
43 }
44
45 /**
46 * Determines if the download task has failed.
47 * @return {@code true} if the download task has failed
48 */
49 public boolean isFailed() {
50 return failed;
51 }
52
53 /**
54 * Marks this download task as failed.
55 * @param failed {@code true} to mark this download task as failed
56 */
57 public void setFailed(boolean failed) {
58 this.failed = failed;
59 }
60
61 protected final void rememberErrorMessage(String message) {
62 errorMessages.add(message);
63 }
64
65 protected final void rememberException(Exception exception) {
66 errorMessages.add(exception);
67 }
68
69 protected final void rememberDownloadedData(T data) {
70 this.downloadedData = data;
71 }
72
73 /**
74 * Replies the downloaded data.
75 * @return The downloaded data.
76 */
77 public final T getDownloadedData() {
78 return downloadedData;
79 }
80
81 @Override
82 public final void setZoomAfterDownload(boolean zoomAfterDownload) {
83 this.zoomAfterDownload = zoomAfterDownload;
84 }
85
86 @Override
87 public List<Object> getErrorObjects() {
88 return errorMessages;
89 }
90
91 @Override
92 public String acceptsDocumentationSummary() {
93 StringBuilder buff = new StringBuilder(128)
94 .append("<tr><td>")
95 .append(getTitle())
96 .append(":</td><td>");
97 String[] patterns = getPatterns();
98 if (patterns.length > 0) {
99 buff.append("<ul>");
100 for (String pattern: patterns) {
101 buff.append("<li>")
102 .append(XmlWriter.encode(pattern))
103 .append("</li>");
104 }
105 buff.append("</ul>");
106 }
107 buff.append("</td></tr>");
108 return buff.toString();
109 }
110
111 /**
112 * Determines if the given URL is accepted by {@link #getPatterns}.
113 * Can be overridden for more complex checking logic.
114 * @param url URL to donwload
115 * @return {@code true} if this URL is accepted
116 */
117 public boolean acceptsUrl(String url) {
118 if (url == null)
119 return false;
120 for (String p: getPatterns()) {
121 if (url.matches(p)) {
122 return true;
123 }
124 }
125 return false;
126 }
127
128 /**
129 * Check / decide if the task is safe for remotecontrol.
130 *
131 * Keep in mind that a potential attacker has full control over the content
132 * of the file that will be downloaded.
133 * If it is possible to run arbitrary code or write to the local file
134 * system, then the task is (obviously) not save for remote execution.
135 *
136 * The default value is false = unsafe. Override in a subclass to
137 * allow running the task via remotecontol.
138 *
139 * @return true if it is safe to download and open any file of the
140 * corresponding format, false otherwise
141 */
142 public boolean isSafeForRemotecontrolRequests() {
143 return false;
144 }
145
146 @Override
147 public boolean acceptsUrl(String url, boolean isRemotecontrol) {
148 if (isRemotecontrol && !isSafeForRemotecontrolRequests())
149 return false;
150 return acceptsUrl(url);
151 }
152
153 // Default name to keep old plugins compatible
154 @Override
155 public String getTitle() {
156 return getClass().getName();
157 }
158
159 @Override
160 public String toString() {
161 return this.getTitle();
162 }
163
164 // Default pattern to keep old plugins compatible
165 @Override
166 public String[] getPatterns() {
167 return new String[]{};
168 }
169
170 /**
171 * Returns the projection bounds of downloaded data.
172 * @return the projection bounds of downloaded data or {@code null}
173 * @since 11774
174 */
175 public ProjectionBounds getDownloadProjectionBounds() {
176 return null;
177 }
178}
Note: See TracBrowser for help on using the repository browser.