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.awt.event.MouseWheelEvent;
|
---|
10 | import java.awt.event.MouseWheelListener;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Iterator;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.logging.Logger;
|
---|
17 |
|
---|
18 | import javax.swing.BorderFactory;
|
---|
19 | import javax.swing.JComponent;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JScrollPane;
|
---|
24 | import javax.swing.JTabbedPane;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
28 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
29 | import org.openstreetmap.josm.tools.GBC;
|
---|
30 | import org.openstreetmap.josm.tools.I18n;
|
---|
31 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * The preference settings.
|
---|
35 | *
|
---|
36 | * @author imi
|
---|
37 | */
|
---|
38 | public class PreferenceDialog extends JTabbedPane implements MouseWheelListener {
|
---|
39 | static private final Logger logger = Logger.getLogger(PreferenceDialog.class.getName());
|
---|
40 |
|
---|
41 | private final static Collection<PreferenceSettingFactory> settingsFactory = new LinkedList<PreferenceSettingFactory>();
|
---|
42 | private final List<PreferenceSetting> settings = new ArrayList<PreferenceSetting>();
|
---|
43 |
|
---|
44 | // some common tabs
|
---|
45 | public final JPanel display = createPreferenceTab("display", tr("Display Settings"), tr("Various settings that influence the visual representation of the whole program."));
|
---|
46 | public final JPanel connection = createPreferenceTab("connection", I18n.tr("Connection Settings"), I18n.tr("Connection Settings for the OSM server."),true);
|
---|
47 | public final JPanel map = createPreferenceTab("map", I18n.tr("Map Settings"), I18n.tr("Settings for the map projection and data interpretation."));
|
---|
48 | public final JPanel audio = createPreferenceTab("audio", I18n.tr("Audio Settings"), I18n.tr("Settings for the audio player and audio markers."));
|
---|
49 |
|
---|
50 | public final javax.swing.JTabbedPane displaycontent = new javax.swing.JTabbedPane();
|
---|
51 | public final javax.swing.JTabbedPane mapcontent = new javax.swing.JTabbedPane();
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Construct a JPanel for the preference settings. Layout is GridBagLayout
|
---|
55 | * and a centered title label and the description are added. The panel
|
---|
56 | * will be shown inside a {@link ScrollPane}
|
---|
57 | * @param icon The name of the icon.
|
---|
58 | * @param title The title of this preference tab.
|
---|
59 | * @param desc A description in one sentence for this tab. Will be displayed
|
---|
60 | * italic under the title.
|
---|
61 | * @return The created panel ready to add other controls.
|
---|
62 | */
|
---|
63 | public JPanel createPreferenceTab(String icon, String title, String desc) {
|
---|
64 | return createPreferenceTab(icon, title, desc, false);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Construct a JPanel for the preference settings. Layout is GridBagLayout
|
---|
69 | * and a centered title label and the description are added.
|
---|
70 | * @param icon The name of the icon.
|
---|
71 | * @param title The title of this preference tab.
|
---|
72 | * @param desc A description in one sentence for this tab. Will be displayed
|
---|
73 | * italic under the title.
|
---|
74 | * @param inScrollPane if <code>true</code> the added tab will show scroll bars
|
---|
75 | * if the panel content is larger than the available space
|
---|
76 | * @return The created panel ready to add other controls.
|
---|
77 | */
|
---|
78 | public JPanel createPreferenceTab(String icon, String title, String desc, boolean inScrollPane) {
|
---|
79 | JPanel p = new JPanel(new GridBagLayout());
|
---|
80 | p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
81 | p.add(new JLabel(title), GBC.eol().anchor(GBC.CENTER).insets(0,5,0,10));
|
---|
82 |
|
---|
83 | JLabel descLabel = new JLabel("<html>"+desc+"</html>");
|
---|
84 | descLabel.setFont(descLabel.getFont().deriveFont(Font.ITALIC));
|
---|
85 | p.add(descLabel, GBC.eol().insets(5,0,5,20).fill(GBC.HORIZONTAL));
|
---|
86 |
|
---|
87 | JComponent tab = p;
|
---|
88 | if (inScrollPane) {
|
---|
89 | JScrollPane sp = new JScrollPane(p);
|
---|
90 | tab = sp;
|
---|
91 | }
|
---|
92 | addTab(null, ImageProvider.get("preferences", icon), tab);
|
---|
93 | setToolTipTextAt(getTabCount()-1, "<html>"+desc+"</html>");
|
---|
94 | return p;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void ok() {
|
---|
98 | boolean requiresRestart = false;
|
---|
99 | for (PreferenceSetting setting : settings)
|
---|
100 | {
|
---|
101 | if(setting.ok()) {
|
---|
102 | requiresRestart = true;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | if (requiresRestart) {
|
---|
106 | JOptionPane.showMessageDialog(
|
---|
107 | Main.parent,
|
---|
108 | tr("You have to restart JOSM for some settings to take effect."),
|
---|
109 | tr("Warning"),
|
---|
110 | JOptionPane.WARNING_MESSAGE
|
---|
111 | );
|
---|
112 | }
|
---|
113 | Main.parent.repaint();
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * If the dialog is closed with Ok, the preferences will be stored to the preferences-
|
---|
118 | * file, otherwise no change of the file happens.
|
---|
119 | */
|
---|
120 | public PreferenceDialog() {
|
---|
121 | super(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
|
---|
122 |
|
---|
123 | super.addMouseWheelListener(this);
|
---|
124 |
|
---|
125 | for (PreferenceSettingFactory factory:settingsFactory) {
|
---|
126 | // logger.info("creating settings: " + factory);
|
---|
127 | PreferenceSetting setting = factory.createPreferenceSetting();
|
---|
128 | if (setting != null) {
|
---|
129 | settings.add(factory.createPreferenceSetting());
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | display.add(displaycontent, GBC.eol().fill(GBC.BOTH));
|
---|
134 | map.add(mapcontent, GBC.eol().fill(GBC.BOTH));
|
---|
135 | for (Iterator<PreferenceSetting> it = settings.iterator(); it.hasNext();) {
|
---|
136 | try {
|
---|
137 | PreferenceSetting settings = it.next();
|
---|
138 | //logger.info("adding gui: " + settings);
|
---|
139 | settings.addGui(this);
|
---|
140 | } catch (SecurityException e) {
|
---|
141 | it.remove();
|
---|
142 | } catch (Throwable e) {
|
---|
143 | /* allow to change most settings even if e.g. a plugin fails */
|
---|
144 | BugReportExceptionHandler.handleException(e);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public List<PreferenceSetting> getSettings() {
|
---|
150 | return settings;
|
---|
151 | }
|
---|
152 |
|
---|
153 | @SuppressWarnings("unchecked")
|
---|
154 | public <T> T getSetting(Class<? extends T> clazz) {
|
---|
155 | for (PreferenceSetting setting:settings) {
|
---|
156 | if (clazz.isAssignableFrom(setting.getClass()))
|
---|
157 | return (T)setting;
|
---|
158 | }
|
---|
159 | return null;
|
---|
160 | }
|
---|
161 |
|
---|
162 | static {
|
---|
163 | // order is important!
|
---|
164 | settingsFactory.add(new DrawingPreference.Factory());
|
---|
165 | settingsFactory.add(new ColorPreference.Factory());
|
---|
166 | settingsFactory.add(new LafPreference.Factory());
|
---|
167 | settingsFactory.add(new LanguagePreference.Factory());
|
---|
168 | settingsFactory.add(new ServerAccessPreference.Factory());
|
---|
169 | settingsFactory.add(new FilePreferences.Factory());
|
---|
170 | settingsFactory.add(new ProxyPreferences.Factory());
|
---|
171 | settingsFactory.add(new ProjectionPreference.Factory());
|
---|
172 | settingsFactory.add(new MapPaintPreference.Factory());
|
---|
173 | settingsFactory.add(new TaggingPresetPreference.Factory());
|
---|
174 | settingsFactory.add(new PluginPreference.Factory());
|
---|
175 | settingsFactory.add(Main.toolbar);
|
---|
176 | settingsFactory.add(new AudioPreference.Factory());
|
---|
177 | settingsFactory.add(new ShortcutPreference.Factory());
|
---|
178 |
|
---|
179 | PluginHandler.getPreferenceSetting(settingsFactory);
|
---|
180 |
|
---|
181 | // always the last: advanced tab
|
---|
182 | settingsFactory.add(new AdvancedPreference.Factory());
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * This mouse wheel listener reacts when a scroll is carried out over the
|
---|
187 | * tab strip and scrolls one tab/down or up, selecting it immediately.
|
---|
188 | */
|
---|
189 | public void mouseWheelMoved(MouseWheelEvent wev) {
|
---|
190 | // Ensure the cursor is over the tab strip
|
---|
191 | if(super.indexAtLocation(wev.getPoint().x, wev.getPoint().y) < 0)
|
---|
192 | return;
|
---|
193 |
|
---|
194 | // Get currently selected tab
|
---|
195 | int newTab = super.getSelectedIndex() + wev.getWheelRotation();
|
---|
196 |
|
---|
197 | // Ensure the new tab index is sound
|
---|
198 | newTab = newTab < 0 ? 0 : newTab;
|
---|
199 | newTab = newTab >= super.getTabCount() ? super.getTabCount() - 1 : newTab;
|
---|
200 |
|
---|
201 | // select new tab
|
---|
202 | super.setSelectedIndex(newTab);
|
---|
203 | }
|
---|
204 | }
|
---|