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

Last change on this file since 7937 was 7937, checked in by bastiK, 9 years ago

add subversion property svn:eol=native

  • Property svn:eol-style set to native
File size: 3.2 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.ChangeEvent;
12import javax.swing.event.ChangeListener;
13import javax.swing.event.DocumentEvent;
14import javax.swing.event.DocumentListener;
15import javax.swing.text.JTextComponent;
16
17import org.openstreetmap.josm.data.imagery.ImageryInfo;
18import org.openstreetmap.josm.gui.widgets.JosmTextArea;
19import 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 */
27public 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}
Note: See TracBrowser for help on using the repository browser.