source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/display/LafPreference.java @ 14138

Last change on this file since 14138 was 14138, checked in by Don-vip, 4 months ago

see #15229 - deprecate Main.platform and related methods - new class PlatformManager

  • Property svn:eol-style set to native
File size: 11.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.display;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagLayout;
8import java.text.DateFormat;
9import java.util.Date;
10
11import javax.swing.BorderFactory;
12import javax.swing.Box;
13import javax.swing.DefaultListCellRenderer;
14import javax.swing.JCheckBox;
15import javax.swing.JFormattedTextField;
16import javax.swing.JLabel;
17import javax.swing.JList;
18import javax.swing.JScrollPane;
19import javax.swing.JSpinner;
20import javax.swing.ListCellRenderer;
21import javax.swing.LookAndFeel;
22import javax.swing.SpinnerNumberModel;
23import javax.swing.UIManager;
24import javax.swing.UIManager.LookAndFeelInfo;
25
26import org.openstreetmap.josm.actions.ExpertToggleAction;
27import org.openstreetmap.josm.data.preferences.StringProperty;
28import org.openstreetmap.josm.gui.MapFrame;
29import org.openstreetmap.josm.gui.MapMover;
30import org.openstreetmap.josm.gui.NavigatableComponent;
31import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
32import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
33import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
34import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
35import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
36import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
37import org.openstreetmap.josm.gui.widgets.FileChooserManager;
38import org.openstreetmap.josm.gui.widgets.JosmComboBox;
39import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
40import org.openstreetmap.josm.spi.preferences.Config;
41import org.openstreetmap.josm.tools.GBC;
42import org.openstreetmap.josm.tools.Logging;
43import org.openstreetmap.josm.tools.PlatformManager;
44import org.openstreetmap.josm.tools.date.DateUtils;
45
46/**
47 * Look-and-feel preferences.
48 */
49public class LafPreference implements SubPreferenceSetting {
50
51    /**
52     * Look-and-feel property.
53     * @since 11713
54     */
55    public static final StringProperty LAF = new StringProperty("laf", PlatformManager.getPlatform().getDefaultStyle());
56
57    static final class LafListCellRenderer implements ListCellRenderer<LookAndFeelInfo> {
58        private final DefaultListCellRenderer def = new DefaultListCellRenderer();
59
60        @Override
61        public Component getListCellRendererComponent(JList<? extends LookAndFeelInfo> list, LookAndFeelInfo value,
62                int index, boolean isSelected, boolean cellHasFocus) {
63            return def.getListCellRendererComponent(list, value.getName(), index, isSelected, cellHasFocus);
64        }
65    }
66
67    /**
68     * Factory used to create a new {@code LafPreference}.
69     */
70    public static class Factory implements PreferenceSettingFactory {
71        @Override
72        public PreferenceSetting createPreferenceSetting() {
73            return new LafPreference();
74        }
75    }
76
77    /**
78     * ComboBox with all look and feels.
79     */
80    private JosmComboBox<LookAndFeelInfo> lafCombo;
81    VerticallyScrollablePanel panel;
82    private final JCheckBox showSplashScreen = new JCheckBox(tr("Show splash screen at startup"));
83    private final JCheckBox showID = new JCheckBox(tr("Show object ID in selection lists"));
84    private final JCheckBox showCoor = new JCheckBox(tr("Show node coordinates in selection lists"));
85    private final JCheckBox showLocalizedName = new JCheckBox(tr("Show localized name in selection lists"));
86    private final JCheckBox modeless = new JCheckBox(tr("Modeless working (Potlatch style)"));
87    private final JCheckBox dynamicButtons = new JCheckBox(tr("Dynamic buttons in side menus"));
88    private final JCheckBox isoDates = new JCheckBox(tr("Display ISO dates"));
89    private final JCheckBox nativeFileChoosers = new JCheckBox(tr("Use native file choosers (nicer, but do not support file filters)"));
90    private final JCheckBox zoomReverseWheel = new JCheckBox(tr("Reverse zoom with mouse wheel"));
91    private final JCheckBox zoomIntermediateSteps = new JCheckBox(tr("Intermediate steps between native resolutions"));
92    private JSpinner spinZoomRatio;
93
94    @Override
95    public void addGui(PreferenceTabbedPane gui) {
96        lafCombo = new JosmComboBox<>(UIManager.getInstalledLookAndFeels());
97
98        // let's try to load additional LookAndFeels and put them into the list
99        if (PlatformManager.isPlatformOsx()) {
100            try {
101                Class<?> cquaqua = Class.forName("ch.randelshofer.quaqua.QuaquaLookAndFeel");
102                Object oquaqua = cquaqua.getConstructor((Class[]) null).newInstance((Object[]) null);
103                // no exception? Then Go!
104                lafCombo.addItem(
105                        new UIManager.LookAndFeelInfo(((LookAndFeel) oquaqua).getName(), "ch.randelshofer.quaqua.QuaquaLookAndFeel")
106                );
107            } catch (ReflectiveOperationException ex) {
108                // just debug, Quaqua may not even be installed...
109                Logging.debug(ex);
110            }
111        }
112
113        String laf = LAF.get();
114        for (int i = 0; i < lafCombo.getItemCount(); ++i) {
115            if (lafCombo.getItemAt(i).getClassName().equals(laf)) {
116                lafCombo.setSelectedIndex(i);
117                break;
118            }
119        }
120
121        lafCombo.setRenderer(new LafListCellRenderer());
122
123        panel = new VerticallyScrollablePanel(new GridBagLayout());
124        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
125
126        // Show splash screen on startup
127        showSplashScreen.setToolTipText(tr("Show splash screen at startup"));
128        showSplashScreen.setSelected(Config.getPref().getBoolean("draw.splashscreen", true));
129        panel.add(showSplashScreen, GBC.eop().insets(20, 0, 0, 0));
130
131        // Show ID in selection
132        showID.setToolTipText(tr("Show object ID in selection lists"));
133        showID.setSelected(Config.getPref().getBoolean("osm-primitives.showid", false));
134
135        // Show Coordinates in selection
136        showCoor.setToolTipText(tr("Show node coordinates in selection lists"));
137        showCoor.setSelected(Config.getPref().getBoolean("osm-primitives.showcoor", false));
138
139        // Show localized names
140        showLocalizedName.setToolTipText(tr("Show localized name in selection lists, if available"));
141        showLocalizedName.setSelected(Config.getPref().getBoolean("osm-primitives.localize-name", true));
142        ExpertToggleAction.addVisibilitySwitcher(showLocalizedName);
143
144        modeless.setToolTipText(tr("Do not require to switch modes (potlatch style workflow)"));
145        modeless.setSelected(MapFrame.MODELESS.get());
146        ExpertToggleAction.addVisibilitySwitcher(modeless);
147
148        panel.add(showID, GBC.eop().insets(20, 0, 0, 0));
149        panel.add(showCoor, GBC.eop().insets(20, 0, 0, 0));
150        panel.add(showLocalizedName, GBC.eop().insets(20, 0, 0, 0));
151        panel.add(modeless, GBC.eop().insets(20, 0, 0, 0));
152
153        dynamicButtons.setToolTipText(tr("Display buttons in right side menus only when mouse is inside the element"));
154        dynamicButtons.setSelected(ToggleDialog.PROP_DYNAMIC_BUTTONS.get());
155        panel.add(dynamicButtons, GBC.eop().insets(20, 0, 0, 0));
156
157        Date today = new Date();
158        isoDates.setToolTipText(tr("Format dates according to {0}. Today''s date will be displayed as {1} instead of {2}",
159                tr("ISO 8601"),
160                DateUtils.newIsoDateFormat().format(today),
161                DateFormat.getDateInstance(DateFormat.SHORT).format(today)));
162        isoDates.setSelected(DateUtils.PROP_ISO_DATES.get());
163        panel.add(isoDates, GBC.eop().insets(20, 0, 0, 0));
164
165        nativeFileChoosers.setToolTipText(
166                tr("Use file choosers that behave more like native ones. They look nicer but do not support some features like file filters"));
167        nativeFileChoosers.setSelected(FileChooserManager.PROP_USE_NATIVE_FILE_DIALOG.get());
168        panel.add(nativeFileChoosers, GBC.eop().insets(20, 0, 0, 0));
169
170        zoomReverseWheel.setToolTipText(
171                tr("Check if you feel opposite direction more convenient"));
172        zoomReverseWheel.setSelected(MapMover.PROP_ZOOM_REVERSE_WHEEL.get());
173        panel.add(zoomReverseWheel, GBC.eop().insets(20, 0, 0, 0));
174
175        zoomIntermediateSteps.setToolTipText(
176                tr("Divide intervals between native resolution levels to smaller steps if they are much larger than zoom ratio"));
177        zoomIntermediateSteps.setSelected(NavigatableComponent.PROP_ZOOM_INTERMEDIATE_STEPS.get());
178        ExpertToggleAction.addVisibilitySwitcher(zoomIntermediateSteps);
179        panel.add(zoomIntermediateSteps, GBC.eop().insets(20, 0, 0, 0));
180
181        panel.add(Box.createVerticalGlue(), GBC.eol().insets(0, 10, 0, 0));
182
183        double logZoomLevel = Math.log(2) / Math.log(NavigatableComponent.PROP_ZOOM_RATIO.get());
184        logZoomLevel = Math.max(1, logZoomLevel);
185        logZoomLevel = Math.min(5, logZoomLevel);
186        JLabel labelZoomRatio = new JLabel(tr("Zoom steps to get double scale"));
187        spinZoomRatio = new JSpinner(new SpinnerNumberModel(logZoomLevel, 1, 5, 1));
188        Component spinZoomRatioEditor = spinZoomRatio.getEditor();
189        JFormattedTextField jftf = ((JSpinner.DefaultEditor) spinZoomRatioEditor).getTextField();
190        jftf.setColumns(2);
191        String zoomRatioToolTipText = tr("Higher value means more steps needed, therefore zoom steps will be smaller");
192        spinZoomRatio.setToolTipText(zoomRatioToolTipText);
193        labelZoomRatio.setToolTipText(zoomRatioToolTipText);
194        labelZoomRatio.setLabelFor(spinZoomRatio);
195        panel.add(labelZoomRatio, GBC.std().insets(20, 0, 0, 0));
196        panel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
197        panel.add(spinZoomRatio, GBC.eol());
198
199        panel.add(new JLabel(tr("Look and Feel")), GBC.std().insets(20, 0, 0, 0));
200        panel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
201        panel.add(lafCombo, GBC.eol().fill(GBC.HORIZONTAL));
202
203        JScrollPane scrollpane = panel.getVerticalScrollPane();
204        scrollpane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
205        gui.getDisplayPreference().addSubTab(this, tr("Look and Feel"), scrollpane);
206    }
207
208    @Override
209    public boolean ok() {
210        boolean mod = false;
211        Config.getPref().putBoolean("draw.splashscreen", showSplashScreen.isSelected());
212        Config.getPref().putBoolean("osm-primitives.showid", showID.isSelected());
213        Config.getPref().putBoolean("osm-primitives.showcoor", showCoor.isSelected());
214        Config.getPref().putBoolean("osm-primitives.localize-name", showLocalizedName.isSelected());
215        MapFrame.MODELESS.put(modeless.isSelected());
216        Config.getPref().putBoolean(ToggleDialog.PROP_DYNAMIC_BUTTONS.getKey(), dynamicButtons.isSelected());
217        Config.getPref().putBoolean(DateUtils.PROP_ISO_DATES.getKey(), isoDates.isSelected());
218        Config.getPref().putBoolean(FileChooserManager.PROP_USE_NATIVE_FILE_DIALOG.getKey(), nativeFileChoosers.isSelected());
219        MapMover.PROP_ZOOM_REVERSE_WHEEL.put(zoomReverseWheel.isSelected());
220        NavigatableComponent.PROP_ZOOM_INTERMEDIATE_STEPS.put(zoomIntermediateSteps.isSelected());
221        NavigatableComponent.PROP_ZOOM_RATIO.put(Math.pow(2, 1/(double) spinZoomRatio.getModel().getValue()));
222        mod |= LAF.put(((LookAndFeelInfo) lafCombo.getSelectedItem()).getClassName());
223        return mod;
224    }
225
226    @Override
227    public boolean isExpert() {
228        return false;
229    }
230
231    @Override
232    public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
233        return gui.getDisplayPreference();
234    }
235}
Note: See TracBrowser for help on using the repository browser.