Changeset 14807 in josm for trunk


Ignore:
Timestamp:
2019-02-24T22:43:26+01:00 (5 years ago)
Author:
Don-vip
Message:

fix #17338 - NPE

Location:
trunk
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/help/HelpBrowser.java

    r14369 r14807  
    99import java.awt.BorderLayout;
    1010import java.awt.Dimension;
    11 import java.awt.Rectangle;
    1211import java.awt.event.ActionEvent;
    1312import java.awt.event.WindowAdapter;
     
    1716import java.nio.charset.StandardCharsets;
    1817import java.util.Locale;
    19 import java.util.regex.Matcher;
    20 import java.util.regex.Pattern;
    2118
    2219import javax.swing.AbstractAction;
     
    3229import javax.swing.event.ChangeEvent;
    3330import javax.swing.event.ChangeListener;
    34 import javax.swing.event.HyperlinkEvent;
    35 import javax.swing.event.HyperlinkListener;
    36 import javax.swing.text.AttributeSet;
    3731import javax.swing.text.BadLocationException;
    3832import javax.swing.text.Document;
    39 import javax.swing.text.Element;
    40 import javax.swing.text.SimpleAttributeSet;
    41 import javax.swing.text.html.HTML.Tag;
    42 import javax.swing.text.html.HTMLDocument;
    4333import javax.swing.text.html.StyleSheet;
    4434
     
    177167        help.setEditorKit(kit);
    178168        help.setEditable(false);
    179         help.addHyperlinkListener(new HyperlinkHandler());
     169        help.addHyperlinkListener(new HyperlinkHandler(this, help));
    180170        help.setContentType("text/html");
    181171        history = new HelpBrowserHistory(this);
     
    247237    public String getUrl() {
    248238        return url;
     239    }
     240
     241    @Override
     242    public void setUrl(String url) {
     243        this.url = url;
    249244    }
    250245
     
    564559    }
    565560
    566     class HyperlinkHandler implements HyperlinkListener {
    567 
    568         /**
    569          * Scrolls the help browser to the element with id <code>id</code>
    570          *
    571          * @param id the id
    572          * @return true, if an element with this id was found and scrolling was successful; false, otherwise
    573          */
    574         protected boolean scrollToElementWithId(String id) {
    575             Document d = help.getDocument();
    576             if (d instanceof HTMLDocument) {
    577                 HTMLDocument doc = (HTMLDocument) d;
    578                 Element element = doc.getElement(id);
    579                 try {
    580                     // Deprecated API to replace only when migrating to Java 9 (replacement not available in Java 8)
    581                     @SuppressWarnings("deprecation")
    582                     Rectangle r = help.modelToView(element.getStartOffset());
    583                     if (r != null) {
    584                         Rectangle vis = help.getVisibleRect();
    585                         r.height = vis.height;
    586                         help.scrollRectToVisible(r);
    587                         return true;
    588                     }
    589                 } catch (BadLocationException e) {
    590                     Logging.warn(tr("Bad location in HTML document. Exception was: {0}", e.toString()));
    591                     Logging.error(e);
    592                 }
    593             }
    594             return false;
    595         }
    596 
    597         /**
    598          * Checks whether the hyperlink event originated on a &lt;a ...&gt; element with
    599          * a relative href consisting of a URL fragment only, i.e.
    600          * &lt;a href="#thisIsALocalFragment"&gt;. If so, replies the fragment, i.e. "thisIsALocalFragment".
    601          *
    602          * Otherwise, replies <code>null</code>
    603          *
    604          * @param e the hyperlink event
    605          * @return the local fragment or <code>null</code>
    606          */
    607         protected String getUrlFragment(HyperlinkEvent e) {
    608             AttributeSet set = e.getSourceElement().getAttributes();
    609             Object value = set.getAttribute(Tag.A);
    610             if (!(value instanceof SimpleAttributeSet))
    611                 return null;
    612             SimpleAttributeSet atts = (SimpleAttributeSet) value;
    613             value = atts.getAttribute(javax.swing.text.html.HTML.Attribute.HREF);
    614             if (value == null)
    615                 return null;
    616             String s = (String) value;
    617             Matcher m = Pattern.compile("(?:"+url+")?#(.+)").matcher(s);
    618             if (m.matches())
    619                 return m.group(1);
    620             return null;
    621         }
    622 
    623         @Override
    624         public void hyperlinkUpdate(HyperlinkEvent e) {
    625             if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
    626                 return;
    627             if (e.getURL() == null || e.getURL().toExternalForm().startsWith(url+'#')) {
    628                 // Probably hyperlink event on a an A-element with a href consisting of a fragment only, i.e. "#ALocalFragment".
    629                 String fragment = getUrlFragment(e);
    630                 if (fragment != null) {
    631                     // first try to scroll to an element with id==fragment. This is the way
    632                     // table of contents are built in the JOSM wiki. If this fails, try to
    633                     // scroll to a <A name="..."> element.
    634                     //
    635                     if (!scrollToElementWithId(fragment)) {
    636                         help.scrollToReference(fragment);
    637                     }
    638                 } else {
    639                     HelpAwareOptionPane.showOptionDialog(
    640                             instance,
    641                             tr("Failed to open help page. The target URL is empty."),
    642                             tr("Failed to open help page"),
    643                             JOptionPane.ERROR_MESSAGE,
    644                             null, /* no icon */
    645                             null, /* standard options, just OK button */
    646                             null, /* default is standard */
    647                             null /* no help context */
    648                     );
    649                 }
    650             } else if (e.getURL().toExternalForm().endsWith("action=edit")) {
    651                 OpenBrowser.displayUrl(e.getURL().toExternalForm());
    652             } else {
    653                 url = e.getURL().toExternalForm();
    654                 if (url.startsWith(HelpUtil.getWikiBaseUrl())) {
    655                     openUrl(url);
    656                 } else {
    657                     OpenBrowser.displayUrl(url);
    658                 }
    659             }
    660         }
    661     }
    662 
    663561    @Override
    664562    public HelpBrowserHistory getHistory() {
  • trunk/src/org/openstreetmap/josm/gui/help/IHelpBrowser.java

    r9665 r14807  
    1313     */
    1414    String getUrl();
     15
     16    /**
     17     * Sets the current URL.
     18     * @param url the current URL
     19     * @since 14807
     20     */
     21    void setUrl(String url);
    1522
    1623    /**
  • trunk/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java

    r14412 r14807  
    6161
    6262            @Override
     63            public void setUrl(String url) {
     64                this.url = url;
     65            }
     66
     67            @Override
    6368            public HelpBrowserHistory getHistory() {
    6469                return history;
Note: See TracChangeset for help on using the changeset viewer.