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

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

findbugs - SIC_INNER_SHOULD_BE_STATIC_ANON

  • Property svn:eol-style set to native
File size: 5.7 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;
5
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.Insets;
9import java.awt.Rectangle;
10import java.awt.event.MouseAdapter;
11import java.awt.event.MouseEvent;
12import java.util.List;
13
14import javax.swing.JLabel;
15import javax.swing.SwingConstants;
16import javax.swing.SwingUtilities;
17import javax.swing.event.HyperlinkEvent.EventType;
18
19import org.openstreetmap.josm.gui.widgets.HtmlPanel;
20import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
21import org.openstreetmap.josm.plugins.PluginInformation;
22import org.openstreetmap.josm.tools.OpenBrowser;
23
24/**
25 * A panel displaying the list of known plugins.
26 */
27public class PluginListPanel extends VerticallyScrollablePanel {
28 static final class PluginCheckBoxMouseAdapter extends MouseAdapter {
29 private final PluginCheckBox cbPlugin;
30
31 PluginCheckBoxMouseAdapter(PluginCheckBox cbPlugin) {
32 this.cbPlugin = cbPlugin;
33 }
34
35 @Override
36 public void mouseClicked(MouseEvent e) {
37 cbPlugin.doClick();
38 }
39 }
40
41 private transient PluginPreferencesModel model;
42
43 /**
44 * Constructs a new {@code PluginListPanel} with a default model.
45 */
46 public PluginListPanel() {
47 this(new PluginPreferencesModel());
48 }
49
50 /**
51 * Constructs a new {@code PluginListPanel} with a given model.
52 * @param model The plugin model
53 */
54 public PluginListPanel(PluginPreferencesModel model) {
55 this.model = model;
56 setLayout(new GridBagLayout());
57 }
58
59 protected static String formatPluginRemoteVersion(PluginInformation pi) {
60 StringBuilder sb = new StringBuilder();
61 if (pi.version == null || pi.version.trim().isEmpty()) {
62 sb.append(tr("unknown"));
63 } else {
64 sb.append(pi.version);
65 if (pi.oldmode) {
66 sb.append('*');
67 }
68 }
69 return sb.toString();
70 }
71
72 protected static String formatPluginLocalVersion(PluginInformation pi) {
73 if (pi == null)
74 return tr("unknown");
75 if (pi.localversion == null || pi.localversion.trim().isEmpty())
76 return tr("unknown");
77 return pi.localversion;
78 }
79
80 protected static String formatCheckboxTooltipText(PluginInformation pi) {
81 if (pi == null)
82 return "";
83 if (pi.downloadlink == null)
84 return tr("Plugin bundled with JOSM");
85 else
86 return pi.downloadlink;
87 }
88
89 /**
90 * Displays a message when the plugin list is empty.
91 */
92 public void displayEmptyPluginListInformation() {
93 GridBagConstraints gbc = new GridBagConstraints();
94 gbc.gridx = 0;
95 gbc.anchor = GridBagConstraints.CENTER;
96 gbc.fill = GridBagConstraints.BOTH;
97 gbc.insets = new Insets(40, 0, 40, 0);
98 gbc.weightx = 1.0;
99 gbc.weighty = 1.0;
100
101 HtmlPanel hint = new HtmlPanel();
102 hint.setText(
103 "<html>"
104 + tr("Please click on <strong>Download list</strong> to download and display a list of available plugins.")
105 + "</html>"
106 );
107 add(hint, gbc);
108 }
109
110 /**
111 * Refreshes the list.
112 */
113 public void refreshView() {
114 final Rectangle visibleRect = getVisibleRect();
115 List<PluginInformation> displayedPlugins = model.getDisplayedPlugins();
116 removeAll();
117
118 GridBagConstraints gbc = new GridBagConstraints();
119 gbc.gridx = 0;
120 gbc.anchor = GridBagConstraints.NORTHWEST;
121 gbc.fill = GridBagConstraints.HORIZONTAL;
122 gbc.weightx = 1.0;
123
124 if (displayedPlugins.isEmpty()) {
125 displayEmptyPluginListInformation();
126 return;
127 }
128
129 int row = -1;
130 for (final PluginInformation pi : displayedPlugins) {
131 boolean selected = model.isSelectedPlugin(pi.getName());
132 String remoteversion = formatPluginRemoteVersion(pi);
133 String localversion = formatPluginLocalVersion(model.getPluginInformation(pi.getName()));
134
135 final PluginCheckBox cbPlugin = new PluginCheckBox(pi, selected, this, model);
136 String pluginText = tr("{0}: Version {1} (local: {2})", pi.getName(), remoteversion, localversion);
137 if (pi.requires != null && !pi.requires.isEmpty()) {
138 pluginText += tr(" (requires: {0})", pi.requires);
139 }
140 JLabel lblPlugin = new JLabel(
141 pluginText,
142 pi.getScaledIcon(),
143 SwingConstants.LEFT);
144 lblPlugin.addMouseListener(new PluginCheckBoxMouseAdapter(cbPlugin));
145
146 gbc.gridx = 0;
147 gbc.gridy = ++row;
148 gbc.insets = new Insets(5, 5, 0, 5);
149 gbc.weighty = 0.0;
150 gbc.weightx = 0.0;
151 add(cbPlugin, gbc);
152
153 gbc.gridx = 1;
154 gbc.weightx = 1.0;
155 add(lblPlugin, gbc);
156
157 HtmlPanel description = new HtmlPanel();
158 description.setText(pi.getDescriptionAsHtml());
159 description.getEditorPane().addHyperlinkListener(e -> {
160 if (e.getEventType() == EventType.ACTIVATED) {
161 OpenBrowser.displayUrl(e.getURL().toString());
162 }
163 });
164 lblPlugin.setLabelFor(description);
165
166 gbc.gridx = 1;
167 gbc.gridy = ++row;
168 gbc.insets = new Insets(3, 25, 5, 5);
169 gbc.weighty = 1.0;
170 add(description, gbc);
171 }
172 revalidate();
173 repaint();
174 SwingUtilities.invokeLater(() -> scrollRectToVisible(visibleRect));
175 }
176}
Note: See TracBrowser for help on using the repository browser.