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.gui.widgets.JosmTextArea;
|
---|
19 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * An abstract imagery panel used to add WMS/TMS imagery sources. See implementations.
|
---|
23 | * @see AddTMSLayerPanel
|
---|
24 | * @see AddWMSLayerPanel
|
---|
25 | * @since 5617
|
---|
26 | */
|
---|
27 | public abstract class AddImageryPanel extends JPanel {
|
---|
28 |
|
---|
29 | protected final JosmTextArea rawUrl = new JosmTextArea(3, 40);
|
---|
30 | protected final JosmTextField name = new JosmTextField();
|
---|
31 |
|
---|
32 | protected final Collection<ContentValidationListener> listeners = new ArrayList<>();
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * A listener notified when the validation status of this panel change.
|
---|
36 | */
|
---|
37 | public interface ContentValidationListener {
|
---|
38 | /**
|
---|
39 | * Called when the validation status of this panel changed
|
---|
40 | * @param isValid true if the conditions required to close this panel are met
|
---|
41 | */
|
---|
42 | public void contentChanged(boolean isValid);
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected AddImageryPanel() {
|
---|
46 | this(new GridBagLayout());
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected AddImageryPanel(LayoutManager layout) {
|
---|
50 | super(layout);
|
---|
51 | registerValidableComponent(name);
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected final void registerValidableComponent(AbstractButton component) {
|
---|
55 | component.addChangeListener(new ChangeListener() {
|
---|
56 | @Override public void stateChanged(ChangeEvent e) { notifyListeners(); }
|
---|
57 | });
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected final void registerValidableComponent(JTextComponent component) {
|
---|
61 | component.getDocument().addDocumentListener(new DocumentListener() {
|
---|
62 | @Override public void removeUpdate(DocumentEvent e) { notifyListeners(); }
|
---|
63 | @Override public void insertUpdate(DocumentEvent e) { notifyListeners(); }
|
---|
64 | @Override public void changedUpdate(DocumentEvent e) { notifyListeners(); }
|
---|
65 | });
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected abstract ImageryInfo getImageryInfo();
|
---|
69 |
|
---|
70 | protected static String sanitize(String s) {
|
---|
71 | return s.replaceAll("[\r\n]+", "").trim();
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected final String getImageryName() {
|
---|
75 | return sanitize(name.getText());
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected final String getImageryRawUrl() {
|
---|
79 | return sanitize(rawUrl.getText());
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected abstract boolean isImageryValid();
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Registers a new ContentValidationListener
|
---|
86 | * @param l The new ContentValidationListener that will be notified of validation status changes
|
---|
87 | */
|
---|
88 | public final void addContentValidationListener(ContentValidationListener l) {
|
---|
89 | if (l != null) {
|
---|
90 | listeners.add(l);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | private final void notifyListeners() {
|
---|
95 | for (ContentValidationListener l : listeners) {
|
---|
96 | l.contentChanged(isImageryValid());
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|