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

Last change on this file since 12841 was 12841, checked in by bastiK, 7 years ago

see #15229 - fix deprecations caused by [12840]

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