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