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

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

sonar - fb-contrib - minor performance improvements:

  • Method passes constant String of length 1 to character overridden method
  • Method needlessly boxes a boolean constant
  • Method uses iterator().next() on a List to get the first item
  • Method converts String to boxed primitive using excessive boxing
  • Method converts String to primitive using excessive boxing
  • Method creates array using constants
  • Class defines List based fields but uses them like Sets
  • Property svn:eol-style set to native
File size: 3.7 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.data.imagery.ImageryInfo.ImageryType;
19import org.openstreetmap.josm.gui.widgets.JosmTextArea;
20import 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 */
28public 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 */
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(new ChangeListener() {
57 @Override
58 public void stateChanged(ChangeEvent e) {
59 notifyListeners();
60 }
61 });
62 }
63
64 protected final void registerValidableComponent(JTextComponent component) {
65 component.getDocument().addDocumentListener(new DocumentListener() {
66 @Override
67 public void removeUpdate(DocumentEvent e) {
68 notifyListeners();
69 }
70
71 @Override
72 public void insertUpdate(DocumentEvent e) {
73 notifyListeners();
74 }
75
76 @Override
77 public void changedUpdate(DocumentEvent e) {
78 notifyListeners();
79 }
80 });
81 }
82
83 protected abstract ImageryInfo getImageryInfo();
84
85 protected static String sanitize(String s) {
86 return s.replaceAll("[\r\n]+", "").trim();
87 }
88
89 protected static String sanitize(String s, ImageryType type) {
90 String ret = s;
91 String imageryType = type.getTypeString() + ':';
92 if (ret.startsWith(imageryType)) {
93 // remove ImageryType from URL
94 ret = ret.substring(imageryType.length());
95 }
96 return sanitize(ret);
97 }
98
99 protected final String getImageryName() {
100 return sanitize(name.getText());
101 }
102
103 protected final String getImageryRawUrl() {
104 return sanitize(rawUrl.getText());
105 }
106
107 protected abstract boolean isImageryValid();
108
109 /**
110 * Registers a new ContentValidationListener
111 * @param l The new ContentValidationListener that will be notified of validation status changes
112 */
113 public final void addContentValidationListener(ContentValidationListener l) {
114 if (l != null) {
115 listeners.add(l);
116 }
117 }
118
119 private void notifyListeners() {
120 for (ContentValidationListener l : listeners) {
121 l.contentChanged(isImageryValid());
122 }
123 }
124}
Note: See TracBrowser for help on using the repository browser.