source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginPreferencesModel.java@ 8510

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

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 12.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.plugin;
3
4import java.io.File;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.Comparator;
9import java.util.HashMap;
10import java.util.HashSet;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Locale;
14import java.util.Map;
15import java.util.Map.Entry;
16import java.util.Observable;
17import java.util.Set;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.plugins.PluginException;
21import org.openstreetmap.josm.plugins.PluginHandler;
22import org.openstreetmap.josm.plugins.PluginInformation;
23
24/**
25 * The plugin model behind a {@code PluginListPanel}.
26 */
27public class PluginPreferencesModel extends Observable {
28 // remember the initial list of active plugins
29 private final Set<String> currentActivePlugins;
30 private final List<PluginInformation> availablePlugins = new ArrayList<>();
31 private String filterExpression;
32 private final List<PluginInformation> displayedPlugins = new ArrayList<>();
33 private final Map<PluginInformation, Boolean> selectedPluginsMap = new HashMap<>();
34 // plugins that still require an update/download
35 private Set<String> pendingDownloads = new HashSet<>();
36
37 /**
38 * Constructs a new {@code PluginPreferencesModel}.
39 */
40 public PluginPreferencesModel() {
41 currentActivePlugins = new HashSet<>();
42 currentActivePlugins.addAll(Main.pref.getCollection("plugins", currentActivePlugins));
43 }
44
45 /**
46 * Filters the list of displayed plugins.
47 * @param filter The filter used against plugin name, description or version
48 */
49 public void filterDisplayedPlugins(String filter) {
50 if (filter == null) {
51 displayedPlugins.clear();
52 displayedPlugins.addAll(availablePlugins);
53 this.filterExpression = null;
54 return;
55 }
56 displayedPlugins.clear();
57 for (PluginInformation pi: availablePlugins) {
58 if (pi.matches(filter)) {
59 displayedPlugins.add(pi);
60 }
61 }
62 filterExpression = filter;
63 clearChanged();
64 notifyObservers();
65 }
66
67 /**
68 * Sets the list of available plugins.
69 * @param available The available plugins
70 */
71 public void setAvailablePlugins(Collection<PluginInformation> available) {
72 availablePlugins.clear();
73 if (available != null) {
74 availablePlugins.addAll(available);
75 }
76 availablePluginsModified();
77 }
78
79 protected final void availablePluginsModified() {
80 sort();
81 filterDisplayedPlugins(filterExpression);
82 Set<String> activePlugins = new HashSet<>();
83 activePlugins.addAll(Main.pref.getCollection("plugins", activePlugins));
84 for (PluginInformation pi: availablePlugins) {
85 if (selectedPluginsMap.get(pi) == null) {
86 if (activePlugins.contains(pi.name)) {
87 selectedPluginsMap.put(pi, Boolean.TRUE);
88 }
89 }
90 }
91 clearChanged();
92 notifyObservers();
93 }
94
95 protected void updateAvailablePlugin(PluginInformation other) {
96 if (other == null) return;
97 PluginInformation pi = getPluginInformation(other.name);
98 if (pi == null) {
99 availablePlugins.add(other);
100 return;
101 }
102 pi.updateFromPluginSite(other);
103 }
104
105 /**
106 * Updates the list of plugin information objects with new information from
107 * plugin update sites.
108 *
109 * @param fromPluginSite plugin information read from plugin update sites
110 */
111 public void updateAvailablePlugins(Collection<PluginInformation> fromPluginSite) {
112 for (PluginInformation other: fromPluginSite) {
113 updateAvailablePlugin(other);
114 }
115 availablePluginsModified();
116 }
117
118 /**
119 * Replies the list of selected plugin information objects
120 *
121 * @return the list of selected plugin information objects
122 */
123 public List<PluginInformation> getSelectedPlugins() {
124 List<PluginInformation> ret = new LinkedList<>();
125 for (PluginInformation pi: availablePlugins) {
126 if (selectedPluginsMap.get(pi) == null) {
127 continue;
128 }
129 if (selectedPluginsMap.get(pi)) {
130 ret.add(pi);
131 }
132 }
133 return ret;
134 }
135
136 /**
137 * Replies the list of selected plugin information objects
138 *
139 * @return the list of selected plugin information objects
140 */
141 public Set<String> getSelectedPluginNames() {
142 Set<String> ret = new HashSet<>();
143 for (PluginInformation pi: getSelectedPlugins()) {
144 ret.add(pi.name);
145 }
146 return ret;
147 }
148
149 /**
150 * Sorts the list of available plugins
151 */
152 protected void sort() {
153 Collections.sort(
154 availablePlugins,
155 new Comparator<PluginInformation>() {
156 @Override
157 public int compare(PluginInformation o1, PluginInformation o2) {
158 String n1 = o1.getName() == null ? "" : o1.getName().toLowerCase(Locale.ENGLISH);
159 String n2 = o2.getName() == null ? "" : o2.getName().toLowerCase(Locale.ENGLISH);
160 return n1.compareTo(n2);
161 }
162 }
163 );
164 }
165
166 /**
167 * Replies the list of plugin informations to display
168 *
169 * @return the list of plugin informations to display
170 */
171 public List<PluginInformation> getDisplayedPlugins() {
172 return displayedPlugins;
173 }
174
175
176 /**
177 * Replies the list of plugins waiting for update or download
178 *
179 * @return the list of plugins waiting for update or download
180 */
181 public List<PluginInformation> getPluginsScheduledForUpdateOrDownload() {
182 List<PluginInformation> ret = new ArrayList<>();
183 for (String plugin: pendingDownloads) {
184 PluginInformation pi = getPluginInformation(plugin);
185 if (pi == null) {
186 continue;
187 }
188 ret.add(pi);
189 }
190 return ret;
191 }
192
193 /**
194 * Sets whether the plugin is selected or not.
195 *
196 * @param name the name of the plugin
197 * @param selected true, if selected; false, otherwise
198 */
199 public void setPluginSelected(String name, boolean selected) {
200 PluginInformation pi = getPluginInformation(name);
201 if (pi != null) {
202 selectedPluginsMap.put(pi, selected);
203 if (pi.isUpdateRequired()) {
204 pendingDownloads.add(pi.name);
205 }
206 }
207 if (!selected) {
208 pendingDownloads.remove(name);
209 }
210 }
211
212 /**
213 * Removes all the plugin in {@code plugins} from the list of plugins
214 * with a pending download
215 *
216 * @param plugins the list of plugins to clear for a pending download
217 */
218 public void clearPendingPlugins(Collection<PluginInformation> plugins) {
219 if (plugins == null || plugins.isEmpty()) return;
220 for (PluginInformation pi: plugins) {
221 pendingDownloads.remove(pi.name);
222 }
223 }
224
225 /**
226 * Replies the plugin info with the name <code>name</code>. null, if no
227 * such plugin info exists.
228 *
229 * @param name the name. If null, replies null.
230 * @return the plugin info.
231 */
232 public PluginInformation getPluginInformation(String name) {
233 for (PluginInformation pi: availablePlugins) {
234 if (pi.getName() != null && pi.getName().equals(name))
235 return pi;
236 }
237 return null;
238 }
239
240 /**
241 * Initializes the model from preferences
242 */
243 public void initFromPreferences() {
244 Collection<String> enabledPlugins = Main.pref.getCollection("plugins", null);
245 if (enabledPlugins == null) {
246 this.selectedPluginsMap.clear();
247 return;
248 }
249 for (String name: enabledPlugins) {
250 PluginInformation pi = getPluginInformation(name);
251 if (pi == null) {
252 continue;
253 }
254 setPluginSelected(name, true);
255 }
256 }
257
258 /**
259 * Replies true if the plugin with name <code>name</code> is currently
260 * selected in the plugin model
261 *
262 * @param name the plugin name
263 * @return true if the plugin is selected; false, otherwise
264 */
265 public boolean isSelectedPlugin(String name) {
266 PluginInformation pi = getPluginInformation(name);
267 if (pi == null) return false;
268 if (selectedPluginsMap.get(pi) == null) return false;
269 return selectedPluginsMap.get(pi);
270 }
271
272 /**
273 * Replies the set of plugins which have been added by the user to
274 * the set of activated plugins.
275 *
276 * @return the set of newly deactivated plugins
277 */
278 public List<PluginInformation> getNewlyActivatedPlugins() {
279 List<PluginInformation> ret = new LinkedList<>();
280 for (Entry<PluginInformation, Boolean> entry: selectedPluginsMap.entrySet()) {
281 PluginInformation pi = entry.getKey();
282 boolean selected = entry.getValue();
283 if (selected && !currentActivePlugins.contains(pi.name)) {
284 ret.add(pi);
285 }
286 }
287 return ret;
288 }
289
290 /**
291 * Replies the set of plugins which have been removed by the user from
292 * the set of activated plugins.
293 *
294 * @return the set of newly deactivated plugins
295 */
296 public List<PluginInformation> getNewlyDeactivatedPlugins() {
297 List<PluginInformation> ret = new LinkedList<>();
298 for (PluginInformation pi: availablePlugins) {
299 if (!currentActivePlugins.contains(pi.name)) {
300 continue;
301 }
302 if (selectedPluginsMap.get(pi) == null || !selectedPluginsMap.get(pi)) {
303 ret.add(pi);
304 }
305 }
306 return ret;
307 }
308
309 /**
310 * Replies the set of all available plugins.
311 *
312 * @return the set of all available plugins
313 */
314 public List<PluginInformation> getAvailablePlugins() {
315 return new LinkedList<>(availablePlugins);
316 }
317
318 /**
319 * Replies the set of plugin names which have been added by the user to
320 * the set of activated plugins.
321 *
322 * @return the set of newly activated plugin names
323 */
324 public Set<String> getNewlyActivatedPluginNames() {
325 Set<String> ret = new HashSet<>();
326 List<PluginInformation> plugins = getNewlyActivatedPlugins();
327 for (PluginInformation pi: plugins) {
328 ret.add(pi.name);
329 }
330 return ret;
331 }
332
333 /**
334 * Replies true if the set of active plugins has been changed by the user
335 * in this preference model. He has either added plugins or removed plugins
336 * being active before.
337 *
338 * @return true if the collection of active plugins has changed
339 */
340 public boolean isActivePluginsChanged() {
341 Set<String> newActivePlugins = getSelectedPluginNames();
342 return !newActivePlugins.equals(currentActivePlugins);
343 }
344
345 /**
346 * Refreshes the local version field on the plugins in <code>plugins</code> with
347 * the version in the manifest of the downloaded "jar.new"-file for this plugin.
348 *
349 * @param plugins the collections of plugins to refresh
350 */
351 public void refreshLocalPluginVersion(Collection<PluginInformation> plugins) {
352 if (plugins == null) return;
353 for (PluginInformation pi : plugins) {
354 File downloadedPluginFile = PluginHandler.findUpdatedJar(pi.name);
355 if (downloadedPluginFile == null) {
356 continue;
357 }
358 try {
359 PluginInformation newinfo = new PluginInformation(downloadedPluginFile, pi.name);
360 PluginInformation oldinfo = getPluginInformation(pi.name);
361 if (oldinfo == null) {
362 // should not happen
363 continue;
364 }
365 oldinfo.updateLocalInfo(newinfo);
366 } catch (PluginException e) {
367 Main.error(e);
368 }
369 }
370 }
371}
Note: See TracBrowser for help on using the repository browser.