001 package org.shiftone.jrat.desktop.util;
002
003
004 import org.shiftone.jrat.util.log.Logger;
005
006 import javax.swing.*;
007 import javax.swing.border.EmptyBorder;
008 import javax.swing.event.HyperlinkEvent;
009 import javax.swing.event.HyperlinkListener;
010 import javax.swing.text.html.HTMLEditorKit;
011 import java.awt.*;
012 import java.awt.event.ActionEvent;
013 import java.awt.event.ActionListener;
014 import java.io.PrintWriter;
015 import java.io.StringWriter;
016 import java.net.URL;
017
018
019 /**
020 * Class BrowserPanel
021 *
022 * @author jeff@shiftone.org (Jeff Drost)
023 */
024 public class BrowserPanel extends JPanel implements HyperlinkListener, ActionListener {
025
026 private static final Logger LOG = Logger.getLogger(BrowserPanel.class);
027 private final URL homePage;
028 private final JToolBar toolBar = new JToolBar();
029 private final JEditorPane editorPane = new JEditorPane();
030 private final JScrollPane scrollPane = new JScrollPane(editorPane);
031 private final JLabel location = new JLabel();
032 private final JButton home = new JButton("Home");
033 private final JButton back = new JButton("Back");
034 private final JButton forword = new JButton("Forward");
035 private URL[] urls = new URL[1024];
036 private int current;
037 private int maxCurrent;
038
039
040 public BrowserPanel(URL homePage) {
041
042 this.homePage = homePage;
043
044 HTMLEditorKit kit = new HTMLEditorKit();
045
046 kit.setLinkCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
047
048 //
049 editorPane.setEditorKit(kit);
050 editorPane.setEditable(false);
051 editorPane.addHyperlinkListener(this);
052 editorPane.setBorder(new EmptyBorder(20, 10, 10, 10));
053
054 editorPane.setBackground(Color.white);
055
056 //
057 setLayout(new BorderLayout());
058 add(toolBar, BorderLayout.NORTH);
059 add(scrollPane, BorderLayout.CENTER);
060 add(location, BorderLayout.SOUTH);
061
062 //
063 toolBar.add(home);
064 toolBar.add(back);
065 toolBar.add(forword);
066 home.addActionListener(this);
067 back.addActionListener(this);
068 forword.addActionListener(this);
069
070 setHyperLink(homePage, true);
071
072 }
073
074
075 /**
076 * Method hyperlinkUpdate
077 */
078 public void hyperlinkUpdate(HyperlinkEvent e) {
079
080 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
081 setHyperLink(e.getURL(), true);
082 }
083 }
084
085
086 /**
087 * Method setHyperLink
088 */
089 public void setHyperLink(URL url, boolean push) {
090
091 try {
092
093 if (url.equals(editorPane.getPage())) {
094 return;
095 }
096
097 editorPane.setPage(url);
098
099 if (push) {
100 current++;
101
102 maxCurrent = current;
103 } else {
104 maxCurrent = Math.max(current, maxCurrent);
105 }
106
107 urls[current] = url;
108
109 location.setText(current + " : " + url.toString());
110 back.setEnabled(current > 1);
111 forword.setEnabled(current < maxCurrent);
112 }
113
114 catch (Exception e) {
115
116 error(e);
117
118 }
119 }
120
121
122 /**
123 * Method error
124 */
125 private void error(Exception e) {
126
127 StringWriter stringWriter = new StringWriter();
128 PrintWriter out = new PrintWriter(stringWriter);
129
130 out.println("<b>Unable to open Documentation</b><p>");
131 e.printStackTrace(out);
132 out.flush();
133 editorPane.setText(stringWriter.toString());
134 }
135
136
137 /**
138 * Method actionPerformed
139 */
140 public void actionPerformed(ActionEvent e) {
141
142 if (e.getSource() == home) {
143
144 setHyperLink(homePage, true);
145
146 } else if (e.getSource() == back) {
147
148 if (current > 1) {
149 current--;
150
151 setHyperLink(urls[current], false);
152 }
153 } else if (e.getSource() == forword) {
154 if (current < maxCurrent) {
155 current++;
156
157 setHyperLink(urls[current], false);
158 }
159 }
160 }
161 }