1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.chainsaw;
19
20 import org.apache.log4j.chainsaw.icons.ChainsawIcons;
21
22 import java.awt.BorderLayout;
23 import java.awt.Color;
24 import java.awt.Dimension;
25 import java.awt.event.ActionEvent;
26
27 import java.io.IOException;
28
29 import java.net.URL;
30
31 import java.util.Stack;
32
33 import javax.swing.AbstractAction;
34 import javax.swing.Action;
35 import javax.swing.BorderFactory;
36 import javax.swing.ImageIcon;
37 import javax.swing.JButton;
38 import javax.swing.JEditorPane;
39 import javax.swing.JPanel;
40 import javax.swing.JScrollPane;
41 import javax.swing.JToolBar;
42 import javax.swing.SwingUtilities;
43 import javax.swing.event.HyperlinkEvent;
44 import javax.swing.event.HyperlinkListener;
45
46
47 /***
48 * An initial Welcome Panel that is used when Chainsaw starts up, can displays
49 * a HTML pages based on URLs.
50 *
51 * @author Paul Smith
52 * @author Scott Deboy <sdeboy@apache.org>
53 */
54 public class WelcomePanel extends JPanel {
55 private Stack urlStack = new Stack();
56 private final JEditorPane textInfo = new JEditorPane();
57 private final URLToolbar urlToolbar = new URLToolbar();
58
59 public WelcomePanel() {
60 super(new BorderLayout());
61 setBackground(Color.white);
62 add(urlToolbar, BorderLayout.NORTH);
63
64 URL helpURL = ChainsawConstants.WELCOME_URL;
65
66 if (helpURL != null) {
67 textInfo.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
68 textInfo.setEditable(false);
69 textInfo.setPreferredSize(new Dimension(320, 240));
70
71 JScrollPane pane = new JScrollPane(textInfo);
72 pane.setBorder(null);
73 add(pane, BorderLayout.CENTER);
74
75 try {
76 textInfo.setPage(helpURL);
77 textInfo.addHyperlinkListener(
78 new HyperlinkListener() {
79 public void hyperlinkUpdate(HyperlinkEvent e) {
80 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
81 urlStack.add(textInfo.getPage());
82
83 try {
84 textInfo.setPage(e.getURL());
85 urlToolbar.updateToolbar();
86 } catch (IOException e1) {
87 e1.printStackTrace();
88 }
89 }
90 }
91 });
92 } catch (Exception e) {
93 e.printStackTrace();
94 }
95 }
96 }
97
98 void setURL(final URL url) {
99 SwingUtilities.invokeLater(
100 new Runnable() {
101 public void run() {
102 try {
103 urlStack.push(textInfo.getPage());
104 textInfo.setPage(url);
105 urlToolbar.updateToolbar();
106 } catch (IOException e) {
107 e.printStackTrace();
108 }
109 }
110 });
111 }
112
113 private class URLToolbar extends JToolBar {
114 private final Action previousAction =
115 new AbstractAction(null, new ImageIcon(ChainsawIcons.ICON_BACK)) {
116 public void actionPerformed(ActionEvent e) {
117 if (urlStack.isEmpty()) {
118 return;
119 }
120
121 setURL((URL) urlStack.pop());
122 }
123 };
124
125 private final Action homeAction =
126 new AbstractAction(null, new ImageIcon(ChainsawIcons.ICON_HOME)) {
127 public void actionPerformed(ActionEvent e) {
128 setURL(ChainsawConstants.WELCOME_URL);
129 urlStack.clear();
130 }
131 };
132
133 private URLToolbar() {
134 setFloatable(false);
135 updateToolbar();
136 previousAction.putValue(Action.SHORT_DESCRIPTION, "Back");
137 homeAction.putValue(Action.SHORT_DESCRIPTION, "Home");
138
139 JButton home = new SmallButton(homeAction);
140 add(home);
141
142 addSeparator();
143
144 JButton previous = new SmallButton(previousAction);
145 previous.setEnabled(false);
146 add(previous);
147
148 addSeparator();
149 }
150
151 void updateToolbar() {
152 previousAction.setEnabled(!urlStack.isEmpty());
153 }
154 }
155
156 /***
157 * @return tooolbar
158 */
159 public JToolBar getToolbar() {
160 return urlToolbar;
161 }
162 }