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