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

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

fix #14031 - change focus with TAB in "add imagery" dialogs

  • 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 * @since 5617
25 */
26public abstract class AddImageryPanel extends JPanel {
27
28 protected final JosmTextArea rawUrl = new JosmTextArea(3, 40).transferFocusOnTab();
29 protected final JosmTextField name = new JosmTextField();
30
31 protected final transient Collection<ContentValidationListener> listeners = new ArrayList<>();
32
33 /**
34 * A listener notified when the validation status of this panel change.
35 * @since 10600 (functional interface)
36 */
37 @FunctionalInterface
38 public interface ContentValidationListener {
39 /**
40 * Called when the validation status of this panel changed
41 * @param isValid true if the conditions required to close this panel are met
42 */
43 void contentChanged(boolean isValid);
44 }
45
46 protected AddImageryPanel() {
47 this(new GridBagLayout());
48 }
49
50 protected AddImageryPanel(LayoutManager layout) {
51 super(layout);
52 registerValidableComponent(name);
53 }
54
55 protected final void registerValidableComponent(AbstractButton component) {
56 component.addChangeListener(e -> notifyListeners());
57 }
58
59 protected final void registerValidableComponent(JTextComponent component) {
60 component.getDocument().addDocumentListener(new DocumentListener() {
61 @Override
62 public void removeUpdate(DocumentEvent e) {
63 notifyListeners();
64 }
65
66 @Override
67 public void insertUpdate(DocumentEvent e) {
68 notifyListeners();
69 }
70
71 @Override
72 public void changedUpdate(DocumentEvent e) {
73 notifyListeners();
74 }
75 });
76 }
77
78 protected abstract ImageryInfo getImageryInfo();
79
80 protected static String sanitize(String s) {
81 return s.replaceAll("[\r\n]+", "").trim();
82 }
83
84 protected static String sanitize(String s, ImageryType type) {
85 String ret = s;
86 String imageryType = type.getTypeString() + ':';
87 if (ret.startsWith(imageryType)) {
88 // remove ImageryType from URL
89 ret = ret.substring(imageryType.length());
90 }
91 return sanitize(ret);
92 }
93
94 protected final String getImageryName() {
95 return sanitize(name.getText());
96 }
97
98 protected final String getImageryRawUrl() {
99 return sanitize(rawUrl.getText());
100 }
101
102 protected abstract boolean isImageryValid();
103
104 /**
105 * Registers a new ContentValidationListener
106 * @param l The new ContentValidationListener that will be notified of validation status changes
107 */
108 public final void addContentValidationListener(ContentValidationListener l) {
109 if (l != null) {
110 listeners.add(l);
111 }
112 }
113
114 private void notifyListeners() {
115 for (ContentValidationListener l : listeners) {
116 l.contentChanged(isImageryValid());
117 }
118 }
119}
Note: See TracBrowser for help on using the repository browser.