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