1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
7 |
|
---|
8 | import java.awt.Color;
|
---|
9 | import java.awt.Component;
|
---|
10 | import java.awt.Dimension;
|
---|
11 | import java.awt.Font;
|
---|
12 | import java.awt.GridBagConstraints;
|
---|
13 | import java.awt.GridBagLayout;
|
---|
14 | import java.awt.Insets;
|
---|
15 | import java.awt.Rectangle;
|
---|
16 | import java.awt.event.ActionEvent;
|
---|
17 | import java.awt.event.FocusAdapter;
|
---|
18 | import java.awt.event.FocusEvent;
|
---|
19 | import java.awt.event.KeyEvent;
|
---|
20 | import java.awt.event.MouseAdapter;
|
---|
21 | import java.awt.event.MouseEvent;
|
---|
22 | import java.io.BufferedReader;
|
---|
23 | import java.io.File;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.net.MalformedURLException;
|
---|
26 | import java.net.URL;
|
---|
27 | import java.util.ArrayList;
|
---|
28 | import java.util.Arrays;
|
---|
29 | import java.util.Collection;
|
---|
30 | import java.util.Collections;
|
---|
31 | import java.util.EventObject;
|
---|
32 | import java.util.HashMap;
|
---|
33 | import java.util.Iterator;
|
---|
34 | import java.util.List;
|
---|
35 | import java.util.Map;
|
---|
36 | import java.util.Objects;
|
---|
37 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
38 | import java.util.regex.Matcher;
|
---|
39 | import java.util.regex.Pattern;
|
---|
40 | import java.util.stream.Collectors;
|
---|
41 | import java.util.stream.IntStream;
|
---|
42 |
|
---|
43 | import javax.swing.AbstractAction;
|
---|
44 | import javax.swing.BorderFactory;
|
---|
45 | import javax.swing.Box;
|
---|
46 | import javax.swing.DefaultListModel;
|
---|
47 | import javax.swing.DefaultListSelectionModel;
|
---|
48 | import javax.swing.JButton;
|
---|
49 | import javax.swing.JCheckBox;
|
---|
50 | import javax.swing.JComponent;
|
---|
51 | import javax.swing.JFileChooser;
|
---|
52 | import javax.swing.JLabel;
|
---|
53 | import javax.swing.JList;
|
---|
54 | import javax.swing.JOptionPane;
|
---|
55 | import javax.swing.JPanel;
|
---|
56 | import javax.swing.JScrollPane;
|
---|
57 | import javax.swing.JSeparator;
|
---|
58 | import javax.swing.JTable;
|
---|
59 | import javax.swing.JToolBar;
|
---|
60 | import javax.swing.KeyStroke;
|
---|
61 | import javax.swing.ListCellRenderer;
|
---|
62 | import javax.swing.ListSelectionModel;
|
---|
63 | import javax.swing.UIManager;
|
---|
64 | import javax.swing.event.CellEditorListener;
|
---|
65 | import javax.swing.event.ChangeEvent;
|
---|
66 | import javax.swing.event.DocumentEvent;
|
---|
67 | import javax.swing.event.DocumentListener;
|
---|
68 | import javax.swing.event.ListSelectionEvent;
|
---|
69 | import javax.swing.event.ListSelectionListener;
|
---|
70 | import javax.swing.event.TableModelEvent;
|
---|
71 | import javax.swing.event.TableModelListener;
|
---|
72 | import javax.swing.filechooser.FileFilter;
|
---|
73 | import javax.swing.table.AbstractTableModel;
|
---|
74 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
75 | import javax.swing.table.TableCellEditor;
|
---|
76 | import javax.swing.table.TableModel;
|
---|
77 |
|
---|
78 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
79 | import org.openstreetmap.josm.data.Version;
|
---|
80 | import org.openstreetmap.josm.data.preferences.NamedColorProperty;
|
---|
81 | import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
|
---|
82 | import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
|
---|
83 | import org.openstreetmap.josm.data.preferences.sources.SourcePrefHelper;
|
---|
84 | import org.openstreetmap.josm.data.preferences.sources.SourceProvider;
|
---|
85 | import org.openstreetmap.josm.data.preferences.sources.SourceType;
|
---|
86 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
87 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
88 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
89 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
90 | import org.openstreetmap.josm.gui.util.FileFilterAllFiles;
|
---|
91 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
92 | import org.openstreetmap.josm.gui.util.ReorderableTableModel;
|
---|
93 | import org.openstreetmap.josm.gui.util.TableHelper;
|
---|
94 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
95 | import org.openstreetmap.josm.gui.widgets.FileChooserManager;
|
---|
96 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
97 | import org.openstreetmap.josm.io.CachedFile;
|
---|
98 | import org.openstreetmap.josm.io.NetworkManager;
|
---|
99 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
100 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
101 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
102 | import org.openstreetmap.josm.tools.GBC;
|
---|
103 | import org.openstreetmap.josm.tools.ImageOverlay;
|
---|
104 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
105 | import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
|
---|
106 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
107 | import org.openstreetmap.josm.tools.Logging;
|
---|
108 | import org.openstreetmap.josm.tools.Utils;
|
---|
109 | import org.xml.sax.SAXException;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Editor for JOSM extensions source entries.
|
---|
113 | * @since 1743
|
---|
114 | */
|
---|
115 | public abstract class SourceEditor extends JPanel {
|
---|
116 |
|
---|
117 | /** the type of source entry **/
|
---|
118 | protected final SourceType sourceType;
|
---|
119 | /** determines if the entry type can be enabled (set as active) **/
|
---|
120 | protected final boolean canEnable;
|
---|
121 |
|
---|
122 | /** the table of active sources **/
|
---|
123 | protected final JTable tblActiveSources;
|
---|
124 | /** the underlying model of active sources **/
|
---|
125 | protected final ActiveSourcesModel activeSourcesModel;
|
---|
126 | /** the list of available sources **/
|
---|
127 | protected final JList<ExtendedSourceEntry> lstAvailableSources;
|
---|
128 | /** the underlying model of available sources **/
|
---|
129 | protected final AvailableSourcesListModel availableSourcesModel;
|
---|
130 | /** the URL from which the available sources are fetched **/
|
---|
131 | protected final String availableSourcesUrl;
|
---|
132 | /** the list of source providers **/
|
---|
133 | protected final transient List<SourceProvider> sourceProviders;
|
---|
134 |
|
---|
135 | private JTable tblIconPaths;
|
---|
136 | private IconPathTableModel iconPathsModel;
|
---|
137 |
|
---|
138 | /** determines if the source providers have been initially loaded **/
|
---|
139 | protected boolean sourcesInitiallyLoaded;
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Constructs a new {@code SourceEditor}.
|
---|
143 | * @param sourceType the type of source managed by this editor
|
---|
144 | * @param availableSourcesUrl the URL to the list of available sources
|
---|
145 | * @param sourceProviders the list of additional source providers, from plugins
|
---|
146 | * @param handleIcons {@code true} if icons may be managed, {@code false} otherwise
|
---|
147 | */
|
---|
148 | protected SourceEditor(SourceType sourceType, String availableSourcesUrl, List<SourceProvider> sourceProviders, boolean handleIcons) {
|
---|
149 |
|
---|
150 | this.sourceType = sourceType;
|
---|
151 | this.canEnable = sourceType == SourceType.MAP_PAINT_STYLE || sourceType == SourceType.TAGCHECKER_RULE;
|
---|
152 |
|
---|
153 | DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
|
---|
154 | this.availableSourcesModel = new AvailableSourcesListModel(selectionModel);
|
---|
155 | this.lstAvailableSources = new JList<>(availableSourcesModel);
|
---|
156 | this.lstAvailableSources.setSelectionModel(selectionModel);
|
---|
157 | final SourceEntryListCellRenderer listCellRenderer = new SourceEntryListCellRenderer();
|
---|
158 | this.lstAvailableSources.setCellRenderer(listCellRenderer);
|
---|
159 | GuiHelper.extendTooltipDelay(lstAvailableSources);
|
---|
160 | this.availableSourcesUrl = availableSourcesUrl;
|
---|
161 | this.sourceProviders = sourceProviders;
|
---|
162 |
|
---|
163 | selectionModel = new DefaultListSelectionModel();
|
---|
164 | activeSourcesModel = new ActiveSourcesModel(selectionModel);
|
---|
165 | tblActiveSources = new ScrollHackTable(activeSourcesModel);
|
---|
166 | tblActiveSources.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
|
---|
167 | tblActiveSources.setSelectionModel(selectionModel);
|
---|
168 | tblActiveSources.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
169 | tblActiveSources.setShowGrid(false);
|
---|
170 | tblActiveSources.setIntercellSpacing(new Dimension(0, 0));
|
---|
171 | tblActiveSources.setTableHeader(null);
|
---|
172 | tblActiveSources.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
---|
173 | SourceEntryTableCellRenderer sourceEntryRenderer = new SourceEntryTableCellRenderer();
|
---|
174 | if (canEnable) {
|
---|
175 | tblActiveSources.getColumnModel().getColumn(0).setMaxWidth(1);
|
---|
176 | tblActiveSources.getColumnModel().getColumn(0).setResizable(false);
|
---|
177 | tblActiveSources.getColumnModel().getColumn(1).setCellRenderer(sourceEntryRenderer);
|
---|
178 | } else {
|
---|
179 | tblActiveSources.getColumnModel().getColumn(0).setCellRenderer(sourceEntryRenderer);
|
---|
180 | }
|
---|
181 |
|
---|
182 | activeSourcesModel.addTableModelListener(e -> {
|
---|
183 | listCellRenderer.updateSources(activeSourcesModel.getSources());
|
---|
184 | lstAvailableSources.repaint();
|
---|
185 | });
|
---|
186 | tblActiveSources.addPropertyChangeListener(evt -> {
|
---|
187 | listCellRenderer.updateSources(activeSourcesModel.getSources());
|
---|
188 | lstAvailableSources.repaint();
|
---|
189 | });
|
---|
190 | // Force Swing to show horizontal scrollbars for the JTable
|
---|
191 | // Yes, this is a little ugly, but should work
|
---|
192 | activeSourcesModel.addTableModelListener(e -> TableHelper.adjustColumnWidth(tblActiveSources, canEnable ? 1 : 0, 800));
|
---|
193 | activeSourcesModel.setActiveSources(getInitialSourcesList());
|
---|
194 |
|
---|
195 | final EditActiveSourceAction editActiveSourceAction = new EditActiveSourceAction();
|
---|
196 | tblActiveSources.getSelectionModel().addListSelectionListener(editActiveSourceAction);
|
---|
197 | tblActiveSources.addMouseListener(new MouseAdapter() {
|
---|
198 | @Override
|
---|
199 | public void mouseClicked(MouseEvent e) {
|
---|
200 | if (e.getClickCount() == 2) {
|
---|
201 | int row = tblActiveSources.rowAtPoint(e.getPoint());
|
---|
202 | int col = tblActiveSources.columnAtPoint(e.getPoint());
|
---|
203 | if (row < 0 || row >= tblActiveSources.getRowCount())
|
---|
204 | return;
|
---|
205 | if (canEnable && col != 1)
|
---|
206 | return;
|
---|
207 | editActiveSourceAction.actionPerformed(null);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | });
|
---|
211 |
|
---|
212 | RemoveActiveSourcesAction removeActiveSourcesAction = new RemoveActiveSourcesAction();
|
---|
213 | tblActiveSources.getSelectionModel().addListSelectionListener(removeActiveSourcesAction);
|
---|
214 | tblActiveSources.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
|
---|
215 | tblActiveSources.getActionMap().put("delete", removeActiveSourcesAction);
|
---|
216 |
|
---|
217 | MoveUpDownAction moveUp = null;
|
---|
218 | MoveUpDownAction moveDown = null;
|
---|
219 | if (sourceType == SourceType.MAP_PAINT_STYLE) {
|
---|
220 | moveUp = new MoveUpDownAction(false);
|
---|
221 | moveDown = new MoveUpDownAction(true);
|
---|
222 | tblActiveSources.getSelectionModel().addListSelectionListener(moveUp);
|
---|
223 | tblActiveSources.getSelectionModel().addListSelectionListener(moveDown);
|
---|
224 | activeSourcesModel.addTableModelListener(moveUp);
|
---|
225 | activeSourcesModel.addTableModelListener(moveDown);
|
---|
226 | }
|
---|
227 |
|
---|
228 | ActivateSourcesAction activateSourcesAction = new ActivateSourcesAction();
|
---|
229 | lstAvailableSources.addListSelectionListener(activateSourcesAction);
|
---|
230 | JButton activate = new JButton(activateSourcesAction);
|
---|
231 |
|
---|
232 | setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
|
---|
233 | setLayout(new GridBagLayout());
|
---|
234 |
|
---|
235 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
236 | gbc.gridx = 0;
|
---|
237 | gbc.gridy = 0;
|
---|
238 | gbc.weightx = 0.5;
|
---|
239 | gbc.gridwidth = 2;
|
---|
240 | gbc.anchor = GBC.WEST;
|
---|
241 | gbc.insets = new Insets(5, 11, 0, 0);
|
---|
242 |
|
---|
243 | add(new JLabel(getStr(I18nString.AVAILABLE_SOURCES)), gbc);
|
---|
244 |
|
---|
245 | gbc.gridx = 2;
|
---|
246 | gbc.insets = new Insets(5, 0, 0, 6);
|
---|
247 |
|
---|
248 | add(new JLabel(getStr(I18nString.ACTIVE_SOURCES)), gbc);
|
---|
249 |
|
---|
250 | gbc.gridwidth = 1;
|
---|
251 | gbc.gridx = 0;
|
---|
252 | gbc.gridy++;
|
---|
253 | gbc.weighty = 0.8;
|
---|
254 | gbc.fill = GBC.BOTH;
|
---|
255 | gbc.anchor = GBC.CENTER;
|
---|
256 | gbc.insets = new Insets(0, 11, 0, 0);
|
---|
257 |
|
---|
258 | JScrollPane sp1 = new JScrollPane(lstAvailableSources);
|
---|
259 | add(sp1, gbc);
|
---|
260 |
|
---|
261 | gbc.gridx = 1;
|
---|
262 | gbc.weightx = 0.0;
|
---|
263 | gbc.fill = GBC.VERTICAL;
|
---|
264 | gbc.insets = new Insets(0, 0, 0, 0);
|
---|
265 |
|
---|
266 | JToolBar middleTB = new JToolBar();
|
---|
267 | middleTB.setFloatable(false);
|
---|
268 | middleTB.setBorderPainted(false);
|
---|
269 | middleTB.setOpaque(false);
|
---|
270 | middleTB.add(Box.createHorizontalGlue());
|
---|
271 | middleTB.add(activate);
|
---|
272 | middleTB.add(Box.createHorizontalGlue());
|
---|
273 | add(middleTB, gbc);
|
---|
274 |
|
---|
275 | gbc.gridx++;
|
---|
276 | gbc.weightx = 0.5;
|
---|
277 | gbc.fill = GBC.BOTH;
|
---|
278 |
|
---|
279 | JScrollPane sp = new JScrollPane(tblActiveSources);
|
---|
280 | add(sp, gbc);
|
---|
281 | sp.setColumnHeaderView(null);
|
---|
282 |
|
---|
283 | gbc.gridx++;
|
---|
284 | gbc.weightx = 0.0;
|
---|
285 | gbc.fill = GBC.VERTICAL;
|
---|
286 | gbc.insets = new Insets(0, 0, 0, 6);
|
---|
287 |
|
---|
288 | JToolBar sideButtonTB = new JToolBar(JToolBar.VERTICAL);
|
---|
289 | sideButtonTB.setFloatable(false);
|
---|
290 | sideButtonTB.setBorderPainted(false);
|
---|
291 | sideButtonTB.setOpaque(false);
|
---|
292 | sideButtonTB.add(new NewActiveSourceAction());
|
---|
293 | sideButtonTB.add(editActiveSourceAction);
|
---|
294 | sideButtonTB.add(removeActiveSourcesAction);
|
---|
295 | sideButtonTB.addSeparator(new Dimension(12, 30));
|
---|
296 | if (sourceType == SourceType.MAP_PAINT_STYLE) {
|
---|
297 | sideButtonTB.add(moveUp);
|
---|
298 | sideButtonTB.add(moveDown);
|
---|
299 | }
|
---|
300 | add(sideButtonTB, gbc);
|
---|
301 |
|
---|
302 | gbc.gridx = 0;
|
---|
303 | gbc.gridy++;
|
---|
304 | gbc.weighty = 0.0;
|
---|
305 | gbc.weightx = 0.5;
|
---|
306 | gbc.fill = GBC.HORIZONTAL;
|
---|
307 | gbc.anchor = GBC.WEST;
|
---|
308 | gbc.insets = new Insets(0, 11, 0, 0);
|
---|
309 |
|
---|
310 | JToolBar bottomLeftTB = new JToolBar();
|
---|
311 | bottomLeftTB.setFloatable(false);
|
---|
312 | bottomLeftTB.setBorderPainted(false);
|
---|
313 | bottomLeftTB.setOpaque(false);
|
---|
314 | bottomLeftTB.add(new ReloadSourcesAction(availableSourcesUrl, sourceProviders));
|
---|
315 | bottomLeftTB.add(Box.createHorizontalGlue());
|
---|
316 | add(bottomLeftTB, gbc);
|
---|
317 |
|
---|
318 | gbc.gridx = 2;
|
---|
319 | gbc.anchor = GBC.CENTER;
|
---|
320 | gbc.insets = new Insets(0, 0, 0, 0);
|
---|
321 |
|
---|
322 | JToolBar bottomRightTB = new JToolBar();
|
---|
323 | bottomRightTB.setFloatable(false);
|
---|
324 | bottomRightTB.setBorderPainted(false);
|
---|
325 | bottomRightTB.setOpaque(false);
|
---|
326 | bottomRightTB.add(Box.createHorizontalGlue());
|
---|
327 | bottomRightTB.add(new JButton(new ResetAction()));
|
---|
328 | add(bottomRightTB, gbc);
|
---|
329 |
|
---|
330 | // Icon configuration
|
---|
331 | if (handleIcons) {
|
---|
332 | buildIcons(gbc);
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | private void buildIcons(GridBagConstraints gbc) {
|
---|
337 | DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
|
---|
338 | iconPathsModel = new IconPathTableModel(selectionModel);
|
---|
339 | tblIconPaths = new JTable(iconPathsModel);
|
---|
340 | tblIconPaths.setSelectionModel(selectionModel);
|
---|
341 | tblIconPaths.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
342 | tblIconPaths.setTableHeader(null);
|
---|
343 | tblIconPaths.getColumnModel().getColumn(0).setCellEditor(new FileOrUrlCellEditor(false));
|
---|
344 | tblIconPaths.setRowHeight(20);
|
---|
345 | tblIconPaths.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
|
---|
346 | iconPathsModel.setIconPaths(getInitialIconPathsList());
|
---|
347 |
|
---|
348 | EditIconPathAction editIconPathAction = new EditIconPathAction();
|
---|
349 | tblIconPaths.getSelectionModel().addListSelectionListener(editIconPathAction);
|
---|
350 |
|
---|
351 | RemoveIconPathAction removeIconPathAction = new RemoveIconPathAction();
|
---|
352 | tblIconPaths.getSelectionModel().addListSelectionListener(removeIconPathAction);
|
---|
353 | tblIconPaths.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
|
---|
354 | tblIconPaths.getActionMap().put("delete", removeIconPathAction);
|
---|
355 |
|
---|
356 | gbc.gridx = 0;
|
---|
357 | gbc.gridy++;
|
---|
358 | gbc.weightx = 1.0;
|
---|
359 | gbc.gridwidth = GBC.REMAINDER;
|
---|
360 | gbc.insets = new Insets(8, 11, 8, 6);
|
---|
361 |
|
---|
362 | add(new JSeparator(), gbc);
|
---|
363 |
|
---|
364 | gbc.gridy++;
|
---|
365 | gbc.insets = new Insets(0, 11, 0, 6);
|
---|
366 |
|
---|
367 | add(new JLabel(tr("Icon paths:")), gbc);
|
---|
368 |
|
---|
369 | gbc.gridy++;
|
---|
370 | gbc.weighty = 0.2;
|
---|
371 | gbc.gridwidth = 3;
|
---|
372 | gbc.fill = GBC.BOTH;
|
---|
373 | gbc.insets = new Insets(0, 11, 0, 0);
|
---|
374 |
|
---|
375 | JScrollPane sp = new JScrollPane(tblIconPaths);
|
---|
376 | add(sp, gbc);
|
---|
377 | sp.setColumnHeaderView(null);
|
---|
378 |
|
---|
379 | gbc.gridx = 3;
|
---|
380 | gbc.gridwidth = 1;
|
---|
381 | gbc.weightx = 0.0;
|
---|
382 | gbc.fill = GBC.VERTICAL;
|
---|
383 | gbc.insets = new Insets(0, 0, 0, 6);
|
---|
384 |
|
---|
385 | JToolBar sideButtonTBIcons = new JToolBar(JToolBar.VERTICAL);
|
---|
386 | sideButtonTBIcons.setFloatable(false);
|
---|
387 | sideButtonTBIcons.setBorderPainted(false);
|
---|
388 | sideButtonTBIcons.setOpaque(false);
|
---|
389 | sideButtonTBIcons.add(new NewIconPathAction());
|
---|
390 | sideButtonTBIcons.add(editIconPathAction);
|
---|
391 | sideButtonTBIcons.add(removeIconPathAction);
|
---|
392 | add(sideButtonTBIcons, gbc);
|
---|
393 | }
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Load the list of source entries that the user has configured.
|
---|
397 | * @return list of source entries that the user has configured
|
---|
398 | */
|
---|
399 | public abstract Collection<? extends SourceEntry> getInitialSourcesList();
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * Load the list of configured icon paths.
|
---|
403 | * @return list of configured icon paths
|
---|
404 | */
|
---|
405 | public abstract Collection<String> getInitialIconPathsList();
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Get the default list of entries (used when resetting the list).
|
---|
409 | * @return default list of entries
|
---|
410 | */
|
---|
411 | public abstract Collection<ExtendedSourceEntry> getDefault();
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * Save the settings after user clicked "Ok".
|
---|
415 | * @return true if restart is required
|
---|
416 | */
|
---|
417 | public abstract boolean finish();
|
---|
418 |
|
---|
419 | /**
|
---|
420 | * Default implementation of {@link #finish}.
|
---|
421 | * @param prefHelper Helper class for specialized extensions preferences
|
---|
422 | * @param iconPref icons path preference
|
---|
423 | * @return true if restart is required
|
---|
424 | */
|
---|
425 | protected boolean doFinish(SourcePrefHelper prefHelper, String iconPref) {
|
---|
426 | boolean changed = prefHelper.put(activeSourcesModel.getSources());
|
---|
427 |
|
---|
428 | if (tblIconPaths != null) {
|
---|
429 | List<String> iconPaths = iconPathsModel.getIconPaths();
|
---|
430 |
|
---|
431 | if (!iconPaths.isEmpty()) {
|
---|
432 | if (Config.getPref().putList(iconPref, iconPaths)) {
|
---|
433 | changed = true;
|
---|
434 | }
|
---|
435 | } else if (Config.getPref().putList(iconPref, null)) {
|
---|
436 | changed = true;
|
---|
437 | }
|
---|
438 | }
|
---|
439 | return changed;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Provide the GUI strings. (There are differences for MapPaint, Preset and TagChecker Rule)
|
---|
444 | * @param ident any {@link I18nString} value
|
---|
445 | * @return the translated string for {@code ident}
|
---|
446 | */
|
---|
447 | protected abstract String getStr(I18nString ident);
|
---|
448 |
|
---|
449 | static final class ScrollHackTable extends JTable {
|
---|
450 | ScrollHackTable(TableModel dm) {
|
---|
451 | super(dm);
|
---|
452 | }
|
---|
453 |
|
---|
454 | // some kind of hack to prevent the table from scrolling slightly to the right when clicking on the text
|
---|
455 | @Override
|
---|
456 | public void scrollRectToVisible(Rectangle aRect) {
|
---|
457 | super.scrollRectToVisible(new Rectangle(0, aRect.y, aRect.width, aRect.height));
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * Identifiers for strings that need to be provided.
|
---|
463 | */
|
---|
464 | public enum I18nString {
|
---|
465 | /** Available (styles|presets|rules) */
|
---|
466 | AVAILABLE_SOURCES,
|
---|
467 | /** Active (styles|presets|rules) */
|
---|
468 | ACTIVE_SOURCES,
|
---|
469 | /** Add a new (style|preset|rule) by entering filename or URL */
|
---|
470 | NEW_SOURCE_ENTRY_TOOLTIP,
|
---|
471 | /** New (style|preset|rule) entry */
|
---|
472 | NEW_SOURCE_ENTRY,
|
---|
473 | /** Remove the selected (styles|presets|rules) from the list of active (styles|presets|rules) */
|
---|
474 | REMOVE_SOURCE_TOOLTIP,
|
---|
475 | /** Edit the filename or URL for the selected active (style|preset|rule) */
|
---|
476 | EDIT_SOURCE_TOOLTIP,
|
---|
477 | /** Add the selected available (styles|presets|rules) to the list of active (styles|presets|rules) */
|
---|
478 | ACTIVATE_TOOLTIP,
|
---|
479 | /** Reloads the list of available (styles|presets|rules) */
|
---|
480 | RELOAD_ALL_AVAILABLE,
|
---|
481 | /** Loading (style|preset|rule) sources */
|
---|
482 | LOADING_SOURCES_FROM,
|
---|
483 | /** Failed to load the list of (style|preset|rule) sources */
|
---|
484 | FAILED_TO_LOAD_SOURCES_FROM,
|
---|
485 | /** /Preferences/(Styles|Presets|Rules)#FailedToLoad(Style|Preset|Rule)Sources */
|
---|
486 | FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC,
|
---|
487 | /** Illegal format of entry in (style|preset|rule) list */
|
---|
488 | ILLEGAL_FORMAT_OF_ENTRY
|
---|
489 | }
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Determines whether the list of active sources has changed.
|
---|
493 | * @return {@code true} if the list of active sources has changed, {@code false} otherwise
|
---|
494 | */
|
---|
495 | public boolean hasActiveSourcesChanged() {
|
---|
496 | Collection<? extends SourceEntry> prev = getInitialSourcesList();
|
---|
497 | List<SourceEntry> cur = activeSourcesModel.getSources();
|
---|
498 | if (prev.size() != cur.size())
|
---|
499 | return true;
|
---|
500 | Iterator<? extends SourceEntry> p = prev.iterator();
|
---|
501 | Iterator<SourceEntry> c = cur.iterator();
|
---|
502 | while (p.hasNext()) {
|
---|
503 | SourceEntry pe = p.next();
|
---|
504 | SourceEntry ce = c.next();
|
---|
505 | if (!Objects.equals(pe.url, ce.url) || !Objects.equals(pe.name, ce.name) || pe.active != ce.active)
|
---|
506 | return true;
|
---|
507 | }
|
---|
508 | return false;
|
---|
509 | }
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Returns the list of active sources.
|
---|
513 | * @return the list of active sources
|
---|
514 | */
|
---|
515 | public Collection<SourceEntry> getActiveSources() {
|
---|
516 | return activeSourcesModel.getSources();
|
---|
517 | }
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Synchronously loads available sources and returns the parsed list.
|
---|
521 | * @return list of available sources
|
---|
522 | * @throws OsmTransferException in case of OSM transfer error
|
---|
523 | * @throws IOException in case of any I/O error
|
---|
524 | * @throws SAXException in case of any SAX error
|
---|
525 | */
|
---|
526 | public final Collection<ExtendedSourceEntry> loadAndGetAvailableSources() throws SAXException, IOException, OsmTransferException {
|
---|
527 | final SourceLoader loader = new SourceLoader(availableSourcesUrl, sourceProviders);
|
---|
528 | loader.realRun();
|
---|
529 | return loader.sources;
|
---|
530 | }
|
---|
531 |
|
---|
532 | /**
|
---|
533 | * Remove sources associated with given indexes from active list.
|
---|
534 | * @param idxs indexes of sources to remove
|
---|
535 | */
|
---|
536 | public void removeSources(Collection<Integer> idxs) {
|
---|
537 | activeSourcesModel.removeIdxs(idxs);
|
---|
538 | }
|
---|
539 |
|
---|
540 | /**
|
---|
541 | * Reload available sources.
|
---|
542 | * @param url the URL from which the available sources are fetched
|
---|
543 | * @param sourceProviders the list of source providers
|
---|
544 | */
|
---|
545 | protected void reloadAvailableSources(String url, List<SourceProvider> sourceProviders) {
|
---|
546 | MainApplication.worker.submit(new SourceLoader(url, sourceProviders));
|
---|
547 | }
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * Performs the initial loading of source providers. Does nothing if already done.
|
---|
551 | */
|
---|
552 | public void initiallyLoadAvailableSources() {
|
---|
553 | if (!sourcesInitiallyLoaded) {
|
---|
554 | reloadAvailableSources(availableSourcesUrl, sourceProviders);
|
---|
555 | }
|
---|
556 | sourcesInitiallyLoaded = true;
|
---|
557 | }
|
---|
558 |
|
---|
559 | /**
|
---|
560 | * List model of available sources.
|
---|
561 | */
|
---|
562 | protected static class AvailableSourcesListModel extends DefaultListModel<ExtendedSourceEntry> {
|
---|
563 | private final transient List<ExtendedSourceEntry> data;
|
---|
564 | private final DefaultListSelectionModel selectionModel;
|
---|
565 |
|
---|
566 | /**
|
---|
567 | * Constructs a new {@code AvailableSourcesListModel}
|
---|
568 | * @param selectionModel selection model
|
---|
569 | */
|
---|
570 | public AvailableSourcesListModel(DefaultListSelectionModel selectionModel) {
|
---|
571 | data = new ArrayList<>();
|
---|
572 | this.selectionModel = selectionModel;
|
---|
573 | }
|
---|
574 |
|
---|
575 | /**
|
---|
576 | * Sets the source list.
|
---|
577 | * @param sources source list
|
---|
578 | */
|
---|
579 | public void setSources(List<ExtendedSourceEntry> sources) {
|
---|
580 | data.clear();
|
---|
581 | if (sources != null) {
|
---|
582 | data.addAll(sources);
|
---|
583 | }
|
---|
584 | fireContentsChanged(this, 0, data.size());
|
---|
585 | }
|
---|
586 |
|
---|
587 | @Override
|
---|
588 | public ExtendedSourceEntry getElementAt(int index) {
|
---|
589 | return data.get(index);
|
---|
590 | }
|
---|
591 |
|
---|
592 | @Override
|
---|
593 | public int getSize() {
|
---|
594 | if (data == null) return 0;
|
---|
595 | return data.size();
|
---|
596 | }
|
---|
597 |
|
---|
598 | /**
|
---|
599 | * Deletes the selected sources.
|
---|
600 | */
|
---|
601 | public void deleteSelected() {
|
---|
602 | Iterator<ExtendedSourceEntry> it = data.iterator();
|
---|
603 | int i = 0;
|
---|
604 | while (it.hasNext()) {
|
---|
605 | it.next();
|
---|
606 | if (selectionModel.isSelectedIndex(i)) {
|
---|
607 | it.remove();
|
---|
608 | }
|
---|
609 | i++;
|
---|
610 | }
|
---|
611 | fireContentsChanged(this, 0, data.size());
|
---|
612 | }
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * Returns the selected sources.
|
---|
616 | * @return the selected sources
|
---|
617 | */
|
---|
618 | public List<ExtendedSourceEntry> getSelected() {
|
---|
619 | return IntStream.range(0, data.size())
|
---|
620 | .filter(selectionModel::isSelectedIndex)
|
---|
621 | .mapToObj(data::get)
|
---|
622 | .collect(Collectors.toList());
|
---|
623 | }
|
---|
624 | }
|
---|
625 |
|
---|
626 | /**
|
---|
627 | * Table model of active sources.
|
---|
628 | */
|
---|
629 | protected class ActiveSourcesModel extends AbstractTableModel implements ReorderableTableModel<SourceEntry> {
|
---|
630 | private transient List<SourceEntry> data;
|
---|
631 | private final DefaultListSelectionModel selectionModel;
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * Constructs a new {@code ActiveSourcesModel}.
|
---|
635 | * @param selectionModel selection model
|
---|
636 | */
|
---|
637 | public ActiveSourcesModel(DefaultListSelectionModel selectionModel) {
|
---|
638 | this.selectionModel = selectionModel;
|
---|
639 | this.data = new ArrayList<>();
|
---|
640 | }
|
---|
641 |
|
---|
642 | @Override
|
---|
643 | public int getColumnCount() {
|
---|
644 | return canEnable ? 2 : 1;
|
---|
645 | }
|
---|
646 |
|
---|
647 | @Override
|
---|
648 | public int getRowCount() {
|
---|
649 | return data == null ? 0 : data.size();
|
---|
650 | }
|
---|
651 |
|
---|
652 | @Override
|
---|
653 | public Object getValueAt(int rowIndex, int columnIndex) {
|
---|
654 | if (canEnable && columnIndex == 0)
|
---|
655 | return data.get(rowIndex).active;
|
---|
656 | else
|
---|
657 | return data.get(rowIndex);
|
---|
658 | }
|
---|
659 |
|
---|
660 | @Override
|
---|
661 | public boolean isCellEditable(int rowIndex, int columnIndex) {
|
---|
662 | return canEnable && columnIndex == 0;
|
---|
663 | }
|
---|
664 |
|
---|
665 | @Override
|
---|
666 | public Class<?> getColumnClass(int column) {
|
---|
667 | if (canEnable && column == 0)
|
---|
668 | return Boolean.class;
|
---|
669 | else return SourceEntry.class;
|
---|
670 | }
|
---|
671 |
|
---|
672 | @Override
|
---|
673 | public void setValueAt(Object aValue, int row, int column) {
|
---|
674 | if (row < 0 || row >= getRowCount() || aValue == null)
|
---|
675 | return;
|
---|
676 | if (canEnable && column == 0) {
|
---|
677 | data.get(row).active = !data.get(row).active;
|
---|
678 | }
|
---|
679 | }
|
---|
680 |
|
---|
681 | /**
|
---|
682 | * Sets active sources.
|
---|
683 | * @param sources active sources
|
---|
684 | */
|
---|
685 | public void setActiveSources(Collection<? extends SourceEntry> sources) {
|
---|
686 | data.clear();
|
---|
687 | if (sources != null) {
|
---|
688 | for (SourceEntry e : sources) {
|
---|
689 | data.add(new SourceEntry(e));
|
---|
690 | }
|
---|
691 | }
|
---|
692 | fireTableDataChanged();
|
---|
693 | }
|
---|
694 |
|
---|
695 | /**
|
---|
696 | * Adds an active source.
|
---|
697 | * @param entry source to add
|
---|
698 | */
|
---|
699 | public void addSource(SourceEntry entry) {
|
---|
700 | if (entry == null) return;
|
---|
701 | data.add(entry);
|
---|
702 | fireTableDataChanged();
|
---|
703 | int idx = data.indexOf(entry);
|
---|
704 | if (idx >= 0) {
|
---|
705 | selectionModel.setSelectionInterval(idx, idx);
|
---|
706 | }
|
---|
707 | }
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * Removes the selected sources.
|
---|
711 | */
|
---|
712 | public void removeSelected() {
|
---|
713 | Iterator<SourceEntry> it = data.iterator();
|
---|
714 | int i = 0;
|
---|
715 | while (it.hasNext()) {
|
---|
716 | it.next();
|
---|
717 | if (selectionModel.isSelectedIndex(i)) {
|
---|
718 | it.remove();
|
---|
719 | }
|
---|
720 | i++;
|
---|
721 | }
|
---|
722 | fireTableDataChanged();
|
---|
723 | }
|
---|
724 |
|
---|
725 | /**
|
---|
726 | * Removes the sources at given indexes.
|
---|
727 | * @param idxs indexes to remove
|
---|
728 | */
|
---|
729 | public void removeIdxs(Collection<Integer> idxs) {
|
---|
730 | data = IntStream.range(0, data.size())
|
---|
731 | .filter(i -> !idxs.contains(i))
|
---|
732 | .mapToObj(i -> data.get(i))
|
---|
733 | .collect(Collectors.toList());
|
---|
734 | fireTableDataChanged();
|
---|
735 | }
|
---|
736 |
|
---|
737 | /**
|
---|
738 | * Adds multiple sources.
|
---|
739 | * @param sources source entries
|
---|
740 | */
|
---|
741 | public void addExtendedSourceEntries(List<ExtendedSourceEntry> sources) {
|
---|
742 | if (sources == null) return;
|
---|
743 | for (ExtendedSourceEntry info: sources) {
|
---|
744 | data.add(new SourceEntry(info.type, info.url, info.name, info.getDisplayName(), true));
|
---|
745 | }
|
---|
746 | fireTableDataChanged();
|
---|
747 | TableHelper.setSelectedIndices(selectionModel, sources.stream().mapToInt(data::indexOf));
|
---|
748 | }
|
---|
749 |
|
---|
750 | /**
|
---|
751 | * Returns the active sources.
|
---|
752 | * @return the active sources
|
---|
753 | */
|
---|
754 | public List<SourceEntry> getSources() {
|
---|
755 | return new ArrayList<>(data);
|
---|
756 | }
|
---|
757 |
|
---|
758 | @Override
|
---|
759 | public DefaultListSelectionModel getSelectionModel() {
|
---|
760 | return selectionModel;
|
---|
761 | }
|
---|
762 |
|
---|
763 | @Override
|
---|
764 | public SourceEntry getValue(int index) {
|
---|
765 | return data.get(index);
|
---|
766 | }
|
---|
767 |
|
---|
768 | @Override
|
---|
769 | public SourceEntry setValue(int index, SourceEntry value) {
|
---|
770 | return data.set(index, value);
|
---|
771 | }
|
---|
772 | }
|
---|
773 |
|
---|
774 | private static void prepareFileChooser(String url, AbstractFileChooser fc) {
|
---|
775 | if (url == null || url.trim().isEmpty()) return;
|
---|
776 | URL sourceUrl = null;
|
---|
777 | try {
|
---|
778 | sourceUrl = new URL(url);
|
---|
779 | } catch (MalformedURLException e) {
|
---|
780 | File f = new File(url);
|
---|
781 | if (f.isFile()) {
|
---|
782 | f = f.getParentFile();
|
---|
783 | }
|
---|
784 | if (f != null) {
|
---|
785 | fc.setCurrentDirectory(f);
|
---|
786 | }
|
---|
787 | return;
|
---|
788 | }
|
---|
789 | if (sourceUrl.getProtocol().startsWith("file")) {
|
---|
790 | File f = new File(sourceUrl.getPath());
|
---|
791 | if (f.isFile()) {
|
---|
792 | f = f.getParentFile();
|
---|
793 | }
|
---|
794 | if (f != null) {
|
---|
795 | fc.setCurrentDirectory(f);
|
---|
796 | }
|
---|
797 | }
|
---|
798 | }
|
---|
799 |
|
---|
800 | /**
|
---|
801 | * Dialog to edit a source entry.
|
---|
802 | */
|
---|
803 | protected class EditSourceEntryDialog extends ExtendedDialog {
|
---|
804 |
|
---|
805 | private final JosmTextField tfTitle;
|
---|
806 | private final JosmTextField tfURL;
|
---|
807 | private JCheckBox cbActive;
|
---|
808 |
|
---|
809 | /**
|
---|
810 | * Constructs a new {@code EditSourceEntryDialog}.
|
---|
811 | * @param parent parent component
|
---|
812 | * @param title dialog title
|
---|
813 | * @param e source entry to edit
|
---|
814 | */
|
---|
815 | public EditSourceEntryDialog(Component parent, String title, SourceEntry e) {
|
---|
816 | super(parent, title, tr("Ok"), tr("Cancel"));
|
---|
817 |
|
---|
818 | JPanel p = new JPanel(new GridBagLayout());
|
---|
819 |
|
---|
820 | tfTitle = new JosmTextField(60);
|
---|
821 | p.add(new JLabel(tr("Name (optional):")), GBC.std().insets(15, 0, 5, 5));
|
---|
822 | p.add(tfTitle, GBC.eol().insets(0, 0, 5, 5));
|
---|
823 |
|
---|
824 | tfURL = new JosmTextField(60);
|
---|
825 | p.add(new JLabel(tr("URL / File:")), GBC.std().insets(15, 0, 5, 0));
|
---|
826 | p.add(tfURL, GBC.std().insets(0, 0, 5, 5));
|
---|
827 | JButton fileChooser = new JButton(new LaunchFileChooserAction());
|
---|
828 | fileChooser.setMargin(new Insets(0, 0, 0, 0));
|
---|
829 | p.add(fileChooser, GBC.eol().insets(0, 0, 5, 5));
|
---|
830 |
|
---|
831 | if (e != null) {
|
---|
832 | if (e.title != null) {
|
---|
833 | tfTitle.setText(e.title);
|
---|
834 | }
|
---|
835 | tfURL.setText(e.url);
|
---|
836 | }
|
---|
837 |
|
---|
838 | if (canEnable) {
|
---|
839 | cbActive = new JCheckBox(tr("active"), e == null || e.active);
|
---|
840 | p.add(cbActive, GBC.eol().insets(15, 0, 5, 0));
|
---|
841 | }
|
---|
842 | setButtonIcons("ok", "cancel");
|
---|
843 | setContent(p);
|
---|
844 |
|
---|
845 | // Make OK button enabled only when a file/URL has been set
|
---|
846 | tfURL.getDocument().addDocumentListener(new DocumentListener() {
|
---|
847 | @Override
|
---|
848 | public void insertUpdate(DocumentEvent e) {
|
---|
849 | updateOkButtonState();
|
---|
850 | }
|
---|
851 |
|
---|
852 | @Override
|
---|
853 | public void removeUpdate(DocumentEvent e) {
|
---|
854 | updateOkButtonState();
|
---|
855 | }
|
---|
856 |
|
---|
857 | @Override
|
---|
858 | public void changedUpdate(DocumentEvent e) {
|
---|
859 | updateOkButtonState();
|
---|
860 | }
|
---|
861 | });
|
---|
862 | }
|
---|
863 |
|
---|
864 | private void updateOkButtonState() {
|
---|
865 | buttons.get(0).setEnabled(!Utils.isStripEmpty(tfURL.getText()));
|
---|
866 | }
|
---|
867 |
|
---|
868 | @Override
|
---|
869 | public void setupDialog() {
|
---|
870 | super.setupDialog();
|
---|
871 | updateOkButtonState();
|
---|
872 | }
|
---|
873 |
|
---|
874 | class LaunchFileChooserAction extends AbstractAction {
|
---|
875 | LaunchFileChooserAction() {
|
---|
876 | new ImageProvider("open").getResource().attachImageIcon(this);
|
---|
877 | putValue(SHORT_DESCRIPTION, tr("Launch a file chooser to select a file"));
|
---|
878 | }
|
---|
879 |
|
---|
880 | @Override
|
---|
881 | public void actionPerformed(ActionEvent e) {
|
---|
882 | FileFilter ff;
|
---|
883 | switch (sourceType) {
|
---|
884 | case MAP_PAINT_STYLE:
|
---|
885 | ff = new ExtensionFileFilter("xml,mapcss,css,zip", "xml", tr("Map paint style file (*.xml, *.mapcss, *.zip)"));
|
---|
886 | break;
|
---|
887 | case TAGGING_PRESET:
|
---|
888 | ff = new ExtensionFileFilter("xml,zip", "xml", tr("Preset definition file (*.xml, *.zip)"));
|
---|
889 | break;
|
---|
890 | case TAGCHECKER_RULE:
|
---|
891 | ff = new ExtensionFileFilter("validator.mapcss,zip", "validator.mapcss", tr("Tag checker rule (*.validator.mapcss, *.zip)"));
|
---|
892 | break;
|
---|
893 | default:
|
---|
894 | Logging.error("Unsupported source type: "+sourceType);
|
---|
895 | return;
|
---|
896 | }
|
---|
897 | FileChooserManager fcm = new FileChooserManager(true)
|
---|
898 | .createFileChooser(true, null, Arrays.asList(ff, FileFilterAllFiles.getInstance()), ff, JFileChooser.FILES_ONLY);
|
---|
899 | prepareFileChooser(tfURL.getText(), fcm.getFileChooser());
|
---|
900 | AbstractFileChooser fc = fcm.openFileChooser(GuiHelper.getFrameForComponent(SourceEditor.this));
|
---|
901 | if (fc != null) {
|
---|
902 | tfURL.setText(fc.getSelectedFile().toString());
|
---|
903 | }
|
---|
904 | }
|
---|
905 | }
|
---|
906 |
|
---|
907 | @Override
|
---|
908 | public String getTitle() {
|
---|
909 | return tfTitle.getText();
|
---|
910 | }
|
---|
911 |
|
---|
912 | /**
|
---|
913 | * Returns the entered URL / File.
|
---|
914 | * @return the entered URL / File
|
---|
915 | */
|
---|
916 | public String getURL() {
|
---|
917 | return tfURL.getText();
|
---|
918 | }
|
---|
919 |
|
---|
920 | /**
|
---|
921 | * Determines if the active combobox is selected.
|
---|
922 | * @return {@code true} if the active combobox is selected
|
---|
923 | */
|
---|
924 | public boolean active() {
|
---|
925 | if (!canEnable)
|
---|
926 | throw new UnsupportedOperationException();
|
---|
927 | return cbActive.isSelected();
|
---|
928 | }
|
---|
929 | }
|
---|
930 |
|
---|
931 | class NewActiveSourceAction extends AbstractAction {
|
---|
932 | NewActiveSourceAction() {
|
---|
933 | putValue(NAME, tr("New"));
|
---|
934 | putValue(SHORT_DESCRIPTION, getStr(I18nString.NEW_SOURCE_ENTRY_TOOLTIP));
|
---|
935 | new ImageProvider("dialogs", "add").getResource().attachImageIcon(this);
|
---|
936 | }
|
---|
937 |
|
---|
938 | @Override
|
---|
939 | public void actionPerformed(ActionEvent evt) {
|
---|
940 | EditSourceEntryDialog editEntryDialog = new EditSourceEntryDialog(
|
---|
941 | SourceEditor.this,
|
---|
942 | getStr(I18nString.NEW_SOURCE_ENTRY),
|
---|
943 | null);
|
---|
944 | editEntryDialog.showDialog();
|
---|
945 | if (editEntryDialog.getValue() == 1) {
|
---|
946 | boolean active = true;
|
---|
947 | if (canEnable) {
|
---|
948 | active = editEntryDialog.active();
|
---|
949 | }
|
---|
950 | final SourceEntry entry = new SourceEntry(sourceType,
|
---|
951 | editEntryDialog.getURL(),
|
---|
952 | null, editEntryDialog.getTitle(), active);
|
---|
953 | entry.title = getTitleForSourceEntry(entry);
|
---|
954 | activeSourcesModel.addSource(entry);
|
---|
955 | activeSourcesModel.fireTableDataChanged();
|
---|
956 | }
|
---|
957 | }
|
---|
958 | }
|
---|
959 |
|
---|
960 | class RemoveActiveSourcesAction extends AbstractAction implements ListSelectionListener {
|
---|
961 |
|
---|
962 | RemoveActiveSourcesAction() {
|
---|
963 | putValue(NAME, tr("Remove"));
|
---|
964 | putValue(SHORT_DESCRIPTION, getStr(I18nString.REMOVE_SOURCE_TOOLTIP));
|
---|
965 | new ImageProvider("dialogs", "delete").getResource().attachImageIcon(this);
|
---|
966 | updateEnabledState();
|
---|
967 | }
|
---|
968 |
|
---|
969 | protected final void updateEnabledState() {
|
---|
970 | setEnabled(tblActiveSources.getSelectedRowCount() > 0);
|
---|
971 | }
|
---|
972 |
|
---|
973 | @Override
|
---|
974 | public void valueChanged(ListSelectionEvent e) {
|
---|
975 | updateEnabledState();
|
---|
976 | }
|
---|
977 |
|
---|
978 | @Override
|
---|
979 | public void actionPerformed(ActionEvent e) {
|
---|
980 | activeSourcesModel.removeSelected();
|
---|
981 | }
|
---|
982 | }
|
---|
983 |
|
---|
984 | class EditActiveSourceAction extends AbstractAction implements ListSelectionListener {
|
---|
985 | EditActiveSourceAction() {
|
---|
986 | putValue(NAME, tr("Edit"));
|
---|
987 | putValue(SHORT_DESCRIPTION, getStr(I18nString.EDIT_SOURCE_TOOLTIP));
|
---|
988 | new ImageProvider("dialogs", "edit").getResource().attachImageIcon(this);
|
---|
989 | updateEnabledState();
|
---|
990 | }
|
---|
991 |
|
---|
992 | protected final void updateEnabledState() {
|
---|
993 | setEnabled(tblActiveSources.getSelectedRowCount() == 1);
|
---|
994 | }
|
---|
995 |
|
---|
996 | @Override
|
---|
997 | public void valueChanged(ListSelectionEvent e) {
|
---|
998 | updateEnabledState();
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | @Override
|
---|
1002 | public void actionPerformed(ActionEvent evt) {
|
---|
1003 | int pos = tblActiveSources.getSelectedRow();
|
---|
1004 | if (pos < 0 || pos >= tblActiveSources.getRowCount())
|
---|
1005 | return;
|
---|
1006 |
|
---|
1007 | SourceEntry e = (SourceEntry) activeSourcesModel.getValueAt(pos, 1);
|
---|
1008 |
|
---|
1009 | EditSourceEntryDialog editEntryDialog = new EditSourceEntryDialog(
|
---|
1010 | SourceEditor.this, tr("Edit source entry:"), e);
|
---|
1011 | editEntryDialog.showDialog();
|
---|
1012 | if (editEntryDialog.getValue() == 1) {
|
---|
1013 | if (e.title != null || !"".equals(editEntryDialog.getTitle())) {
|
---|
1014 | e.title = editEntryDialog.getTitle();
|
---|
1015 | e.title = getTitleForSourceEntry(e);
|
---|
1016 | }
|
---|
1017 | e.url = editEntryDialog.getURL();
|
---|
1018 | if (canEnable) {
|
---|
1019 | e.active = editEntryDialog.active();
|
---|
1020 | }
|
---|
1021 | activeSourcesModel.fireTableRowsUpdated(pos, pos);
|
---|
1022 | }
|
---|
1023 | }
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | /**
|
---|
1027 | * The action to move the currently selected entries up or down in the list.
|
---|
1028 | */
|
---|
1029 | class MoveUpDownAction extends AbstractAction implements ListSelectionListener, TableModelListener {
|
---|
1030 | private final int increment;
|
---|
1031 |
|
---|
1032 | MoveUpDownAction(boolean isDown) {
|
---|
1033 | increment = isDown ? 1 : -1;
|
---|
1034 | new ImageProvider("dialogs", isDown ? "down" : "up").getResource().attachImageIcon(this, true);
|
---|
1035 | putValue(SHORT_DESCRIPTION, isDown ? tr("Move the selected entry one row down.") : tr("Move the selected entry one row up."));
|
---|
1036 | updateEnabledState();
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | public final void updateEnabledState() {
|
---|
1040 | setEnabled(activeSourcesModel.canMove(increment));
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | @Override
|
---|
1044 | public void actionPerformed(ActionEvent e) {
|
---|
1045 | activeSourcesModel.move(increment, tblActiveSources.getSelectedRows());
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | @Override
|
---|
1049 | public void valueChanged(ListSelectionEvent e) {
|
---|
1050 | updateEnabledState();
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | @Override
|
---|
1054 | public void tableChanged(TableModelEvent e) {
|
---|
1055 | updateEnabledState();
|
---|
1056 | }
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | class ActivateSourcesAction extends AbstractAction implements ListSelectionListener {
|
---|
1060 | ActivateSourcesAction() {
|
---|
1061 | putValue(SHORT_DESCRIPTION, getStr(I18nString.ACTIVATE_TOOLTIP));
|
---|
1062 | new ImageProvider("preferences", "activate-right").getResource().attachImageIcon(this);
|
---|
1063 | updateEnabledState();
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | protected final void updateEnabledState() {
|
---|
1067 | setEnabled(lstAvailableSources.getSelectedIndices().length > 0);
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | @Override
|
---|
1071 | public void valueChanged(ListSelectionEvent e) {
|
---|
1072 | updateEnabledState();
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | @Override
|
---|
1076 | public void actionPerformed(ActionEvent e) {
|
---|
1077 | List<ExtendedSourceEntry> sources = availableSourcesModel.getSelected();
|
---|
1078 | int josmVersion = Version.getInstance().getVersion();
|
---|
1079 | if (josmVersion != Version.JOSM_UNKNOWN_VERSION) {
|
---|
1080 | Collection<String> messages = new ArrayList<>();
|
---|
1081 | for (ExtendedSourceEntry entry : sources) {
|
---|
1082 | if (entry.minJosmVersion != null && entry.minJosmVersion > josmVersion) {
|
---|
1083 | messages.add(tr("Entry ''{0}'' requires JOSM Version {1}. (Currently running: {2})",
|
---|
1084 | entry.title,
|
---|
1085 | Integer.toString(entry.minJosmVersion),
|
---|
1086 | Integer.toString(josmVersion))
|
---|
1087 | );
|
---|
1088 | }
|
---|
1089 | }
|
---|
1090 | if (!messages.isEmpty()) {
|
---|
1091 | ExtendedDialog dlg = new ExtendedDialog(MainApplication.getMainFrame(), tr("Warning"), tr("Cancel"), tr("Continue anyway"));
|
---|
1092 | dlg.setButtonIcons(
|
---|
1093 | ImageProvider.get("cancel"),
|
---|
1094 | new ImageProvider("ok").setMaxSize(ImageSizes.LARGEICON).addOverlay(
|
---|
1095 | new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1.0, 1.0)).get()
|
---|
1096 | );
|
---|
1097 | dlg.setToolTipTexts(
|
---|
1098 | tr("Cancel and return to the previous dialog"),
|
---|
1099 | tr("Ignore warning and install style anyway"));
|
---|
1100 | dlg.setContent("<html>" + tr("Some entries have unmet dependencies:") +
|
---|
1101 | "<br>" + String.join("<br>", messages) + "</html>");
|
---|
1102 | dlg.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
1103 | if (dlg.showDialog().getValue() != 2)
|
---|
1104 | return;
|
---|
1105 | }
|
---|
1106 | }
|
---|
1107 | activeSourcesModel.addExtendedSourceEntries(sources);
|
---|
1108 | }
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | class ResetAction extends AbstractAction {
|
---|
1112 |
|
---|
1113 | ResetAction() {
|
---|
1114 | putValue(NAME, tr("Reset"));
|
---|
1115 | putValue(SHORT_DESCRIPTION, tr("Reset to default"));
|
---|
1116 | new ImageProvider("preferences", "reset").getResource().attachImageIcon(this);
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | @Override
|
---|
1120 | public void actionPerformed(ActionEvent e) {
|
---|
1121 | activeSourcesModel.setActiveSources(getDefault());
|
---|
1122 | }
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | class ReloadSourcesAction extends AbstractAction {
|
---|
1126 | private final String url;
|
---|
1127 | private final transient List<SourceProvider> sourceProviders;
|
---|
1128 |
|
---|
1129 | ReloadSourcesAction(String url, List<SourceProvider> sourceProviders) {
|
---|
1130 | putValue(NAME, tr("Reload"));
|
---|
1131 | putValue(SHORT_DESCRIPTION, tr(getStr(I18nString.RELOAD_ALL_AVAILABLE), url));
|
---|
1132 | new ImageProvider("dialogs", "refresh").getResource().attachImageIcon(this);
|
---|
1133 | this.url = url;
|
---|
1134 | this.sourceProviders = sourceProviders;
|
---|
1135 | setEnabled(!NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | @Override
|
---|
1139 | public void actionPerformed(ActionEvent e) {
|
---|
1140 | CachedFile.cleanup(url);
|
---|
1141 | reloadAvailableSources(url, sourceProviders);
|
---|
1142 | }
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | /**
|
---|
1146 | * Table model for icons paths.
|
---|
1147 | */
|
---|
1148 | protected static class IconPathTableModel extends AbstractTableModel {
|
---|
1149 | private final List<String> data;
|
---|
1150 | private final DefaultListSelectionModel selectionModel;
|
---|
1151 |
|
---|
1152 | /**
|
---|
1153 | * Constructs a new {@code IconPathTableModel}.
|
---|
1154 | * @param selectionModel selection model
|
---|
1155 | */
|
---|
1156 | public IconPathTableModel(DefaultListSelectionModel selectionModel) {
|
---|
1157 | this.selectionModel = selectionModel;
|
---|
1158 | this.data = new ArrayList<>();
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | @Override
|
---|
1162 | public int getColumnCount() {
|
---|
1163 | return 1;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | @Override
|
---|
1167 | public int getRowCount() {
|
---|
1168 | return data == null ? 0 : data.size();
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | @Override
|
---|
1172 | public Object getValueAt(int rowIndex, int columnIndex) {
|
---|
1173 | return data.get(rowIndex);
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | @Override
|
---|
1177 | public boolean isCellEditable(int rowIndex, int columnIndex) {
|
---|
1178 | return true;
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | @Override
|
---|
1182 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
---|
1183 | updatePath(rowIndex, (String) aValue);
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | /**
|
---|
1187 | * Sets the icons paths.
|
---|
1188 | * @param paths icons paths
|
---|
1189 | */
|
---|
1190 | public void setIconPaths(Collection<String> paths) {
|
---|
1191 | data.clear();
|
---|
1192 | if (paths != null) {
|
---|
1193 | data.addAll(paths);
|
---|
1194 | }
|
---|
1195 | sort();
|
---|
1196 | fireTableDataChanged();
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | /**
|
---|
1200 | * Adds an icon path.
|
---|
1201 | * @param path icon path to add
|
---|
1202 | */
|
---|
1203 | public void addPath(String path) {
|
---|
1204 | if (path == null) return;
|
---|
1205 | data.add(path);
|
---|
1206 | sort();
|
---|
1207 | fireTableDataChanged();
|
---|
1208 | int idx = data.indexOf(path);
|
---|
1209 | if (idx >= 0) {
|
---|
1210 | selectionModel.setSelectionInterval(idx, idx);
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | /**
|
---|
1215 | * Updates icon path at given index.
|
---|
1216 | * @param pos position
|
---|
1217 | * @param path new path
|
---|
1218 | */
|
---|
1219 | public void updatePath(int pos, String path) {
|
---|
1220 | if (path == null) return;
|
---|
1221 | if (pos < 0 || pos >= getRowCount()) return;
|
---|
1222 | data.set(pos, path);
|
---|
1223 | sort();
|
---|
1224 | fireTableDataChanged();
|
---|
1225 | int idx = data.indexOf(path);
|
---|
1226 | if (idx >= 0) {
|
---|
1227 | selectionModel.setSelectionInterval(idx, idx);
|
---|
1228 | }
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | /**
|
---|
1232 | * Removes the selected path.
|
---|
1233 | */
|
---|
1234 | public void removeSelected() {
|
---|
1235 | Iterator<String> it = data.iterator();
|
---|
1236 | int i = 0;
|
---|
1237 | while (it.hasNext()) {
|
---|
1238 | it.next();
|
---|
1239 | if (selectionModel.isSelectedIndex(i)) {
|
---|
1240 | it.remove();
|
---|
1241 | }
|
---|
1242 | i++;
|
---|
1243 | }
|
---|
1244 | fireTableDataChanged();
|
---|
1245 | selectionModel.clearSelection();
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | /**
|
---|
1249 | * Sorts paths lexicographically.
|
---|
1250 | */
|
---|
1251 | protected void sort() {
|
---|
1252 | data.sort((o1, o2) -> {
|
---|
1253 | if (o1.isEmpty() && o2.isEmpty())
|
---|
1254 | return 0;
|
---|
1255 | if (o1.isEmpty()) return 1;
|
---|
1256 | if (o2.isEmpty()) return -1;
|
---|
1257 | return o1.compareTo(o2);
|
---|
1258 | });
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | /**
|
---|
1262 | * Returns the icon paths.
|
---|
1263 | * @return the icon paths
|
---|
1264 | */
|
---|
1265 | public List<String> getIconPaths() {
|
---|
1266 | return new ArrayList<>(data);
|
---|
1267 | }
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | class NewIconPathAction extends AbstractAction {
|
---|
1271 | NewIconPathAction() {
|
---|
1272 | putValue(NAME, tr("New"));
|
---|
1273 | putValue(SHORT_DESCRIPTION, tr("Add a new icon path"));
|
---|
1274 | new ImageProvider("dialogs", "add").getResource().attachImageIcon(this);
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 | @Override
|
---|
1278 | public void actionPerformed(ActionEvent e) {
|
---|
1279 | iconPathsModel.addPath("");
|
---|
1280 | tblIconPaths.editCellAt(iconPathsModel.getRowCount() -1, 0);
|
---|
1281 | }
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | class RemoveIconPathAction extends AbstractAction implements ListSelectionListener {
|
---|
1285 | RemoveIconPathAction() {
|
---|
1286 | putValue(NAME, tr("Remove"));
|
---|
1287 | putValue(SHORT_DESCRIPTION, tr("Remove the selected icon paths"));
|
---|
1288 | new ImageProvider("dialogs", "delete").getResource().attachImageIcon(this);
|
---|
1289 | updateEnabledState();
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | protected final void updateEnabledState() {
|
---|
1293 | setEnabled(tblIconPaths.getSelectedRowCount() > 0);
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | @Override
|
---|
1297 | public void valueChanged(ListSelectionEvent e) {
|
---|
1298 | updateEnabledState();
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | @Override
|
---|
1302 | public void actionPerformed(ActionEvent e) {
|
---|
1303 | iconPathsModel.removeSelected();
|
---|
1304 | }
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | class EditIconPathAction extends AbstractAction implements ListSelectionListener {
|
---|
1308 | EditIconPathAction() {
|
---|
1309 | putValue(NAME, tr("Edit"));
|
---|
1310 | putValue(SHORT_DESCRIPTION, tr("Edit the selected icon path"));
|
---|
1311 | new ImageProvider("dialogs", "edit").getResource().attachImageIcon(this);
|
---|
1312 | updateEnabledState();
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | protected final void updateEnabledState() {
|
---|
1316 | setEnabled(tblIconPaths.getSelectedRowCount() == 1);
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | @Override
|
---|
1320 | public void valueChanged(ListSelectionEvent e) {
|
---|
1321 | updateEnabledState();
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | @Override
|
---|
1325 | public void actionPerformed(ActionEvent e) {
|
---|
1326 | int row = tblIconPaths.getSelectedRow();
|
---|
1327 | tblIconPaths.editCellAt(row, 0);
|
---|
1328 | }
|
---|
1329 | }
|
---|
1330 |
|
---|
1331 | static class SourceEntryListCellRenderer extends JLabel implements ListCellRenderer<ExtendedSourceEntry> {
|
---|
1332 |
|
---|
1333 | private static final NamedColorProperty SOURCE_ENTRY_ACTIVE_BACKGROUND_COLOR = new NamedColorProperty(
|
---|
1334 | marktr("External resource entry: Active"),
|
---|
1335 | new Color(200, 255, 200));
|
---|
1336 | private static final NamedColorProperty SOURCE_ENTRY_INACTIVE_BACKGROUND_COLOR = new NamedColorProperty(
|
---|
1337 | marktr("External resource entry: Inactive"),
|
---|
1338 | new Color(200, 200, 200));
|
---|
1339 |
|
---|
1340 | private final Map<String, SourceEntry> entryByUrl = new HashMap<>();
|
---|
1341 |
|
---|
1342 | @Override
|
---|
1343 | public Component getListCellRendererComponent(JList<? extends ExtendedSourceEntry> list, ExtendedSourceEntry value,
|
---|
1344 | int index, boolean isSelected, boolean cellHasFocus) {
|
---|
1345 | String s = value.toString();
|
---|
1346 | setText(s);
|
---|
1347 | if (isSelected) {
|
---|
1348 | setBackground(list.getSelectionBackground());
|
---|
1349 | setForeground(list.getSelectionForeground());
|
---|
1350 | } else {
|
---|
1351 | setBackground(list.getBackground());
|
---|
1352 | setForeground(list.getForeground());
|
---|
1353 | }
|
---|
1354 | setEnabled(list.isEnabled());
|
---|
1355 | setFont(list.getFont());
|
---|
1356 | setFont(getFont().deriveFont(Font.PLAIN));
|
---|
1357 | setOpaque(true);
|
---|
1358 | setToolTipText(value.getTooltip());
|
---|
1359 | if (!isSelected) {
|
---|
1360 | final SourceEntry sourceEntry = entryByUrl.get(value.url);
|
---|
1361 | GuiHelper.setBackgroundReadable(this, sourceEntry == null ? UIManager.getColor("Table.background") :
|
---|
1362 | sourceEntry.active ? SOURCE_ENTRY_ACTIVE_BACKGROUND_COLOR.get() : SOURCE_ENTRY_INACTIVE_BACKGROUND_COLOR.get());
|
---|
1363 | }
|
---|
1364 | final ImageSizes size = ImageSizes.TABLE;
|
---|
1365 | setIcon(value.icon == null ? ImageProvider.getEmpty(size) : value.icon.getImageIconBounded(size.getImageDimension()));
|
---|
1366 | return this;
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | public void updateSources(List<SourceEntry> sources) {
|
---|
1370 | synchronized (entryByUrl) {
|
---|
1371 | entryByUrl.clear();
|
---|
1372 | for (SourceEntry i : sources) {
|
---|
1373 | entryByUrl.put(i.url, i);
|
---|
1374 | }
|
---|
1375 | }
|
---|
1376 | }
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 | class SourceLoader extends PleaseWaitRunnable {
|
---|
1380 | private final String url;
|
---|
1381 | private final List<SourceProvider> sourceProviders;
|
---|
1382 | private CachedFile cachedFile;
|
---|
1383 | private boolean canceled;
|
---|
1384 | private final List<ExtendedSourceEntry> sources = new ArrayList<>();
|
---|
1385 |
|
---|
1386 | SourceLoader(String url, List<SourceProvider> sourceProviders) {
|
---|
1387 | super(tr(getStr(I18nString.LOADING_SOURCES_FROM), url));
|
---|
1388 | this.url = url;
|
---|
1389 | this.sourceProviders = sourceProviders;
|
---|
1390 | }
|
---|
1391 |
|
---|
1392 | @Override
|
---|
1393 | protected void cancel() {
|
---|
1394 | canceled = true;
|
---|
1395 | Utils.close(cachedFile);
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | protected void warn(Exception e) {
|
---|
1399 | String emsg = Utils.escapeReservedCharactersHTML(e.getMessage() != null ? e.getMessage() : e.toString());
|
---|
1400 | final String msg = tr(getStr(I18nString.FAILED_TO_LOAD_SOURCES_FROM), url, emsg);
|
---|
1401 |
|
---|
1402 | GuiHelper.runInEDT(() -> HelpAwareOptionPane.showOptionDialog(
|
---|
1403 | MainApplication.getMainFrame(),
|
---|
1404 | msg,
|
---|
1405 | tr("Error"),
|
---|
1406 | JOptionPane.ERROR_MESSAGE,
|
---|
1407 | ht(getStr(I18nString.FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC))
|
---|
1408 | ));
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | @Override
|
---|
1412 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
1413 | try {
|
---|
1414 | sources.addAll(getDefault());
|
---|
1415 |
|
---|
1416 | for (SourceProvider provider : sourceProviders) {
|
---|
1417 | for (SourceEntry src : provider.getSources()) {
|
---|
1418 | if (src instanceof ExtendedSourceEntry) {
|
---|
1419 | sources.add((ExtendedSourceEntry) src);
|
---|
1420 | }
|
---|
1421 | }
|
---|
1422 | }
|
---|
1423 | readFile();
|
---|
1424 | if (sources.removeIf(extendedSourceEntry -> "xml".equals(extendedSourceEntry.styleType))) {
|
---|
1425 | Logging.debug("Removing XML source entry");
|
---|
1426 | }
|
---|
1427 | } catch (IOException e) {
|
---|
1428 | if (canceled)
|
---|
1429 | // ignore the exception and return
|
---|
1430 | return;
|
---|
1431 | OsmTransferException ex = new OsmTransferException(e);
|
---|
1432 | ex.setUrl(url);
|
---|
1433 | warn(ex);
|
---|
1434 | }
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | protected void readFile() throws IOException {
|
---|
1438 | final String lang = LanguageInfo.getLanguageCodeXML();
|
---|
1439 | cachedFile = new CachedFile(url);
|
---|
1440 | try (BufferedReader reader = cachedFile.getContentReader()) {
|
---|
1441 |
|
---|
1442 | String line;
|
---|
1443 | ExtendedSourceEntry last = null;
|
---|
1444 |
|
---|
1445 | while ((line = reader.readLine()) != null && !canceled) {
|
---|
1446 | if (line.trim().isEmpty()) {
|
---|
1447 | continue; // skip empty lines
|
---|
1448 | }
|
---|
1449 | if (line.startsWith("\t")) {
|
---|
1450 | Matcher m = Pattern.compile("^\t([^:]+): *(.+)$").matcher(line);
|
---|
1451 | if (!m.matches()) {
|
---|
1452 | Logging.error(tr(getStr(I18nString.ILLEGAL_FORMAT_OF_ENTRY), url, line));
|
---|
1453 | continue;
|
---|
1454 | }
|
---|
1455 | if (last != null) {
|
---|
1456 | String key = m.group(1);
|
---|
1457 | String value = m.group(2);
|
---|
1458 | if ("author".equals(key) && last.author == null) {
|
---|
1459 | last.author = value;
|
---|
1460 | } else if ("version".equals(key)) {
|
---|
1461 | last.version = value;
|
---|
1462 | } else if ("icon".equals(key) && last.icon == null) {
|
---|
1463 | last.icon = new ImageProvider(value).setOptional(true).getResource();
|
---|
1464 | } else if ("link".equals(key) && last.link == null) {
|
---|
1465 | last.link = value;
|
---|
1466 | } else if ("description".equals(key) && last.description == null) {
|
---|
1467 | last.description = value;
|
---|
1468 | } else if ((lang + "shortdescription").equals(key) && last.title == null) {
|
---|
1469 | last.title = value;
|
---|
1470 | } else if ("shortdescription".equals(key) && last.title == null) {
|
---|
1471 | last.title = value;
|
---|
1472 | } else if ((lang + "title").equals(key) && last.title == null) {
|
---|
1473 | last.title = value;
|
---|
1474 | } else if ("title".equals(key) && last.title == null) {
|
---|
1475 | last.title = value;
|
---|
1476 | } else if ("name".equals(key) && last.name == null) {
|
---|
1477 | last.name = value;
|
---|
1478 | } else if ((lang + "author").equals(key)) {
|
---|
1479 | last.author = value;
|
---|
1480 | } else if ((lang + "link").equals(key)) {
|
---|
1481 | last.link = value;
|
---|
1482 | } else if ((lang + "description").equals(key)) {
|
---|
1483 | last.description = value;
|
---|
1484 | } else if ("min-josm-version".equals(key)) {
|
---|
1485 | try {
|
---|
1486 | last.minJosmVersion = Integer.valueOf(value);
|
---|
1487 | } catch (NumberFormatException e) {
|
---|
1488 | // ignore
|
---|
1489 | Logging.trace(e);
|
---|
1490 | }
|
---|
1491 | } else if ("style-type".equals(key)) {
|
---|
1492 | last.styleType = value;
|
---|
1493 | }
|
---|
1494 | }
|
---|
1495 | } else {
|
---|
1496 | last = null;
|
---|
1497 | Matcher m = Pattern.compile("^(.+);(.+)$").matcher(line);
|
---|
1498 | if (m.matches()) {
|
---|
1499 | last = new ExtendedSourceEntry(sourceType, m.group(1), m.group(2));
|
---|
1500 | sources.add(last);
|
---|
1501 | } else {
|
---|
1502 | Logging.error(tr(getStr(I18nString.ILLEGAL_FORMAT_OF_ENTRY), url, line));
|
---|
1503 | }
|
---|
1504 | }
|
---|
1505 | }
|
---|
1506 | }
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | @Override
|
---|
1510 | protected void finish() {
|
---|
1511 | Collections.sort(sources);
|
---|
1512 | availableSourcesModel.setSources(sources);
|
---|
1513 | }
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | static class SourceEntryTableCellRenderer extends DefaultTableCellRenderer {
|
---|
1517 | @Override
|
---|
1518 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
1519 | if (value == null)
|
---|
1520 | return this;
|
---|
1521 | return super.getTableCellRendererComponent(table,
|
---|
1522 | fromSourceEntry((SourceEntry) value), isSelected, hasFocus, row, column);
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | private static String fromSourceEntry(SourceEntry entry) {
|
---|
1526 | if (entry == null)
|
---|
1527 | return null;
|
---|
1528 | StringBuilder s = new StringBuilder(128).append("<html><b>");
|
---|
1529 | if (entry.title != null) {
|
---|
1530 | s.append(Utils.escapeReservedCharactersHTML(entry.title)).append("</b> <span color=\"gray\">");
|
---|
1531 | }
|
---|
1532 | s.append(entry.url);
|
---|
1533 | if (entry.title != null) {
|
---|
1534 | s.append("</span>");
|
---|
1535 | }
|
---|
1536 | s.append("</html>");
|
---|
1537 | return s.toString();
|
---|
1538 | }
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | class FileOrUrlCellEditor extends JPanel implements TableCellEditor {
|
---|
1542 | private final JosmTextField tfFileName = new JosmTextField();
|
---|
1543 | private final CopyOnWriteArrayList<CellEditorListener> listeners;
|
---|
1544 | private String value;
|
---|
1545 | private final boolean isFile;
|
---|
1546 |
|
---|
1547 | /**
|
---|
1548 | * build the GUI
|
---|
1549 | */
|
---|
1550 | protected final void build() {
|
---|
1551 | setLayout(new GridBagLayout());
|
---|
1552 | GridBagConstraints gc = new GridBagConstraints();
|
---|
1553 | gc.gridx = 0;
|
---|
1554 | gc.gridy = 0;
|
---|
1555 | gc.fill = GridBagConstraints.BOTH;
|
---|
1556 | gc.weightx = 1.0;
|
---|
1557 | gc.weighty = 1.0;
|
---|
1558 | add(tfFileName, gc);
|
---|
1559 |
|
---|
1560 | gc.gridx = 1;
|
---|
1561 | gc.gridy = 0;
|
---|
1562 | gc.fill = GridBagConstraints.BOTH;
|
---|
1563 | gc.weightx = 0.0;
|
---|
1564 | gc.weighty = 1.0;
|
---|
1565 | add(new JButton(new LaunchFileChooserAction()));
|
---|
1566 |
|
---|
1567 | tfFileName.addFocusListener(
|
---|
1568 | new FocusAdapter() {
|
---|
1569 | @Override
|
---|
1570 | public void focusGained(FocusEvent e) {
|
---|
1571 | tfFileName.selectAll();
|
---|
1572 | }
|
---|
1573 | }
|
---|
1574 | );
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | FileOrUrlCellEditor(boolean isFile) {
|
---|
1578 | this.isFile = isFile;
|
---|
1579 | listeners = new CopyOnWriteArrayList<>();
|
---|
1580 | build();
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 | @Override
|
---|
1584 | public void addCellEditorListener(CellEditorListener l) {
|
---|
1585 | if (l != null) {
|
---|
1586 | listeners.addIfAbsent(l);
|
---|
1587 | }
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | protected void fireEditingCanceled() {
|
---|
1591 | for (CellEditorListener l: listeners) {
|
---|
1592 | l.editingCanceled(new ChangeEvent(this));
|
---|
1593 | }
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 | protected void fireEditingStopped() {
|
---|
1597 | for (CellEditorListener l: listeners) {
|
---|
1598 | l.editingStopped(new ChangeEvent(this));
|
---|
1599 | }
|
---|
1600 | }
|
---|
1601 |
|
---|
1602 | @Override
|
---|
1603 | public void cancelCellEditing() {
|
---|
1604 | fireEditingCanceled();
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | @Override
|
---|
1608 | public Object getCellEditorValue() {
|
---|
1609 | return value;
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | @Override
|
---|
1613 | public boolean isCellEditable(EventObject anEvent) {
|
---|
1614 | if (anEvent instanceof MouseEvent)
|
---|
1615 | return ((MouseEvent) anEvent).getClickCount() >= 2;
|
---|
1616 | return true;
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | @Override
|
---|
1620 | public void removeCellEditorListener(CellEditorListener l) {
|
---|
1621 | listeners.remove(l);
|
---|
1622 | }
|
---|
1623 |
|
---|
1624 | @Override
|
---|
1625 | public boolean shouldSelectCell(EventObject anEvent) {
|
---|
1626 | return true;
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | @Override
|
---|
1630 | public boolean stopCellEditing() {
|
---|
1631 | value = tfFileName.getText();
|
---|
1632 | fireEditingStopped();
|
---|
1633 | return true;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | public void setInitialValue(String initialValue) {
|
---|
1637 | this.value = initialValue;
|
---|
1638 | if (initialValue == null) {
|
---|
1639 | this.tfFileName.setText("");
|
---|
1640 | } else {
|
---|
1641 | this.tfFileName.setText(initialValue);
|
---|
1642 | }
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | @Override
|
---|
1646 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
1647 | setInitialValue((String) value);
|
---|
1648 | tfFileName.selectAll();
|
---|
1649 | return this;
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | class LaunchFileChooserAction extends AbstractAction {
|
---|
1653 | LaunchFileChooserAction() {
|
---|
1654 | putValue(NAME, "...");
|
---|
1655 | putValue(SHORT_DESCRIPTION, tr("Launch a file chooser to select a file"));
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | @Override
|
---|
1659 | public void actionPerformed(ActionEvent e) {
|
---|
1660 | FileChooserManager fcm = new FileChooserManager(true).createFileChooser();
|
---|
1661 | if (!isFile) {
|
---|
1662 | fcm.getFileChooser().setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
---|
1663 | }
|
---|
1664 | prepareFileChooser(tfFileName.getText(), fcm.getFileChooser());
|
---|
1665 | AbstractFileChooser fc = fcm.openFileChooser(GuiHelper.getFrameForComponent(SourceEditor.this));
|
---|
1666 | if (fc != null) {
|
---|
1667 | tfFileName.setText(fc.getSelectedFile().toString());
|
---|
1668 | }
|
---|
1669 | }
|
---|
1670 | }
|
---|
1671 | }
|
---|
1672 |
|
---|
1673 | /**
|
---|
1674 | * Defers loading of sources to the first time the adequate tab is selected.
|
---|
1675 | * @param tab The preferences tab
|
---|
1676 | * @param component The tab component
|
---|
1677 | * @since 6670
|
---|
1678 | */
|
---|
1679 | public final void deferLoading(final DefaultTabPreferenceSetting tab, final Component component) {
|
---|
1680 | tab.getTabPane().addChangeListener(e -> {
|
---|
1681 | if (tab.getTabPane().getSelectedComponent() == component) {
|
---|
1682 | initiallyLoadAvailableSources();
|
---|
1683 | }
|
---|
1684 | });
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | /**
|
---|
1688 | * Returns the title of the given source entry.
|
---|
1689 | * @param entry source entry
|
---|
1690 | * @return the title of the given source entry, or null if empty
|
---|
1691 | */
|
---|
1692 | protected String getTitleForSourceEntry(SourceEntry entry) {
|
---|
1693 | return "".equals(entry.title) ? null : entry.title;
|
---|
1694 | }
|
---|
1695 | }
|
---|