1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.plugin;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.Set;
|
---|
12 |
|
---|
13 | import javax.swing.JCheckBox;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
17 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
18 | import org.openstreetmap.josm.tools.Utils;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * A plugin checkbox.
|
---|
22 | * @since 10228
|
---|
23 | */
|
---|
24 | public 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 | }
|
---|