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