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.Font;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.awt.event.ActionListener;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.logging.Logger;
|
---|
14 |
|
---|
15 | import javax.swing.JCheckBox;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.event.HyperlinkEvent;
|
---|
18 | import javax.swing.event.HyperlinkListener;
|
---|
19 | import javax.swing.event.HyperlinkEvent.EventType;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.gui.JMultilineLabel;
|
---|
22 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
23 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
---|
24 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
25 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
26 |
|
---|
27 | public class PluginListPanel extends VerticallyScrollablePanel{
|
---|
28 | @SuppressWarnings("unused")
|
---|
29 | private static final Logger logger = Logger.getLogger(PluginListPanel.class.getName());
|
---|
30 |
|
---|
31 | private PluginPreferencesModel model;
|
---|
32 |
|
---|
33 | public PluginListPanel() {
|
---|
34 | model = new PluginPreferencesModel();
|
---|
35 | setLayout(new GridBagLayout());
|
---|
36 | }
|
---|
37 |
|
---|
38 | public PluginListPanel(PluginPreferencesModel model) {
|
---|
39 | this.model = model;
|
---|
40 | setLayout(new GridBagLayout());
|
---|
41 | }
|
---|
42 |
|
---|
43 | protected String formatPluginRemoteVersion(PluginInformation pi) {
|
---|
44 | StringBuilder sb = new StringBuilder();
|
---|
45 | if (pi.version == null || pi.version.trim().equals("")) {
|
---|
46 | sb.append(tr("unknown"));
|
---|
47 | } else {
|
---|
48 | sb.append(pi.version);
|
---|
49 | if (pi.oldmode) {
|
---|
50 | sb.append("*");
|
---|
51 | }
|
---|
52 | }
|
---|
53 | return sb.toString();
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected String formatPluginLocalVersion(PluginInformation pi) {
|
---|
57 | if (pi == null) return tr("unknown");
|
---|
58 | if (pi.localversion == null || pi.localversion.trim().equals(""))
|
---|
59 | return tr("unknown");
|
---|
60 | return pi.localversion;
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected String formatCheckboxTooltipText(PluginInformation pi) {
|
---|
64 | if (pi == null) return "";
|
---|
65 | if (pi.downloadlink == null)
|
---|
66 | return tr("Plugin bundled with JOSM");
|
---|
67 | else
|
---|
68 | return pi.downloadlink;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void displayEmptyPluginListInformation() {
|
---|
72 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
73 | gbc.gridx = 0;
|
---|
74 | gbc.anchor = GridBagConstraints.CENTER;
|
---|
75 | gbc.fill = GridBagConstraints.BOTH;
|
---|
76 | gbc.insets = new Insets(40,0,40,0);
|
---|
77 | gbc.weightx = 1.0;
|
---|
78 | gbc.weighty = 1.0;
|
---|
79 |
|
---|
80 | JMultilineLabel hint = new JMultilineLabel("");
|
---|
81 | hint.setFont(hint.getFont().deriveFont(Font.PLAIN));
|
---|
82 | hint.setHorizontalAlignment(JLabel.CENTER);
|
---|
83 | hint.setText(
|
---|
84 | "<html>"
|
---|
85 | + tr("Please click on <strong>Download list</strong> to download and display a list of available plugins.")
|
---|
86 | + "</html>"
|
---|
87 | );
|
---|
88 | add(hint, gbc);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void refreshView() {
|
---|
92 | List<PluginInformation> displayedPlugins = model.getDisplayedPlugins();
|
---|
93 | removeAll();
|
---|
94 |
|
---|
95 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
96 | gbc.gridx = 0;
|
---|
97 | gbc.anchor = GridBagConstraints.NORTHWEST;
|
---|
98 | gbc.fill = GridBagConstraints.HORIZONTAL;
|
---|
99 | gbc.weightx = 1.0;
|
---|
100 |
|
---|
101 | if (displayedPlugins.isEmpty()) {
|
---|
102 | displayEmptyPluginListInformation();
|
---|
103 | return;
|
---|
104 | }
|
---|
105 |
|
---|
106 | int row = -1;
|
---|
107 | for (final PluginInformation pi : displayedPlugins) {
|
---|
108 | boolean selected = model.isSelectedPlugin(pi.getName());
|
---|
109 | String remoteversion = formatPluginRemoteVersion(pi);
|
---|
110 | String localversion = formatPluginLocalVersion(model.getPluginInformation(pi.getName()));
|
---|
111 |
|
---|
112 | final JCheckBox cbPlugin = new JCheckBox(
|
---|
113 | tr("{0}: Version {1} (local: {2})", pi.getName(), remoteversion, localversion)
|
---|
114 | );
|
---|
115 | cbPlugin.setSelected(selected);
|
---|
116 | cbPlugin.setToolTipText(formatCheckboxTooltipText(pi));
|
---|
117 | cbPlugin.addActionListener(new ActionListener(){
|
---|
118 | public void actionPerformed(ActionEvent e) {
|
---|
119 | model.setPluginSelected(pi.getName(), cbPlugin.isSelected());
|
---|
120 | }
|
---|
121 | });
|
---|
122 | gbc.gridy = ++row;
|
---|
123 | gbc.insets = new Insets(5,5,0,5);
|
---|
124 | gbc.weighty = 0.0;
|
---|
125 | add(cbPlugin, gbc);
|
---|
126 |
|
---|
127 | HtmlPanel description = new HtmlPanel();
|
---|
128 | description.setText(pi.getDescriptionAsHtml());
|
---|
129 | description.getEditorPane().addHyperlinkListener(new HyperlinkListener() {
|
---|
130 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
131 | if(e.getEventType() == EventType.ACTIVATED) {
|
---|
132 | OpenBrowser.displayUrl(e.getURL().toString());
|
---|
133 | }
|
---|
134 | }
|
---|
135 | });
|
---|
136 |
|
---|
137 | gbc.gridy = ++row;
|
---|
138 | gbc.insets = new Insets(3,25,5,5);
|
---|
139 | gbc.weighty = 1.0;
|
---|
140 | add(description, gbc);
|
---|
141 | }
|
---|
142 | revalidate();
|
---|
143 | repaint();
|
---|
144 | }
|
---|
145 | }
|
---|