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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

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