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