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