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