Changeset 9644 in josm


Ignore:
Timestamp:
2016-01-27T01:11:06+01:00 (8 years ago)
Author:
Don-vip
Message:

help browser: code refactoring, add first unit tests

Location:
trunk
Files:
3 added
2 edited

Legend:

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

    r9078 r9644  
    88import java.awt.BorderLayout;
    99import java.awt.Dimension;
     10import java.awt.GraphicsEnvironment;
    1011import java.awt.Rectangle;
    1112import java.awt.event.ActionEvent;
     
    5859 * Help browser displaying HTML pages fetched from JOSM wiki.
    5960 */
    60 public class HelpBrowser extends JDialog {
     61public class HelpBrowser extends JDialog implements IHelpBrowser {
     62
    6163    /** the unique instance */
    6264    private static HelpBrowser instance;
    6365
    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 */
    6767    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    }
    6894
    6995    /**
     
    110136    }
    111137
    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         @Override
    125         public void actionPerformed(ActionEvent e) {
    126             HelpBrowser.getInstance().setVisible(true);
    127         }
    128     };
    129 
    130138    /**
    131139     * Builds the style sheet used in the internal help browser
     
    136144        StyleSheet ss = new StyleSheet();
    137145        StringBuilder css = new StringBuilder();
    138         try (BufferedReader reader = new BufferedReader(
     146        try (BufferedReader breader = new BufferedReader(
    139147                new InputStreamReader(
    140148                        getClass().getResourceAsStream("/data/help-browser.css"), StandardCharsets.UTF_8
    141149                )
    142150        )) {
    143             String line = null;
    144             while ((line = reader.readLine()) != null) {
     151            String line;
     152            while ((line = breader.readLine()) != null) {
    145153                css.append(line);
    146154                css.append('\n');
     
    157165    protected JToolBar buildToolBar() {
    158166        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)));
    163171        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)));
    166174        return tb;
    167175    }
     
    226234    }
    227235
    228     /**
    229      * Constructs a new {@code HelpBrowser}.
    230      */
    231     public HelpBrowser() {
    232         reader = new HelpContentReader(HelpUtil.getWikiBaseUrl());
    233         build();
    234     }
    235 
    236236    protected void loadTopic(String content) {
    237237        Document document = help.getEditorKit().createDefaultDocument();
     
    244244    }
    245245
    246     /**
    247      * Replies the current URL
    248      *
    249      * @return the current URL
    250      */
     246    @Override
    251247    public String getUrl() {
    252248        return url;
     
    363359    }
    364360
    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
    373362    public void openUrl(String url) {
    374363        if (!isVisible()) {
     
    410399    }
    411400
    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
    418402    public void openHelpTopic(String relativeHelpTopic) {
    419403        if (!isVisible()) {
     
    426410    }
    427411
    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);
    430428            putValue(SHORT_DESCRIPTION, tr("Open the current help page in an external browser"));
    431429            putValue(SMALL_ICON, ImageProvider.get("help", "internet"));
     
    434432        @Override
    435433        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
    441440        /**
    442441         * Constructs a new {@code EditAction}.
    443          */
    444         EditAction() {
     442         * @param browser help browser
     443         */
     444        EditAction(IHelpBrowser browser) {
     445            super(browser);
    445446            putValue(SHORT_DESCRIPTION, tr("Edit the current help page"));
    446447            putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
     
    449450        @Override
    450451        public void actionPerformed(ActionEvent e) {
    451             String url = getUrl();
     452            String url = browser.getUrl();
    452453            if (url == null)
    453454                return;
     
    457458                        + "is an external URL. Editing is only possible for help topics<br>"
    458459                        + "on the help server <tt>{1}</tt>.</html>",
    459                         getUrl(),
     460                        url,
    460461                        HelpUtil.getWikiBaseUrl()
    461462                );
    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                }
    468471                return;
    469472            }
     
    473476    }
    474477
    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);
    477486            putValue(SHORT_DESCRIPTION, tr("Reload the current help page"));
    478487            putValue(SMALL_ICON, ImageProvider.get("dialogs", "refresh"));
     
    481490        @Override
    482491        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);
    493505            putValue(SHORT_DESCRIPTION, tr("Go to the previous page"));
    494506            putValue(SMALL_ICON, ImageProvider.get("help", "previous"));
    495             setEnabled(history.canGoBack());
     507            setEnabled(browser.getHistory().canGoBack());
    496508        }
    497509
    498510        @Override
    499511        public void actionPerformed(ActionEvent e) {
    500             history.back();
     512            browser.getHistory().back();
    501513        }
    502514
    503515        @Override
    504516        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);
    515530            putValue(SHORT_DESCRIPTION, tr("Go to the next page"));
    516531            putValue(SMALL_ICON, ImageProvider.get("help", "next"));
    517             setEnabled(history.canGoForward());
     532            setEnabled(browser.getHistory().canGoForward());
    518533        }
    519534
    520535        @Override
    521536        public void actionPerformed(ActionEvent e) {
    522             history.forward();
     537            browser.getHistory().forward();
    523538        }
    524539
    525540        @Override
    526541        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
    532548        /**
    533549         * Constructs a new {@code HomeAction}.
    534          */
    535         HomeAction() {
     550         * @param browser help browser
     551         */
     552        HomeAction(IHelpBrowser browser) {
     553            super(browser);
    536554            putValue(SHORT_DESCRIPTION, tr("Go to the JOSM help home page"));
    537555            putValue(SMALL_ICON, ImageProvider.get("help", "home"));
     
    540558        @Override
    541559        public void actionPerformed(ActionEvent e) {
    542             openHelpTopic("/");
     560            browser.openHelpTopic("/");
    543561        }
    544562    }
     
    576594         * Checks whether the hyperlink event originated on a &lt;a ...&gt; element with
    577595         * a relative href consisting of a URL fragment only, i.e.
    578          * &lt;a href="#thisIsALocalFragment"&gt;. If so, replies the fragment, i.e.
    579          * "thisIsALocalFragment".
     596         * &lt;a href="#thisIsALocalFragment"&gt;. If so, replies the fragment, i.e. "thisIsALocalFragment".
    580597         *
    581598         * Otherwise, replies <code>null</code>
     
    587604            AttributeSet set = e.getSourceElement().getAttributes();
    588605            Object value = set.getAttribute(Tag.A);
    589             if (!(value instanceof SimpleAttributeSet)) return null;
     606            if (!(value instanceof SimpleAttributeSet))
     607                return null;
    590608            SimpleAttributeSet atts = (SimpleAttributeSet) value;
    591609            value = atts.getAttribute(javax.swing.text.html.HTML.Attribute.HREF);
    592             if (value == null) return null;
     610            if (value == null)
     611                return null;
    593612            String s = (String) value;
    594613            if (s.matches("#.*"))
     
    632651        }
    633652    }
     653
     654    @Override
     655    public HelpBrowserHistory getHistory() {
     656        return history;
     657    }
    634658}
  • trunk/src/org/openstreetmap/josm/gui/help/HelpBrowserHistory.java

    r9078 r9644  
    77import java.util.Observable;
    88
     9/**
     10 * Help browser history.
     11 * @since 2274
     12 */
    913public class HelpBrowserHistory extends Observable {
    10     private final HelpBrowser browser;
     14    private final IHelpBrowser browser;
    1115    private List<String> history;
    1216    private int historyPos;
    1317
    14     public HelpBrowserHistory(HelpBrowser browser) {
     18    /**
     19     * Constructs a new {@code HelpBrowserHistory}.
     20     * @param browser help browser
     21     */
     22    public HelpBrowserHistory(IHelpBrowser browser) {
    1523        this.browser = browser;
    1624        history = new ArrayList<>();
    1725    }
    1826
     27    /**
     28     * Clears the history.
     29     */
    1930    public void clear() {
    2031        history.clear();
     
    2435    }
    2536
     37    /**
     38     * Determines if the help browser can go back.
     39     * @return {@code true} if a previous history position exists
     40     */
    2641    public boolean canGoBack() {
    2742        return historyPos > 0;
    2843    }
    2944
     45    /**
     46     * Determines if the help browser can go forward.
     47     * @return {@code true} if a following history position exists
     48     */
    3049    public boolean canGoForward() {
    3150        return historyPos + 1 < history.size();
    3251    }
    3352
     53    /**
     54     * Go back.
     55     */
    3456    public void back() {
    3557        historyPos--;
    36         if (historyPos < 0) return;
     58        if (historyPos < 0)
     59            return;
    3760        String url = history.get(historyPos);
    3861        browser.openUrl(url);
     
    4164    }
    4265
     66    /**
     67     * Go forward.
     68     */
    4369    public void forward() {
    4470        historyPos++;
    45         if (historyPos >= history.size()) return;
     71        if (historyPos >= history.size())
     72            return;
    4673        String url = history.get(historyPos);
    4774        browser.openUrl(url);
     
    5077    }
    5178
     79    /**
     80     * Remembers the new current URL.
     81     * @param url the new current URL
     82     */
    5283    public void setCurrentUrl(String url) {
    5384        boolean add = true;
Note: See TracChangeset for help on using the changeset viewer.