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

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

SonarQube - fix minor issues

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.awt.LayoutManager;
8import java.awt.event.ItemEvent;
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.List;
13import java.util.Map;
14import java.util.concurrent.TimeUnit;
15
16import javax.swing.AbstractButton;
17import javax.swing.JCheckBox;
18import javax.swing.JComboBox;
19import javax.swing.JLabel;
20import javax.swing.JPanel;
21import javax.swing.JSpinner;
22import javax.swing.SpinnerNumberModel;
23import javax.swing.event.DocumentEvent;
24import javax.swing.event.DocumentListener;
25import javax.swing.text.JTextComponent;
26
27import org.openstreetmap.josm.actions.ExpertToggleAction;
28import org.openstreetmap.josm.data.imagery.ImageryInfo;
29import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
30import org.openstreetmap.josm.data.imagery.TMSCachedTileLoaderJob;
31import org.openstreetmap.josm.gui.widgets.JosmTextArea;
32import org.openstreetmap.josm.gui.widgets.JosmTextField;
33import org.openstreetmap.josm.tools.GBC;
34import org.openstreetmap.josm.tools.Logging;
35
36/**
37 * An abstract imagery panel used to add WMS/TMS imagery sources. See implementations.
38 * @see AddTMSLayerPanel
39 * @see AddWMSLayerPanel
40 * @see AddWMTSLayerPanel
41 * @since 5617
42 */
43public abstract class AddImageryPanel extends JPanel {
44
45 protected final JosmTextArea rawUrl = new JosmTextArea(3, 40).transferFocusOnTab();
46 protected final JosmTextField name = new JosmTextField();
47
48 protected final transient Collection<ContentValidationListener> listeners = new ArrayList<>();
49
50 private final JCheckBox validGeoreference = new JCheckBox(tr("Is layer properly georeferenced?"));
51 private HeadersTable headersTable;
52 private JSpinner minimumCacheExpiry;
53 private JComboBox<String> minimumCacheExpiryUnit;
54 private TimeUnit currentUnit;
55
56 /**
57 * A listener notified when the validation status of this panel change.
58 * @since 10600 (functional interface)
59 */
60 @FunctionalInterface
61 public interface ContentValidationListener {
62 /**
63 * Called when the validation status of this panel changed
64 * @param isValid true if the conditions required to close this panel are met
65 */
66 void contentChanged(boolean isValid);
67 }
68
69 protected AddImageryPanel() {
70 this(new GridBagLayout());
71 headersTable = new HeadersTable();
72 minimumCacheExpiry = new JSpinner(new SpinnerNumberModel(
73 (Number) TimeUnit.MILLISECONDS.toSeconds(TMSCachedTileLoaderJob.MINIMUM_EXPIRES.get()),
74 0L,
75 Long.valueOf(Integer.MAX_VALUE),
76 1
77 ));
78 List<String> units = Arrays.asList(tr("seconds"), tr("minutes"), tr("hours"), tr("days"));
79 minimumCacheExpiryUnit = new JComboBox<>(units.toArray(new String[]{}));
80 currentUnit = TimeUnit.SECONDS;
81 minimumCacheExpiryUnit.addItemListener(e -> {
82 if (e.getStateChange() == ItemEvent.SELECTED) {
83 long newValue = 0;
84 switch (units.indexOf(e.getItem())) {
85 case 0:
86 newValue = currentUnit.toSeconds((long) minimumCacheExpiry.getValue());
87 currentUnit = TimeUnit.SECONDS;
88 break;
89 case 1:
90 newValue = currentUnit.toMinutes((long) minimumCacheExpiry.getValue());
91 currentUnit = TimeUnit.MINUTES;
92 break;
93 case 2:
94 newValue = currentUnit.toHours((long) minimumCacheExpiry.getValue());
95 currentUnit = TimeUnit.HOURS;
96 break;
97 case 3:
98 newValue = currentUnit.toDays((long) minimumCacheExpiry.getValue());
99 currentUnit = TimeUnit.DAYS;
100 break;
101 default:
102 Logging.warn("Unkown unit: " + units.indexOf(e.getItem()));
103 }
104 minimumCacheExpiry.setValue(newValue);
105 }
106 });
107
108
109 }
110
111 protected void addCommonSettings() {
112 if (ExpertToggleAction.isExpert()) {
113 add(new JLabel(tr("Minimum cache expiry: ")));
114 add(minimumCacheExpiry);
115 add(minimumCacheExpiryUnit, GBC.eol());
116 add(new JLabel(tr("Set custom HTTP headers (if needed):")), GBC.eop());
117 add(headersTable, GBC.eol().fill());
118 add(validGeoreference, GBC.eop().fill(GBC.HORIZONTAL));
119 }
120 }
121
122 protected Map<String, String> getCommonHeaders() {
123 return headersTable.getHeaders();
124 }
125
126 protected boolean getCommonIsValidGeoreference() {
127 return validGeoreference.isSelected();
128 }
129
130 protected AddImageryPanel(LayoutManager layout) {
131 super(layout);
132 registerValidableComponent(name);
133 }
134
135 protected final void registerValidableComponent(AbstractButton component) {
136 component.addChangeListener(e -> notifyListeners());
137 }
138
139 protected final void registerValidableComponent(JTextComponent component) {
140 component.getDocument().addDocumentListener(new DocumentListener() {
141 @Override
142 public void removeUpdate(DocumentEvent e) {
143 notifyListeners();
144 }
145
146 @Override
147 public void insertUpdate(DocumentEvent e) {
148 notifyListeners();
149 }
150
151 @Override
152 public void changedUpdate(DocumentEvent e) {
153 notifyListeners();
154 }
155 });
156 }
157
158 protected abstract ImageryInfo getImageryInfo();
159
160 protected static String sanitize(String s) {
161 return s.replaceAll("[\r\n]+", "").trim();
162 }
163
164 protected static String sanitize(String s, ImageryType type) {
165 String ret = s;
166 String imageryType = type.getTypeString() + ':';
167 if (ret.startsWith(imageryType)) {
168 // remove ImageryType from URL
169 ret = ret.substring(imageryType.length());
170 }
171 return sanitize(ret);
172 }
173
174 protected final String getImageryName() {
175 return sanitize(name.getText());
176 }
177
178 protected final String getImageryRawUrl() {
179 return sanitize(rawUrl.getText());
180 }
181
182 protected abstract boolean isImageryValid();
183
184 /**
185 * Registers a new ContentValidationListener
186 * @param l The new ContentValidationListener that will be notified of validation status changes
187 */
188 public final void addContentValidationListener(ContentValidationListener l) {
189 if (l != null) {
190 listeners.add(l);
191 }
192 }
193
194 private void notifyListeners() {
195 for (ContentValidationListener l : listeners) {
196 l.contentChanged(isImageryValid());
197 }
198 }
199}
Note: See TracBrowser for help on using the repository browser.