source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddImageryPanel.java@ 13206

Last change on this file since 13206 was 12530, checked in by Don-vip, 7 years ago

fix #15081 - "Add Imagery URL" help link does not exist - use Help/Preferences/Imagery

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.imagery;
3
4import java.awt.GridBagLayout;
5import java.awt.LayoutManager;
6import java.util.ArrayList;
7import java.util.Collection;
8
9import javax.swing.AbstractButton;
10import javax.swing.JPanel;
11import javax.swing.event.DocumentEvent;
12import javax.swing.event.DocumentListener;
13import javax.swing.text.JTextComponent;
14
15import org.openstreetmap.josm.data.imagery.ImageryInfo;
16import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
17import org.openstreetmap.josm.gui.widgets.JosmTextArea;
18import org.openstreetmap.josm.gui.widgets.JosmTextField;
19
20/**
21 * An abstract imagery panel used to add WMS/TMS imagery sources. See implementations.
22 * @see AddTMSLayerPanel
23 * @see AddWMSLayerPanel
24 * @see AddWMTSLayerPanel
25 * @since 5617
26 */
27public abstract class AddImageryPanel extends JPanel {
28
29 protected final JosmTextArea rawUrl = new JosmTextArea(3, 40).transferFocusOnTab();
30 protected final JosmTextField name = new JosmTextField();
31
32 protected final transient Collection<ContentValidationListener> listeners = new ArrayList<>();
33
34 /**
35 * A listener notified when the validation status of this panel change.
36 * @since 10600 (functional interface)
37 */
38 @FunctionalInterface
39 public interface ContentValidationListener {
40 /**
41 * Called when the validation status of this panel changed
42 * @param isValid true if the conditions required to close this panel are met
43 */
44 void contentChanged(boolean isValid);
45 }
46
47 protected AddImageryPanel() {
48 this(new GridBagLayout());
49 }
50
51 protected AddImageryPanel(LayoutManager layout) {
52 super(layout);
53 registerValidableComponent(name);
54 }
55
56 protected final void registerValidableComponent(AbstractButton component) {
57 component.addChangeListener(e -> notifyListeners());
58 }
59
60 protected final void registerValidableComponent(JTextComponent component) {
61 component.getDocument().addDocumentListener(new DocumentListener() {
62 @Override
63 public void removeUpdate(DocumentEvent e) {
64 notifyListeners();
65 }
66
67 @Override
68 public void insertUpdate(DocumentEvent e) {
69 notifyListeners();
70 }
71
72 @Override
73 public void changedUpdate(DocumentEvent e) {
74 notifyListeners();
75 }
76 });
77 }
78
79 protected abstract ImageryInfo getImageryInfo();
80
81 protected static String sanitize(String s) {
82 return s.replaceAll("[\r\n]+", "").trim();
83 }
84
85 protected static String sanitize(String s, ImageryType type) {
86 String ret = s;
87 String imageryType = type.getTypeString() + ':';
88 if (ret.startsWith(imageryType)) {
89 // remove ImageryType from URL
90 ret = ret.substring(imageryType.length());
91 }
92 return sanitize(ret);
93 }
94
95 protected final String getImageryName() {
96 return sanitize(name.getText());
97 }
98
99 protected final String getImageryRawUrl() {
100 return sanitize(rawUrl.getText());
101 }
102
103 protected abstract boolean isImageryValid();
104
105 /**
106 * Registers a new ContentValidationListener
107 * @param l The new ContentValidationListener that will be notified of validation status changes
108 */
109 public final void addContentValidationListener(ContentValidationListener l) {
110 if (l != null) {
111 listeners.add(l);
112 }
113 }
114
115 private void notifyListeners() {
116 for (ContentValidationListener l : listeners) {
117 l.contentChanged(isImageryValid());
118 }
119 }
120}
Note: See TracBrowser for help on using the repository browser.