[5731] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[5617] | 2 | package org.openstreetmap.josm.gui.preferences.imagery;
|
---|
| 3 |
|
---|
[5731] | 4 | import java.awt.GridBagLayout;
|
---|
[5617] | 5 | import java.awt.LayoutManager;
|
---|
[5731] | 6 | import java.util.ArrayList;
|
---|
| 7 | import java.util.Collection;
|
---|
| 8 |
|
---|
| 9 | import javax.swing.AbstractButton;
|
---|
[5617] | 10 | import javax.swing.JPanel;
|
---|
[5731] | 11 | import javax.swing.event.DocumentEvent;
|
---|
| 12 | import javax.swing.event.DocumentListener;
|
---|
| 13 | import javax.swing.text.JTextComponent;
|
---|
| 14 |
|
---|
[5617] | 15 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
[8568] | 16 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
[5886] | 17 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
| 18 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
[5617] | 19 |
|
---|
[5731] | 20 | /**
|
---|
| 21 | * An abstract imagery panel used to add WMS/TMS imagery sources. See implementations.
|
---|
| 22 | * @see AddTMSLayerPanel
|
---|
| 23 | * @see AddWMSLayerPanel
|
---|
[12530] | 24 | * @see AddWMTSLayerPanel
|
---|
[5731] | 25 | * @since 5617
|
---|
| 26 | */
|
---|
[5617] | 27 | public abstract class AddImageryPanel extends JPanel {
|
---|
[5731] | 28 |
|
---|
[11308] | 29 | protected final JosmTextArea rawUrl = new JosmTextArea(3, 40).transferFocusOnTab();
|
---|
[5886] | 30 | protected final JosmTextField name = new JosmTextField();
|
---|
[6070] | 31 |
|
---|
[8308] | 32 | protected final transient Collection<ContentValidationListener> listeners = new ArrayList<>();
|
---|
[6070] | 33 |
|
---|
[5731] | 34 | /**
|
---|
| 35 | * A listener notified when the validation status of this panel change.
|
---|
[10600] | 36 | * @since 10600 (functional interface)
|
---|
[5731] | 37 | */
|
---|
[10600] | 38 | @FunctionalInterface
|
---|
[5731] | 39 | public interface ContentValidationListener {
|
---|
| 40 | /**
|
---|
| 41 | * Called when the validation status of this panel changed
|
---|
[6070] | 42 | * @param isValid true if the conditions required to close this panel are met
|
---|
[5731] | 43 | */
|
---|
[8512] | 44 | void contentChanged(boolean isValid);
|
---|
[5617] | 45 | }
|
---|
| 46 |
|
---|
[5731] | 47 | protected AddImageryPanel() {
|
---|
| 48 | this(new GridBagLayout());
|
---|
[5617] | 49 | }
|
---|
| 50 |
|
---|
| 51 | protected AddImageryPanel(LayoutManager layout) {
|
---|
| 52 | super(layout);
|
---|
[5731] | 53 | registerValidableComponent(name);
|
---|
[5617] | 54 | }
|
---|
| 55 |
|
---|
[5731] | 56 | protected final void registerValidableComponent(AbstractButton component) {
|
---|
[10611] | 57 | component.addChangeListener(e -> notifyListeners());
|
---|
[5617] | 58 | }
|
---|
| 59 |
|
---|
[5731] | 60 | protected final void registerValidableComponent(JTextComponent component) {
|
---|
| 61 | component.getDocument().addDocumentListener(new DocumentListener() {
|
---|
[8510] | 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 | }
|
---|
[5731] | 76 | });
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | protected abstract ImageryInfo getImageryInfo();
|
---|
| 80 |
|
---|
| 81 | protected static String sanitize(String s) {
|
---|
| 82 | return s.replaceAll("[\r\n]+", "").trim();
|
---|
| 83 | }
|
---|
[6070] | 84 |
|
---|
[8568] | 85 | protected static String sanitize(String s, ImageryType type) {
|
---|
| 86 | String ret = s;
|
---|
[8846] | 87 | String imageryType = type.getTypeString() + ':';
|
---|
[8568] | 88 | if (ret.startsWith(imageryType)) {
|
---|
| 89 | // remove ImageryType from URL
|
---|
| 90 | ret = ret.substring(imageryType.length());
|
---|
| 91 | }
|
---|
| 92 | return sanitize(ret);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[5731] | 95 | protected final String getImageryName() {
|
---|
| 96 | return sanitize(name.getText());
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | protected final String getImageryRawUrl() {
|
---|
| 100 | return sanitize(rawUrl.getText());
|
---|
| 101 | }
|
---|
[6070] | 102 |
|
---|
[5731] | 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 |
|
---|
[8512] | 115 | private void notifyListeners() {
|
---|
[5731] | 116 | for (ContentValidationListener l : listeners) {
|
---|
| 117 | l.contentChanged(isImageryValid());
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
[5617] | 120 | }
|
---|