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

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

added much improved preferences for external styles and presets

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