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 javax.swing.*;
23 import javax.swing.event.HyperlinkEvent;
24 import java.awt.*;
25 import java.awt.event.ActionEvent;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.util.Stack;
29
30
31
32
33
34
35
36
37
38 public class WelcomePanel extends JPanel {
39 private Stack<URL> urlStack = new Stack<>();
40 private final JEditorPane textInfo = new JEditorPane();
41 private final URLToolbar urlToolbar = new URLToolbar();
42
43 public WelcomePanel() {
44 super(new BorderLayout());
45 setBackground(Color.white);
46 add(urlToolbar, BorderLayout.NORTH);
47
48 URL helpURL = ChainsawConstants.WELCOME_URL;
49
50 if (helpURL != null) {
51 textInfo.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
52
53 JScrollPane pane = new JScrollPane(textInfo);
54 pane.setBorder(null);
55 add(pane, BorderLayout.CENTER);
56
57 try {
58 textInfo.setEditable(false);
59 textInfo.setPreferredSize(new Dimension(320, 240));
60 textInfo.setPage(helpURL);
61 JTextComponentFormatter.applySystemFontAndSize(textInfo);
62 textInfo.addHyperlinkListener(
63 e -> {
64 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
65 urlStack.add(textInfo.getPage());
66
67 try {
68 textInfo.setPage(e.getURL());
69 urlToolbar.updateToolbar();
70 } catch (IOException e1) {
71 e1.printStackTrace();
72 }
73 }
74 });
75 } catch (Exception e) {
76 e.printStackTrace();
77 }
78 }
79 }
80
81 void setURL(final URL url) {
82 SwingUtilities.invokeLater(
83 () -> {
84 try {
85 urlStack.push(textInfo.getPage());
86 textInfo.setPage(url);
87
88 JTextComponentFormatter.applySystemFontAndSize(textInfo);
89 urlToolbar.updateToolbar();
90 } catch (IOException e) {
91 e.printStackTrace();
92 }
93 });
94 }
95
96 private class URLToolbar extends JToolBar {
97 private final Action previousAction =
98 new AbstractAction(null, new ImageIcon(ChainsawIcons.ICON_BACK)) {
99 public void actionPerformed(ActionEvent e) {
100 if (urlStack.isEmpty()) {
101 return;
102 }
103
104 setURL(urlStack.pop());
105 }
106 };
107
108 private final Action homeAction =
109 new AbstractAction(null, new ImageIcon(ChainsawIcons.ICON_HOME)) {
110 public void actionPerformed(ActionEvent e) {
111 setURL(ChainsawConstants.WELCOME_URL);
112 urlStack.clear();
113 }
114 };
115
116 private URLToolbar() {
117 setFloatable(false);
118 updateToolbar();
119 previousAction.putValue(Action.SHORT_DESCRIPTION, "Back");
120 homeAction.putValue(Action.SHORT_DESCRIPTION, "Home");
121
122 JButton home = new SmallButton(homeAction);
123 add(home);
124
125 addSeparator();
126
127 JButton previous = new SmallButton(previousAction);
128 previous.setEnabled(false);
129 add(previous);
130
131 addSeparator();
132 }
133
134 void updateToolbar() {
135 previousAction.setEnabled(!urlStack.isEmpty());
136 }
137 }
138
139
140
141
142 public JToolBar getToolbar() {
143 return urlToolbar;
144 }
145 }