source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceTabbedPane.java@ 8030

Last change on this file since 8030 was 8030, checked in by bastiK, 9 years ago

see #11090 - refresh plugin info after download & before loading

  • Property svn:eol-style set to native
File size: 24.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Font;
8import java.awt.GridBagLayout;
9import java.awt.Image;
10import java.awt.event.MouseWheelEvent;
11import java.awt.event.MouseWheelListener;
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.LinkedList;
15import java.util.List;
16
17import javax.swing.BorderFactory;
18import javax.swing.Icon;
19import javax.swing.ImageIcon;
20import javax.swing.JLabel;
21import javax.swing.JOptionPane;
22import javax.swing.JPanel;
23import javax.swing.JScrollPane;
24import javax.swing.JTabbedPane;
25import javax.swing.SwingUtilities;
26import javax.swing.event.ChangeEvent;
27import javax.swing.event.ChangeListener;
28
29import org.openstreetmap.josm.Main;
30import org.openstreetmap.josm.actions.ExpertToggleAction;
31import org.openstreetmap.josm.actions.ExpertToggleAction.ExpertModeChangeListener;
32import org.openstreetmap.josm.actions.RestartAction;
33import org.openstreetmap.josm.gui.HelpAwareOptionPane;
34import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
35import org.openstreetmap.josm.gui.preferences.advanced.AdvancedPreference;
36import org.openstreetmap.josm.gui.preferences.audio.AudioPreference;
37import org.openstreetmap.josm.gui.preferences.display.ColorPreference;
38import org.openstreetmap.josm.gui.preferences.display.DisplayPreference;
39import org.openstreetmap.josm.gui.preferences.display.DrawingPreference;
40import org.openstreetmap.josm.gui.preferences.display.LafPreference;
41import org.openstreetmap.josm.gui.preferences.display.LanguagePreference;
42import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
43import org.openstreetmap.josm.gui.preferences.map.BackupPreference;
44import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
45import org.openstreetmap.josm.gui.preferences.map.MapPreference;
46import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
47import org.openstreetmap.josm.gui.preferences.plugin.PluginPreference;
48import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
49import org.openstreetmap.josm.gui.preferences.remotecontrol.RemoteControlPreference;
50import org.openstreetmap.josm.gui.preferences.server.AuthenticationPreference;
51import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
52import org.openstreetmap.josm.gui.preferences.server.ServerAccessPreference;
53import org.openstreetmap.josm.gui.preferences.shortcut.ShortcutPreference;
54import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
55import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
56import org.openstreetmap.josm.gui.preferences.validator.ValidatorTestsPreference;
57import org.openstreetmap.josm.plugins.PluginDownloadTask;
58import org.openstreetmap.josm.plugins.PluginHandler;
59import org.openstreetmap.josm.plugins.PluginInformation;
60import org.openstreetmap.josm.tools.BugReportExceptionHandler;
61import org.openstreetmap.josm.tools.CheckParameterUtil;
62import org.openstreetmap.josm.tools.GBC;
63import org.openstreetmap.josm.tools.ImageProvider;
64
65/**
66 * The preference settings.
67 *
68 * @author imi
69 */
70public final class PreferenceTabbedPane extends JTabbedPane implements MouseWheelListener, ExpertModeChangeListener, ChangeListener {
71
72 /**
73 * Allows PreferenceSettings to do validation of entered values when ok was pressed.
74 * If data is invalid then event can return false to cancel closing of preferences dialog.
75 *
76 */
77 public interface ValidationListener {
78 /**
79 *
80 * @return True if preferences can be saved
81 */
82 boolean validatePreferences();
83 }
84
85 private static interface PreferenceTab {
86 public TabPreferenceSetting getTabPreferenceSetting();
87 public Component getComponent();
88 }
89
90 public static final class PreferencePanel extends JPanel implements PreferenceTab {
91 private final TabPreferenceSetting preferenceSetting;
92
93 private PreferencePanel(TabPreferenceSetting preferenceSetting) {
94 super(new GridBagLayout());
95 CheckParameterUtil.ensureParameterNotNull(preferenceSetting);
96 this.preferenceSetting = preferenceSetting;
97 buildPanel();
98 }
99
100 protected void buildPanel() {
101 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
102 add(new JLabel(preferenceSetting.getTitle()), GBC.eol().insets(0,5,0,10).anchor(GBC.NORTHWEST));
103
104 JLabel descLabel = new JLabel("<html>"+preferenceSetting.getDescription()+"</html>");
105 descLabel.setFont(descLabel.getFont().deriveFont(Font.ITALIC));
106 add(descLabel, GBC.eol().insets(5,0,5,20).fill(GBC.HORIZONTAL));
107 }
108
109 @Override
110 public final TabPreferenceSetting getTabPreferenceSetting() {
111 return preferenceSetting;
112 }
113
114 @Override
115 public Component getComponent() {
116 return this;
117 }
118 }
119
120 public static final class PreferenceScrollPane extends JScrollPane implements PreferenceTab {
121 private final TabPreferenceSetting preferenceSetting;
122
123 private PreferenceScrollPane(Component view, TabPreferenceSetting preferenceSetting) {
124 super(view);
125 this.preferenceSetting = preferenceSetting;
126 }
127
128 private PreferenceScrollPane(PreferencePanel preferencePanel) {
129 this(preferencePanel.getComponent(), preferencePanel.getTabPreferenceSetting());
130 }
131
132 @Override
133 public final TabPreferenceSetting getTabPreferenceSetting() {
134 return preferenceSetting;
135 }
136
137 @Override
138 public Component getComponent() {
139 return this;
140 }
141 }
142
143 // all created tabs
144 private final List<PreferenceTab> tabs = new ArrayList<>();
145 private static final Collection<PreferenceSettingFactory> settingsFactories = new LinkedList<>();
146 private static final PreferenceSettingFactory advancedPreferenceFactory = new AdvancedPreference.Factory();
147 private final List<PreferenceSetting> settings = new ArrayList<>();
148
149 // distinct list of tabs that have been initialized (we do not initialize tabs until they are displayed to speed up dialog startup)
150 private final List<PreferenceSetting> settingsInitialized = new ArrayList<>();
151
152 List<ValidationListener> validationListeners = new ArrayList<>();
153
154 /**
155 * Add validation listener to currently open preferences dialog. Calling to removeValidationListener is not necessary, all listeners will
156 * be automatically removed when dialog is closed
157 * @param validationListener
158 */
159 public void addValidationListener(ValidationListener validationListener) {
160 validationListeners.add(validationListener);
161 }
162
163 /**
164 * Construct a PreferencePanel for the preference settings. Layout is GridBagLayout
165 * and a centered title label and the description are added.
166 * @return The created panel ready to add other controls.
167 */
168 public PreferencePanel createPreferenceTab(TabPreferenceSetting caller) {
169 return createPreferenceTab(caller, false);
170 }
171
172 /**
173 * Construct a PreferencePanel for the preference settings. Layout is GridBagLayout
174 * and a centered title label and the description are added.
175 * @param inScrollPane if <code>true</code> the added tab will show scroll bars
176 * if the panel content is larger than the available space
177 * @return The created panel ready to add other controls.
178 */
179 public PreferencePanel createPreferenceTab(TabPreferenceSetting caller, boolean inScrollPane) {
180 CheckParameterUtil.ensureParameterNotNull(caller);
181 PreferencePanel p = new PreferencePanel(caller);
182
183 PreferenceTab tab = p;
184 if (inScrollPane) {
185 PreferenceScrollPane sp = new PreferenceScrollPane(p);
186 tab = sp;
187 }
188 tabs.add(tab);
189 return p;
190 }
191
192 private static interface TabIdentifier {
193 public boolean identify(TabPreferenceSetting tps, Object param);
194 }
195
196 private void selectTabBy(TabIdentifier method, Object param) {
197 for (int i=0; i<getTabCount(); i++) {
198 Component c = getComponentAt(i);
199 if (c instanceof PreferenceTab) {
200 PreferenceTab tab = (PreferenceTab) c;
201 if (method.identify(tab.getTabPreferenceSetting(), param)) {
202 setSelectedIndex(i);
203 return;
204 }
205 }
206 }
207 }
208
209 public void selectTabByName(String name) {
210 selectTabBy(new TabIdentifier(){
211 @Override
212 public boolean identify(TabPreferenceSetting tps, Object name) {
213 return name != null && tps != null && tps.getIconName() != null && name.equals(tps.getIconName());
214 }}, name);
215 }
216
217 public void selectTabByPref(Class<? extends TabPreferenceSetting> clazz) {
218 selectTabBy(new TabIdentifier(){
219 @Override
220 public boolean identify(TabPreferenceSetting tps, Object clazz) {
221 return tps.getClass().isAssignableFrom((Class<?>) clazz);
222 }}, clazz);
223 }
224
225 public boolean selectSubTabByPref(Class<? extends SubPreferenceSetting> clazz) {
226 for (PreferenceSetting setting : settings) {
227 if (clazz.isInstance(setting)) {
228 final SubPreferenceSetting sub = (SubPreferenceSetting) setting;
229 final TabPreferenceSetting tab = sub.getTabPreferenceSetting(PreferenceTabbedPane.this);
230 selectTabBy(new TabIdentifier(){
231 @Override
232 public boolean identify(TabPreferenceSetting tps, Object unused) {
233 return tps.equals(tab);
234 }}, null);
235 return tab.selectSubTab(sub);
236 }
237 }
238 return false;
239 }
240
241 /**
242 * Returns the {@code DisplayPreference} object.
243 * @return the {@code DisplayPreference} object.
244 */
245 public final DisplayPreference getDisplayPreference() {
246 return getSetting(DisplayPreference.class);
247 }
248
249 /**
250 * Returns the {@code MapPreference} object.
251 * @return the {@code MapPreference} object.
252 */
253 public final MapPreference getMapPreference() {
254 return getSetting(MapPreference.class);
255 }
256
257 /**
258 * Returns the {@code PluginPreference} object.
259 * @return the {@code PluginPreference} object.
260 */
261 public final PluginPreference getPluginPreference() {
262 return getSetting(PluginPreference.class);
263 }
264
265 /**
266 * Returns the {@code ImageryPreference} object.
267 * @return the {@code ImageryPreference} object.
268 */
269 public final ImageryPreference getImageryPreference() {
270 return getSetting(ImageryPreference.class);
271 }
272
273 /**
274 * Returns the {@code ShortcutPreference} object.
275 * @return the {@code ShortcutPreference} object.
276 */
277 public final ShortcutPreference getShortcutPreference() {
278 return getSetting(ShortcutPreference.class);
279 }
280
281 /**
282 * Returns the {@code ServerAccessPreference} object.
283 * @return the {@code ServerAccessPreference} object.
284 * @since 6523
285 */
286 public final ServerAccessPreference getServerPreference() {
287 return getSetting(ServerAccessPreference.class);
288 }
289
290 /**
291 * Returns the {@code ValidatorPreference} object.
292 * @return the {@code ValidatorPreference} object.
293 * @since 6665
294 */
295 public final ValidatorPreference getValidatorPreference() {
296 return getSetting(ValidatorPreference.class);
297 }
298
299 /**
300 * Saves preferences.
301 */
302 public void savePreferences() {
303 // create a task for downloading plugins if the user has activated, yet not downloaded,
304 // new plugins
305 //
306 final PluginPreference preference = getPluginPreference();
307 final List<PluginInformation> toDownload = preference.getPluginsScheduledForUpdateOrDownload();
308 final PluginDownloadTask task;
309 if (toDownload != null && ! toDownload.isEmpty()) {
310 task = new PluginDownloadTask(this, toDownload, tr("Download plugins"));
311 } else {
312 task = null;
313 }
314
315 // this is the task which will run *after* the plugins are downloaded
316 //
317 final Runnable continuation = new Runnable() {
318 @Override
319 public void run() {
320 boolean requiresRestart = false;
321
322 for (PreferenceSetting setting : settingsInitialized) {
323 if (setting.ok()) {
324 requiresRestart = true;
325 }
326 }
327
328 // build the messages. We only display one message, including the status
329 // information from the plugin download task and - if necessary - a hint
330 // to restart JOSM
331 //
332 StringBuilder sb = new StringBuilder();
333 sb.append("<html>");
334 if (task != null && !task.isCanceled()) {
335 PluginHandler.refreshLocalUpdatedPluginInfo(task.getDownloadedPlugins());
336 sb.append(PluginPreference.buildDownloadSummary(task));
337 }
338 if (requiresRestart) {
339 sb.append(tr("You have to restart JOSM for some settings to take effect."));
340 sb.append("<br/><br/>");
341 sb.append(tr("Would you like to restart now?"));
342 }
343 sb.append("</html>");
344
345 // display the message, if necessary
346 //
347 if (requiresRestart) {
348 final ButtonSpec [] options = RestartAction.getButtonSpecs();
349 if (0 == HelpAwareOptionPane.showOptionDialog(
350 Main.parent,
351 sb.toString(),
352 tr("Restart"),
353 JOptionPane.INFORMATION_MESSAGE,
354 null, /* no special icon */
355 options,
356 options[0],
357 null /* no special help */
358 )) {
359 Main.main.menu.restart.actionPerformed(null);
360 }
361 } else if (task != null && !task.isCanceled()) {
362 JOptionPane.showMessageDialog(
363 Main.parent,
364 sb.toString(),
365 tr("Warning"),
366 JOptionPane.WARNING_MESSAGE
367 );
368 }
369
370 // load the plugins that can be loaded at runtime
371 List<PluginInformation> newPlugins = preference.getNewlyActivatedPlugins();
372 if (newPlugins != null) {
373 Collection<PluginInformation> downloadedPlugins = null;
374 if (task != null && !task.isCanceled()) {
375 downloadedPlugins = task.getDownloadedPlugins();
376 }
377 List<PluginInformation> toLoad = new ArrayList<>();
378 for (PluginInformation pi : newPlugins) {
379 if (toDownload.contains(pi) && downloadedPlugins != null && !downloadedPlugins.contains(pi)) {
380 continue; // failed download
381 }
382 if (pi.canloadatruntime) {
383 toLoad.add(pi);
384 }
385 }
386 if (!toLoad.isEmpty()) {
387 PluginHandler.loadPlugins(PreferenceTabbedPane.this, toLoad, null); // FIXME: progress bar
388 }
389 }
390
391 Main.parent.repaint();
392 }
393 };
394
395 if (task != null) {
396 // if we have to launch a plugin download task we do it asynchronously, followed
397 // by the remaining "save preferences" activites run on the Swing EDT.
398 //
399 Main.worker.submit(task);
400 Main.worker.submit(
401 new Runnable() {
402 @Override
403 public void run() {
404 SwingUtilities.invokeLater(continuation);
405 }
406 }
407 );
408 } else {
409 // no need for asynchronous activities. Simply run the remaining "save preference"
410 // activities on this thread (we are already on the Swing EDT
411 //
412 continuation.run();
413 }
414 }
415
416 /**
417 * If the dialog is closed with Ok, the preferences will be stored to the preferences-
418 * file, otherwise no change of the file happens.
419 */
420 public PreferenceTabbedPane() {
421 super(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
422 super.addMouseWheelListener(this);
423 super.getModel().addChangeListener(this);
424 ExpertToggleAction.addExpertModeChangeListener(this);
425 }
426
427 public void buildGui() {
428 Collection<PreferenceSettingFactory> factories = new ArrayList<>(settingsFactories);
429 factories.addAll(PluginHandler.getPreferenceSetting());
430 factories.add(advancedPreferenceFactory);
431
432 for (PreferenceSettingFactory factory : factories) {
433 PreferenceSetting setting = factory.createPreferenceSetting();
434 if (setting != null) {
435 settings.add(setting);
436 }
437 }
438 addGUITabs(false);
439 }
440
441 private void addGUITabsForSetting(Icon icon, TabPreferenceSetting tps) {
442 for (PreferenceTab tab : tabs) {
443 if (tab.getTabPreferenceSetting().equals(tps)) {
444 insertGUITabsForSetting(icon, tps, getTabCount());
445 }
446 }
447 }
448
449 private void insertGUITabsForSetting(Icon icon, TabPreferenceSetting tps, int index) {
450 int position = index;
451 for (PreferenceTab tab : tabs) {
452 if (tab.getTabPreferenceSetting().equals(tps)) {
453 insertTab(null, icon, tab.getComponent(), tps.getTooltip(), position++);
454 }
455 }
456 }
457
458 private void addGUITabs(boolean clear) {
459 boolean expert = ExpertToggleAction.isExpert();
460 Component sel = getSelectedComponent();
461 if (clear) {
462 removeAll();
463 }
464 // Inspect each tab setting
465 for (PreferenceSetting setting : settings) {
466 if (setting instanceof TabPreferenceSetting) {
467 TabPreferenceSetting tps = (TabPreferenceSetting) setting;
468 if (expert || !tps.isExpert()) {
469 // Get icon
470 String iconName = tps.getIconName();
471 ImageIcon icon = iconName != null && iconName.length() > 0 ? ImageProvider.get("preferences", iconName) : null;
472 // See #6985 - Force icons to be 48x48 pixels
473 if (icon != null && (icon.getIconHeight() != 48 || icon.getIconWidth() != 48)) {
474 icon = new ImageIcon(icon.getImage().getScaledInstance(48, 48, Image.SCALE_DEFAULT));
475 }
476 if (settingsInitialized.contains(tps)) {
477 // If it has been initialized, add corresponding tab(s)
478 addGUITabsForSetting(icon, tps);
479 } else {
480 // If it has not been initialized, create an empty tab with only icon and tooltip
481 addTab(null, icon, new PreferencePanel(tps), tps.getTooltip());
482 }
483 }
484 } else if (!(setting instanceof SubPreferenceSetting)) {
485 Main.warn("Ignoring preferences "+setting);
486 }
487 }
488 try {
489 if (sel != null) {
490 setSelectedComponent(sel);
491 }
492 } catch (IllegalArgumentException e) {
493 Main.warn(e);
494 }
495 }
496
497 @Override
498 public void expertChanged(boolean isExpert) {
499 addGUITabs(true);
500 }
501
502 public List<PreferenceSetting> getSettings() {
503 return settings;
504 }
505
506 @SuppressWarnings("unchecked")
507 public <T> T getSetting(Class<? extends T> clazz) {
508 for (PreferenceSetting setting:settings) {
509 if (clazz.isAssignableFrom(setting.getClass()))
510 return (T)setting;
511 }
512 return null;
513 }
514
515 static {
516 // order is important!
517 settingsFactories.add(new DisplayPreference.Factory());
518 settingsFactories.add(new DrawingPreference.Factory());
519 settingsFactories.add(new ColorPreference.Factory());
520 settingsFactories.add(new LafPreference.Factory());
521 settingsFactories.add(new LanguagePreference.Factory());
522 settingsFactories.add(new ServerAccessPreference.Factory());
523 settingsFactories.add(new AuthenticationPreference.Factory());
524 settingsFactories.add(new ProxyPreference.Factory());
525 settingsFactories.add(new MapPreference.Factory());
526 settingsFactories.add(new ProjectionPreference.Factory());
527 settingsFactories.add(new MapPaintPreference.Factory());
528 settingsFactories.add(new TaggingPresetPreference.Factory());
529 settingsFactories.add(new BackupPreference.Factory());
530 settingsFactories.add(new PluginPreference.Factory());
531 settingsFactories.add(Main.toolbar);
532 settingsFactories.add(new AudioPreference.Factory());
533 settingsFactories.add(new ShortcutPreference.Factory());
534 settingsFactories.add(new ValidatorPreference.Factory());
535 settingsFactories.add(new ValidatorTestsPreference.Factory());
536 settingsFactories.add(new ValidatorTagCheckerRulesPreference.Factory());
537 settingsFactories.add(new RemoteControlPreference.Factory());
538 settingsFactories.add(new ImageryPreference.Factory());
539 }
540
541 /**
542 * This mouse wheel listener reacts when a scroll is carried out over the
543 * tab strip and scrolls one tab/down or up, selecting it immediately.
544 */
545 @Override
546 public void mouseWheelMoved(MouseWheelEvent wev) {
547 // Ensure the cursor is over the tab strip
548 if(super.indexAtLocation(wev.getPoint().x, wev.getPoint().y) < 0)
549 return;
550
551 // Get currently selected tab
552 int newTab = super.getSelectedIndex() + wev.getWheelRotation();
553
554 // Ensure the new tab index is sound
555 newTab = newTab < 0 ? 0 : newTab;
556 newTab = newTab >= super.getTabCount() ? super.getTabCount() - 1 : newTab;
557
558 // select new tab
559 super.setSelectedIndex(newTab);
560 }
561
562 @Override
563 public void stateChanged(ChangeEvent e) {
564 int index = getSelectedIndex();
565 Component sel = getSelectedComponent();
566 if (index > -1 && sel instanceof PreferenceTab) {
567 PreferenceTab tab = (PreferenceTab) sel;
568 TabPreferenceSetting preferenceSettings = tab.getTabPreferenceSetting();
569 if (!settingsInitialized.contains(preferenceSettings)) {
570 try {
571 getModel().removeChangeListener(this);
572 preferenceSettings.addGui(this);
573 // Add GUI for sub preferences
574 for (PreferenceSetting setting : settings) {
575 if (setting instanceof SubPreferenceSetting) {
576 SubPreferenceSetting sps = (SubPreferenceSetting) setting;
577 if (sps.getTabPreferenceSetting(this) == preferenceSettings) {
578 try {
579 sps.addGui(this);
580 } catch (SecurityException ex) {
581 Main.error(ex);
582 } catch (Exception ex) {
583 BugReportExceptionHandler.handleException(ex);
584 } finally {
585 settingsInitialized.add(sps);
586 }
587 }
588 }
589 }
590 Icon icon = getIconAt(index);
591 remove(index);
592 insertGUITabsForSetting(icon, preferenceSettings, index);
593 setSelectedIndex(index);
594 } catch (SecurityException ex) {
595 Main.error(ex);
596 } catch (Exception ex) {
597 // allow to change most settings even if e.g. a plugin fails
598 BugReportExceptionHandler.handleException(ex);
599 } finally {
600 settingsInitialized.add(preferenceSettings);
601 getModel().addChangeListener(this);
602 }
603 }
604 }
605 }
606}
Note: See TracBrowser for help on using the repository browser.