source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceDialog.java@ 1955

Last change on this file since 1955 was 1955, checked in by stoecker, 15 years ago

close #3112 - patch by bastiK - rework username/password handling to allow password hooks

  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Font;
7import java.awt.GridBagLayout;
8import java.awt.ScrollPane;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.Iterator;
12import java.util.LinkedList;
13import java.util.List;
14
15import javax.swing.BorderFactory;
16import javax.swing.JComponent;
17import javax.swing.JLabel;
18import javax.swing.JOptionPane;
19import javax.swing.JPanel;
20import javax.swing.JScrollPane;
21import javax.swing.JTabbedPane;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.gui.OptionPaneUtil;
25import org.openstreetmap.josm.plugins.PluginHandler;
26import org.openstreetmap.josm.tools.BugReportExceptionHandler;
27import org.openstreetmap.josm.tools.GBC;
28import org.openstreetmap.josm.tools.I18n;
29import org.openstreetmap.josm.tools.ImageProvider;
30
31/**
32 * The preference settings.
33 *
34 * @author imi
35 */
36public class PreferenceDialog extends JTabbedPane {
37
38 private final static Collection<PreferenceSettingFactory> settingsFactory = new LinkedList<PreferenceSettingFactory>();
39 private final List<PreferenceSetting> settings = new ArrayList<PreferenceSetting>();
40
41 // some common tabs
42 public final JPanel display = createPreferenceTab("display", tr("Display Settings"), tr("Various settings that influence the visual representation of the whole program."));
43 public final JPanel connection = createPreferenceTab("connection", I18n.tr("Connection Settings"), I18n.tr("Connection Settings for the OSM server."),true);
44 public final JPanel map = createPreferenceTab("map", I18n.tr("Map Settings"), I18n.tr("Settings for the map projection and data interpretation."));
45 public final JPanel audio = createPreferenceTab("audio", I18n.tr("Audio Settings"), I18n.tr("Settings for the audio player and audio markers."));
46
47 public final javax.swing.JTabbedPane displaycontent = new javax.swing.JTabbedPane();
48 public final javax.swing.JTabbedPane mapcontent = new javax.swing.JTabbedPane();
49
50 /**
51 * Construct a JPanel for the preference settings. Layout is GridBagLayout
52 * and a centered title label and the description are added. The panel
53 * will be shown inside a {@link ScrollPane}
54 * @param icon The name of the icon.
55 * @param title The title of this preference tab.
56 * @param desc A description in one sentence for this tab. Will be displayed
57 * italic under the title.
58 * @return The created panel ready to add other controls.
59 */
60 public JPanel createPreferenceTab(String icon, String title, String desc) {
61 return createPreferenceTab(icon, title, desc, false);
62 }
63
64 /**
65 * Construct a JPanel for the preference settings. Layout is GridBagLayout
66 * and a centered title label and the description are added.
67 * @param icon The name of the icon.
68 * @param title The title of this preference tab.
69 * @param desc A description in one sentence for this tab. Will be displayed
70 * italic under the title.
71 * @param inScrollPane if <code>true</code> the added tab will show scroll bars
72 * if the panel content is larger than the available space
73 * @return The created panel ready to add other controls.
74 */
75 public JPanel createPreferenceTab(String icon, String title, String desc, boolean inScrollPane) {
76 JPanel p = new JPanel(new GridBagLayout());
77 p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
78 p.add(new JLabel(title), GBC.eol().anchor(GBC.CENTER).insets(0,5,0,10));
79
80 JLabel descLabel = new JLabel("<html>"+desc+"</html>");
81 descLabel.setFont(descLabel.getFont().deriveFont(Font.ITALIC));
82 p.add(descLabel, GBC.eol().insets(5,0,5,20).fill(GBC.HORIZONTAL));
83
84 JComponent tab = p;
85 if (inScrollPane) {
86 JScrollPane sp = new JScrollPane(p);
87 tab = sp;
88 }
89 addTab(null, ImageProvider.get("preferences", icon), tab);
90 setToolTipTextAt(getTabCount()-1, "<html>"+desc+"</html>");
91 return p;
92 }
93
94 public void ok() {
95 boolean requiresRestart = false;
96 for (PreferenceSetting setting : settings)
97 {
98 if(setting.ok()) {
99 requiresRestart = true;
100 }
101 }
102 if (requiresRestart) {
103 OptionPaneUtil.showMessageDialog(
104 Main.parent,
105 tr("You have to restart JOSM for some settings to take effect."),
106 tr("Warning"),
107 JOptionPane.WARNING_MESSAGE
108 );
109 }
110 Main.parent.repaint();
111 }
112
113 /**
114 * If the dialog is closed with Ok, the preferences will be stored to the preferences-
115 * file, otherwise no change of the file happens.
116 */
117 public PreferenceDialog() {
118 super(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
119
120 for (PreferenceSettingFactory factory:settingsFactory) {
121
122 PreferenceSetting setting = factory.createPreferenceSetting();
123 if (setting != null) {
124 settings.add(factory.createPreferenceSetting());
125 }
126 }
127
128 display.add(displaycontent, GBC.eol().fill(GBC.BOTH));
129 map.add(mapcontent, GBC.eol().fill(GBC.BOTH));
130 for (Iterator<PreferenceSetting> it = settings.iterator(); it.hasNext();) {
131 try {
132 it.next().addGui(this);
133 } catch (SecurityException e) {
134 it.remove();
135 } catch (Throwable e) {
136 /* allow to change most settings even if e.g. a plugin fails */
137 BugReportExceptionHandler.handleException(e);
138 }
139 }
140 }
141
142 public List<PreferenceSetting> getSettings() {
143 return settings;
144 }
145
146 @SuppressWarnings("unchecked")
147 public <T> T getSetting(Class<? extends T> clazz) {
148 for (PreferenceSetting setting:settings) {
149 if (clazz.isAssignableFrom(setting.getClass()))
150 return (T)setting;
151 }
152 return null;
153 }
154
155 static {
156 // order is important!
157 settingsFactory.add(new DrawingPreference.Factory());
158 settingsFactory.add(new ColorPreference.Factory());
159 settingsFactory.add(new LafPreference.Factory());
160 settingsFactory.add(new LanguagePreference.Factory());
161 settingsFactory.add(new ServerAccessPreference.Factory());
162 settingsFactory.add(new FilePreferences.Factory());
163 settingsFactory.add(new ProxyPreferences.Factory());
164 settingsFactory.add(new ProjectionPreference.Factory());
165 settingsFactory.add(new MapPaintPreference.Factory());
166 settingsFactory.add(new TaggingPresetPreference.Factory());
167 settingsFactory.add(new PluginPreference.Factory());
168 settingsFactory.add(Main.toolbar);
169 settingsFactory.add(new AudioPreference.Factory());
170 settingsFactory.add(new ShortcutPreference.Factory());
171
172 PluginHandler.getPreferenceSetting(settingsFactory);
173
174 // always the last: advanced tab
175 settingsFactory.add(new AdvancedPreference.Factory());
176 }
177}
Note: See TracBrowser for help on using the repository browser.