source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginCheckBox.java@ 13799

Last change on this file since 13799 was 11848, checked in by Don-vip, 7 years ago

fix #14613 - Special HTML characters not escaped in GUI error messages

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.plugin;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.Component;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.util.HashSet;
11import java.util.Set;
12
13import javax.swing.JCheckBox;
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.plugins.PluginHandler;
17import org.openstreetmap.josm.plugins.PluginInformation;
18import org.openstreetmap.josm.tools.Utils;
19
20/**
21 * A plugin checkbox.
22 * @since 10228
23 */
24public class PluginCheckBox extends JCheckBox implements ActionListener {
25 private final transient PluginInformation pi;
26 private final PluginListPanel panel;
27 private final transient PluginPreferencesModel ppModel;
28
29 PluginCheckBox(PluginInformation pi, boolean selected, PluginListPanel panel, PluginPreferencesModel ppModel) {
30 this.pi = pi;
31 this.panel = panel;
32 this.ppModel = ppModel;
33 setSelected(selected);
34 setToolTipText(PluginListPanel.formatCheckboxTooltipText(pi));
35 addActionListener(this);
36 }
37
38 protected void selectRequiredPlugins(PluginInformation info) {
39 if (info != null && info.requires != null) {
40 for (String s : info.getRequiredPlugins()) {
41 if (!ppModel.isSelectedPlugin(s)) {
42 ppModel.setPluginSelected(s, true);
43 selectRequiredPlugins(ppModel.getPluginInformation(s));
44 }
45 }
46 }
47 }
48
49 @Override
50 public void actionPerformed(ActionEvent e) {
51 // Select/unselect corresponding plugin in the model
52 ppModel.setPluginSelected(pi.getName(), isSelected());
53 // Does the newly selected plugin require other plugins ?
54 if (isSelected() && pi.requires != null) {
55 // Select required plugins
56 selectRequiredPlugins(pi);
57 // Alert user if plugin requirements are not met
58 PluginHandler.checkRequiredPluginsPreconditions(panel, ppModel.getAvailablePlugins(), pi, false);
59 } else if (!isSelected()) {
60 // If the plugin has been unselected, was it required by other plugins still selected ?
61 Set<String> otherPlugins = new HashSet<>();
62 for (PluginInformation p : ppModel.getAvailablePlugins()) {
63 if (!p.equals(pi) && p.requires != null && ppModel.isSelectedPlugin(p.getName())) {
64 for (String s : p.getRequiredPlugins()) {
65 if (s.equals(pi.getName())) {
66 otherPlugins.add(p.getName());
67 break;
68 }
69 }
70 }
71 }
72 if (!otherPlugins.isEmpty()) {
73 alertPluginStillRequired(panel, pi.getName(), otherPlugins);
74 }
75 }
76 }
77
78 /**
79 * Alerts the user if an unselected plugin is still required by another plugins
80 *
81 * @param parent The parent Component used to display error popup
82 * @param plugin the plugin
83 * @param otherPlugins the other plugins
84 */
85 private static void alertPluginStillRequired(Component parent, String plugin, Set<String> otherPlugins) {
86 StringBuilder sb = new StringBuilder("<html>")
87 .append(trn("Plugin {0} is still required by this plugin:",
88 "Plugin {0} is still required by these {1} plugins:",
89 otherPlugins.size(),
90 Utils.escapeReservedCharactersHTML(plugin),
91 otherPlugins.size()))
92 .append(Utils.joinAsHtmlUnorderedList(otherPlugins))
93 .append("</html>");
94 JOptionPane.showMessageDialog(
95 parent,
96 sb.toString(),
97 tr("Warning"),
98 JOptionPane.WARNING_MESSAGE
99 );
100 }
101}
Note: See TracBrowser for help on using the repository browser.