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

Last change on this file since 6070 was 6070, checked in by stoecker, 11 years ago

see #8853 remove tabs, trailing spaces, windows line ends, strange characters

  • Property svn:eol-style set to native
File size: 9.1 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;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.Component;
8import java.awt.GridBagConstraints;
9import java.awt.GridBagLayout;
10import java.awt.Insets;
11import java.awt.Rectangle;
12import java.awt.event.ActionEvent;
13import java.awt.event.ActionListener;
14import java.util.HashSet;
15import java.util.List;
16import java.util.Set;
17
18import javax.swing.JCheckBox;
19import javax.swing.JLabel;
20import javax.swing.JOptionPane;
21import javax.swing.SwingConstants;
22import javax.swing.SwingUtilities;
23import javax.swing.event.HyperlinkEvent;
24import javax.swing.event.HyperlinkEvent.EventType;
25import javax.swing.event.HyperlinkListener;
26
27import org.openstreetmap.josm.gui.widgets.HtmlPanel;
28import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
29import org.openstreetmap.josm.plugins.PluginHandler;
30import org.openstreetmap.josm.plugins.PluginInformation;
31import org.openstreetmap.josm.tools.OpenBrowser;
32
33public class PluginListPanel extends VerticallyScrollablePanel{
34 private PluginPreferencesModel model;
35
36 public PluginListPanel() {
37 this(new PluginPreferencesModel());
38 }
39
40 public PluginListPanel(PluginPreferencesModel model) {
41 this.model = model;
42 setLayout(new GridBagLayout());
43 }
44
45 protected String formatPluginRemoteVersion(PluginInformation pi) {
46 StringBuilder sb = new StringBuilder();
47 if (pi.version == null || pi.version.trim().equals("")) {
48 sb.append(tr("unknown"));
49 } else {
50 sb.append(pi.version);
51 if (pi.oldmode) {
52 sb.append("*");
53 }
54 }
55 return sb.toString();
56 }
57
58 protected String formatPluginLocalVersion(PluginInformation pi) {
59 if (pi == null) return tr("unknown");
60 if (pi.localversion == null || pi.localversion.trim().equals(""))
61 return tr("unknown");
62 return pi.localversion;
63 }
64
65 protected String formatCheckboxTooltipText(PluginInformation pi) {
66 if (pi == null) return "";
67 if (pi.downloadlink == null)
68 return tr("Plugin bundled with JOSM");
69 else
70 return pi.downloadlink;
71 }
72
73 public void displayEmptyPluginListInformation() {
74 GridBagConstraints gbc = new GridBagConstraints();
75 gbc.gridx = 0;
76 gbc.anchor = GridBagConstraints.CENTER;
77 gbc.fill = GridBagConstraints.BOTH;
78 gbc.insets = new Insets(40,0,40,0);
79 gbc.weightx = 1.0;
80 gbc.weighty = 1.0;
81
82 HtmlPanel hint = new HtmlPanel();
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 /**
92 * A plugin checkbox.
93 *
94 */
95 private class JPluginCheckBox extends JCheckBox {
96 public final PluginInformation pi;
97 public JPluginCheckBox(final PluginInformation pi, boolean selected) {
98 this.pi = pi;
99 setSelected(selected);
100 setToolTipText(formatCheckboxTooltipText(pi));
101 addActionListener(new PluginCbActionListener(this));
102 }
103 }
104
105 /**
106 * Listener called when the user selects/unselects a plugin checkbox.
107 *
108 */
109 private class PluginCbActionListener implements ActionListener {
110 private final JPluginCheckBox cb;
111 public PluginCbActionListener(JPluginCheckBox cb) {
112 this.cb = cb;
113 }
114 protected void selectRequiredPlugins(PluginInformation info) {
115 if (info != null && info.requires != null) {
116 for (String s : info.getRequiredPlugins()) {
117 if (!model.isSelectedPlugin(s)) {
118 model.setPluginSelected(s, true);
119 selectRequiredPlugins(model.getPluginInformation(s));
120 }
121 }
122 }
123 }
124 public void actionPerformed(ActionEvent e) {
125 // Select/unselect corresponding plugin in the model
126 model.setPluginSelected(cb.pi.getName(), cb.isSelected());
127 // Does the newly selected plugin require other plugins ?
128 if (cb.isSelected() && cb.pi.requires != null) {
129 // Select required plugins
130 selectRequiredPlugins(cb.pi);
131 // Alert user if plugin requirements are not met
132 PluginHandler.checkRequiredPluginsPreconditions(PluginListPanel.this, model.getAvailablePlugins(), cb.pi, false);
133 }
134 // If the plugin has been unselected, was it required by other plugins still selected ?
135 else if (!cb.isSelected()) {
136 Set<String> otherPlugins = new HashSet<String>();
137 for (PluginInformation pi : model.getAvailablePlugins()) {
138 if (!pi.equals(cb.pi) && pi.requires != null && model.isSelectedPlugin(pi.getName())) {
139 for (String s : pi.getRequiredPlugins()) {
140 if (s.equals(cb.pi.getName())) {
141 otherPlugins.add(pi.getName());
142 break;
143 }
144 }
145 }
146 }
147 if (!otherPlugins.isEmpty()) {
148 alertPluginStillRequired(PluginListPanel.this, cb.pi.getName(), otherPlugins);
149 }
150 }
151 }
152 };
153
154
155 /**
156 * Alerts the user if an unselected plugin is still required by another plugins
157 *
158 * @param parent The parent Component used to display error popup
159 * @param plugin the plugin
160 * @param otherPlugins the other plugins
161 */
162 private static void alertPluginStillRequired(Component parent, String plugin, Set<String> otherPlugins) {
163 StringBuilder sb = new StringBuilder();
164 sb.append("<html>");
165 sb.append(trn("Plugin {0} is still required by this plugin:",
166 "Plugin {0} is still required by these {1} plugins:",
167 otherPlugins.size(),
168 plugin,
169 otherPlugins.size()
170 ));
171 sb.append("<ul>");
172 for (String p: otherPlugins) {
173 sb.append("<li>").append(p).append("</li>");
174 }
175 sb.append("</ul>").append("</html>");
176 JOptionPane.showMessageDialog(
177 parent,
178 sb.toString(),
179 tr("Warning"),
180 JOptionPane.WARNING_MESSAGE
181 );
182 }
183
184 public void refreshView() {
185 final Rectangle visibleRect = getVisibleRect();
186 List<PluginInformation> displayedPlugins = model.getDisplayedPlugins();
187 removeAll();
188
189 GridBagConstraints gbc = new GridBagConstraints();
190 gbc.gridx = 0;
191 gbc.anchor = GridBagConstraints.NORTHWEST;
192 gbc.fill = GridBagConstraints.HORIZONTAL;
193 gbc.weightx = 1.0;
194
195 if (displayedPlugins.isEmpty()) {
196 displayEmptyPluginListInformation();
197 return;
198 }
199
200 int row = -1;
201 for (final PluginInformation pi : displayedPlugins) {
202 boolean selected = model.isSelectedPlugin(pi.getName());
203 String remoteversion = formatPluginRemoteVersion(pi);
204 String localversion = formatPluginLocalVersion(model.getPluginInformation(pi.getName()));
205
206 JPluginCheckBox cbPlugin = new JPluginCheckBox(pi, selected);
207 String pluginText = tr("{0}: Version {1} (local: {2})", pi.getName(), remoteversion, localversion);
208 if (pi.requires != null && !pi.requires.isEmpty()) {
209 pluginText += tr(" (requires: {0})", pi.requires);
210 }
211 JLabel lblPlugin = new JLabel(
212 pluginText,
213 pi.getScaledIcon(),
214 SwingConstants.LEFT);
215
216 gbc.gridx = 0;
217 gbc.gridy = ++row;
218 gbc.insets = new Insets(5,5,0,5);
219 gbc.weighty = 0.0;
220 gbc.weightx = 0.0;
221 add(cbPlugin, gbc);
222
223 gbc.gridx = 1;
224 gbc.weightx = 1.0;
225 add(lblPlugin, gbc);
226
227 HtmlPanel description = new HtmlPanel();
228 description.setText(pi.getDescriptionAsHtml());
229 description.getEditorPane().addHyperlinkListener(new HyperlinkListener() {
230 public void hyperlinkUpdate(HyperlinkEvent e) {
231 if(e.getEventType() == EventType.ACTIVATED) {
232 OpenBrowser.displayUrl(e.getURL().toString());
233 }
234 }
235 });
236
237 gbc.gridx = 1;
238 gbc.gridy = ++row;
239 gbc.insets = new Insets(3,25,5,5);
240 gbc.weighty = 1.0;
241 add(description, gbc);
242 }
243 revalidate();
244 repaint();
245 if (visibleRect != null && visibleRect.width > 0 && visibleRect.height > 0) {
246 SwingUtilities.invokeLater(new Runnable() {
247 @Override
248 public void run() {
249 scrollRectToVisible(visibleRect);
250 }
251 });
252 }
253 }
254}
Note: See TracBrowser for help on using the repository browser.