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.GridBagConstraints;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.Insets;
|
---|
11 | import java.awt.Rectangle;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.ActionListener;
|
---|
14 | import java.awt.event.MouseAdapter;
|
---|
15 | import java.awt.event.MouseEvent;
|
---|
16 | import java.util.HashSet;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Set;
|
---|
19 |
|
---|
20 | import javax.swing.JCheckBox;
|
---|
21 | import javax.swing.JLabel;
|
---|
22 | import javax.swing.JOptionPane;
|
---|
23 | import javax.swing.SwingConstants;
|
---|
24 | import javax.swing.SwingUtilities;
|
---|
25 | import javax.swing.event.HyperlinkEvent;
|
---|
26 | import javax.swing.event.HyperlinkEvent.EventType;
|
---|
27 | import javax.swing.event.HyperlinkListener;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
---|
31 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
32 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
33 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
34 | import org.openstreetmap.josm.tools.Utils;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * A panel displaying the list of known plugins.
|
---|
38 | */
|
---|
39 | public class PluginListPanel extends VerticallyScrollablePanel {
|
---|
40 | private transient PluginPreferencesModel model;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Constructs a new {@code PluginListPanel} with a default model.
|
---|
44 | */
|
---|
45 | public PluginListPanel() {
|
---|
46 | this(new PluginPreferencesModel());
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Constructs a new {@code PluginListPanel} with a given model.
|
---|
51 | * @param model The plugin model
|
---|
52 | */
|
---|
53 | public PluginListPanel(PluginPreferencesModel model) {
|
---|
54 | this.model = model;
|
---|
55 | setLayout(new GridBagLayout());
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected String formatPluginRemoteVersion(PluginInformation pi) {
|
---|
59 | StringBuilder sb = new StringBuilder();
|
---|
60 | if (pi.version == null || pi.version.trim().isEmpty()) {
|
---|
61 | sb.append(tr("unknown"));
|
---|
62 | } else {
|
---|
63 | sb.append(pi.version);
|
---|
64 | if (pi.oldmode) {
|
---|
65 | sb.append("*");
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return sb.toString();
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected String formatPluginLocalVersion(PluginInformation pi) {
|
---|
72 | if (pi == null) return tr("unknown");
|
---|
73 | if (pi.localversion == null || pi.localversion.trim().isEmpty())
|
---|
74 | return tr("unknown");
|
---|
75 | return pi.localversion;
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected String formatCheckboxTooltipText(PluginInformation pi) {
|
---|
79 | if (pi == null) return "";
|
---|
80 | if (pi.downloadlink == null)
|
---|
81 | return tr("Plugin bundled with JOSM");
|
---|
82 | else
|
---|
83 | return pi.downloadlink;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Displays a message when the plugin list is empty.
|
---|
88 | */
|
---|
89 | public void displayEmptyPluginListInformation() {
|
---|
90 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
91 | gbc.gridx = 0;
|
---|
92 | gbc.anchor = GridBagConstraints.CENTER;
|
---|
93 | gbc.fill = GridBagConstraints.BOTH;
|
---|
94 | gbc.insets = new Insets(40,0,40,0);
|
---|
95 | gbc.weightx = 1.0;
|
---|
96 | gbc.weighty = 1.0;
|
---|
97 |
|
---|
98 | HtmlPanel hint = new HtmlPanel();
|
---|
99 | hint.setText(
|
---|
100 | "<html>"
|
---|
101 | + tr("Please click on <strong>Download list</strong> to download and display a list of available plugins.")
|
---|
102 | + "</html>"
|
---|
103 | );
|
---|
104 | add(hint, gbc);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * A plugin checkbox.
|
---|
109 | *
|
---|
110 | */
|
---|
111 | private class JPluginCheckBox extends JCheckBox {
|
---|
112 | public final transient PluginInformation pi;
|
---|
113 | public JPluginCheckBox(final PluginInformation pi, boolean selected) {
|
---|
114 | this.pi = pi;
|
---|
115 | setSelected(selected);
|
---|
116 | setToolTipText(formatCheckboxTooltipText(pi));
|
---|
117 | addActionListener(new PluginCbActionListener(this));
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Listener called when the user selects/unselects a plugin checkbox.
|
---|
123 | *
|
---|
124 | */
|
---|
125 | private class PluginCbActionListener implements ActionListener {
|
---|
126 | private final JPluginCheckBox cb;
|
---|
127 | public PluginCbActionListener(JPluginCheckBox cb) {
|
---|
128 | this.cb = cb;
|
---|
129 | }
|
---|
130 | protected void selectRequiredPlugins(PluginInformation info) {
|
---|
131 | if (info != null && info.requires != null) {
|
---|
132 | for (String s : info.getRequiredPlugins()) {
|
---|
133 | if (!model.isSelectedPlugin(s)) {
|
---|
134 | model.setPluginSelected(s, true);
|
---|
135 | selectRequiredPlugins(model.getPluginInformation(s));
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | @Override
|
---|
141 | public void actionPerformed(ActionEvent e) {
|
---|
142 | // Select/unselect corresponding plugin in the model
|
---|
143 | model.setPluginSelected(cb.pi.getName(), cb.isSelected());
|
---|
144 | // Does the newly selected plugin require other plugins ?
|
---|
145 | if (cb.isSelected() && cb.pi.requires != null) {
|
---|
146 | // Select required plugins
|
---|
147 | selectRequiredPlugins(cb.pi);
|
---|
148 | // Alert user if plugin requirements are not met
|
---|
149 | PluginHandler.checkRequiredPluginsPreconditions(PluginListPanel.this, model.getAvailablePlugins(), cb.pi, false);
|
---|
150 | } else if (!cb.isSelected()) {
|
---|
151 | // If the plugin has been unselected, was it required by other plugins still selected ?
|
---|
152 | Set<String> otherPlugins = new HashSet<>();
|
---|
153 | for (PluginInformation pi : model.getAvailablePlugins()) {
|
---|
154 | if (!pi.equals(cb.pi) && pi.requires != null && model.isSelectedPlugin(pi.getName())) {
|
---|
155 | for (String s : pi.getRequiredPlugins()) {
|
---|
156 | if (s.equals(cb.pi.getName())) {
|
---|
157 | otherPlugins.add(pi.getName());
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | if (!otherPlugins.isEmpty()) {
|
---|
164 | alertPluginStillRequired(PluginListPanel.this, cb.pi.getName(), otherPlugins);
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Alerts the user if an unselected plugin is still required by another plugins
|
---|
173 | *
|
---|
174 | * @param parent The parent Component used to display error popup
|
---|
175 | * @param plugin the plugin
|
---|
176 | * @param otherPlugins the other plugins
|
---|
177 | */
|
---|
178 | private static void alertPluginStillRequired(Component parent, String plugin, Set<String> otherPlugins) {
|
---|
179 | StringBuilder sb = new StringBuilder();
|
---|
180 | sb.append("<html>");
|
---|
181 | sb.append(trn("Plugin {0} is still required by this plugin:",
|
---|
182 | "Plugin {0} is still required by these {1} plugins:",
|
---|
183 | otherPlugins.size(),
|
---|
184 | plugin,
|
---|
185 | otherPlugins.size()
|
---|
186 | ));
|
---|
187 | sb.append(Utils.joinAsHtmlUnorderedList(otherPlugins));
|
---|
188 | sb.append("</html>");
|
---|
189 | JOptionPane.showMessageDialog(
|
---|
190 | parent,
|
---|
191 | sb.toString(),
|
---|
192 | tr("Warning"),
|
---|
193 | JOptionPane.WARNING_MESSAGE
|
---|
194 | );
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Refreshes the list.
|
---|
199 | */
|
---|
200 | public void refreshView() {
|
---|
201 | final Rectangle visibleRect = getVisibleRect();
|
---|
202 | List<PluginInformation> displayedPlugins = model.getDisplayedPlugins();
|
---|
203 | removeAll();
|
---|
204 |
|
---|
205 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
206 | gbc.gridx = 0;
|
---|
207 | gbc.anchor = GridBagConstraints.NORTHWEST;
|
---|
208 | gbc.fill = GridBagConstraints.HORIZONTAL;
|
---|
209 | gbc.weightx = 1.0;
|
---|
210 |
|
---|
211 | if (displayedPlugins.isEmpty()) {
|
---|
212 | displayEmptyPluginListInformation();
|
---|
213 | return;
|
---|
214 | }
|
---|
215 |
|
---|
216 | int row = -1;
|
---|
217 | for (final PluginInformation pi : displayedPlugins) {
|
---|
218 | boolean selected = model.isSelectedPlugin(pi.getName());
|
---|
219 | String remoteversion = formatPluginRemoteVersion(pi);
|
---|
220 | String localversion = formatPluginLocalVersion(model.getPluginInformation(pi.getName()));
|
---|
221 |
|
---|
222 | final JPluginCheckBox cbPlugin = new JPluginCheckBox(pi, selected);
|
---|
223 | String pluginText = tr("{0}: Version {1} (local: {2})", pi.getName(), remoteversion, localversion);
|
---|
224 | if (pi.requires != null && !pi.requires.isEmpty()) {
|
---|
225 | pluginText += tr(" (requires: {0})", pi.requires);
|
---|
226 | }
|
---|
227 | JLabel lblPlugin = new JLabel(
|
---|
228 | pluginText,
|
---|
229 | pi.getScaledIcon(),
|
---|
230 | SwingConstants.LEFT);
|
---|
231 | lblPlugin.addMouseListener(new MouseAdapter() {
|
---|
232 | @Override
|
---|
233 | public void mouseClicked(MouseEvent e) {
|
---|
234 | cbPlugin.doClick();
|
---|
235 | }
|
---|
236 | });
|
---|
237 |
|
---|
238 | gbc.gridx = 0;
|
---|
239 | gbc.gridy = ++row;
|
---|
240 | gbc.insets = new Insets(5,5,0,5);
|
---|
241 | gbc.weighty = 0.0;
|
---|
242 | gbc.weightx = 0.0;
|
---|
243 | add(cbPlugin, gbc);
|
---|
244 |
|
---|
245 | gbc.gridx = 1;
|
---|
246 | gbc.weightx = 1.0;
|
---|
247 | add(lblPlugin, gbc);
|
---|
248 |
|
---|
249 | HtmlPanel description = new HtmlPanel();
|
---|
250 | description.setText(pi.getDescriptionAsHtml());
|
---|
251 | description.getEditorPane().addHyperlinkListener(new HyperlinkListener() {
|
---|
252 | @Override
|
---|
253 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
254 | if(e.getEventType() == EventType.ACTIVATED) {
|
---|
255 | OpenBrowser.displayUrl(e.getURL().toString());
|
---|
256 | }
|
---|
257 | }
|
---|
258 | });
|
---|
259 |
|
---|
260 | gbc.gridx = 1;
|
---|
261 | gbc.gridy = ++row;
|
---|
262 | gbc.insets = new Insets(3,25,5,5);
|
---|
263 | gbc.weighty = 1.0;
|
---|
264 | add(description, gbc);
|
---|
265 | }
|
---|
266 | revalidate();
|
---|
267 | repaint();
|
---|
268 | if (visibleRect != null && visibleRect.width > 0 && visibleRect.height > 0) {
|
---|
269 | SwingUtilities.invokeLater(new Runnable() {
|
---|
270 | @Override
|
---|
271 | public void run() {
|
---|
272 | scrollRectToVisible(visibleRect);
|
---|
273 | }
|
---|
274 | });
|
---|
275 | }
|
---|
276 | }
|
---|
277 | }
|
---|