1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.projection;
|
---|
3 |
|
---|
4 | import java.awt.GridBagLayout;
|
---|
5 | import java.awt.event.ActionListener;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.Collections;
|
---|
8 |
|
---|
9 | import javax.swing.JLabel;
|
---|
10 | import javax.swing.JPanel;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
13 | import org.openstreetmap.josm.tools.GBC;
|
---|
14 | import org.openstreetmap.josm.tools.Utils;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * A projection choice, that offers a list of projections in a combo-box.
|
---|
18 | */
|
---|
19 | public abstract class ListProjectionChoice extends AbstractProjectionChoice {
|
---|
20 |
|
---|
21 | protected int index; // 0-based index
|
---|
22 | protected int defaultIndex;
|
---|
23 | protected String[] entries;
|
---|
24 | protected String label;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code ListProjectionChoice}.
|
---|
28 | *
|
---|
29 | * @param name the display name
|
---|
30 | * @param id the unique id for this ProjectionChoice
|
---|
31 | * @param entries the list of display entries for the combo-box
|
---|
32 | * @param label a label shown left to the combo-box
|
---|
33 | * @param defaultIndex the default index for the combo-box
|
---|
34 | */
|
---|
35 | public ListProjectionChoice(String name, String id, String[] entries, String label, int defaultIndex) {
|
---|
36 | super(name, id);
|
---|
37 | this.entries = Utils.copyArray(entries);
|
---|
38 | this.label = label;
|
---|
39 | this.defaultIndex = defaultIndex;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Constructs a new {@code ListProjectionChoice}.
|
---|
44 | * @param name the display name
|
---|
45 | * @param id the unique id for this ProjectionChoice
|
---|
46 | * @param entries the list of display entries for the combo-box
|
---|
47 | * @param label a label shown left to the combo-box
|
---|
48 | */
|
---|
49 | public ListProjectionChoice(String name, String id, String[] entries, String label) {
|
---|
50 | this(name, id, entries, label, 0);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Convert 0-based index to preference value.
|
---|
55 | */
|
---|
56 | protected abstract String indexToZone(int idx);
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Convert preference value to 0-based index.
|
---|
60 | */
|
---|
61 | protected abstract int zoneToIndex(String zone);
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public void setPreferences(Collection<String> args) {
|
---|
65 | String zone = null;
|
---|
66 | if (args != null && args.size() >= 1) {
|
---|
67 | zone = args.iterator().next();
|
---|
68 | }
|
---|
69 | int idx;
|
---|
70 | if (zone == null) {
|
---|
71 | idx = defaultIndex;
|
---|
72 | } else {
|
---|
73 | idx = zoneToIndex(zone);
|
---|
74 | if (idx < 0 || idx >= entries.length) {
|
---|
75 | idx = defaultIndex;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | this.index = idx;
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected class CBPanel extends JPanel {
|
---|
82 | public JosmComboBox<String> prefcb;
|
---|
83 |
|
---|
84 | public CBPanel(String[] entries, int initialIndex, String label, final ActionListener listener) {
|
---|
85 | prefcb = new JosmComboBox<>(entries);
|
---|
86 |
|
---|
87 | prefcb.setSelectedIndex(initialIndex);
|
---|
88 | this.setLayout(new GridBagLayout());
|
---|
89 | this.add(new JLabel(label), GBC.std().insets(5,5,0,5));
|
---|
90 | this.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
91 | this.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
92 | this.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
|
---|
93 |
|
---|
94 | if (listener != null) {
|
---|
95 | prefcb.addActionListener(listener);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public JPanel getPreferencePanel(ActionListener listener) {
|
---|
102 | return new CBPanel(entries, index, label, listener);
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public Collection<String> getPreferences(JPanel panel) {
|
---|
107 | if (!(panel instanceof CBPanel)) {
|
---|
108 | throw new IllegalArgumentException("Unsupported panel: "+panel);
|
---|
109 | }
|
---|
110 | CBPanel p = (CBPanel) panel;
|
---|
111 | int idx = p.prefcb.getSelectedIndex();
|
---|
112 | return Collections.singleton(indexToZone(idx));
|
---|
113 | }
|
---|
114 | }
|
---|