Changeset 6797 in josm for trunk/src/org/openstreetmap/josm/plugins
- Timestamp:
- 2014-02-01T02:15:45+01:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java
r6643 r6797 32 32 import java.util.Set; 33 33 import java.util.TreeSet; 34 import java.util.concurrent.Callable; 34 35 import java.util.concurrent.ExecutionException; 35 36 import java.util.concurrent.ExecutorService; 36 37 import java.util.concurrent.Executors; 37 38 import java.util.concurrent.Future; 39 import java.util.concurrent.FutureTask; 38 40 import java.util.jar.JarFile; 39 41 … … 58 60 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 59 61 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 62 import org.openstreetmap.josm.gui.util.GuiHelper; 60 63 import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 61 64 import org.openstreetmap.josm.gui.widgets.JosmTextArea; 62 import org.openstreetmap.josm.tools.CheckParameterUtil;63 65 import org.openstreetmap.josm.tools.GBC; 64 66 import org.openstreetmap.josm.tools.I18n; … … 183 185 * List of unmaintained plugins. Not really up-to-date as the vast majority of plugins are not really maintained after a few months, sadly... 184 186 */ 185 finalpublic static String [] UNMAINTAINED_PLUGINS = new String[] {"gpsbabelgui", "Intersect_way"};187 public static final String [] UNMAINTAINED_PLUGINS = new String[] {"gpsbabelgui", "Intersect_way"}; 186 188 187 189 /** … … 208 210 } 209 211 } 212 213 private static PluginDownloadTask pluginDownloadTask = null; 210 214 211 215 public static Collection<ClassLoader> getResourceClassLoaders() { … … 819 823 * 820 824 * @param parent the parent component for message boxes 821 * @param plugins the collection of plugins to update. Must not be null.825 * @param pluginsWanted the collection of plugins to update. Updates all plugins if {@code null} 822 826 * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null. 823 827 * @throws IllegalArgumentException thrown if plugins is null 824 828 */ 825 public static List<PluginInformation> updatePlugins(Component parent, 826 List<PluginInformation> plugins, ProgressMonitor monitor) 827 throws IllegalArgumentException{ 828 CheckParameterUtil.ensureParameterNotNull(plugins, "plugins"); 829 public static Collection<PluginInformation> updatePlugins(Component parent, 830 Collection<PluginInformation> pluginsWanted, ProgressMonitor monitor) 831 throws IllegalArgumentException { 832 Collection<PluginInformation> plugins = null; 833 pluginDownloadTask = null; 829 834 if (monitor == null) { 830 835 monitor = NullProgressMonitor.INSTANCE; … … 847 852 allPlugins = task1.getAvailablePlugins(); 848 853 plugins = buildListOfPluginsToLoad(parent,monitor.createSubTaskMonitor(1, false)); 854 // If only some plugins have to be updated, filter the list 855 if (pluginsWanted != null && !pluginsWanted.isEmpty()) { 856 for (Iterator<PluginInformation> it = plugins.iterator(); it.hasNext();) { 857 PluginInformation pi = it.next(); 858 boolean found = false; 859 for (PluginInformation piw : pluginsWanted) { 860 if (pi.name.equals(piw.name)) { 861 found = true; 862 break; 863 } 864 } 865 if (!found) { 866 it.remove(); 867 } 868 } 869 } 849 870 } catch (ExecutionException e) { 850 871 Main.warn(tr("Failed to download plugin information list")+": ExecutionException"); … … 886 907 // try to update the locally installed plugins 887 908 // 888 PluginDownloadTasktask2= new PluginDownloadTask(909 pluginDownloadTask = new PluginDownloadTask( 889 910 monitor.createSubTaskMonitor(1,false), 890 911 pluginsToDownload, … … 892 913 ); 893 914 894 future = service.submit( task2);915 future = service.submit(pluginDownloadTask); 895 916 try { 896 917 future.get(); … … 907 928 // Update Plugin info for downloaded plugins 908 929 // 909 refreshLocalUpdatedPluginInfo( task2.getDownloadedPlugins());930 refreshLocalUpdatedPluginInfo(pluginDownloadTask.getDownloadedPlugins()); 910 931 911 932 // notify user if downloading a locally installed plugin failed 912 933 // 913 if (! task2.getFailedPlugins().isEmpty()) {914 alertFailedPluginUpdate(parent, task2.getFailedPlugins());934 if (! pluginDownloadTask.getFailedPlugins().isEmpty()) { 935 alertFailedPluginUpdate(parent, pluginDownloadTask.getFailedPlugins()); 915 936 return plugins; 916 937 } … … 919 940 monitor.finishTask(); 920 941 } 921 // remember the update because it was successful 922 // 923 Main.pref.putInteger("pluginmanager.version", Version.getInstance().getVersion()); 924 Main.pref.put("pluginmanager.lastupdate", Long.toString(System.currentTimeMillis())); 942 if (pluginsWanted == null) { 943 // if all plugins updated, remember the update because it was successful 944 // 945 Main.pref.putInteger("pluginmanager.version", Version.getInstance().getVersion()); 946 Main.pref.put("pluginmanager.lastupdate", Long.toString(System.currentTimeMillis())); 947 } 925 948 return plugins; 926 949 } … … 1094 1117 } 1095 1118 1096 private static boolean confirmDeactivatingPluginAfterException(PluginProxy plugin) { 1097 ButtonSpec [] options = new ButtonSpec[] { 1119 private static int askUpdateDisableKeepPluginAfterException(PluginProxy plugin) { 1120 final ButtonSpec[] options = new ButtonSpec[] { 1121 new ButtonSpec( 1122 tr("Update plugin"), 1123 ImageProvider.get("dialogs", "refresh"), 1124 tr("Click to update the plugin ''{0}''", plugin.getPluginInformation().name), 1125 null /* no specific help context */ 1126 ), 1098 1127 new ButtonSpec( 1099 1128 tr("Disable plugin"), … … 1110 1139 }; 1111 1140 1112 StringBuffer msg = new StringBuffer(); 1141 final StringBuffer msg = new StringBuffer(); 1113 1142 msg.append("<html>"); 1114 1143 msg.append(tr("An unexpected exception occurred that may have come from the ''{0}'' plugin.", plugin.getPluginInformation().name)); 1115 1144 msg.append("<br>"); 1116 if(plugin.getPluginInformation().author != null) { 1145 if (plugin.getPluginInformation().author != null) { 1117 1146 msg.append(tr("According to the information within the plugin, the author is {0}.", plugin.getPluginInformation().author)); 1118 1147 msg.append("<br>"); 1119 1148 } 1120 1149 msg.append(tr("Try updating to the newest version of this plugin before reporting a bug.")); 1121 msg.append("<br>");1122 msg.append(tr("Should the plugin be disabled?"));1123 1150 msg.append("</html>"); 1124 1151 1125 int ret = HelpAwareOptionPane.showOptionDialog( 1126 Main.parent, 1127 msg.toString(), 1128 tr("Update plugins"), 1129 JOptionPane.QUESTION_MESSAGE, 1130 null, 1131 options, 1132 options[0], 1133 ht("/ErrorMessages#ErrorInPlugin") 1134 ); 1135 return ret == 0; 1152 try { 1153 FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() { 1154 @Override 1155 public Integer call() { 1156 return HelpAwareOptionPane.showOptionDialog( 1157 Main.parent, 1158 msg.toString(), 1159 tr("Update plugins"), 1160 JOptionPane.QUESTION_MESSAGE, 1161 null, 1162 options, 1163 options[0], 1164 ht("/ErrorMessages#ErrorInPlugin") 1165 ); 1166 } 1167 }); 1168 GuiHelper.runInEDT(task); 1169 return task.get(); 1170 } catch (InterruptedException e) { 1171 Main.warn(e); 1172 } catch (ExecutionException e) { 1173 Main.warn(e); 1174 } 1175 return -1; 1136 1176 } 1137 1177 … … 1163 1203 /** 1164 1204 * Checks whether the exception <code>e</code> was thrown by a plugin. If so, 1165 * conditionally deactivates the plugin, but asks the user first. 1205 * conditionally updates or deactivates the plugin, but asks the user first. 1166 1206 * 1167 1207 * @param e the exception 1168 */ 1169 public static void disablePluginAfterException(Throwable e) { 1208 * @return plugin download task if the plugin has been updated to a newer version, {@code null} if it has been disabled or kept as it 1209 */ 1210 public static PluginDownloadTask updateOrdisablePluginAfterException(Throwable e) { 1170 1211 PluginProxy plugin = null; 1171 1212 // Check for an explicit problem when calling a plugin function … … 1178 1219 if (plugin == null) 1179 1220 // don't know what plugin threw the exception 1180 return; 1221 return null; 1181 1222 1182 1223 Set<String> plugins = new HashSet<String>( 1183 1224 Main.pref.getCollection("plugins",Collections.<String> emptySet()) 1184 1225 ); 1185 if (! plugins.contains(plugin.getPluginInformation().name)) 1226 final PluginInformation pluginInfo = plugin.getPluginInformation(); 1227 if (! plugins.contains(pluginInfo.name)) 1186 1228 // plugin not activated ? strange in this context but anyway, don't bother 1187 1229 // the user with dialogs, skip conditional deactivation 1188 return; 1189 1190 if (!confirmDeactivatingPluginAfterException(plugin)) 1230 return null; 1231 1232 switch (askUpdateDisableKeepPluginAfterException(plugin)) { 1233 case 0: 1234 // update the plugin 1235 updatePlugins(Main.parent, Collections.singleton(pluginInfo), null); 1236 return pluginDownloadTask; 1237 case 1: 1238 // deactivate the plugin 1239 plugins.remove(plugin.getPluginInformation().name); 1240 Main.pref.putCollection("plugins", plugins); 1241 GuiHelper.runInEDTAndWait(new Runnable() { 1242 @Override 1243 public void run() { 1244 JOptionPane.showMessageDialog( 1245 Main.parent, 1246 tr("The plugin has been removed from the configuration. Please restart JOSM to unload the plugin."), 1247 tr("Information"), 1248 JOptionPane.INFORMATION_MESSAGE 1249 ); 1250 } 1251 }); 1252 return null; 1253 default: 1191 1254 // user doesn't want to deactivate the plugin 1192 return; 1193 1194 // deactivate the plugin 1195 plugins.remove(plugin.getPluginInformation().name); 1196 Main.pref.putCollection("plugins", plugins); 1197 JOptionPane.showMessageDialog( 1198 Main.parent, 1199 tr("The plugin has been removed from the configuration. Please restart JOSM to unload the plugin."), 1200 tr("Information"), 1201 JOptionPane.INFORMATION_MESSAGE 1202 ); 1203 return; 1255 return null; 1256 } 1204 1257 } 1205 1258
Note:
See TracChangeset
for help on using the changeset viewer.