Changeset 5731 in josm for trunk/src/org


Ignore:
Timestamp:
2013-02-19T22:38:52+01:00 (12 years ago)
Author:
Don-vip
Message:

fix #8444 - NPE when no WMS url is entered in WMS add dialog + various UI improvements in add WMS/TMS dialogs (OK button enabled status is dynamic, new Help button, name suggested for wms_endpoint)

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddImageryPanel.java

    r5617 r5731  
     1// License: GPL. For details, see LICENSE file.
    12package org.openstreetmap.josm.gui.preferences.imagery;
    23
     4import java.awt.GridBagLayout;
    35import java.awt.LayoutManager;
     6import java.util.ArrayList;
     7import java.util.Collection;
     8
     9import javax.swing.AbstractButton;
    410import javax.swing.JPanel;
     11import javax.swing.JTextArea;
     12import javax.swing.JTextField;
     13import javax.swing.event.ChangeEvent;
     14import javax.swing.event.ChangeListener;
     15import javax.swing.event.DocumentEvent;
     16import javax.swing.event.DocumentListener;
     17import javax.swing.text.JTextComponent;
     18
    519import org.openstreetmap.josm.data.imagery.ImageryInfo;
    620
     21/**
     22 * An abstract imagery panel used to add WMS/TMS imagery sources. See implementations.
     23 * @see AddTMSLayerPanel
     24 * @see AddWMSLayerPanel
     25 * @since 5617
     26 */
    727public abstract class AddImageryPanel extends JPanel {
    8     protected AddImageryPanel() {
     28
     29    protected final JTextArea rawUrl = new JTextArea(3, 40);
     30    protected final JTextField name = new JTextField();
     31   
     32    protected final Collection<ContentValidationListener> listeners = new ArrayList<ContentValidationListener>();
     33   
     34    /**
     35     * A listener notified when the validation status of this panel change.
     36     */
     37    public interface ContentValidationListener {
     38        /**
     39         * Called when the validation status of this panel changed
     40         * @param isValid true if the conditions required to close this panel are met   
     41         */
     42        public void contentChanged(boolean isValid);
    943    }
    1044
    11     protected AddImageryPanel(boolean isDoubleBuffered) {
    12         super(isDoubleBuffered);
     45    protected AddImageryPanel() {
     46        this(new GridBagLayout());
    1347    }
    1448
    1549    protected AddImageryPanel(LayoutManager layout) {
    1650        super(layout);
     51        registerValidableComponent(name);
    1752    }
    1853
    19     protected AddImageryPanel(LayoutManager layout, boolean isDoubleBuffered) {
    20         super(layout, isDoubleBuffered);
     54    protected final void registerValidableComponent(AbstractButton component) {
     55        component.addChangeListener(new ChangeListener() {
     56            @Override public void stateChanged(ChangeEvent e) { notifyListeners(); }
     57        });
    2158    }
    2259
    23     abstract ImageryInfo getImageryInfo();
     60    protected final void registerValidableComponent(JTextComponent component) {
     61        component.getDocument().addDocumentListener(new DocumentListener() {
     62            @Override public void removeUpdate(DocumentEvent e) { notifyListeners(); }
     63            @Override public void insertUpdate(DocumentEvent e) { notifyListeners(); }
     64            @Override public void changedUpdate(DocumentEvent e) { notifyListeners(); }
     65        });
     66    }
     67
     68    protected abstract ImageryInfo getImageryInfo();
     69
     70    protected static String sanitize(String s) {
     71        return s.replaceAll("[\r\n]+", "").trim();
     72    }
     73   
     74    protected final String getImageryName() {
     75        return sanitize(name.getText());
     76    }
     77
     78    protected final String getImageryRawUrl() {
     79        return sanitize(rawUrl.getText());
     80    }
     81   
     82    protected abstract boolean isImageryValid();
     83
     84    /**
     85     * Registers a new ContentValidationListener
     86     * @param l The new ContentValidationListener that will be notified of validation status changes
     87     */
     88    public final void addContentValidationListener(ContentValidationListener l) {
     89        if (l != null) {
     90            listeners.add(l);
     91        }
     92    }
     93
     94    private final void notifyListeners() {
     95        for (ContentValidationListener l : listeners) {
     96            l.contentChanged(isImageryValid());
     97        }
     98    }
    2499}
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddTMSLayerPanel.java

    r5617 r5731  
    55
    66import java.awt.Dimension;
    7 import java.awt.GridBagLayout;
    87import java.awt.event.KeyAdapter;
    98import java.awt.event.KeyEvent;
    109import java.util.Arrays;
     10
    1111import javax.swing.JLabel;
    1212import javax.swing.JTextArea;
    1313import javax.swing.JTextField;
    1414import javax.swing.text.View;
     15
    1516import org.openstreetmap.josm.data.imagery.ImageryInfo;
    1617import org.openstreetmap.josm.tools.GBC;
     
    1920public class AddTMSLayerPanel extends AddImageryPanel {
    2021
    21     private final JTextArea rawUrl = new JTextArea(3, 40);
    2222    private final JTextField tmsZoom = new JTextField();
    2323    private final JTextArea tmsUrl = new JTextArea(3, 40);
    24     private final JTextField name = new JTextField();
    2524    private final KeyAdapter keyAdapter = new KeyAdapter() {
    2625        @Override
     
    3130
    3231    public AddTMSLayerPanel() {
    33         super(new GridBagLayout());
    3432
    3533        add(new JLabel(tr("1. Enter URL")), GBC.eol());
     
    6058        add(name, GBC.eop().fill());
    6159
    62     }
    63 
    64     private String sanitize(String s) {
    65         return s.replaceAll("[\r\n]+", "").trim();
     60        registerValidableComponent(tmsUrl);
    6661    }
    6762
     
    7368        }
    7469        a.append(":");
    75         a.append(sanitize(rawUrl.getText()));
     70        a.append(getImageryRawUrl());
    7671        return a.toString();
    7772    }
     
    7974    @Override
    8075    public ImageryInfo getImageryInfo() {
    81         return new ImageryInfo(name.getText(), tmsUrl.getText());
     76        return new ImageryInfo(getImageryName(), getTmsUrl());
    8277    }
    8378
     
    9186                (int) Math.ceil(view.getPreferredSpan(View.Y_AXIS)));
    9287    }
     88   
     89    protected final String getTmsUrl() {
     90        return sanitize(tmsUrl.getText());
     91    }
     92
     93    protected boolean isImageryValid() {
     94        return !getImageryName().isEmpty() && !getTmsUrl().isEmpty();
     95    }
    9396}
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java

    r5650 r5731  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.Dimension;
    7 import java.awt.GridBagLayout;
    86import java.awt.event.ActionEvent;
    97import java.awt.event.ActionListener;
     
    1412import java.io.IOException;
    1513import java.net.MalformedURLException;
     14
    1615import javax.swing.JButton;
    1716import javax.swing.JCheckBox;
     
    2019import javax.swing.JScrollPane;
    2120import javax.swing.JTextArea;
    22 import javax.swing.JTextField;
     21
    2322import org.openstreetmap.josm.data.imagery.ImageryInfo;
    2423import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
     
    3029
    3130    private final WMSImagery wms = new WMSImagery();
    32     private final JTextArea rawUrl = new JTextArea(3, 40);
    3331    private final JCheckBox endpoint = new JCheckBox(tr("Store WMS endpoint only, select layers at usage"));
    3432    private final WMSLayerTree tree = new WMSLayerTree();
     33    private final JLabel wmsInstruction;
    3534    private final JTextArea wmsUrl = new JTextArea(3, 40);
    36     private final JTextField name = new JTextField();
     35    private final JButton showBounds = new JButton(tr("Show bounds"));
    3736
    3837    public AddWMSLayerPanel() {
    39         super(new GridBagLayout());
    4038
    4139        add(new JLabel(tr("1. Enter service URL")), GBC.eol());
     
    4846        add(endpoint, GBC.eol().fill());
    4947        add(new JScrollPane(tree.getLayerTree()), GBC.eol().fill().weight(1, 100));
    50         final JButton showBounds = new JButton(tr("Show bounds"));
     48
    5149        showBounds.setEnabled(false);
    5250        add(new JScrollPane(showBounds), GBC.eop().fill());
    5351
    54         add(new JLabel(tr("3. Verify generated WMS URL")), GBC.eol());
     52        add(wmsInstruction = new JLabel(tr("3. Verify generated WMS URL")), GBC.eol());
    5553        add(wmsUrl, GBC.eop().fill());
    5654        wmsUrl.setLineWrap(true);
     
    8583                tree.getLayerTree().setEnabled(!endpoint.isSelected());
    8684                showBounds.setEnabled(!endpoint.isSelected());
     85                wmsInstruction.setEnabled(!endpoint.isSelected());
    8786                wmsUrl.setEnabled(!endpoint.isSelected());
     87                if (endpoint.isSelected()) {
     88                    name.setText(wms.getServiceUrl().getHost());
     89                } else {
     90                    onLayerSelectionChanged();
     91                }
    8892            }
    8993        });
     
    9195        tree.getLayerTree().addPropertyChangeListener("selectedLayers", new PropertyChangeListener() {
    9296            @Override
    93             public void propertyChange(PropertyChangeEvent evt) {
    94                 if (wms.getServiceUrl() != null) {
    95                     wmsUrl.setText(wms.buildGetMapUrl(tree.getSelectedLayers()));
    96                     name.setText(wms.getServiceUrl().getHost() + ": " + Utils.join(", ", tree.getSelectedLayers()));
    97                 }
    98                 showBounds.setEnabled(tree.getSelectedLayers().size() == 1);
     97            public void propertyChange(PropertyChangeEvent evt) {
     98                onLayerSelectionChanged();
    9999            }
    100100        });
     
    113113            }
    114114        });
    115 
     115       
     116        registerValidableComponent(endpoint);
     117        registerValidableComponent(rawUrl);
     118        registerValidableComponent(wmsUrl);
     119    }
     120   
     121    protected final void onLayerSelectionChanged() {
     122        if (wms.getServiceUrl() != null) {
     123            wmsUrl.setText(wms.buildGetMapUrl(tree.getSelectedLayers()));
     124            name.setText(wms.getServiceUrl().getHost() + ": " + Utils.join(", ", tree.getSelectedLayers()));
     125        }
     126        showBounds.setEnabled(tree.getSelectedLayers().size() == 1);
    116127    }
    117128
     
    120131        final ImageryInfo info;
    121132        if (endpoint.isSelected()) {
    122             info = new ImageryInfo(name.getText(), rawUrl.getText());
     133            info = new ImageryInfo(getImageryName(), getImageryRawUrl());
    123134            info.setImageryType(ImageryInfo.ImageryType.WMS_ENDPOINT);
    124135        } else {
    125             info = wms.toImageryInfo(name.getText(), tree.getSelectedLayers());
    126             info.setUrl(wmsUrl.getText());
     136            info = wms.toImageryInfo(getImageryName(), tree.getSelectedLayers());
     137            info.setUrl(getWmsUrl());
    127138        }
    128139        return info;
    129140    }
     141   
     142    protected final String getWmsUrl() {
     143        return sanitize(wmsUrl.getText());
     144    }
     145   
     146    protected boolean isImageryValid() {
     147        if (getImageryName().isEmpty()) {
     148            return false;
     149        }
     150        if (endpoint.isSelected()) {
     151            return !getImageryRawUrl().isEmpty();
     152        } else {
     153            return !getWmsUrl().isEmpty();
     154        }
     155    }
    130156}
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java

    r5729 r5731  
    6262import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
    6363import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
    64 import org.openstreetmap.josm.gui.util.GuiHelper;
    6564import org.openstreetmap.josm.tools.GBC;
    6665import org.openstreetmap.josm.tools.ImageProvider;
     
    437436                    throw new IllegalStateException("Type " + type + " not supported");
    438437                }
    439                 GuiHelper.prepareResizeableOptionPane(p, new Dimension(250, 350));
    440                 int answer = JOptionPane.showConfirmDialog(
    441                         gui, p, tr("Add Imagery URL"),
    442                         JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    443                 if (answer == JOptionPane.OK_OPTION) {
     438               
     439                final AddImageryDialog addDialog = new AddImageryDialog(gui, p);
     440                addDialog.showDialog();
     441               
     442                if (addDialog.getValue() == 1) {
    444443                    try {
    445444                        activeModel.addRow(p.getImageryInfo());
     
    456455            }
    457456        }
    458 
     457       
    459458        private class RemoveEntryAction extends AbstractAction implements ListSelectionListener {
    460459
  • trunk/src/org/openstreetmap/josm/io/imagery/WMSImagery.java

    r5617 r5731  
    1616import java.util.Set;
    1717import java.util.regex.Pattern;
     18
    1819import javax.xml.parsers.DocumentBuilder;
    1920import javax.xml.parsers.DocumentBuilderFactory;
    20 import javax.xml.parsers.ParserConfigurationException;
     21
    2122import org.openstreetmap.josm.data.Bounds;
    2223import org.openstreetmap.josm.data.imagery.ImageryInfo;
     
    6061
    6162    String buildRootUrl() {
     63        if (serviceUrl == null) {
     64            return null;
     65        }
    6266        StringBuilder a = new StringBuilder(serviceUrl.getProtocol());
    6367        a.append("://");
Note: See TracChangeset for help on using the changeset viewer.