1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.projection;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.io.Serializable;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.Comparator;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.regex.Matcher;
|
---|
16 | import java.util.regex.Pattern;
|
---|
17 |
|
---|
18 | import javax.swing.AbstractListModel;
|
---|
19 | import javax.swing.JList;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 | import javax.swing.JScrollPane;
|
---|
22 | import javax.swing.event.DocumentEvent;
|
---|
23 | import javax.swing.event.DocumentListener;
|
---|
24 | import javax.swing.event.ListSelectionEvent;
|
---|
25 | import javax.swing.event.ListSelectionListener;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
28 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
29 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
30 | import org.openstreetmap.josm.tools.GBC;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Projection choice that lists all known projects by code.
|
---|
34 | */
|
---|
35 | public class CodeProjectionChoice extends AbstractProjectionChoice implements SubPrefsOptions {
|
---|
36 |
|
---|
37 | private String code;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Constructs a new {@code CodeProjectionChoice}.
|
---|
41 | */
|
---|
42 | public CodeProjectionChoice() {
|
---|
43 | super(tr("By Code (EPSG)"), /* NO-ICON */ "core:code");
|
---|
44 | }
|
---|
45 |
|
---|
46 | private static class CodeSelectionPanel extends JPanel implements ListSelectionListener, DocumentListener {
|
---|
47 |
|
---|
48 | public JosmTextField filter;
|
---|
49 | private ProjectionCodeListModel model;
|
---|
50 | public JList<String> selectionList;
|
---|
51 | private transient List<String> data;
|
---|
52 | private transient List<String> filteredData;
|
---|
53 | private static final String DEFAULT_CODE = "EPSG:3857";
|
---|
54 | private String lastCode = DEFAULT_CODE;
|
---|
55 | private transient ActionListener listener;
|
---|
56 |
|
---|
57 | public CodeSelectionPanel(String initialCode, ActionListener listener) {
|
---|
58 | this.listener = listener;
|
---|
59 | data = new ArrayList<>(Projections.getAllProjectionCodes());
|
---|
60 | Collections.sort(data, new CodeComparator());
|
---|
61 | filteredData = new ArrayList<>(data);
|
---|
62 | build();
|
---|
63 | setCode(initialCode != null ? initialCode : DEFAULT_CODE);
|
---|
64 | selectionList.addListSelectionListener(this);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Comparator that compares the number part of the code numerically.
|
---|
69 | */
|
---|
70 | private static class CodeComparator implements Comparator<String>, Serializable {
|
---|
71 | private static final long serialVersionUID = 1L;
|
---|
72 | private final Pattern codePattern = Pattern.compile("([a-zA-Z]+):(\\d+)");
|
---|
73 | @Override
|
---|
74 | public int compare(String c1, String c2) {
|
---|
75 | Matcher matcher1 = codePattern.matcher(c1);
|
---|
76 | Matcher matcher2 = codePattern.matcher(c2);
|
---|
77 | if (matcher1.matches()) {
|
---|
78 | if (matcher2.matches()) {
|
---|
79 | int cmp1 = matcher1.group(1).compareTo(matcher2.group(1));
|
---|
80 | if (cmp1 != 0) return cmp1;
|
---|
81 | int num1 = Integer.parseInt(matcher1.group(2));
|
---|
82 | int num2 = Integer.parseInt(matcher2.group(2));
|
---|
83 | return Integer.valueOf(num1).compareTo(num2);
|
---|
84 | } else
|
---|
85 | return -1;
|
---|
86 | } else if (matcher2.matches())
|
---|
87 | return 1;
|
---|
88 | return c1.compareTo(c2);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * List model for the filtered view on the list of all codes.
|
---|
94 | */
|
---|
95 | private class ProjectionCodeListModel extends AbstractListModel<String> {
|
---|
96 | @Override
|
---|
97 | public int getSize() {
|
---|
98 | return filteredData.size();
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public String getElementAt(int index) {
|
---|
103 | if (index >= 0 && index < filteredData.size())
|
---|
104 | return filteredData.get(index);
|
---|
105 | else
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void fireContentsChanged() {
|
---|
110 | fireContentsChanged(this, 0, this.getSize()-1);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void build() {
|
---|
115 | filter = new JosmTextField(30);
|
---|
116 | filter.setColumns(10);
|
---|
117 | filter.getDocument().addDocumentListener(this);
|
---|
118 |
|
---|
119 | selectionList = new JList<>(data.toArray(new String[0]));
|
---|
120 | selectionList.setModel(model = new ProjectionCodeListModel());
|
---|
121 | JScrollPane scroll = new JScrollPane(selectionList);
|
---|
122 | scroll.setPreferredSize(new Dimension(200, 214));
|
---|
123 |
|
---|
124 | this.setLayout(new GridBagLayout());
|
---|
125 | this.add(filter, GBC.eol().weight(1.0, 0.0));
|
---|
126 | this.add(scroll, GBC.eol());
|
---|
127 | }
|
---|
128 |
|
---|
129 | public String getCode() {
|
---|
130 | int idx = selectionList.getSelectedIndex();
|
---|
131 | if (idx == -1) return lastCode;
|
---|
132 | return filteredData.get(selectionList.getSelectedIndex());
|
---|
133 | }
|
---|
134 |
|
---|
135 | public final void setCode(String code) {
|
---|
136 | int idx = filteredData.indexOf(code);
|
---|
137 | if (idx != -1) {
|
---|
138 | selectionList.setSelectedIndex(idx);
|
---|
139 | selectionList.ensureIndexIsVisible(idx);
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | public void valueChanged(ListSelectionEvent e) {
|
---|
145 | listener.actionPerformed(null);
|
---|
146 | lastCode = getCode();
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public void insertUpdate(DocumentEvent e) {
|
---|
151 | updateFilter();
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public void removeUpdate(DocumentEvent e) {
|
---|
156 | updateFilter();
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | public void changedUpdate(DocumentEvent e) {
|
---|
161 | updateFilter();
|
---|
162 | }
|
---|
163 |
|
---|
164 | private void updateFilter() {
|
---|
165 | filteredData.clear();
|
---|
166 | String filterTxt = filter.getText().trim().toLowerCase();
|
---|
167 | for (String code : data) {
|
---|
168 | if (code.toLowerCase().contains(filterTxt)) {
|
---|
169 | filteredData.add(code);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | model.fireContentsChanged();
|
---|
173 | int idx = filteredData.indexOf(lastCode);
|
---|
174 | if (idx == -1) {
|
---|
175 | selectionList.clearSelection();
|
---|
176 | if (selectionList.getModel().getSize() > 0) {
|
---|
177 | selectionList.ensureIndexIsVisible(0);
|
---|
178 | }
|
---|
179 | } else {
|
---|
180 | selectionList.setSelectedIndex(idx);
|
---|
181 | selectionList.ensureIndexIsVisible(idx);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | public Projection getProjection() {
|
---|
188 | return Projections.getProjectionByCode(code);
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | public String getCurrentCode() {
|
---|
194 | // not needed - getProjection() is overridden
|
---|
195 | throw new UnsupportedOperationException();
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public String getProjectionName() {
|
---|
200 | // not needed - getProjection() is overridden
|
---|
201 | throw new UnsupportedOperationException();
|
---|
202 | }
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | public void setPreferences(Collection<String> args) {
|
---|
206 | if (args != null && !args.isEmpty()) {
|
---|
207 | code = args.iterator().next();
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | public JPanel getPreferencePanel(ActionListener listener) {
|
---|
213 | return new CodeSelectionPanel(code, listener);
|
---|
214 | }
|
---|
215 |
|
---|
216 | @Override
|
---|
217 | public Collection<String> getPreferences(JPanel panel) {
|
---|
218 | if (!(panel instanceof CodeSelectionPanel)) {
|
---|
219 | throw new IllegalArgumentException("Unsupported panel: "+panel);
|
---|
220 | }
|
---|
221 | CodeSelectionPanel csPanel = (CodeSelectionPanel) panel;
|
---|
222 | return Collections.singleton(csPanel.getCode());
|
---|
223 | }
|
---|
224 |
|
---|
225 | /* don't return all possible codes - this projection choice it too generic */
|
---|
226 | @Override
|
---|
227 | public String[] allCodes() {
|
---|
228 | return new String[0];
|
---|
229 | }
|
---|
230 |
|
---|
231 | /* not needed since allCodes() returns empty array */
|
---|
232 | @Override
|
---|
233 | public Collection<String> getPreferencesFromCode(String code) {
|
---|
234 | return null;
|
---|
235 | }
|
---|
236 |
|
---|
237 | @Override
|
---|
238 | public boolean showProjectionCode() {
|
---|
239 | return true;
|
---|
240 | }
|
---|
241 |
|
---|
242 | @Override
|
---|
243 | public boolean showProjectionName() {
|
---|
244 | return true;
|
---|
245 | }
|
---|
246 |
|
---|
247 | }
|
---|