source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginListPanel.java@ 8426

Last change on this file since 8426 was 8426, checked in by Don-vip, 9 years ago

Accessibility - global use of JLabel.setLabelFor() + various fixes (javadoc, code style)

  • Property svn:eol-style set to native
File size: 9.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.GridBagConstraints;
9import java.awt.GridBagLayout;
10import java.awt.Insets;
11import java.awt.Rectangle;
12import java.awt.event.ActionEvent;
13import java.awt.event.ActionListener;
14import java.awt.event.MouseAdapter;
15import java.awt.event.MouseEvent;
16import java.util.HashSet;
17import java.util.List;
18import java.util.Set;
19
20import javax.swing.JCheckBox;
21import javax.swing.JLabel;
22import javax.swing.JOptionPane;
23import javax.swing.SwingConstants;
24import javax.swing.SwingUtilities;
25import javax.swing.event.HyperlinkEvent;
26import javax.swing.event.HyperlinkEvent.EventType;
27import javax.swing.event.HyperlinkListener;
28
29import org.openstreetmap.josm.gui.widgets.HtmlPanel;
30import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
31import org.openstreetmap.josm.plugins.PluginHandler;
32import org.openstreetmap.josm.plugins.PluginInformation;
33import org.openstreetmap.josm.tools.OpenBrowser;
34import org.openstreetmap.josm.tools.Utils;
35
36/**
37 * A panel displaying the list of known plugins.
38 */
39public 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 .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 .append(Utils.joinAsHtmlUnorderedList(otherPlugins))
187 .append("</html>");
188 JOptionPane.showMessageDialog(
189 parent,
190 sb.toString(),
191 tr("Warning"),
192 JOptionPane.WARNING_MESSAGE
193 );
194 }
195
196 /**
197 * Refreshes the list.
198 */
199 public void refreshView() {
200 final Rectangle visibleRect = getVisibleRect();
201 List<PluginInformation> displayedPlugins = model.getDisplayedPlugins();
202 removeAll();
203
204 GridBagConstraints gbc = new GridBagConstraints();
205 gbc.gridx = 0;
206 gbc.anchor = GridBagConstraints.NORTHWEST;
207 gbc.fill = GridBagConstraints.HORIZONTAL;
208 gbc.weightx = 1.0;
209
210 if (displayedPlugins.isEmpty()) {
211 displayEmptyPluginListInformation();
212 return;
213 }
214
215 int row = -1;
216 for (final PluginInformation pi : displayedPlugins) {
217 boolean selected = model.isSelectedPlugin(pi.getName());
218 String remoteversion = formatPluginRemoteVersion(pi);
219 String localversion = formatPluginLocalVersion(model.getPluginInformation(pi.getName()));
220
221 final JPluginCheckBox cbPlugin = new JPluginCheckBox(pi, selected);
222 String pluginText = tr("{0}: Version {1} (local: {2})", pi.getName(), remoteversion, localversion);
223 if (pi.requires != null && !pi.requires.isEmpty()) {
224 pluginText += tr(" (requires: {0})", pi.requires);
225 }
226 JLabel lblPlugin = new JLabel(
227 pluginText,
228 pi.getScaledIcon(),
229 SwingConstants.LEFT);
230 lblPlugin.addMouseListener(new MouseAdapter() {
231 @Override
232 public void mouseClicked(MouseEvent e) {
233 cbPlugin.doClick();
234 }
235 });
236
237 gbc.gridx = 0;
238 gbc.gridy = ++row;
239 gbc.insets = new Insets(5,5,0,5);
240 gbc.weighty = 0.0;
241 gbc.weightx = 0.0;
242 add(cbPlugin, gbc);
243
244 gbc.gridx = 1;
245 gbc.weightx = 1.0;
246 add(lblPlugin, gbc);
247
248 HtmlPanel description = new HtmlPanel();
249 description.setText(pi.getDescriptionAsHtml());
250 description.getEditorPane().addHyperlinkListener(new HyperlinkListener() {
251 @Override
252 public void hyperlinkUpdate(HyperlinkEvent e) {
253 if(e.getEventType() == EventType.ACTIVATED) {
254 OpenBrowser.displayUrl(e.getURL().toString());
255 }
256 }
257 });
258 lblPlugin.setLabelFor(description);
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}
Note: See TracBrowser for help on using the repository browser.