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

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

fix #16816 - new option to show object version in lists, similar to object ID

  • Property svn:eol-style set to native
File size: 11.9 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 showVersion = new JCheckBox(tr("Show object version in selection lists"));
85    private final JCheckBox showCoor = new JCheckBox(tr("Show node coordinates in selection lists"));
86    private final JCheckBox showLocalizedName = new JCheckBox(tr("Show localized name in selection lists"));
87    private final JCheckBox modeless = new JCheckBox(tr("Modeless working (Potlatch style)"));
88    private final JCheckBox dynamicButtons = new JCheckBox(tr("Dynamic buttons in side menus"));
89    private final JCheckBox isoDates = new JCheckBox(tr("Display ISO dates"));
90    private final JCheckBox nativeFileChoosers = new JCheckBox(tr("Use native file choosers (nicer, but do not support file filters)"));
91    private final JCheckBox zoomReverseWheel = new JCheckBox(tr("Reverse zoom with mouse wheel"));
92    private final JCheckBox zoomIntermediateSteps = new JCheckBox(tr("Intermediate steps between native resolutions"));
93    private JSpinner spinZoomRatio;
94
95    @Override
96    public void addGui(PreferenceTabbedPane gui) {
97        lafCombo = new JosmComboBox<>(UIManager.getInstalledLookAndFeels());
98
99        // let's try to load additional LookAndFeels and put them into the list
100        if (PlatformManager.isPlatformOsx()) {
101            try {
102                Class<?> cquaqua = Class.forName("ch.randelshofer.quaqua.QuaquaLookAndFeel");
103                Object oquaqua = cquaqua.getConstructor((Class[]) null).newInstance((Object[]) null);
104                // no exception? Then Go!
105                lafCombo.addItem(
106                        new UIManager.LookAndFeelInfo(((LookAndFeel) oquaqua).getName(), "ch.randelshofer.quaqua.QuaquaLookAndFeel")
107                );
108            } catch (ReflectiveOperationException ex) {
109                // just debug, Quaqua may not even be installed...
110                Logging.debug(ex);
111            }
112        }
113
114        String laf = LAF.get();
115        for (int i = 0; i < lafCombo.getItemCount(); ++i) {
116            if (lafCombo.getItemAt(i).getClassName().equals(laf)) {
117                lafCombo.setSelectedIndex(i);
118                break;
119            }
120        }
121
122        lafCombo.setRenderer(new LafListCellRenderer());
123
124        panel = new VerticallyScrollablePanel(new GridBagLayout());
125        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
126
127        // Show splash screen on startup
128        showSplashScreen.setToolTipText(tr("Show splash screen at startup"));
129        showSplashScreen.setSelected(Config.getPref().getBoolean("draw.splashscreen", true));
130        panel.add(showSplashScreen, GBC.eop().insets(20, 0, 0, 0));
131
132        // Show ID in selection
133        showID.setToolTipText(tr("Show object ID in selection lists"));
134        showID.setSelected(Config.getPref().getBoolean("osm-primitives.showid", false));
135
136        // Show version in selection
137        showVersion.setToolTipText(tr("Show object version in selection lists"));
138        showVersion.setSelected(Config.getPref().getBoolean("osm-primitives.showversion", false));
139
140        // Show Coordinates in selection
141        showCoor.setToolTipText(tr("Show node coordinates in selection lists"));
142        showCoor.setSelected(Config.getPref().getBoolean("osm-primitives.showcoor", false));
143
144        // Show localized names
145        showLocalizedName.setToolTipText(tr("Show localized name in selection lists, if available"));
146        showLocalizedName.setSelected(Config.getPref().getBoolean("osm-primitives.localize-name", true));
147        ExpertToggleAction.addVisibilitySwitcher(showLocalizedName);
148
149        modeless.setToolTipText(tr("Do not require to switch modes (potlatch style workflow)"));
150        modeless.setSelected(MapFrame.MODELESS.get());
151        ExpertToggleAction.addVisibilitySwitcher(modeless);
152
153        panel.add(showID, GBC.eop().insets(20, 0, 0, 0));
154        panel.add(showVersion, GBC.eop().insets(20, 0, 0, 0));
155        panel.add(showCoor, GBC.eop().insets(20, 0, 0, 0));
156        panel.add(showLocalizedName, GBC.eop().insets(20, 0, 0, 0));
157        panel.add(modeless, GBC.eop().insets(20, 0, 0, 0));
158
159        dynamicButtons.setToolTipText(tr("Display buttons in right side menus only when mouse is inside the element"));
160        dynamicButtons.setSelected(ToggleDialog.PROP_DYNAMIC_BUTTONS.get());
161        panel.add(dynamicButtons, GBC.eop().insets(20, 0, 0, 0));
162
163        Date today = new Date();
164        isoDates.setToolTipText(tr("Format dates according to {0}. Today''s date will be displayed as {1} instead of {2}",
165                tr("ISO 8601"),
166                DateUtils.newIsoDateFormat().format(today),
167                DateFormat.getDateInstance(DateFormat.SHORT).format(today)));
168        isoDates.setSelected(DateUtils.PROP_ISO_DATES.get());
169        panel.add(isoDates, GBC.eop().insets(20, 0, 0, 0));
170
171        nativeFileChoosers.setToolTipText(
172                tr("Use file choosers that behave more like native ones. They look nicer but do not support some features like file filters"));
173        nativeFileChoosers.setSelected(FileChooserManager.PROP_USE_NATIVE_FILE_DIALOG.get());
174        panel.add(nativeFileChoosers, GBC.eop().insets(20, 0, 0, 0));
175
176        zoomReverseWheel.setToolTipText(
177                tr("Check if you feel opposite direction more convenient"));
178        zoomReverseWheel.setSelected(MapMover.PROP_ZOOM_REVERSE_WHEEL.get());
179        panel.add(zoomReverseWheel, GBC.eop().insets(20, 0, 0, 0));
180
181        zoomIntermediateSteps.setToolTipText(
182                tr("Divide intervals between native resolution levels to smaller steps if they are much larger than zoom ratio"));
183        zoomIntermediateSteps.setSelected(NavigatableComponent.PROP_ZOOM_INTERMEDIATE_STEPS.get());
184        ExpertToggleAction.addVisibilitySwitcher(zoomIntermediateSteps);
185        panel.add(zoomIntermediateSteps, GBC.eop().insets(20, 0, 0, 0));
186
187        panel.add(Box.createVerticalGlue(), GBC.eol().insets(0, 10, 0, 0));
188
189        double logZoomLevel = Math.log(2) / Math.log(NavigatableComponent.PROP_ZOOM_RATIO.get());
190        logZoomLevel = Math.max(1, logZoomLevel);
191        logZoomLevel = Math.min(5, logZoomLevel);
192        JLabel labelZoomRatio = new JLabel(tr("Zoom steps to get double scale"));
193        spinZoomRatio = new JSpinner(new SpinnerNumberModel(logZoomLevel, 1, 5, 1));
194        Component spinZoomRatioEditor = spinZoomRatio.getEditor();
195        JFormattedTextField jftf = ((JSpinner.DefaultEditor) spinZoomRatioEditor).getTextField();
196        jftf.setColumns(2);
197        String zoomRatioToolTipText = tr("Higher value means more steps needed, therefore zoom steps will be smaller");
198        spinZoomRatio.setToolTipText(zoomRatioToolTipText);
199        labelZoomRatio.setToolTipText(zoomRatioToolTipText);
200        labelZoomRatio.setLabelFor(spinZoomRatio);
201        panel.add(labelZoomRatio, GBC.std().insets(20, 0, 0, 0));
202        panel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
203        panel.add(spinZoomRatio, GBC.eol());
204
205        panel.add(new JLabel(tr("Look and Feel")), GBC.std().insets(20, 0, 0, 0));
206        panel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
207        panel.add(lafCombo, GBC.eol().fill(GBC.HORIZONTAL));
208
209        JScrollPane scrollpane = panel.getVerticalScrollPane();
210        scrollpane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
211        gui.getDisplayPreference().addSubTab(this, tr("Look and Feel"), scrollpane);
212    }
213
214    @Override
215    public boolean ok() {
216        boolean mod = false;
217        Config.getPref().putBoolean("draw.splashscreen", showSplashScreen.isSelected());
218        Config.getPref().putBoolean("osm-primitives.showid", showID.isSelected());
219        Config.getPref().putBoolean("osm-primitives.showversion", showVersion.isSelected());
220        Config.getPref().putBoolean("osm-primitives.showcoor", showCoor.isSelected());
221        Config.getPref().putBoolean("osm-primitives.localize-name", showLocalizedName.isSelected());
222        MapFrame.MODELESS.put(modeless.isSelected());
223        Config.getPref().putBoolean(ToggleDialog.PROP_DYNAMIC_BUTTONS.getKey(), dynamicButtons.isSelected());
224        Config.getPref().putBoolean(DateUtils.PROP_ISO_DATES.getKey(), isoDates.isSelected());
225        Config.getPref().putBoolean(FileChooserManager.PROP_USE_NATIVE_FILE_DIALOG.getKey(), nativeFileChoosers.isSelected());
226        MapMover.PROP_ZOOM_REVERSE_WHEEL.put(zoomReverseWheel.isSelected());
227        NavigatableComponent.PROP_ZOOM_INTERMEDIATE_STEPS.put(zoomIntermediateSteps.isSelected());
228        NavigatableComponent.PROP_ZOOM_RATIO.put(Math.pow(2, 1/(double) spinZoomRatio.getModel().getValue()));
229        mod |= LAF.put(((LookAndFeelInfo) lafCombo.getSelectedItem()).getClassName());
230        return mod;
231    }
232
233    @Override
234    public boolean isExpert() {
235        return false;
236    }
237
238    @Override
239    public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
240        return gui.getDisplayPreference();
241    }
242}
Note: See TracBrowser for help on using the repository browser.