1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.ButtonModel;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.visitor.paint.MapRendererFactory;
|
---|
15 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.paint.WireframeMapRenderer;
|
---|
17 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
18 |
|
---|
19 | public class WireframeToggleAction extends JosmAction {
|
---|
20 | private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>();
|
---|
21 | //FIXME: replace with property Action.SELECTED_KEY when migrating to
|
---|
22 | // Java 6
|
---|
23 | private boolean selected;
|
---|
24 | public WireframeToggleAction() {
|
---|
25 | super(
|
---|
26 | tr("Wireframe View"),
|
---|
27 | null, /* no icon */
|
---|
28 | tr("Enable/disable rendering the map as wireframe only"),
|
---|
29 | Shortcut.registerShortcut("menu:view:wireframe", tr("Toggle Wireframe view"),KeyEvent.VK_W, Shortcut.CTRL),
|
---|
30 | false /* register toolbar */
|
---|
31 | );
|
---|
32 | putValue("toolbar", "wireframe");
|
---|
33 | Main.toolbar.register(this);
|
---|
34 | selected = MapRendererFactory.getInstance().isWireframeMapRendererActive();
|
---|
35 | notifySelectedState();
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void addButtonModel(ButtonModel model) {
|
---|
39 | if (model != null && !buttonModels.contains(model)) {
|
---|
40 | buttonModels.add(model);
|
---|
41 | model.setSelected(selected);
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public void removeButtonModel(ButtonModel model) {
|
---|
46 | if (model != null && buttonModels.contains(model)) {
|
---|
47 | buttonModels.remove(model);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected void notifySelectedState() {
|
---|
52 | for (ButtonModel model: buttonModels) {
|
---|
53 | if (model.isSelected() != selected) {
|
---|
54 | model.setSelected(selected);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected void toggleSelectedState() {
|
---|
60 | selected = !selected;
|
---|
61 | //Main.pref.put("draw.wireframe", selected);
|
---|
62 | if (selected){
|
---|
63 | MapRendererFactory.getInstance().activate(WireframeMapRenderer.class);
|
---|
64 | } else {
|
---|
65 | MapRendererFactory.getInstance().activate(StyledMapRenderer.class);
|
---|
66 | }
|
---|
67 |
|
---|
68 | notifySelectedState();
|
---|
69 | if (Main.isDisplayingMapView()) {
|
---|
70 | Main.map.mapView.repaint();
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void actionPerformed(ActionEvent e) {
|
---|
76 | toggleSelectedState();
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | protected void updateEnabledState() {
|
---|
81 | setEnabled(Main.isDisplayingMapView() && Main.main.getEditLayer() != null);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public boolean isSelected() {
|
---|
85 | return selected;
|
---|
86 | }
|
---|
87 | }
|
---|