1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.imagery;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.FlowLayout;
|
---|
11 | import java.awt.Font;
|
---|
12 | import java.awt.GridBagConstraints;
|
---|
13 | import java.awt.GridBagLayout;
|
---|
14 | import java.awt.event.ActionEvent;
|
---|
15 | import java.awt.event.ActionListener;
|
---|
16 | import java.awt.event.MouseEvent;
|
---|
17 | import java.io.IOException;
|
---|
18 | import java.net.MalformedURLException;
|
---|
19 | import java.net.URL;
|
---|
20 | import java.util.ArrayList;
|
---|
21 | import java.util.HashMap;
|
---|
22 | import java.util.HashSet;
|
---|
23 | import java.util.List;
|
---|
24 | import java.util.Map;
|
---|
25 | import java.util.Set;
|
---|
26 |
|
---|
27 | import javax.swing.AbstractAction;
|
---|
28 | import javax.swing.BorderFactory;
|
---|
29 | import javax.swing.Box;
|
---|
30 | import javax.swing.JButton;
|
---|
31 | import javax.swing.JLabel;
|
---|
32 | import javax.swing.JOptionPane;
|
---|
33 | import javax.swing.JPanel;
|
---|
34 | import javax.swing.JScrollPane;
|
---|
35 | import javax.swing.JSeparator;
|
---|
36 | import javax.swing.JTabbedPane;
|
---|
37 | import javax.swing.JTable;
|
---|
38 | import javax.swing.JToolBar;
|
---|
39 | import javax.swing.event.ListSelectionEvent;
|
---|
40 | import javax.swing.event.ListSelectionListener;
|
---|
41 | import javax.swing.event.TableModelEvent;
|
---|
42 | import javax.swing.event.TableModelListener;
|
---|
43 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
44 | import javax.swing.table.DefaultTableModel;
|
---|
45 | import javax.swing.table.TableColumnModel;
|
---|
46 |
|
---|
47 | import org.openstreetmap.gui.jmapviewer.Coordinate;
|
---|
48 | import org.openstreetmap.gui.jmapviewer.JMapViewer;
|
---|
49 | import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;
|
---|
50 | import org.openstreetmap.gui.jmapviewer.MapRectangleImpl;
|
---|
51 | import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon;
|
---|
52 | import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle;
|
---|
53 | import org.openstreetmap.josm.Main;
|
---|
54 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
55 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryBounds;
|
---|
56 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
57 | import org.openstreetmap.josm.data.imagery.OffsetBookmark;
|
---|
58 | import org.openstreetmap.josm.data.imagery.Shape;
|
---|
59 | import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
|
---|
60 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
61 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
62 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
63 | import org.openstreetmap.josm.gui.widgets.JosmEditorPane;
|
---|
64 | import org.openstreetmap.josm.tools.GBC;
|
---|
65 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
66 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
67 |
|
---|
68 | public class ImageryPreference extends DefaultTabPreferenceSetting {
|
---|
69 | public static class Factory implements PreferenceSettingFactory {
|
---|
70 | @Override
|
---|
71 | public PreferenceSetting createPreferenceSetting() {
|
---|
72 | return new ImageryPreference();
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | private ImageryPreference() {
|
---|
77 | super("imagery", tr("Imagery Preferences"), tr("Modify list of imagery layers displayed in the Imagery menu"), false, new JTabbedPane());
|
---|
78 | }
|
---|
79 |
|
---|
80 | private ImageryProvidersPanel imageryProviders;
|
---|
81 | private ImageryLayerInfo layerInfo;
|
---|
82 |
|
---|
83 | private CommonSettingsPanel commonSettings;
|
---|
84 | private WMSSettingsPanel wmsSettings;
|
---|
85 | private TMSSettingsPanel tmsSettings;
|
---|
86 |
|
---|
87 | private void addSettingsSection(final JPanel p, String name, JPanel section) {
|
---|
88 | addSettingsSection(p, name, section, GBC.eol());
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void addSettingsSection(final JPanel p, String name, JPanel section, GBC gbc) {
|
---|
92 | final JLabel lbl = new JLabel(name);
|
---|
93 | lbl.setFont(lbl.getFont().deriveFont(Font.BOLD));
|
---|
94 | p.add(lbl,GBC.std());
|
---|
95 | p.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 0));
|
---|
96 | p.add(section, gbc.insets(20,5,0,10));
|
---|
97 | }
|
---|
98 |
|
---|
99 | private Component buildSettingsPanel(final PreferenceTabbedPane gui) {
|
---|
100 | final JPanel p = new JPanel(new GridBagLayout());
|
---|
101 | p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
102 |
|
---|
103 | addSettingsSection(p, tr("Common Settings"), commonSettings = new CommonSettingsPanel());
|
---|
104 | addSettingsSection(p, tr("WMS Settings"), wmsSettings = new WMSSettingsPanel(),
|
---|
105 | GBC.eol().fill(GBC.HORIZONTAL));
|
---|
106 | addSettingsSection(p, tr("TMS Settings"), tmsSettings = new TMSSettingsPanel(),
|
---|
107 | GBC.eol().fill(GBC.HORIZONTAL));
|
---|
108 |
|
---|
109 | p.add(new JPanel(),GBC.eol().fill(GBC.BOTH));
|
---|
110 | return new JScrollPane(p);
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public void addGui(final PreferenceTabbedPane gui) {
|
---|
115 | JPanel p = gui.createPreferenceTab(this);
|
---|
116 | JTabbedPane pane = getTabPane();
|
---|
117 | layerInfo = new ImageryLayerInfo(ImageryLayerInfo.instance);
|
---|
118 | imageryProviders = new ImageryProvidersPanel(gui, layerInfo);
|
---|
119 | pane.addTab(tr("Imagery providers"), imageryProviders);
|
---|
120 | pane.addTab(tr("Settings"), buildSettingsPanel(gui));
|
---|
121 | pane.addTab(tr("Offset bookmarks"), new OffsetBookmarksPanel(gui));
|
---|
122 | loadSettings();
|
---|
123 | p.add(pane,GBC.std().fill(GBC.BOTH));
|
---|
124 | }
|
---|
125 |
|
---|
126 | public ImageryProvidersPanel getProvidersPanel() {
|
---|
127 | return imageryProviders;
|
---|
128 | }
|
---|
129 |
|
---|
130 | private void loadSettings() {
|
---|
131 | commonSettings.loadSettings();
|
---|
132 | wmsSettings.loadSettings();
|
---|
133 | tmsSettings.loadSettings();
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | public boolean ok() {
|
---|
138 | layerInfo.save();
|
---|
139 | ImageryLayerInfo.instance.clear();
|
---|
140 | ImageryLayerInfo.instance.load();
|
---|
141 | Main.main.menu.imageryMenu.refreshOffsetMenu();
|
---|
142 | OffsetBookmark.saveBookmarks();
|
---|
143 |
|
---|
144 | boolean commonRestartRequired = commonSettings.saveSettings();
|
---|
145 | boolean wmsRestartRequired = wmsSettings.saveSettings();
|
---|
146 | boolean tmsRestartRequired = tmsSettings.saveSettings();
|
---|
147 |
|
---|
148 | return commonRestartRequired || wmsRestartRequired || tmsRestartRequired;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Updates a server URL in the preferences dialog. Used by plugins.
|
---|
153 | *
|
---|
154 | * @param server
|
---|
155 | * The server name
|
---|
156 | * @param url
|
---|
157 | * The server URL
|
---|
158 | */
|
---|
159 | public void setServerUrl(String server, String url) {
|
---|
160 | for (int i = 0; i < imageryProviders.activeModel.getRowCount(); i++) {
|
---|
161 | if (server.equals(imageryProviders.activeModel.getValueAt(i, 0).toString())) {
|
---|
162 | imageryProviders.activeModel.setValueAt(url, i, 1);
|
---|
163 | return;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | imageryProviders.activeModel.addRow(new String[] { server, url });
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Gets a server URL in the preferences dialog. Used by plugins.
|
---|
171 | *
|
---|
172 | * @param server
|
---|
173 | * The server name
|
---|
174 | * @return The server URL
|
---|
175 | */
|
---|
176 | public String getServerUrl(String server) {
|
---|
177 | for (int i = 0; i < imageryProviders.activeModel.getRowCount(); i++) {
|
---|
178 | if (server.equals(imageryProviders.activeModel.getValueAt(i, 0).toString()))
|
---|
179 | return imageryProviders.activeModel.getValueAt(i, 1).toString();
|
---|
180 | }
|
---|
181 | return null;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public static class ImageryProvidersPanel extends JPanel {
|
---|
185 | // Public JTables and JMapViewer
|
---|
186 | public final JTable activeTable;
|
---|
187 | public final JTable defaultTable;
|
---|
188 | public final JMapViewer defaultMap;
|
---|
189 |
|
---|
190 | // Public models
|
---|
191 | public final ImageryLayerTableModel activeModel;
|
---|
192 | public final ImageryDefaultLayerTableModel defaultModel;
|
---|
193 |
|
---|
194 | // Public JToolbars
|
---|
195 | public final JToolBar activeToolbar;
|
---|
196 | public final JToolBar middleToolbar;
|
---|
197 | public final JToolBar defaultToolbar;
|
---|
198 |
|
---|
199 | // Private members
|
---|
200 | private final PreferenceTabbedPane gui;
|
---|
201 | private final ImageryLayerInfo layerInfo;
|
---|
202 |
|
---|
203 | private static class ImageryTableCellRenderer extends DefaultTableCellRenderer {
|
---|
204 |
|
---|
205 | private List<ImageryInfo> layers;
|
---|
206 |
|
---|
207 | public ImageryTableCellRenderer(List<ImageryInfo> layers) {
|
---|
208 | this.layers = layers;
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | public Component getTableCellRendererComponent(JTable table, Object value, boolean
|
---|
213 | isSelected, boolean hasFocus, int row, int column) {
|
---|
214 | JLabel label = (JLabel) super.getTableCellRendererComponent(
|
---|
215 | table, value, isSelected, hasFocus, row, column);
|
---|
216 | label.setBackground(Main.pref.getUIColor("Table.background"));
|
---|
217 | if (isSelected) {
|
---|
218 | label.setForeground(Main.pref.getUIColor("Table.foreground"));
|
---|
219 | }
|
---|
220 | if (value != null) { // Fix #8159
|
---|
221 | String t = value.toString();
|
---|
222 | for (ImageryInfo l : layers) {
|
---|
223 | if (l.getExtendedUrl().equals(t)) {
|
---|
224 | label.setBackground(Main.pref.getColor(
|
---|
225 | marktr("Imagery Background: Default"),
|
---|
226 | new Color(200,255,200)));
|
---|
227 | break;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | return label;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | public ImageryProvidersPanel(final PreferenceTabbedPane gui, ImageryLayerInfo layerInfoArg) {
|
---|
236 | super(new GridBagLayout());
|
---|
237 | this.gui = gui;
|
---|
238 | this.layerInfo = layerInfoArg;
|
---|
239 | this.activeModel = new ImageryLayerTableModel();
|
---|
240 |
|
---|
241 | activeTable = new JTable(activeModel) {
|
---|
242 | @Override
|
---|
243 | public String getToolTipText(MouseEvent e) {
|
---|
244 | java.awt.Point p = e.getPoint();
|
---|
245 | return activeModel.getValueAt(rowAtPoint(p), columnAtPoint(p)).toString();
|
---|
246 | }
|
---|
247 | };
|
---|
248 |
|
---|
249 | defaultModel = new ImageryDefaultLayerTableModel();
|
---|
250 | defaultTable = new JTable(defaultModel) {
|
---|
251 | @Override
|
---|
252 | public String getToolTipText(MouseEvent e) {
|
---|
253 | java.awt.Point p = e.getPoint();
|
---|
254 | return (String) defaultModel.getValueAt(rowAtPoint(p), columnAtPoint(p));
|
---|
255 | }
|
---|
256 | };
|
---|
257 |
|
---|
258 | defaultModel.addTableModelListener(
|
---|
259 | new TableModelListener() {
|
---|
260 | @Override
|
---|
261 | public void tableChanged(TableModelEvent e) {
|
---|
262 | activeTable.repaint();
|
---|
263 | }
|
---|
264 | }
|
---|
265 | );
|
---|
266 |
|
---|
267 | activeModel.addTableModelListener(
|
---|
268 | new TableModelListener() {
|
---|
269 | @Override
|
---|
270 | public void tableChanged(TableModelEvent e) {
|
---|
271 | defaultTable.repaint();
|
---|
272 | }
|
---|
273 | }
|
---|
274 | );
|
---|
275 |
|
---|
276 | TableColumnModel mod = defaultTable.getColumnModel();
|
---|
277 | mod.getColumn(2).setPreferredWidth(800);
|
---|
278 | mod.getColumn(2).setCellRenderer(new ImageryTableCellRenderer(layerInfo.getLayers()));
|
---|
279 | mod.getColumn(1).setPreferredWidth(400);
|
---|
280 | mod.getColumn(0).setPreferredWidth(50);
|
---|
281 |
|
---|
282 | mod = activeTable.getColumnModel();
|
---|
283 | mod.getColumn(1).setPreferredWidth(800);
|
---|
284 | mod.getColumn(1).setCellRenderer(new ImageryTableCellRenderer(layerInfo.getDefaultLayers()));
|
---|
285 | mod.getColumn(0).setPreferredWidth(200);
|
---|
286 |
|
---|
287 | RemoveEntryAction remove = new RemoveEntryAction();
|
---|
288 | activeTable.getSelectionModel().addListSelectionListener(remove);
|
---|
289 |
|
---|
290 | add(new JLabel(tr("Available default entries:")), GBC.eol().insets(5, 5, 0, 0));
|
---|
291 | // Add default item list
|
---|
292 | JScrollPane scrolldef = new JScrollPane(defaultTable);
|
---|
293 | scrolldef.setPreferredSize(new Dimension(200, 200));
|
---|
294 | add(scrolldef, GBC.std().insets(0, 5, 0, 0).fill(GridBagConstraints.BOTH).weight(1.0, 0.6).insets(5, 0, 0, 0));
|
---|
295 |
|
---|
296 | // Add default item map
|
---|
297 | defaultMap = new JMapViewer();
|
---|
298 | defaultMap.setZoomContolsVisible(false);
|
---|
299 | defaultMap.setMinimumSize(new Dimension(100, 200));
|
---|
300 | add(defaultMap, GBC.std().insets(5, 5, 0, 0).fill(GridBagConstraints.BOTH).weight(0.33, 0.6).insets(5, 0, 0, 0));
|
---|
301 |
|
---|
302 | defaultTable.getSelectionModel().addListSelectionListener(new DefListSelectionListener());
|
---|
303 |
|
---|
304 | defaultToolbar = new JToolBar(JToolBar.VERTICAL);
|
---|
305 | defaultToolbar.setFloatable(false);
|
---|
306 | defaultToolbar.setBorderPainted(false);
|
---|
307 | defaultToolbar.setOpaque(false);
|
---|
308 | defaultToolbar.add(new ReloadAction());
|
---|
309 | add(defaultToolbar, GBC.eol().anchor(GBC.SOUTH).insets(0, 0, 5, 0));
|
---|
310 |
|
---|
311 | ActivateAction activate = new ActivateAction();
|
---|
312 | defaultTable.getSelectionModel().addListSelectionListener(activate);
|
---|
313 | JButton btnActivate = new JButton(activate);
|
---|
314 |
|
---|
315 | middleToolbar = new JToolBar(JToolBar.HORIZONTAL);
|
---|
316 | middleToolbar.setFloatable(false);
|
---|
317 | middleToolbar.setBorderPainted(false);
|
---|
318 | middleToolbar.setOpaque(false);
|
---|
319 | middleToolbar.add(btnActivate);
|
---|
320 | add(middleToolbar, GBC.eol().anchor(GBC.CENTER).insets(5, 15, 5, 0));
|
---|
321 |
|
---|
322 | add(Box.createHorizontalGlue(), GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
---|
323 |
|
---|
324 | add(new JLabel(tr("Selected entries:")), GBC.eol().insets(5, 0, 0, 0));
|
---|
325 | JScrollPane scroll = new JScrollPane(activeTable);
|
---|
326 | add(scroll, GBC.std().fill(GridBagConstraints.BOTH).span(GridBagConstraints.RELATIVE).weight(1.0, 0.4).insets(5, 0, 0, 5));
|
---|
327 | scroll.setPreferredSize(new Dimension(200, 200));
|
---|
328 |
|
---|
329 | activeToolbar = new JToolBar(JToolBar.VERTICAL);
|
---|
330 | activeToolbar.setFloatable(false);
|
---|
331 | activeToolbar.setBorderPainted(false);
|
---|
332 | activeToolbar.setOpaque(false);
|
---|
333 | activeToolbar.add(new NewEntryAction(ImageryInfo.ImageryType.WMS));
|
---|
334 | activeToolbar.add(new NewEntryAction(ImageryInfo.ImageryType.TMS));
|
---|
335 | //activeToolbar.add(edit); TODO
|
---|
336 | activeToolbar.add(remove);
|
---|
337 | add(activeToolbar, GBC.eol().anchor(GBC.NORTH).insets(0, 0, 5, 5));
|
---|
338 |
|
---|
339 | }
|
---|
340 |
|
---|
341 | // Listener of default providers list selection
|
---|
342 | private final class DefListSelectionListener implements ListSelectionListener {
|
---|
343 | // The current drawn rectangles and polygons
|
---|
344 | private final Map<Integer, MapRectangle> mapRectangles;
|
---|
345 | private final Map<Integer, List<MapPolygon>> mapPolygons;
|
---|
346 |
|
---|
347 | private DefListSelectionListener() {
|
---|
348 | this.mapRectangles = new HashMap<Integer, MapRectangle>();
|
---|
349 | this.mapPolygons = new HashMap<Integer, List<MapPolygon>>();
|
---|
350 | }
|
---|
351 |
|
---|
352 | @Override
|
---|
353 | public void valueChanged(ListSelectionEvent e) {
|
---|
354 | // First index is set to -1 when the list is refreshed, so discard all map rectangles and polygons
|
---|
355 | if (e.getFirstIndex() == -1) {
|
---|
356 | defaultMap.removeAllMapRectangles();
|
---|
357 | defaultMap.removeAllMapPolygons();
|
---|
358 | mapRectangles.clear();
|
---|
359 | mapPolygons.clear();
|
---|
360 | // Only process complete (final) selection events
|
---|
361 | } else if (!e.getValueIsAdjusting()) {
|
---|
362 | for (int i = e.getFirstIndex(); i<=e.getLastIndex(); i++) {
|
---|
363 | updateBoundsAndShapes(i);
|
---|
364 | }
|
---|
365 | // If needed, adjust map to show all map rectangles and polygons
|
---|
366 | if (!mapRectangles.isEmpty() || !mapPolygons.isEmpty()) {
|
---|
367 | defaultMap.setDisplayToFitMapElements(false, true, true);
|
---|
368 | defaultMap.zoomOut();
|
---|
369 | }
|
---|
370 | }
|
---|
371 | }
|
---|
372 |
|
---|
373 | private void updateBoundsAndShapes(int i) {
|
---|
374 | ImageryBounds bounds = defaultModel.getRow(i).getBounds();
|
---|
375 | if (bounds != null) {
|
---|
376 | List<Shape> shapes = bounds.getShapes();
|
---|
377 | if (shapes != null && !shapes.isEmpty()) {
|
---|
378 | if (defaultTable.getSelectionModel().isSelectedIndex(i)) {
|
---|
379 | if (!mapPolygons.containsKey(i)) {
|
---|
380 | List<MapPolygon> list = new ArrayList<MapPolygon>();
|
---|
381 | mapPolygons.put(i, list);
|
---|
382 | // Add new map polygons
|
---|
383 | for (Shape shape : shapes) {
|
---|
384 | MapPolygon polygon = new MapPolygonImpl(shape.getPoints());
|
---|
385 | list.add(polygon);
|
---|
386 | defaultMap.addMapPolygon(polygon);
|
---|
387 | }
|
---|
388 | }
|
---|
389 | } else if (mapPolygons.containsKey(i)) {
|
---|
390 | // Remove previously drawn map polygons
|
---|
391 | for (MapPolygon polygon : mapPolygons.get(i)) {
|
---|
392 | defaultMap.removeMapPolygon(polygon);
|
---|
393 | }
|
---|
394 | mapPolygons.remove(i);
|
---|
395 | }
|
---|
396 | // Only display bounds when no polygons (shapes) are defined for this provider
|
---|
397 | } else {
|
---|
398 | if (defaultTable.getSelectionModel().isSelectedIndex(i)) {
|
---|
399 | if (!mapRectangles.containsKey(i)) {
|
---|
400 | // Add new map rectangle
|
---|
401 | Coordinate topLeft = new Coordinate(bounds.getMax().lat(), bounds.getMin().lon());
|
---|
402 | Coordinate bottomRight = new Coordinate(bounds.getMin().lat(), bounds.getMax().lon());
|
---|
403 | MapRectangle rectangle = new MapRectangleImpl(topLeft, bottomRight);
|
---|
404 | mapRectangles.put(i, rectangle);
|
---|
405 | defaultMap.addMapRectangle(rectangle);
|
---|
406 | }
|
---|
407 | } else if (mapRectangles.containsKey(i)) {
|
---|
408 | // Remove previously drawn map rectangle
|
---|
409 | defaultMap.removeMapRectangle(mapRectangles.get(i));
|
---|
410 | mapRectangles.remove(i);
|
---|
411 | }
|
---|
412 | }
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | private class NewEntryAction extends AbstractAction {
|
---|
418 |
|
---|
419 | private final ImageryInfo.ImageryType type;
|
---|
420 |
|
---|
421 | public NewEntryAction(ImageryInfo.ImageryType type) {
|
---|
422 | putValue(NAME, type.toString());
|
---|
423 | putValue(SHORT_DESCRIPTION, tr("Add a new {0} entry by entering the URL", type.toString()));
|
---|
424 | putValue(SMALL_ICON, ImageProvider.get("dialogs",
|
---|
425 | "add" + (ImageryInfo.ImageryType.WMS.equals(type) ? "_wms" : ImageryInfo.ImageryType.TMS.equals(type) ? "_tms" : "")));
|
---|
426 | this.type = type;
|
---|
427 | }
|
---|
428 |
|
---|
429 | @Override
|
---|
430 | public void actionPerformed(ActionEvent evt) {
|
---|
431 | final AddImageryPanel p;
|
---|
432 | if (ImageryInfo.ImageryType.WMS.equals(type)) {
|
---|
433 | p = new AddWMSLayerPanel();
|
---|
434 | } else if (ImageryInfo.ImageryType.TMS.equals(type)) {
|
---|
435 | p = new AddTMSLayerPanel();
|
---|
436 | } else {
|
---|
437 | throw new IllegalStateException("Type " + type + " not supported");
|
---|
438 | }
|
---|
439 |
|
---|
440 | final AddImageryDialog addDialog = new AddImageryDialog(gui, p);
|
---|
441 | addDialog.showDialog();
|
---|
442 |
|
---|
443 | if (addDialog.getValue() == 1) {
|
---|
444 | try {
|
---|
445 | activeModel.addRow(p.getImageryInfo());
|
---|
446 | } catch (IllegalArgumentException ex) {
|
---|
447 | if (ex.getMessage() == null || ex.getMessage().isEmpty())
|
---|
448 | throw ex;
|
---|
449 | else {
|
---|
450 | JOptionPane.showMessageDialog(Main.parent,
|
---|
451 | ex.getMessage(), tr("Error"),
|
---|
452 | JOptionPane.ERROR_MESSAGE);
|
---|
453 | }
|
---|
454 | }
|
---|
455 | }
|
---|
456 | }
|
---|
457 | }
|
---|
458 |
|
---|
459 | private class RemoveEntryAction extends AbstractAction implements ListSelectionListener {
|
---|
460 |
|
---|
461 | public RemoveEntryAction() {
|
---|
462 | putValue(NAME, tr("Remove"));
|
---|
463 | putValue(SHORT_DESCRIPTION, tr("Remove entry"));
|
---|
464 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
|
---|
465 | updateEnabledState();
|
---|
466 | }
|
---|
467 |
|
---|
468 | protected void updateEnabledState() {
|
---|
469 | setEnabled(activeTable.getSelectedRowCount() > 0);
|
---|
470 | }
|
---|
471 |
|
---|
472 | @Override
|
---|
473 | public void valueChanged(ListSelectionEvent e) {
|
---|
474 | updateEnabledState();
|
---|
475 | }
|
---|
476 |
|
---|
477 | @Override
|
---|
478 | public void actionPerformed(ActionEvent e) {
|
---|
479 | Integer i;
|
---|
480 | while ((i = activeTable.getSelectedRow()) != -1) {
|
---|
481 | activeModel.removeRow(i);
|
---|
482 | }
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | private class ActivateAction extends AbstractAction implements ListSelectionListener {
|
---|
487 | public ActivateAction() {
|
---|
488 | putValue(NAME, tr("Activate"));
|
---|
489 | putValue(SHORT_DESCRIPTION, tr("copy selected defaults"));
|
---|
490 | putValue(SMALL_ICON, ImageProvider.get("preferences", "activate-down"));
|
---|
491 | }
|
---|
492 |
|
---|
493 | protected void updateEnabledState() {
|
---|
494 | setEnabled(defaultTable.getSelectedRowCount() > 0);
|
---|
495 | }
|
---|
496 |
|
---|
497 | @Override
|
---|
498 | public void valueChanged(ListSelectionEvent e) {
|
---|
499 | updateEnabledState();
|
---|
500 | }
|
---|
501 |
|
---|
502 | @Override
|
---|
503 | public void actionPerformed(ActionEvent e) {
|
---|
504 | int[] lines = defaultTable.getSelectedRows();
|
---|
505 | if (lines.length == 0) {
|
---|
506 | JOptionPane.showMessageDialog(
|
---|
507 | gui,
|
---|
508 | tr("Please select at least one row to copy."),
|
---|
509 | tr("Information"),
|
---|
510 | JOptionPane.INFORMATION_MESSAGE);
|
---|
511 | return;
|
---|
512 | }
|
---|
513 |
|
---|
514 | Set<String> acceptedEulas = new HashSet<String>();
|
---|
515 |
|
---|
516 | outer: for (int i = 0; i < lines.length; i++) {
|
---|
517 | ImageryInfo info = defaultModel.getRow(lines[i]);
|
---|
518 |
|
---|
519 | // Check if an entry with exactly the same values already
|
---|
520 | // exists
|
---|
521 | for (int j = 0; j < activeModel.getRowCount(); j++) {
|
---|
522 | if (info.equalsBaseValues(activeModel.getRow(j))) {
|
---|
523 | // Select the already existing row so the user has
|
---|
524 | // some feedback in case an entry exists
|
---|
525 | activeTable.getSelectionModel().setSelectionInterval(j, j);
|
---|
526 | activeTable.scrollRectToVisible(activeTable.getCellRect(j, 0, true));
|
---|
527 | continue outer;
|
---|
528 | }
|
---|
529 | }
|
---|
530 |
|
---|
531 | String eulaURL = info.getEulaAcceptanceRequired();
|
---|
532 | // If set and not already accepted, ask for EULA acceptance
|
---|
533 | if (eulaURL != null && !acceptedEulas.contains(eulaURL)) {
|
---|
534 | if (confirmEulaAcceptance(gui, eulaURL)) {
|
---|
535 | acceptedEulas.add(eulaURL);
|
---|
536 | } else {
|
---|
537 | continue outer;
|
---|
538 | }
|
---|
539 | }
|
---|
540 |
|
---|
541 | activeModel.addRow(new ImageryInfo(info));
|
---|
542 | int lastLine = activeModel.getRowCount() - 1;
|
---|
543 | activeTable.getSelectionModel().setSelectionInterval(lastLine, lastLine);
|
---|
544 | activeTable.scrollRectToVisible(activeTable.getCellRect(lastLine, 0, true));
|
---|
545 | }
|
---|
546 | }
|
---|
547 | }
|
---|
548 |
|
---|
549 | private class ReloadAction extends AbstractAction {
|
---|
550 | public ReloadAction() {
|
---|
551 | putValue(SHORT_DESCRIPTION, tr("reload defaults"));
|
---|
552 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "refresh"));
|
---|
553 | }
|
---|
554 |
|
---|
555 | @Override
|
---|
556 | public void actionPerformed(ActionEvent evt) {
|
---|
557 | layerInfo.loadDefaults(true);
|
---|
558 | defaultModel.fireTableDataChanged();
|
---|
559 | }
|
---|
560 | }
|
---|
561 |
|
---|
562 | /**
|
---|
563 | * The table model for imagery layer list
|
---|
564 | */
|
---|
565 | public class ImageryLayerTableModel extends DefaultTableModel {
|
---|
566 | public ImageryLayerTableModel() {
|
---|
567 | setColumnIdentifiers(new String[] { tr("Menu Name"), tr("Imagery URL")});
|
---|
568 | }
|
---|
569 |
|
---|
570 | public ImageryInfo getRow(int row) {
|
---|
571 | return layerInfo.getLayers().get(row);
|
---|
572 | }
|
---|
573 |
|
---|
574 | public void addRow(ImageryInfo i) {
|
---|
575 | layerInfo.add(i);
|
---|
576 | int p = getRowCount() - 1;
|
---|
577 | fireTableRowsInserted(p, p);
|
---|
578 | }
|
---|
579 |
|
---|
580 | @Override
|
---|
581 | public void removeRow(int i) {
|
---|
582 | layerInfo.remove(getRow(i));
|
---|
583 | fireTableRowsDeleted(i, i);
|
---|
584 | }
|
---|
585 |
|
---|
586 | @Override
|
---|
587 | public int getRowCount() {
|
---|
588 | return layerInfo.getLayers().size();
|
---|
589 | }
|
---|
590 |
|
---|
591 | @Override
|
---|
592 | public Object getValueAt(int row, int column) {
|
---|
593 | ImageryInfo info = layerInfo.getLayers().get(row);
|
---|
594 | switch (column) {
|
---|
595 | case 0:
|
---|
596 | return info.getName();
|
---|
597 | case 1:
|
---|
598 | return info.getExtendedUrl();
|
---|
599 | default:
|
---|
600 | throw new ArrayIndexOutOfBoundsException();
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | @Override
|
---|
605 | public void setValueAt(Object o, int row, int column) {
|
---|
606 | if (layerInfo.getLayers().size() <= row) return;
|
---|
607 | ImageryInfo info = layerInfo.getLayers().get(row);
|
---|
608 | switch (column) {
|
---|
609 | case 0:
|
---|
610 | info.setName((String) o);
|
---|
611 | break;
|
---|
612 | case 1:
|
---|
613 | info.setExtendedUrl((String)o);
|
---|
614 | break;
|
---|
615 | default:
|
---|
616 | throw new ArrayIndexOutOfBoundsException();
|
---|
617 | }
|
---|
618 | }
|
---|
619 |
|
---|
620 | @Override
|
---|
621 | public boolean isCellEditable(int row, int column) {
|
---|
622 | return true;
|
---|
623 | }
|
---|
624 | }
|
---|
625 |
|
---|
626 | /**
|
---|
627 | * The table model for the default imagery layer list
|
---|
628 | */
|
---|
629 | public class ImageryDefaultLayerTableModel extends DefaultTableModel {
|
---|
630 | public ImageryDefaultLayerTableModel() {
|
---|
631 | setColumnIdentifiers(new String[]{"", tr("Menu Name (Default)"), tr("Imagery URL (Default)")});
|
---|
632 | }
|
---|
633 |
|
---|
634 | public ImageryInfo getRow(int row) {
|
---|
635 | return layerInfo.getDefaultLayers().get(row);
|
---|
636 | }
|
---|
637 |
|
---|
638 | @Override
|
---|
639 | public int getRowCount() {
|
---|
640 | return layerInfo.getDefaultLayers().size();
|
---|
641 | }
|
---|
642 |
|
---|
643 | @Override
|
---|
644 | public Object getValueAt(int row, int column) {
|
---|
645 | ImageryInfo info = layerInfo.getDefaultLayers().get(row);
|
---|
646 | switch (column) {
|
---|
647 | case 0:
|
---|
648 | return info.getCountryCode();
|
---|
649 | case 1:
|
---|
650 | return info.getName();
|
---|
651 | case 2:
|
---|
652 | return info.getExtendedUrl();
|
---|
653 | }
|
---|
654 | return null;
|
---|
655 | }
|
---|
656 |
|
---|
657 | @Override
|
---|
658 | public boolean isCellEditable(int row, int column) {
|
---|
659 | return false;
|
---|
660 | }
|
---|
661 | }
|
---|
662 |
|
---|
663 | private boolean confirmEulaAcceptance(PreferenceTabbedPane gui, String eulaUrl) {
|
---|
664 | URL url = null;
|
---|
665 | try {
|
---|
666 | url = new URL(eulaUrl.replaceAll("\\{lang\\}", LanguageInfo.getWikiLanguagePrefix()));
|
---|
667 | JosmEditorPane htmlPane = null;
|
---|
668 | try {
|
---|
669 | htmlPane = new JosmEditorPane(url);
|
---|
670 | } catch (IOException e1) {
|
---|
671 | // give a second chance with a default Locale 'en'
|
---|
672 | try {
|
---|
673 | url = new URL(eulaUrl.replaceAll("\\{lang\\}", ""));
|
---|
674 | htmlPane = new JosmEditorPane(url);
|
---|
675 | } catch (IOException e2) {
|
---|
676 | JOptionPane.showMessageDialog(gui ,tr("EULA license URL not available: {0}", eulaUrl));
|
---|
677 | return false;
|
---|
678 | }
|
---|
679 | }
|
---|
680 | Box box = Box.createVerticalBox();
|
---|
681 | htmlPane.setEditable(false);
|
---|
682 | JScrollPane scrollPane = new JScrollPane(htmlPane);
|
---|
683 | scrollPane.setPreferredSize(new Dimension(400, 400));
|
---|
684 | box.add(scrollPane);
|
---|
685 | int option = JOptionPane.showConfirmDialog(Main.parent, box, tr("Please abort if you are not sure"), JOptionPane.YES_NO_OPTION,
|
---|
686 | JOptionPane.WARNING_MESSAGE);
|
---|
687 | if (option == JOptionPane.YES_OPTION)
|
---|
688 | return true;
|
---|
689 | } catch (MalformedURLException e2) {
|
---|
690 | JOptionPane.showMessageDialog(gui ,tr("Malformed URL for the EULA licence: {0}", eulaUrl));
|
---|
691 | }
|
---|
692 | return false;
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 | static class OffsetBookmarksPanel extends JPanel {
|
---|
697 | List<OffsetBookmark> bookmarks = OffsetBookmark.allBookmarks;
|
---|
698 | OffsetsBookmarksModel model = new OffsetsBookmarksModel();
|
---|
699 |
|
---|
700 | public OffsetBookmarksPanel(final PreferenceTabbedPane gui) {
|
---|
701 | super(new GridBagLayout());
|
---|
702 | final JTable list = new JTable(model) {
|
---|
703 | @Override
|
---|
704 | public String getToolTipText(MouseEvent e) {
|
---|
705 | java.awt.Point p = e.getPoint();
|
---|
706 | return model.getValueAt(rowAtPoint(p), columnAtPoint(p)).toString();
|
---|
707 | }
|
---|
708 | };
|
---|
709 | JScrollPane scroll = new JScrollPane(list);
|
---|
710 | add(scroll, GBC.eol().fill(GridBagConstraints.BOTH));
|
---|
711 | scroll.setPreferredSize(new Dimension(200, 200));
|
---|
712 |
|
---|
713 | TableColumnModel mod = list.getColumnModel();
|
---|
714 | mod.getColumn(0).setPreferredWidth(150);
|
---|
715 | mod.getColumn(1).setPreferredWidth(200);
|
---|
716 | mod.getColumn(2).setPreferredWidth(300);
|
---|
717 | mod.getColumn(3).setPreferredWidth(150);
|
---|
718 | mod.getColumn(4).setPreferredWidth(150);
|
---|
719 |
|
---|
720 | JPanel buttonPanel = new JPanel(new FlowLayout());
|
---|
721 |
|
---|
722 | JButton add = new JButton(tr("Add"));
|
---|
723 | buttonPanel.add(add, GBC.std().insets(0, 5, 0, 0));
|
---|
724 | add.addActionListener(new ActionListener() {
|
---|
725 | @Override
|
---|
726 | public void actionPerformed(ActionEvent e) {
|
---|
727 | OffsetBookmark b = new OffsetBookmark(Main.getProjection().toCode(),"","",0,0);
|
---|
728 | model.addRow(b);
|
---|
729 | }
|
---|
730 | });
|
---|
731 |
|
---|
732 | JButton delete = new JButton(tr("Delete"));
|
---|
733 | buttonPanel.add(delete, GBC.std().insets(0, 5, 0, 0));
|
---|
734 | delete.addActionListener(new ActionListener() {
|
---|
735 | @Override
|
---|
736 | public void actionPerformed(ActionEvent e) {
|
---|
737 | if (list.getSelectedRow() == -1) {
|
---|
738 | JOptionPane.showMessageDialog(gui, tr("Please select the row to delete."));
|
---|
739 | } else {
|
---|
740 | Integer i;
|
---|
741 | while ((i = list.getSelectedRow()) != -1) {
|
---|
742 | model.removeRow(i);
|
---|
743 | }
|
---|
744 | }
|
---|
745 | }
|
---|
746 | });
|
---|
747 |
|
---|
748 | add(buttonPanel,GBC.eol());
|
---|
749 | }
|
---|
750 |
|
---|
751 | /**
|
---|
752 | * The table model for imagery offsets list
|
---|
753 | */
|
---|
754 | class OffsetsBookmarksModel extends DefaultTableModel {
|
---|
755 | public OffsetsBookmarksModel() {
|
---|
756 | setColumnIdentifiers(new String[] { tr("Projection"), tr("Layer"), tr("Name"), tr("Easting"), tr("Northing"),});
|
---|
757 | }
|
---|
758 |
|
---|
759 | public OffsetBookmark getRow(int row) {
|
---|
760 | return bookmarks.get(row);
|
---|
761 | }
|
---|
762 |
|
---|
763 | public void addRow(OffsetBookmark i) {
|
---|
764 | bookmarks.add(i);
|
---|
765 | int p = getRowCount() - 1;
|
---|
766 | fireTableRowsInserted(p, p);
|
---|
767 | }
|
---|
768 |
|
---|
769 | @Override
|
---|
770 | public void removeRow(int i) {
|
---|
771 | bookmarks.remove(getRow(i));
|
---|
772 | fireTableRowsDeleted(i, i);
|
---|
773 | }
|
---|
774 |
|
---|
775 | @Override
|
---|
776 | public int getRowCount() {
|
---|
777 | return bookmarks.size();
|
---|
778 | }
|
---|
779 |
|
---|
780 | @Override
|
---|
781 | public Object getValueAt(int row, int column) {
|
---|
782 | OffsetBookmark info = bookmarks.get(row);
|
---|
783 | switch (column) {
|
---|
784 | case 0:
|
---|
785 | if (info.projectionCode == null) return "";
|
---|
786 | return info.projectionCode.toString();
|
---|
787 | case 1:
|
---|
788 | return info.layerName;
|
---|
789 | case 2:
|
---|
790 | return info.name;
|
---|
791 | case 3:
|
---|
792 | return info.dx;
|
---|
793 | case 4:
|
---|
794 | return info.dy;
|
---|
795 | default:
|
---|
796 | throw new ArrayIndexOutOfBoundsException();
|
---|
797 | }
|
---|
798 | }
|
---|
799 |
|
---|
800 | @Override
|
---|
801 | public void setValueAt(Object o, int row, int column) {
|
---|
802 | OffsetBookmark info = bookmarks.get(row);
|
---|
803 | switch (column) {
|
---|
804 | case 1:
|
---|
805 | info.layerName = o.toString();
|
---|
806 | break;
|
---|
807 | case 2:
|
---|
808 | info.name = o.toString();
|
---|
809 | break;
|
---|
810 | case 3:
|
---|
811 | info.dx = Double.parseDouble((String) o);
|
---|
812 | break;
|
---|
813 | case 4:
|
---|
814 | info.dy = Double.parseDouble((String) o);
|
---|
815 | break;
|
---|
816 | default:
|
---|
817 | throw new ArrayIndexOutOfBoundsException();
|
---|
818 | }
|
---|
819 | }
|
---|
820 |
|
---|
821 | @Override
|
---|
822 | public boolean isCellEditable(int row, int column) {
|
---|
823 | return column >= 1;
|
---|
824 | }
|
---|
825 | }
|
---|
826 | }
|
---|
827 |
|
---|
828 | public static void initialize() {
|
---|
829 | ImageryLayerInfo.instance.clear();
|
---|
830 | ImageryLayerInfo.instance.loadDefaults(false);
|
---|
831 | ImageryLayerInfo.instance.load();
|
---|
832 | OffsetBookmark.loadBookmarks();
|
---|
833 | Main.main.menu.imageryMenu.refreshImageryMenu();
|
---|
834 | Main.main.menu.imageryMenu.refreshOffsetMenu();
|
---|
835 | }
|
---|
836 | }
|
---|