1 /*
2  *
3  * Copyright (C) 2007,2008  Markko Merzin (markko.merzin@ut.ee)
4  *
5  * This file is part of ImageJ plugin Volumest.
6  * 
7  * Volumest plugin is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *****************************************************************************
21 */
22package ee.ut.mrz.volumest;
23
24import java.io.IOException;
25import java.io.InputStream;
26import java.util.ArrayList;
27import java.net.URL;
28import javax.swing.event.HyperlinkEvent;
29import javax.swing.event.HyperlinkListener;
30import javax.swing.text.BadLocationException;
31import javax.swing.text.rtf.RTFEditorKit;
32
33/**
34 * Simple help browser.
35 * @author Markko Merzin, markko.merzin@ut.ee
36 */
37public class HelpBrowser extends javax.swing.JFrame implements HyperlinkListener {
38
39    ArrayList<URL> browsehistory = new ArrayList<URL>();
40
41    /** Creates new form HelpBrowser */
42    public HelpBrowser() {
43        initComponents();
44
45        URL url = this.getClass().getResource("/help/index.html");
46
47        try {
48            jEditorPane1.setPage(url);
49            browsehistory.add(url);
50        } catch (IOException ex) {
51            ex.printStackTrace();
52        }
53
54        jEditorPane1.addHyperlinkListener(this);
55
56
57    }
58
59    /**
60     * Creates new HelpBrowser and opens specificed url in it.
61     * @param url URL to open.
62     */
63    public HelpBrowser(URL url) {
64        initComponents();
65
66        try {
67            jEditorPane1.setPage(url);
68            browsehistory.add(url);
69        } catch (IOException ex) {
70            ex.printStackTrace();
71        }
72
73        jEditorPane1.addHyperlinkListener(this);
74
75
76    }
77
78    /**
79     * Opens requested helppage from jar.
80     * @param file Helpfile to open
81     */
82    public HelpBrowser(String file) {
83        initComponents();
84
85        URL url = this.getClass().getResource(file);
86
87        try {
88            jEditorPane1.setPage(url);
89            browsehistory.add(url);
90        } catch (IOException ex) {
91            ex.printStackTrace();
92        }
93
94        jEditorPane1.addHyperlinkListener(this);
95
96    }
97
98    /** This method is called from within the constructor to
99     * initialize the form.
00     * WARNING: Do NOT modify this code. The content of this method is
01     * always regenerated by the Form Editor.
02     */
03    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
04    private void initComponents() {
05        java.awt.GridBagConstraints gridBagConstraints;
06
07        jScrollPane1 = new javax.swing.JScrollPane();
08        jEditorPane1 = new javax.swing.JEditorPane();
09        jPanel1 = new javax.swing.JPanel();
10        button1 = new java.awt.Button();
11        backButton = new java.awt.Button();
12        closeButton = new java.awt.Button();
13
14        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
15        getContentPane().setLayout(new java.awt.GridBagLayout());
16
17        jEditorPane1.setEditable(false);
18        jScrollPane1.setViewportView(jEditorPane1);
19
20        gridBagConstraints = new java.awt.GridBagConstraints();
21        gridBagConstraints.gridx = 0;
22        gridBagConstraints.gridy = 0;
23        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
24        gridBagConstraints.ipadx = 400;
25        gridBagConstraints.ipady = 300;
26        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
27        gridBagConstraints.weightx = 1.0;
28        gridBagConstraints.weighty = 1.0;
29        getContentPane().add(jScrollPane1, gridBagConstraints);
30
31        jPanel1.setLayout(new java.awt.GridBagLayout());
32
33        button1.setLabel("about");
34        button1.addActionListener(new java.awt.event.ActionListener() {
35            public void actionPerformed(java.awt.event.ActionEvent evt) {
36                aboutPressed(evt);
37            }
38        });
39        gridBagConstraints = new java.awt.GridBagConstraints();
40        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
41        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
42        jPanel1.add(button1, gridBagConstraints);
43
44        backButton.setLabel("back");
45        backButton.addActionListener(new java.awt.event.ActionListener() {
46            public void actionPerformed(java.awt.event.ActionEvent evt) {
47                goBack(evt);
48            }
49        });
50        gridBagConstraints = new java.awt.GridBagConstraints();
51        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
52        jPanel1.add(backButton, gridBagConstraints);
53
54        closeButton.setLabel("close");
55        closeButton.addActionListener(new java.awt.event.ActionListener() {
56            public void actionPerformed(java.awt.event.ActionEvent evt) {
57                closeWindow(evt);
58            }
59        });
60        gridBagConstraints = new java.awt.GridBagConstraints();
61        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
62        jPanel1.add(closeButton, gridBagConstraints);
63
64        gridBagConstraints = new java.awt.GridBagConstraints();
65        gridBagConstraints.gridx = 0;
66        gridBagConstraints.gridy = 1;
67        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
68        getContentPane().add(jPanel1, gridBagConstraints);
69
70        pack();
71    }// </editor-fold>//GEN-END:initComponents
72    private void closeWindow(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeWindow
73        dispose();
74    }//GEN-LAST:event_closeWindow
75
76    private void goBack(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goBack
77
78        if (browsehistory.size() > 1) {
79            browsehistory.remove(browsehistory.size() - 1);
80
81            URL url = (URL) browsehistory.get(browsehistory.size() - 1);
82
83            try {
84                jEditorPane1.setContentType("text/html");
85                jEditorPane1.setPage(url);
86            } catch (IOException ex) {
87                ex.printStackTrace();
88            }
89
90        }
91    }//GEN-LAST:event_goBack
92
93    private void aboutPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutPressed
94        URL url = this.getClass().getResource("/help/about.html");
95
96        try {
97            jEditorPane1.setPage(url);
98            browsehistory.add(url);
99        } catch (IOException ex) {
00            ex.printStackTrace();
01        }
02
03    }//GEN-LAST:event_aboutPressed
04
05    /**
06     * Processes link events.
07     * @param hyperlinkEvent See: javax.swing.event.HyperlinkEvent
08     */
09    /*
10    public static void main(String args[]) {
11    java.awt.EventQueue.invokeLater(new Runnable() {
12    public void run() {
13    new HelpBrowser().setVisible(true);
14    }
15    });
16    }
17     */
18    public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
19        if (hyperlinkEvent.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
20            URL url = hyperlinkEvent.getURL();
21            browsehistory.add(url);
22            try {
23                if (url.getFile().endsWith(".rtf")) {
24
25                    jEditorPane1.setContentType("text/rtf");
26
27                    RTFEditorKit rtf = new RTFEditorKit();
28
29                    InputStream fi = url.openStream();
30                    rtf.read(fi, jEditorPane1.getDocument(), 0);
31
32                    fi.close();
33
34                } else {
35                    jEditorPane1.setContentType("text/html");
36                    jEditorPane1.setPage(url);
37
38                }
39
40            } catch (IOException ioe) {
41                jEditorPane1.setContentType("text/plain");
42                jEditorPane1.setText(ioe.toString());
43
44            } catch (BadLocationException ex) {
45                jEditorPane1.setContentType("text/plain");
46                jEditorPane1.setText(ex.toString());
47            }
48        }
49    }
50    // Variables declaration - do not modify//GEN-BEGIN:variables
51    private java.awt.Button backButton;
52    private java.awt.Button button1;
53    private java.awt.Button closeButton;
54    private javax.swing.JEditorPane jEditorPane1;
55    private javax.swing.JPanel jPanel1;
56    private javax.swing.JScrollPane jScrollPane1;
57    // End of variables declaration//GEN-END:variables
58}
59