1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.bbox;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.Enumeration;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Objects;
|
---|
15 |
|
---|
16 | import javax.swing.AbstractButton;
|
---|
17 | import javax.swing.ButtonGroup;
|
---|
18 | import javax.swing.ButtonModel;
|
---|
19 | import javax.swing.JCheckBoxMenuItem;
|
---|
20 | import javax.swing.JPopupMenu;
|
---|
21 | import javax.swing.JRadioButtonMenuItem;
|
---|
22 | import javax.swing.JToggleButton;
|
---|
23 |
|
---|
24 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
25 | import org.openstreetmap.josm.gui.widgets.PopupMenuButton;
|
---|
26 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Button that allows to choose the imagery source used for slippy map background.
|
---|
30 | * @since 1390
|
---|
31 | */
|
---|
32 | public class SourceButton extends PopupMenuButton {
|
---|
33 | protected class TileSourceButtonModel extends JToggleButton.ToggleButtonModel implements ActionListener {
|
---|
34 | protected final TileSource tileSource;
|
---|
35 |
|
---|
36 | public TileSourceButtonModel(TileSource tileSource) {
|
---|
37 | this.tileSource = tileSource;
|
---|
38 | this.addActionListener(this);
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void actionPerformed(ActionEvent e) {
|
---|
43 | if (SourceButton.this.slippyMapBBoxChooser.getTileController().getTileSource() != this.tileSource) { // prevent infinite recursion
|
---|
44 | SourceButton.this.slippyMapBBoxChooser.toggleMapSource(this.tileSource);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected final SlippyMapBBoxChooser slippyMapBBoxChooser;
|
---|
50 | protected final ButtonModel showDownloadAreaButtonModel;
|
---|
51 | private List<TileSource> sources;
|
---|
52 | private ButtonGroup sourceButtonGroup;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Constructs a new {@code SourceButton}.
|
---|
56 | * @param slippyMapBBoxChooser parent slippy map
|
---|
57 | * @param sources list of imagery sources to display
|
---|
58 | * @param showDownloadAreaButtonModel model for the "Show downloaded area" button
|
---|
59 | * @since 12955
|
---|
60 | */
|
---|
61 | public SourceButton(
|
---|
62 | SlippyMapBBoxChooser slippyMapBBoxChooser,
|
---|
63 | Collection<TileSource> sources,
|
---|
64 | ButtonModel showDownloadAreaButtonModel
|
---|
65 | ) {
|
---|
66 | super(new ImageProvider("dialogs/layerlist").getResource().getImageIcon(new Dimension(16, 16)));
|
---|
67 | this.showDownloadAreaButtonModel = showDownloadAreaButtonModel;
|
---|
68 | this.slippyMapBBoxChooser = slippyMapBBoxChooser;
|
---|
69 | this.setPreferredSize(new Dimension(24, 24));
|
---|
70 | this.setSources(sources);
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected void generatePopupMenu() {
|
---|
74 | JPopupMenu pm = new JPopupMenu();
|
---|
75 | this.sourceButtonGroup = new ButtonGroup();
|
---|
76 | for (TileSource ts : this.sources) {
|
---|
77 | JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(ts.getName());
|
---|
78 | TileSourceButtonModel buttonModel = new TileSourceButtonModel(ts);
|
---|
79 | menuItem.setModel(buttonModel);
|
---|
80 | pm.add(menuItem);
|
---|
81 | this.sourceButtonGroup.add(menuItem);
|
---|
82 |
|
---|
83 | // attempt to initialize button group matching current state of slippyMapBBoxChooser
|
---|
84 | buttonModel.setSelected(this.slippyMapBBoxChooser.getTileController().getTileSource() == ts);
|
---|
85 | }
|
---|
86 |
|
---|
87 | pm.addSeparator();
|
---|
88 |
|
---|
89 | JCheckBoxMenuItem showDownloadAreaItem = new JCheckBoxMenuItem(tr("Show downloaded area"));
|
---|
90 | showDownloadAreaItem.setModel(this.showDownloadAreaButtonModel);
|
---|
91 | pm.add(showDownloadAreaItem);
|
---|
92 |
|
---|
93 | this.setPopupMenu(pm);
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void setSourceDefault() {
|
---|
97 | Enumeration<AbstractButton> elems = this.sourceButtonGroup.getElements();
|
---|
98 | if (elems.hasMoreElements()) {
|
---|
99 | elems.nextElement().setSelected(true);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Set the tile sources.
|
---|
105 | * @param sources The tile sources to display
|
---|
106 | * @since 6364
|
---|
107 | */
|
---|
108 | public final void setSources(Collection<TileSource> sources) {
|
---|
109 | this.sources = new ArrayList<>(Objects.requireNonNull(sources, "sources"));
|
---|
110 | this.generatePopupMenu();
|
---|
111 | if (this.sourceButtonGroup.getSelection() == null) {
|
---|
112 | this.setSourceDefault();
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Get the tile sources.
|
---|
118 | * @return unmodifiable collection of tile sources
|
---|
119 | */
|
---|
120 | public final Collection<TileSource> getSources() {
|
---|
121 | return Collections.unmodifiableCollection(this.sources);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Get the currently-selected tile source.
|
---|
126 | * @return currently-selected tile source
|
---|
127 | */
|
---|
128 | public final TileSource getCurrentSource() {
|
---|
129 | TileSourceButtonModel buttonModel = (TileSourceButtonModel) this.sourceButtonGroup.getSelection();
|
---|
130 | if (buttonModel != null) {
|
---|
131 | return buttonModel.tileSource;
|
---|
132 | }
|
---|
133 | return null;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Changes the current imagery source used for slippy map background.
|
---|
138 | * @param tileSource the new imagery source to use
|
---|
139 | */
|
---|
140 | public void setCurrentMap(TileSource tileSource) {
|
---|
141 | Enumeration<AbstractButton> elems = this.sourceButtonGroup.getElements();
|
---|
142 | while (elems.hasMoreElements()) {
|
---|
143 | AbstractButton b = elems.nextElement();
|
---|
144 | if (((TileSourceButtonModel) b.getModel()).tileSource == tileSource) {
|
---|
145 | b.setSelected(true);
|
---|
146 | return;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | // failed to find the correct one
|
---|
150 | this.setSourceDefault();
|
---|
151 | }
|
---|
152 | }
|
---|