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 | import javax.swing.SwingUtilities;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.plugins.PluginDownloadTask;
|
---|
29 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
30 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
31 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
32 | import org.openstreetmap.josm.tools.GBC;
|
---|
33 | import org.openstreetmap.josm.tools.I18n;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * The preference settings.
|
---|
38 | *
|
---|
39 | * @author imi
|
---|
40 | */
|
---|
41 | public class PreferenceTabbedPane extends JTabbedPane implements MouseWheelListener {
|
---|
42 | @SuppressWarnings("unused")
|
---|
43 | static private final Logger logger = Logger.getLogger(PreferenceTabbedPane.class.getName());
|
---|
44 |
|
---|
45 | private final static Collection<PreferenceSettingFactory> settingsFactory = new LinkedList<PreferenceSettingFactory>();
|
---|
46 | private final List<PreferenceSetting> settings = new ArrayList<PreferenceSetting>();
|
---|
47 |
|
---|
48 | // some common tabs
|
---|
49 | public final JPanel display = createPreferenceTab("display", tr("Display Settings"), tr("Various settings that influence the visual representation of the whole program."));
|
---|
50 | public final JPanel connection = createPreferenceTab("connection", I18n.tr("Connection Settings"), I18n.tr("Connection Settings for the OSM server."),false);
|
---|
51 | public final JPanel map = createPreferenceTab("map", I18n.tr("Map Settings"), I18n.tr("Settings for the map projection and data interpretation."));
|
---|
52 | public final JPanel audio = createPreferenceTab("audio", I18n.tr("Audio Settings"), I18n.tr("Settings for the audio player and audio markers."));
|
---|
53 | public final JPanel plugins = createPreferenceTab("plugin", tr("Plugins"), tr("Configure available plugins."), false);
|
---|
54 |
|
---|
55 | public final javax.swing.JTabbedPane displaycontent = new javax.swing.JTabbedPane();
|
---|
56 | public final javax.swing.JTabbedPane mapcontent = new javax.swing.JTabbedPane();
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Construct a JPanel for the preference settings. Layout is GridBagLayout
|
---|
60 | * and a centered title label and the description are added. The panel
|
---|
61 | * will be shown inside a {@link ScrollPane}
|
---|
62 | * @param icon The name of the icon.
|
---|
63 | * @param title The title of this preference tab.
|
---|
64 | * @param desc A description in one sentence for this tab. Will be displayed
|
---|
65 | * italic under the title.
|
---|
66 | * @return The created panel ready to add other controls.
|
---|
67 | */
|
---|
68 | public JPanel createPreferenceTab(String icon, String title, String desc) {
|
---|
69 | return createPreferenceTab(icon, title, desc, false);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Construct a JPanel for the preference settings. Layout is GridBagLayout
|
---|
74 | * and a centered title label and the description are added.
|
---|
75 | * @param icon The name of the icon.
|
---|
76 | * @param title The title of this preference tab.
|
---|
77 | * @param desc A description in one sentence for this tab. Will be displayed
|
---|
78 | * italic under the title.
|
---|
79 | * @param inScrollPane if <code>true</code> the added tab will show scroll bars
|
---|
80 | * if the panel content is larger than the available space
|
---|
81 | * @return The created panel ready to add other controls.
|
---|
82 | */
|
---|
83 | public JPanel createPreferenceTab(String icon, String title, String desc, boolean inScrollPane) {
|
---|
84 | JPanel p = new JPanel(new GridBagLayout());
|
---|
85 | p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
86 | p.add(new JLabel(title), GBC.eol().insets(0,5,0,10).anchor(GBC.NORTHWEST));
|
---|
87 |
|
---|
88 | JLabel descLabel = new JLabel("<html>"+desc+"</html>");
|
---|
89 | descLabel.setFont(descLabel.getFont().deriveFont(Font.ITALIC));
|
---|
90 | p.add(descLabel, GBC.eol().insets(5,0,5,20).fill(GBC.HORIZONTAL));
|
---|
91 |
|
---|
92 | JComponent tab = p;
|
---|
93 | if (inScrollPane) {
|
---|
94 | JScrollPane sp = new JScrollPane(p);
|
---|
95 | tab = sp;
|
---|
96 | }
|
---|
97 | addTab(null, ImageProvider.get("preferences", icon), tab);
|
---|
98 | setToolTipTextAt(getTabCount()-1, "<html>"+desc+"</html>");
|
---|
99 | return p;
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected PluginPreference getPluginPreference() {
|
---|
103 | for (PreferenceSetting setting: settings) {
|
---|
104 | if (setting instanceof PluginPreference)
|
---|
105 | return (PluginPreference) setting;
|
---|
106 | }
|
---|
107 | return null;
|
---|
108 | }
|
---|
109 |
|
---|
110 | public void savePreferences() {
|
---|
111 |
|
---|
112 | // create a task for downloading plugins if the user has activated, yet not downloaded,
|
---|
113 | // new plugins
|
---|
114 | //
|
---|
115 | final PluginPreference preference = getPluginPreference();
|
---|
116 | final List<PluginInformation> toDownload = preference.getPluginsScheduledForUpdateOrDownload();
|
---|
117 | final PluginDownloadTask task;
|
---|
118 | if (! toDownload.isEmpty()) {
|
---|
119 | task = new PluginDownloadTask(this, toDownload, tr("Download plugins"));
|
---|
120 | } else {
|
---|
121 | task = null;
|
---|
122 | }
|
---|
123 |
|
---|
124 | // this is the task which will run *after* the plugins are downloaded
|
---|
125 | //
|
---|
126 | final Runnable continuation = new Runnable() {
|
---|
127 | public void run() {
|
---|
128 | boolean requiresRestart = false;
|
---|
129 | if (task != null && !task.isCanceled()) {
|
---|
130 | if (!task.getDownloadedPlugins().isEmpty()) {
|
---|
131 | requiresRestart = true;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | for (PreferenceSetting setting : settings) {
|
---|
136 | if (setting.ok()) {
|
---|
137 | requiresRestart = true;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | // build the messages. We only display one message, including the status
|
---|
142 | // information from the plugin download task and - if necessary - a hint
|
---|
143 | // to restart JOSM
|
---|
144 | //
|
---|
145 | StringBuffer sb = new StringBuffer();
|
---|
146 | sb.append("<html>");
|
---|
147 | if (task != null && !task.isCanceled()) {
|
---|
148 | sb.append(PluginPreference.buildDownloadSummary(task));
|
---|
149 | }
|
---|
150 | if (requiresRestart) {
|
---|
151 | sb.append(tr("You have to restart JOSM for some settings to take effect."));
|
---|
152 | }
|
---|
153 | sb.append("</html>");
|
---|
154 |
|
---|
155 | // display the message, if necessary
|
---|
156 | //
|
---|
157 | if ((task != null && !task.isCanceled()) || requiresRestart) {
|
---|
158 | JOptionPane.showMessageDialog(
|
---|
159 | Main.parent,
|
---|
160 | sb.toString(),
|
---|
161 | tr("Warning"),
|
---|
162 | JOptionPane.WARNING_MESSAGE
|
---|
163 | );
|
---|
164 | }
|
---|
165 | Main.parent.repaint();
|
---|
166 | }
|
---|
167 | };
|
---|
168 |
|
---|
169 | if (task != null) {
|
---|
170 | // if we have to launch a plugin download task we do it asynchronously, followed
|
---|
171 | // by the remaining "save preferences" activites run on the Swing EDT.
|
---|
172 | //
|
---|
173 | Main.worker.submit(task);
|
---|
174 | Main.worker.submit(
|
---|
175 | new Runnable() {
|
---|
176 | public void run() {
|
---|
177 | SwingUtilities.invokeLater(continuation);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | );
|
---|
181 | } else {
|
---|
182 | // no need for asynchronous activities. Simply run the remaining "save preference"
|
---|
183 | // activities on this thread (we are already on the Swing EDT
|
---|
184 | //
|
---|
185 | continuation.run();
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * If the dialog is closed with Ok, the preferences will be stored to the preferences-
|
---|
191 | * file, otherwise no change of the file happens.
|
---|
192 | */
|
---|
193 | public PreferenceTabbedPane() {
|
---|
194 | super(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
|
---|
195 | super.addMouseWheelListener(this);
|
---|
196 | }
|
---|
197 |
|
---|
198 | public void buildGui() {
|
---|
199 | for (PreferenceSettingFactory factory:settingsFactory) {
|
---|
200 | // logger.info("creating settings: " + factory);
|
---|
201 | PreferenceSetting setting = factory.createPreferenceSetting();
|
---|
202 | if (setting != null) {
|
---|
203 | settings.add(factory.createPreferenceSetting());
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | display.add(displaycontent, GBC.eol().fill(GBC.BOTH));
|
---|
208 | map.add(mapcontent, GBC.eol().fill(GBC.BOTH));
|
---|
209 | for (Iterator<PreferenceSetting> it = settings.iterator(); it.hasNext();) {
|
---|
210 | try {
|
---|
211 | PreferenceSetting settings = it.next();
|
---|
212 | //logger.info("adding gui: " + settings);
|
---|
213 | settings.addGui(this);
|
---|
214 | } catch (SecurityException e) {
|
---|
215 | it.remove();
|
---|
216 | } catch (Throwable e) {
|
---|
217 | /* allow to change most settings even if e.g. a plugin fails */
|
---|
218 | BugReportExceptionHandler.handleException(e);
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | public List<PreferenceSetting> getSettings() {
|
---|
224 | return settings;
|
---|
225 | }
|
---|
226 |
|
---|
227 | @SuppressWarnings("unchecked")
|
---|
228 | public <T> T getSetting(Class<? extends T> clazz) {
|
---|
229 | for (PreferenceSetting setting:settings) {
|
---|
230 | if (clazz.isAssignableFrom(setting.getClass()))
|
---|
231 | return (T)setting;
|
---|
232 | }
|
---|
233 | return null;
|
---|
234 | }
|
---|
235 |
|
---|
236 | static {
|
---|
237 | // order is important!
|
---|
238 | settingsFactory.add(new DrawingPreference.Factory());
|
---|
239 | settingsFactory.add(new ColorPreference.Factory());
|
---|
240 | settingsFactory.add(new LafPreference.Factory());
|
---|
241 | settingsFactory.add(new LanguagePreference.Factory());
|
---|
242 | settingsFactory.add(new ServerAccessPreference.Factory());
|
---|
243 | settingsFactory.add(new ProjectionPreference.Factory());
|
---|
244 | settingsFactory.add(new MapPaintPreference.Factory());
|
---|
245 | settingsFactory.add(new TaggingPresetPreference.Factory());
|
---|
246 | settingsFactory.add(new PluginPreference.Factory());
|
---|
247 | settingsFactory.add(Main.toolbar);
|
---|
248 | settingsFactory.add(new AudioPreference.Factory());
|
---|
249 | settingsFactory.add(new ShortcutPreference.Factory());
|
---|
250 |
|
---|
251 | PluginHandler.getPreferenceSetting(settingsFactory);
|
---|
252 |
|
---|
253 | // always the last: advanced tab
|
---|
254 | settingsFactory.add(new AdvancedPreference.Factory());
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * This mouse wheel listener reacts when a scroll is carried out over the
|
---|
259 | * tab strip and scrolls one tab/down or up, selecting it immediately.
|
---|
260 | */
|
---|
261 | public void mouseWheelMoved(MouseWheelEvent wev) {
|
---|
262 | // Ensure the cursor is over the tab strip
|
---|
263 | if(super.indexAtLocation(wev.getPoint().x, wev.getPoint().y) < 0)
|
---|
264 | return;
|
---|
265 |
|
---|
266 | // Get currently selected tab
|
---|
267 | int newTab = super.getSelectedIndex() + wev.getWheelRotation();
|
---|
268 |
|
---|
269 | // Ensure the new tab index is sound
|
---|
270 | newTab = newTab < 0 ? 0 : newTab;
|
---|
271 | newTab = newTab >= super.getTabCount() ? super.getTabCount() - 1 : newTab;
|
---|
272 |
|
---|
273 | // select new tab
|
---|
274 | super.setSelectedIndex(newTab);
|
---|
275 | }
|
---|
276 | }
|
---|