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;
007import java.awt.event.ActionEvent;
008import java.awt.event.ActionListener;
009
010import javax.swing.BorderFactory;
011import javax.swing.Box;
012import javax.swing.JCheckBox;
013import javax.swing.JLabel;
014import javax.swing.JPanel;
015import javax.swing.JScrollPane;
016
017import org.openstreetmap.josm.Main;
018import org.openstreetmap.josm.actions.ExpertToggleAction;
019import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
020import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
021import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
022import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
023import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
024import org.openstreetmap.josm.gui.util.GuiHelper;
025import org.openstreetmap.josm.tools.GBC;
026
027/**
028 * Map drawing preferences.
029 */
030public class DrawingPreference implements SubPreferenceSetting {
031
032    /**
033     * Factory used to create a new {@code DrawingPreference}.
034     */
035    public static class Factory implements PreferenceSettingFactory {
036        @Override
037        public PreferenceSetting createPreferenceSetting() {
038            return new DrawingPreference();
039        }
040    }
041
042    private GPXSettingsPanel gpxPanel;
043    private final JCheckBox directionHint = new JCheckBox(tr("Draw Direction Arrows"));
044    private final JCheckBox headArrow = new JCheckBox(tr("Only on the head of a way."));
045    private final JCheckBox onewayArrow = new JCheckBox(tr("Draw oneway arrows."));
046    private final JCheckBox segmentOrderNumber = new JCheckBox(tr("Draw segment order numbers"));
047    private final JCheckBox sourceBounds = new JCheckBox(tr("Draw boundaries of downloaded data"));
048    private final JCheckBox virtualNodes = new JCheckBox(tr("Draw virtual nodes in select mode"));
049    private final JCheckBox inactive = new JCheckBox(tr("Draw inactive layers in other color"));
050    private final JCheckBox discardableKeys = new JCheckBox(tr("Display discardable keys"));
051
052    // Options that affect performance
053    private final JCheckBox useHighlighting = new JCheckBox(tr("Highlight target ways and nodes"));
054    private final JCheckBox drawHelperLine = new JCheckBox(tr("Draw rubber-band helper line"));
055    private final JCheckBox useAntialiasing = new JCheckBox(tr("Smooth map graphics (antialiasing)"));
056    private final JCheckBox useWireframeAntialiasing = new JCheckBox(tr("Smooth map graphics in wireframe mode (antialiasing)"));
057    private final JCheckBox outlineOnly = new JCheckBox(tr("Draw only outlines of areas"));
058
059    @Override
060    public void addGui(PreferenceTabbedPane gui) {
061        gpxPanel = new GPXSettingsPanel();
062        gui.addValidationListener(gpxPanel);
063        JPanel panel = gpxPanel;
064
065        JScrollPane scrollpane = new JScrollPane(panel);
066        scrollpane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
067        GuiHelper.setDefaultIncrement(scrollpane);
068        gui.getDisplayPreference().addSubTab(this, tr("GPS Points"), scrollpane);
069        panel = new JPanel(new GridBagLayout());
070        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
071
072        // directionHint
073        directionHint.addActionListener(new ActionListener() {
074            @Override
075            public void actionPerformed(ActionEvent e) {
076                if (directionHint.isSelected()) {
077                    headArrow.setSelected(Main.pref.getBoolean("draw.segment.head_only", false));
078                } else {
079                    headArrow.setSelected(false);
080                }
081                headArrow.setEnabled(directionHint.isSelected());
082            }
083        });
084        directionHint.setToolTipText(tr("Draw direction hints for way segments."));
085        directionHint.setSelected(Main.pref.getBoolean("draw.segment.direction", false));
086
087        // only on the head of a way
088        headArrow.setToolTipText(tr("Only on the head of a way."));
089        headArrow.setSelected(Main.pref.getBoolean("draw.segment.head_only", false));
090        headArrow.setEnabled(directionHint.isSelected());
091
092        // draw oneway arrows
093        onewayArrow.setToolTipText(tr("Draw arrows in the direction of oneways and other directed features."));
094        onewayArrow.setSelected(Main.pref.getBoolean("draw.oneway", true));
095
096        // segment order number
097        segmentOrderNumber.setToolTipText(tr("Draw the order numbers of all segments within their way."));
098        segmentOrderNumber.setSelected(Main.pref.getBoolean("draw.segment.order_number", false));
099
100        // downloaded area
101        sourceBounds.setToolTipText(tr("Draw the boundaries of data loaded from the server."));
102        sourceBounds.setSelected(Main.pref.getBoolean("draw.data.downloaded_area", true));
103
104        // virtual nodes
105        virtualNodes.setToolTipText(tr("Draw virtual nodes in select mode for easy way modification."));
106        virtualNodes.setSelected(Main.pref.getInteger("mappaint.node.virtual-size", 8) != 0);
107
108        // background layers in inactive color
109        inactive.setToolTipText(tr("Draw the inactive data layers in a different color."));
110        inactive.setSelected(Main.pref.getBoolean("draw.data.inactive_color", true));
111
112        // antialiasing
113        useAntialiasing.setToolTipText(tr("Apply antialiasing to the map view resulting in a smoother appearance."));
114        useAntialiasing.setSelected(Main.pref.getBoolean("mappaint.use-antialiasing", true));
115
116        // wireframe mode antialiasing
117        useWireframeAntialiasing.setToolTipText(tr("Apply antialiasing to the map view in wireframe mode resulting in a smoother appearance."));
118        useWireframeAntialiasing.setSelected(Main.pref.getBoolean("mappaint.wireframe.use-antialiasing", false));
119
120        // highlighting
121        useHighlighting.setToolTipText(tr("Hightlight target nodes and ways while drawing or selecting"));
122        useHighlighting.setSelected(Main.pref.getBoolean("draw.target-highlight", true));
123
124        drawHelperLine.setToolTipText(tr("Draw rubber-band helper line"));
125        drawHelperLine.setSelected(Main.pref.getBoolean("draw.helper-line", true));
126
127        // outlineOnly
128        outlineOnly.setToolTipText(tr("This option suppresses the filling of areas, overriding anything specified in the selected style."));
129        outlineOnly.setSelected(Main.pref.getBoolean("draw.data.area_outline_only", false));
130
131        // discardable keys
132        discardableKeys.setToolTipText(tr("Display keys which have been deemed uninteresting to the point that they can be silently removed."));
133        discardableKeys.setSelected(Main.pref.getBoolean("display.discardable-keys", false));
134
135        JLabel performanceLabel = new JLabel(tr("Options that affect drawing performance"));
136
137        panel.add(new JLabel(tr("Segment drawing options")),
138                GBC.eop().insets(5, 10, 0, 0));
139        panel.add(directionHint, GBC.eop().insets(20, 0, 0, 0));
140        panel.add(headArrow, GBC.eop().insets(40, 0, 0, 0));
141        panel.add(onewayArrow, GBC.eop().insets(20, 0, 0, 0));
142        panel.add(segmentOrderNumber, GBC.eop().insets(20, 0, 0, 0));
143
144        panel.add(new JLabel(tr("Select and draw mode options")),
145                GBC.eop().insets(5, 10, 0, 0));
146        panel.add(virtualNodes, GBC.eop().insets(20, 0, 0, 0));
147        panel.add(drawHelperLine, GBC.eop().insets(20, 0, 0, 0));
148
149        panel.add(performanceLabel, GBC.eop().insets(5, 10, 0, 0));
150        panel.add(useAntialiasing, GBC.eop().insets(20, 0, 0, 0));
151        panel.add(useWireframeAntialiasing, GBC.eop().insets(20, 0, 0, 0));
152        panel.add(useHighlighting, GBC.eop().insets(20, 0, 0, 0));
153        panel.add(outlineOnly, GBC.eol().insets(20, 0, 0, 0));
154
155        panel.add(new JLabel(tr("Other options")),
156                GBC.eop().insets(5, 10, 0, 0));
157        panel.add(sourceBounds, GBC.eop().insets(20, 0, 0, 0));
158        panel.add(inactive, GBC.eop().insets(20, 0, 0, 0));
159        panel.add(discardableKeys, GBC.eop().insets(20, 0, 0, 0));
160
161        ExpertToggleAction.addVisibilitySwitcher(performanceLabel);
162        ExpertToggleAction.addVisibilitySwitcher(useAntialiasing);
163        ExpertToggleAction.addVisibilitySwitcher(useWireframeAntialiasing);
164        ExpertToggleAction.addVisibilitySwitcher(useHighlighting);
165        ExpertToggleAction.addVisibilitySwitcher(outlineOnly);
166        ExpertToggleAction.addVisibilitySwitcher(discardableKeys);
167
168        panel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
169        scrollpane = new JScrollPane(panel);
170        scrollpane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
171        GuiHelper.setDefaultIncrement(scrollpane);
172        gui.getDisplayPreference().addSubTab(this, tr("OSM Data"), scrollpane);
173    }
174
175    @Override
176    public boolean ok() {
177        boolean restart = gpxPanel.savePreferences();
178        Main.pref.put("draw.data.area_outline_only", outlineOnly.isSelected());
179        Main.pref.put("draw.segment.direction", directionHint.isSelected());
180        Main.pref.put("draw.segment.head_only", headArrow.isSelected());
181        Main.pref.put("draw.oneway", onewayArrow.isSelected());
182        Main.pref.put("draw.segment.order_number", segmentOrderNumber.isSelected());
183        Main.pref.put("draw.data.downloaded_area", sourceBounds.isSelected());
184        Main.pref.put("draw.data.inactive_color", inactive.isSelected());
185        Main.pref.put("mappaint.use-antialiasing", useAntialiasing.isSelected());
186        Main.pref.put("mappaint.wireframe.use-antialiasing", useWireframeAntialiasing.isSelected());
187        Main.pref.put("draw.target-highlight", useHighlighting.isSelected());
188        Main.pref.put("draw.helper-line", drawHelperLine.isSelected());
189        Main.pref.put("display.discardable-keys", discardableKeys.isSelected());
190        int vn = Main.pref.getInteger("mappaint.node.virtual-size", 8);
191        if (virtualNodes.isSelected()) {
192            if (vn < 1) {
193                vn = 8;
194            }
195        } else {
196            vn = 0;
197        }
198        Main.pref.putInteger("mappaint.node.virtual-size", vn);
199        return restart;
200    }
201
202    @Override
203    public boolean isExpert() {
204        return false;
205    }
206
207    @Override
208    public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
209        return gui.getDisplayPreference();
210    }
211}