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

Last change on this file since 3083 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

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