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