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