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