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