- Timestamp:
- 2016-01-27T01:11:06+01:00 (9 years ago)
- Location:
- trunk
- Files:
-
- 3 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/help/HelpBrowser.java
r9078 r9644 8 8 import java.awt.BorderLayout; 9 9 import java.awt.Dimension; 10 import java.awt.GraphicsEnvironment; 10 11 import java.awt.Rectangle; 11 12 import java.awt.event.ActionEvent; … … 58 59 * Help browser displaying HTML pages fetched from JOSM wiki. 59 60 */ 60 public class HelpBrowser extends JDialog { 61 public class HelpBrowser extends JDialog implements IHelpBrowser { 62 61 63 /** the unique instance */ 62 64 private static HelpBrowser instance; 63 65 64 /** the menu item in the windows menu. Required to properly 65 * hide on dialog close. 66 */ 66 /** the menu item in the windows menu. Required to properly hide on dialog close */ 67 67 private JMenuItem windowMenuItem; 68 69 /** the help browser */ 70 private JosmEditorPane help; 71 72 /** the help browser history */ 73 private transient HelpBrowserHistory history; 74 75 /** the currently displayed URL */ 76 private String url; 77 78 private final transient HelpContentReader reader; 79 80 private static final JosmAction focusAction = new JosmAction(tr("JOSM Help Browser"), "help", "", null, false, false) { 81 @Override 82 public void actionPerformed(ActionEvent e) { 83 HelpBrowser.getInstance().setVisible(true); 84 } 85 }; 86 87 /** 88 * Constructs a new {@code HelpBrowser}. 89 */ 90 public HelpBrowser() { 91 reader = new HelpContentReader(HelpUtil.getWikiBaseUrl()); 92 build(); 93 } 68 94 69 95 /** … … 110 136 } 111 137 112 /** the help browser */113 private JosmEditorPane help;114 115 /** the help browser history */116 private transient HelpBrowserHistory history;117 118 /** the currently displayed URL */119 private String url;120 121 private final transient HelpContentReader reader;122 123 private static final JosmAction focusAction = new JosmAction(tr("JOSM Help Browser"), "help", "", null, false, false) {124 @Override125 public void actionPerformed(ActionEvent e) {126 HelpBrowser.getInstance().setVisible(true);127 }128 };129 130 138 /** 131 139 * Builds the style sheet used in the internal help browser … … 136 144 StyleSheet ss = new StyleSheet(); 137 145 StringBuilder css = new StringBuilder(); 138 try (BufferedReader reader = new BufferedReader(146 try (BufferedReader breader = new BufferedReader( 139 147 new InputStreamReader( 140 148 getClass().getResourceAsStream("/data/help-browser.css"), StandardCharsets.UTF_8 141 149 ) 142 150 )) { 143 String line = null;144 while ((line = reader.readLine()) != null) {151 String line; 152 while ((line = breader.readLine()) != null) { 145 153 css.append(line); 146 154 css.append('\n'); … … 157 165 protected JToolBar buildToolBar() { 158 166 JToolBar tb = new JToolBar(); 159 tb.add(new JButton(new HomeAction( )));160 tb.add(new JButton(new BackAction( history)));161 tb.add(new JButton(new ForwardAction( history)));162 tb.add(new JButton(new ReloadAction( )));167 tb.add(new JButton(new HomeAction(this))); 168 tb.add(new JButton(new BackAction(this))); 169 tb.add(new JButton(new ForwardAction(this))); 170 tb.add(new JButton(new ReloadAction(this))); 163 171 tb.add(new JSeparator()); 164 tb.add(new JButton(new OpenInBrowserAction( )));165 tb.add(new JButton(new EditAction( )));172 tb.add(new JButton(new OpenInBrowserAction(this))); 173 tb.add(new JButton(new EditAction(this))); 166 174 return tb; 167 175 } … … 226 234 } 227 235 228 /**229 * Constructs a new {@code HelpBrowser}.230 */231 public HelpBrowser() {232 reader = new HelpContentReader(HelpUtil.getWikiBaseUrl());233 build();234 }235 236 236 protected void loadTopic(String content) { 237 237 Document document = help.getEditorKit().createDefaultDocument(); … … 244 244 } 245 245 246 /** 247 * Replies the current URL 248 * 249 * @return the current URL 250 */ 246 @Override 251 247 public String getUrl() { 252 248 return url; … … 363 359 } 364 360 365 /** 366 * Opens an URL and displays the content. 367 * 368 * If the URL is the locator of an absolute help topic, help content is loaded from 369 * the JOSM wiki. Otherwise, the help browser loads the page from the given URL 370 * 371 * @param url the url 372 */ 361 @Override 373 362 public void openUrl(String url) { 374 363 if (!isVisible()) { … … 410 399 } 411 400 412 /** 413 * Loads and displays the help information for a help topic given 414 * by a relative help topic name, i.e. "/Action/New" 415 * 416 * @param relativeHelpTopic the relative help topic 417 */ 401 @Override 418 402 public void openHelpTopic(String relativeHelpTopic) { 419 403 if (!isVisible()) { … … 426 410 } 427 411 428 class OpenInBrowserAction extends AbstractAction { 429 OpenInBrowserAction() { 412 abstract static class AbstractBrowserAction extends AbstractAction { 413 protected final transient IHelpBrowser browser; 414 415 protected AbstractBrowserAction(IHelpBrowser browser) { 416 this.browser = browser; 417 } 418 } 419 420 static class OpenInBrowserAction extends AbstractBrowserAction { 421 422 /** 423 * Constructs a new {@code OpenInBrowserAction}. 424 * @param browser help browser 425 */ 426 OpenInBrowserAction(IHelpBrowser browser) { 427 super(browser); 430 428 putValue(SHORT_DESCRIPTION, tr("Open the current help page in an external browser")); 431 429 putValue(SMALL_ICON, ImageProvider.get("help", "internet")); … … 434 432 @Override 435 433 public void actionPerformed(ActionEvent e) { 436 OpenBrowser.displayUrl(getUrl()); 437 } 438 } 439 440 class EditAction extends AbstractAction { 434 OpenBrowser.displayUrl(browser.getUrl()); 435 } 436 } 437 438 static class EditAction extends AbstractBrowserAction { 439 441 440 /** 442 441 * Constructs a new {@code EditAction}. 443 */ 444 EditAction() { 442 * @param browser help browser 443 */ 444 EditAction(IHelpBrowser browser) { 445 super(browser); 445 446 putValue(SHORT_DESCRIPTION, tr("Edit the current help page")); 446 447 putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit")); … … 449 450 @Override 450 451 public void actionPerformed(ActionEvent e) { 451 String url = getUrl();452 String url = browser.getUrl(); 452 453 if (url == null) 453 454 return; … … 457 458 + "is an external URL. Editing is only possible for help topics<br>" 458 459 + "on the help server <tt>{1}</tt>.</html>", 459 getUrl(),460 url, 460 461 HelpUtil.getWikiBaseUrl() 461 462 ); 462 JOptionPane.showMessageDialog( 463 Main.parent, 464 message, 465 tr("Warning"), 466 JOptionPane.WARNING_MESSAGE 467 ); 463 if (!GraphicsEnvironment.isHeadless()) { 464 JOptionPane.showMessageDialog( 465 Main.parent, 466 message, 467 tr("Warning"), 468 JOptionPane.WARNING_MESSAGE 469 ); 470 } 468 471 return; 469 472 } … … 473 476 } 474 477 475 class ReloadAction extends AbstractAction { 476 ReloadAction() { 478 static class ReloadAction extends AbstractBrowserAction { 479 480 /** 481 * Constructs a new {@code ReloadAction}. 482 * @param browser help browser 483 */ 484 ReloadAction(IHelpBrowser browser) { 485 super(browser); 477 486 putValue(SHORT_DESCRIPTION, tr("Reload the current help page")); 478 487 putValue(SMALL_ICON, ImageProvider.get("dialogs", "refresh")); … … 481 490 @Override 482 491 public void actionPerformed(ActionEvent e) { 483 openUrl(getUrl()); 484 } 485 } 486 487 static class BackAction extends AbstractAction implements Observer { 488 private final transient HelpBrowserHistory history; 489 490 BackAction(HelpBrowserHistory history) { 491 this.history = history; 492 history.addObserver(this); 492 browser.openUrl(browser.getUrl()); 493 } 494 } 495 496 static class BackAction extends AbstractBrowserAction implements Observer { 497 498 /** 499 * Constructs a new {@code BackAction}. 500 * @param browser help browser 501 */ 502 BackAction(IHelpBrowser browser) { 503 super(browser); 504 browser.getHistory().addObserver(this); 493 505 putValue(SHORT_DESCRIPTION, tr("Go to the previous page")); 494 506 putValue(SMALL_ICON, ImageProvider.get("help", "previous")); 495 setEnabled( history.canGoBack());507 setEnabled(browser.getHistory().canGoBack()); 496 508 } 497 509 498 510 @Override 499 511 public void actionPerformed(ActionEvent e) { 500 history.back();512 browser.getHistory().back(); 501 513 } 502 514 503 515 @Override 504 516 public void update(Observable o, Object arg) { 505 setEnabled(history.canGoBack()); 506 } 507 } 508 509 static class ForwardAction extends AbstractAction implements Observer { 510 private final transient HelpBrowserHistory history; 511 512 ForwardAction(HelpBrowserHistory history) { 513 this.history = history; 514 history.addObserver(this); 517 setEnabled(browser.getHistory().canGoBack()); 518 } 519 } 520 521 static class ForwardAction extends AbstractBrowserAction implements Observer { 522 523 /** 524 * Constructs a new {@code ForwardAction}. 525 * @param browser help browser 526 */ 527 ForwardAction(IHelpBrowser browser) { 528 super(browser); 529 browser.getHistory().addObserver(this); 515 530 putValue(SHORT_DESCRIPTION, tr("Go to the next page")); 516 531 putValue(SMALL_ICON, ImageProvider.get("help", "next")); 517 setEnabled( history.canGoForward());532 setEnabled(browser.getHistory().canGoForward()); 518 533 } 519 534 520 535 @Override 521 536 public void actionPerformed(ActionEvent e) { 522 history.forward();537 browser.getHistory().forward(); 523 538 } 524 539 525 540 @Override 526 541 public void update(Observable o, Object arg) { 527 setEnabled(history.canGoForward()); 528 } 529 } 530 531 class HomeAction extends AbstractAction { 542 setEnabled(browser.getHistory().canGoForward()); 543 } 544 } 545 546 static class HomeAction extends AbstractBrowserAction { 547 532 548 /** 533 549 * Constructs a new {@code HomeAction}. 534 */ 535 HomeAction() { 550 * @param browser help browser 551 */ 552 HomeAction(IHelpBrowser browser) { 553 super(browser); 536 554 putValue(SHORT_DESCRIPTION, tr("Go to the JOSM help home page")); 537 555 putValue(SMALL_ICON, ImageProvider.get("help", "home")); … … 540 558 @Override 541 559 public void actionPerformed(ActionEvent e) { 542 openHelpTopic("/");560 browser.openHelpTopic("/"); 543 561 } 544 562 } … … 576 594 * Checks whether the hyperlink event originated on a <a ...> element with 577 595 * a relative href consisting of a URL fragment only, i.e. 578 * <a href="#thisIsALocalFragment">. If so, replies the fragment, i.e. 579 * "thisIsALocalFragment". 596 * <a href="#thisIsALocalFragment">. If so, replies the fragment, i.e. "thisIsALocalFragment". 580 597 * 581 598 * Otherwise, replies <code>null</code> … … 587 604 AttributeSet set = e.getSourceElement().getAttributes(); 588 605 Object value = set.getAttribute(Tag.A); 589 if (!(value instanceof SimpleAttributeSet)) return null; 606 if (!(value instanceof SimpleAttributeSet)) 607 return null; 590 608 SimpleAttributeSet atts = (SimpleAttributeSet) value; 591 609 value = atts.getAttribute(javax.swing.text.html.HTML.Attribute.HREF); 592 if (value == null) return null; 610 if (value == null) 611 return null; 593 612 String s = (String) value; 594 613 if (s.matches("#.*")) … … 632 651 } 633 652 } 653 654 @Override 655 public HelpBrowserHistory getHistory() { 656 return history; 657 } 634 658 } -
trunk/src/org/openstreetmap/josm/gui/help/HelpBrowserHistory.java
r9078 r9644 7 7 import java.util.Observable; 8 8 9 /** 10 * Help browser history. 11 * @since 2274 12 */ 9 13 public class HelpBrowserHistory extends Observable { 10 private final HelpBrowser browser;14 private final IHelpBrowser browser; 11 15 private List<String> history; 12 16 private int historyPos; 13 17 14 public HelpBrowserHistory(HelpBrowser browser) { 18 /** 19 * Constructs a new {@code HelpBrowserHistory}. 20 * @param browser help browser 21 */ 22 public HelpBrowserHistory(IHelpBrowser browser) { 15 23 this.browser = browser; 16 24 history = new ArrayList<>(); 17 25 } 18 26 27 /** 28 * Clears the history. 29 */ 19 30 public void clear() { 20 31 history.clear(); … … 24 35 } 25 36 37 /** 38 * Determines if the help browser can go back. 39 * @return {@code true} if a previous history position exists 40 */ 26 41 public boolean canGoBack() { 27 42 return historyPos > 0; 28 43 } 29 44 45 /** 46 * Determines if the help browser can go forward. 47 * @return {@code true} if a following history position exists 48 */ 30 49 public boolean canGoForward() { 31 50 return historyPos + 1 < history.size(); 32 51 } 33 52 53 /** 54 * Go back. 55 */ 34 56 public void back() { 35 57 historyPos--; 36 if (historyPos < 0) return; 58 if (historyPos < 0) 59 return; 37 60 String url = history.get(historyPos); 38 61 browser.openUrl(url); … … 41 64 } 42 65 66 /** 67 * Go forward. 68 */ 43 69 public void forward() { 44 70 historyPos++; 45 if (historyPos >= history.size()) return; 71 if (historyPos >= history.size()) 72 return; 46 73 String url = history.get(historyPos); 47 74 browser.openUrl(url); … … 50 77 } 51 78 79 /** 80 * Remembers the new current URL. 81 * @param url the new current URL 82 */ 52 83 public void setCurrentUrl(String url) { 53 84 boolean add = true;
Note:
See TracChangeset
for help on using the changeset viewer.