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