Changeset 14807 in josm
- Timestamp:
- 2019-02-24T22:43:26+01:00 (5 years ago)
- Location:
- trunk
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/help/HelpBrowser.java
r14369 r14807 9 9 import java.awt.BorderLayout; 10 10 import java.awt.Dimension; 11 import java.awt.Rectangle;12 11 import java.awt.event.ActionEvent; 13 12 import java.awt.event.WindowAdapter; … … 17 16 import java.nio.charset.StandardCharsets; 18 17 import java.util.Locale; 19 import java.util.regex.Matcher;20 import java.util.regex.Pattern;21 18 22 19 import javax.swing.AbstractAction; … … 32 29 import javax.swing.event.ChangeEvent; 33 30 import javax.swing.event.ChangeListener; 34 import javax.swing.event.HyperlinkEvent;35 import javax.swing.event.HyperlinkListener;36 import javax.swing.text.AttributeSet;37 31 import javax.swing.text.BadLocationException; 38 32 import 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;43 33 import javax.swing.text.html.StyleSheet; 44 34 … … 177 167 help.setEditorKit(kit); 178 168 help.setEditable(false); 179 help.addHyperlinkListener(new HyperlinkHandler( ));169 help.addHyperlinkListener(new HyperlinkHandler(this, help)); 180 170 help.setContentType("text/html"); 181 171 history = new HelpBrowserHistory(this); … … 247 237 public String getUrl() { 248 238 return url; 239 } 240 241 @Override 242 public void setUrl(String url) { 243 this.url = url; 249 244 } 250 245 … … 564 559 } 565 560 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 id572 * @return true, if an element with this id was found and scrolling was successful; false, otherwise573 */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 <a ...> element with599 * a relative href consisting of a URL fragment only, i.e.600 * <a href="#thisIsALocalFragment">. If so, replies the fragment, i.e. "thisIsALocalFragment".601 *602 * Otherwise, replies <code>null</code>603 *604 * @param e the hyperlink event605 * @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 @Override624 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 way632 // table of contents are built in the JOSM wiki. If this fails, try to633 // 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 663 561 @Override 664 562 public HelpBrowserHistory getHistory() { -
trunk/src/org/openstreetmap/josm/gui/help/IHelpBrowser.java
r9665 r14807 13 13 */ 14 14 String getUrl(); 15 16 /** 17 * Sets the current URL. 18 * @param url the current URL 19 * @since 14807 20 */ 21 void setUrl(String url); 15 22 16 23 /** -
trunk/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java
r14412 r14807 61 61 62 62 @Override 63 public void setUrl(String url) { 64 this.url = url; 65 } 66 67 @Override 63 68 public HelpBrowserHistory getHistory() { 64 69 return history;
Note:
See TracChangeset
for help on using the changeset viewer.