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

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

update javadoc

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