001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.display;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GridBagLayout;
007
008import javax.swing.BorderFactory;
009import javax.swing.Box;
010import javax.swing.JCheckBox;
011import javax.swing.JLabel;
012import javax.swing.JPanel;
013
014import org.openstreetmap.josm.actions.ExpertToggleAction;
015import org.openstreetmap.josm.data.preferences.BooleanProperty;
016import org.openstreetmap.josm.gui.autofilter.AutoFilterManager;
017import org.openstreetmap.josm.gui.autofilter.AutoFilterRule;
018import org.openstreetmap.josm.gui.help.HelpUtil;
019import org.openstreetmap.josm.gui.layer.OsmDataLayer;
020import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
021import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
022import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
023import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
024import org.openstreetmap.josm.gui.widgets.JosmComboBox;
025import org.openstreetmap.josm.spi.preferences.Config;
026import org.openstreetmap.josm.tools.GBC;
027
028/**
029 * "OSM Data" drawing preferences.
030 */
031public class DrawingPreference extends DefaultTabPreferenceSetting {
032
033    /**
034     * Factory used to create a new {@code DrawingPreference}.
035     */
036    public static class Factory implements PreferenceSettingFactory {
037        @Override
038        public PreferenceSetting createPreferenceSetting() {
039            return new DrawingPreference();
040        }
041    }
042
043    /**
044     * Property controlling whether to draw boundaries of downloaded data
045     * @since 14648
046     */
047    public static final BooleanProperty SOURCE_BOUNDS_PROP = new BooleanProperty("draw.data.downloaded_area", true);
048
049    private final JCheckBox directionHint = new JCheckBox(tr("Draw Direction Arrows"));
050    private final JCheckBox headArrow = new JCheckBox(tr("Only on the head of a way."));
051    private final JCheckBox onewayArrow = new JCheckBox(tr("Draw oneway arrows."));
052    private final JCheckBox segmentOrderNumber = new JCheckBox(tr("Draw segment order numbers"));
053    private final JCheckBox segmentOrderNumberOnSelectedWay = new JCheckBox(tr("Draw segment order numbers on selected way"));
054    private final JCheckBox sourceBounds = new JCheckBox(tr("Draw boundaries of downloaded data"));
055    private final JCheckBox virtualNodes = new JCheckBox(tr("Draw virtual nodes in select mode"));
056    private final JCheckBox inactive = new JCheckBox(tr("Draw inactive layers in other color"));
057    private final JCheckBox discardableKeys = new JCheckBox(tr("Display discardable keys"));
058    private final JCheckBox autoFilters = new JCheckBox(tr("Use auto filters"));
059    private final JCheckBox autoFiltersHiding = new JCheckBox(tr("Hide auto-filtered elements"));
060
061    private final JLabel lblRule = new JLabel(tr("Rule"));
062    private final JosmComboBox<AutoFilterRule> autoFilterRules = new JosmComboBox<>(
063            AutoFilterManager.getInstance().getAutoFilterRules().toArray(new AutoFilterRule[] {}));
064
065    // Options that affect performance
066    private final JCheckBox useHighlighting = new JCheckBox(tr("Highlight target ways and nodes"));
067    private final JCheckBox drawHelperLine = new JCheckBox(tr("Draw rubber-band helper line"));
068    private final JCheckBox useAntialiasing = new JCheckBox(tr("Smooth map graphics (antialiasing)"));
069    private final JCheckBox useWireframeAntialiasing = new JCheckBox(tr("Smooth map graphics in wireframe mode (antialiasing)"));
070    private final JCheckBox outlineOnly = new JCheckBox(tr("Draw only outlines of areas"));
071    private final JCheckBox hideLabelsWhileDragging = new JCheckBox(tr("Hide labels while dragging the map"));
072
073    DrawingPreference() {
074        super("layer/osmdata_small", tr("OSM Data"), tr("Settings that control the drawing of OSM data."));
075    }
076
077    @Override
078    public void addGui(PreferenceTabbedPane gui) {
079        JPanel panel = new JPanel(new GridBagLayout());
080        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
081
082        // directionHint
083        directionHint.addActionListener(e -> {
084            if (directionHint.isSelected()) {
085                headArrow.setSelected(Config.getPref().getBoolean("draw.segment.head_only", false));
086            } else {
087                headArrow.setSelected(false);
088            }
089            headArrow.setEnabled(directionHint.isSelected());
090        });
091        directionHint.setToolTipText(tr("Draw direction hints for way segments."));
092        directionHint.setSelected(Config.getPref().getBoolean("draw.segment.direction", false));
093
094        // only on the head of a way
095        headArrow.setToolTipText(tr("Only on the head of a way."));
096        headArrow.setSelected(Config.getPref().getBoolean("draw.segment.head_only", false));
097        headArrow.setEnabled(directionHint.isSelected());
098
099        // draw oneway arrows
100        onewayArrow.setToolTipText(tr("Draw arrows in the direction of oneways and other directed features."));
101        onewayArrow.setSelected(Config.getPref().getBoolean("draw.oneway", true));
102
103        // segment order number
104        segmentOrderNumber.setToolTipText(tr("Draw the order numbers of all segments within their way."));
105        segmentOrderNumber.setSelected(Config.getPref().getBoolean("draw.segment.order_number", false));
106        segmentOrderNumberOnSelectedWay.setToolTipText(tr("Draw the order numbers of all segments within their way."));
107        segmentOrderNumberOnSelectedWay.setSelected(Config.getPref().getBoolean("draw.segment.order_number.on_selected", false));
108
109        // downloaded area
110        sourceBounds.setToolTipText(tr("Draw the boundaries of data loaded from the server."));
111        sourceBounds.setSelected(SOURCE_BOUNDS_PROP.get());
112
113        // virtual nodes
114        virtualNodes.setToolTipText(tr("Draw virtual nodes in select mode for easy way modification."));
115        virtualNodes.setSelected(Config.getPref().getInt("mappaint.node.virtual-size", 8) != 0);
116
117        // background layers in inactive color
118        inactive.setToolTipText(tr("Draw the inactive data layers in a different color."));
119        inactive.setSelected(Config.getPref().getBoolean("draw.data.inactive_color", true));
120
121        // antialiasing
122        useAntialiasing.setToolTipText(tr("Apply antialiasing to the map view resulting in a smoother appearance."));
123        useAntialiasing.setSelected(Config.getPref().getBoolean("mappaint.use-antialiasing", true));
124
125        // wireframe mode antialiasing
126        useWireframeAntialiasing.setToolTipText(tr("Apply antialiasing to the map view in wireframe mode resulting in a smoother appearance."));
127        useWireframeAntialiasing.setSelected(Config.getPref().getBoolean("mappaint.wireframe.use-antialiasing", false));
128
129        // highlighting
130        useHighlighting.setToolTipText(tr("Highlight target nodes and ways while drawing or selecting"));
131        useHighlighting.setSelected(Config.getPref().getBoolean("draw.target-highlight", true));
132
133        drawHelperLine.setToolTipText(tr("Draw rubber-band helper line"));
134        drawHelperLine.setSelected(Config.getPref().getBoolean("draw.helper-line", true));
135
136        // outlineOnly
137        outlineOnly.setToolTipText(tr("This option suppresses the filling of areas, overriding anything specified in the selected style."));
138        outlineOnly.setSelected(Config.getPref().getBoolean("draw.data.area_outline_only", false));
139
140        // hideLabelsWhileDragging
141        hideLabelsWhileDragging.setToolTipText(tr("This option hides the textual labels of OSM objects while dragging the map."));
142        hideLabelsWhileDragging.setSelected(OsmDataLayer.PROPERTY_HIDE_LABELS_WHILE_DRAGGING.get());
143
144        // discardable keys
145        discardableKeys.setToolTipText(tr("Display keys which have been deemed uninteresting to the point that they can be silently removed."));
146        discardableKeys.setSelected(Config.getPref().getBoolean("display.discardable-keys", false));
147
148        // auto filters
149        autoFilters.setToolTipText(tr("Display buttons to automatically filter numeric values of a predefined tag"));
150        autoFilters.setSelected(AutoFilterManager.PROP_AUTO_FILTER_ENABLED.get());
151        autoFilters.addActionListener(e -> {
152            lblRule.setEnabled(autoFilters.isSelected());
153            autoFilterRules.setEnabled(autoFilters.isSelected());
154            autoFiltersHiding.setEnabled(autoFilters.isSelected());
155        });
156        autoFilterRules.setToolTipText("Rule defining which tag will provide automatic filters, below a certain zoom level");
157        autoFilterRules.setSelectedItem(AutoFilterManager.getInstance().getAutoFilterRule(AutoFilterManager.PROP_AUTO_FILTER_RULE.get()));
158        autoFiltersHiding.setToolTipText(
159            tr("Completely hide elements which do not match the automatic filter instead of merely disabling them"));
160        autoFiltersHiding.setSelected(AutoFilterManager.PROP_AUTO_FILTER_HIDING.get());
161
162        JLabel performanceLabel = new JLabel(tr("Options that affect drawing performance"));
163
164        panel.add(new JLabel(tr("Segment drawing options")),
165                GBC.eop().insets(5, 10, 0, 0));
166        panel.add(directionHint, GBC.eop().insets(20, 0, 0, 0));
167        panel.add(headArrow, GBC.eop().insets(40, 0, 0, 0));
168        panel.add(onewayArrow, GBC.eop().insets(20, 0, 0, 0));
169        panel.add(segmentOrderNumber, GBC.eop().insets(20, 0, 0, 0));
170        panel.add(segmentOrderNumberOnSelectedWay, GBC.eop().insets(20, 0, 0, 0));
171
172        panel.add(new JLabel(tr("Select and draw mode options")),
173                GBC.eop().insets(5, 10, 0, 0));
174        panel.add(virtualNodes, GBC.eop().insets(20, 0, 0, 0));
175        panel.add(drawHelperLine, GBC.eop().insets(20, 0, 0, 0));
176
177        panel.add(performanceLabel, GBC.eop().insets(5, 10, 0, 0));
178        panel.add(useAntialiasing, GBC.eop().insets(20, 0, 0, 0));
179        panel.add(useWireframeAntialiasing, GBC.eop().insets(20, 0, 0, 0));
180        panel.add(useHighlighting, GBC.eop().insets(20, 0, 0, 0));
181        panel.add(outlineOnly, GBC.eol().insets(20, 0, 0, 0));
182        panel.add(hideLabelsWhileDragging, GBC.eol().insets(20, 0, 0, 0));
183
184        panel.add(new JLabel(tr("Other options")),
185                GBC.eop().insets(5, 10, 0, 0));
186        panel.add(sourceBounds, GBC.eop().insets(20, 0, 0, 0));
187        panel.add(inactive, GBC.eop().insets(20, 0, 0, 0));
188        panel.add(discardableKeys, GBC.eop().insets(20, 0, 0, 0));
189        panel.add(autoFilters, GBC.eop().insets(20, 0, 0, 0));
190        panel.add(lblRule, GBC.std().insets(40, 0, 0, 0));
191        panel.add(autoFilterRules, GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0, 0, 0));
192        panel.add(autoFiltersHiding, GBC.eop().insets(40, 0, 0, 0));
193
194        ExpertToggleAction.addVisibilitySwitcher(performanceLabel);
195        ExpertToggleAction.addVisibilitySwitcher(useAntialiasing);
196        ExpertToggleAction.addVisibilitySwitcher(useWireframeAntialiasing);
197        ExpertToggleAction.addVisibilitySwitcher(useHighlighting);
198        ExpertToggleAction.addVisibilitySwitcher(outlineOnly);
199        ExpertToggleAction.addVisibilitySwitcher(hideLabelsWhileDragging);
200        ExpertToggleAction.addVisibilitySwitcher(discardableKeys);
201
202        panel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
203        createPreferenceTabWithScrollPane(gui, panel);
204    }
205
206    @Override
207    public boolean ok() {
208        OsmDataLayer.PROPERTY_HIDE_LABELS_WHILE_DRAGGING.put(hideLabelsWhileDragging.isSelected());
209        Config.getPref().putBoolean("draw.data.area_outline_only", outlineOnly.isSelected());
210        Config.getPref().putBoolean("draw.segment.direction", directionHint.isSelected());
211        Config.getPref().putBoolean("draw.segment.head_only", headArrow.isSelected());
212        Config.getPref().putBoolean("draw.oneway", onewayArrow.isSelected());
213        Config.getPref().putBoolean("draw.segment.order_number", segmentOrderNumber.isSelected());
214        Config.getPref().putBoolean("draw.segment.order_number.on_selected", segmentOrderNumberOnSelectedWay.isSelected());
215        SOURCE_BOUNDS_PROP.put(sourceBounds.isSelected());
216        Config.getPref().putBoolean("draw.data.inactive_color", inactive.isSelected());
217        Config.getPref().putBoolean("mappaint.use-antialiasing", useAntialiasing.isSelected());
218        Config.getPref().putBoolean("mappaint.wireframe.use-antialiasing", useWireframeAntialiasing.isSelected());
219        Config.getPref().putBoolean("draw.target-highlight", useHighlighting.isSelected());
220        Config.getPref().putBoolean("draw.helper-line", drawHelperLine.isSelected());
221        Config.getPref().putBoolean("display.discardable-keys", discardableKeys.isSelected());
222        AutoFilterManager.PROP_AUTO_FILTER_ENABLED.put(autoFilters.isSelected());
223        AutoFilterManager.PROP_AUTO_FILTER_HIDING.put(autoFiltersHiding.isSelected());
224        AutoFilterManager.PROP_AUTO_FILTER_RULE.put(((AutoFilterRule) autoFilterRules.getSelectedItem()).getKey());
225        int vn = Config.getPref().getInt("mappaint.node.virtual-size", 8);
226        if (virtualNodes.isSelected()) {
227            if (vn < 1) {
228                vn = 8;
229            }
230        } else {
231            vn = 0;
232        }
233        Config.getPref().putInt("mappaint.node.virtual-size", vn);
234        return false;
235    }
236
237    @Override
238    public boolean isExpert() {
239        return false;
240    }
241
242    @Override
243    public String getHelpContext() {
244        return HelpUtil.ht("/Preferences/DrawingPreference");
245    }
246}