source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginListPanel.java@ 5120

Last change on this file since 5120 was 5120, checked in by Don-vip, 12 years ago

fix #7541 - clean plugin list initialization + keep scrollbar position when refreshing the list

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.plugin;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.Insets;
9import java.awt.Rectangle;
10import java.awt.event.ActionEvent;
11import java.awt.event.ActionListener;
12import java.util.List;
13
14import javax.swing.JCheckBox;
15import javax.swing.JLabel;
16import javax.swing.SwingConstants;
17import javax.swing.SwingUtilities;
18import javax.swing.event.HyperlinkEvent;
19import javax.swing.event.HyperlinkEvent.EventType;
20import javax.swing.event.HyperlinkListener;
21
22import org.openstreetmap.josm.gui.widgets.HtmlPanel;
23import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
24import org.openstreetmap.josm.plugins.PluginInformation;
25import org.openstreetmap.josm.tools.OpenBrowser;
26
27public 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}
Note: See TracBrowser for help on using the repository browser.