View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * An initial Welcome Panel that is used when Chainsaw starts up, can displays
33   * a HTML pages based on URLs.
34   *
35   * @author Paul Smith
36   * @author Scott Deboy <sdeboy@apache.org>
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                      //not all pages displayed in the Welcome Panel are html-based (example receiver config is an xml file)..
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      * @return tooolbar
141      */
142     public JToolBar getToolbar() {
143         return urlToolbar;
144     }
145 }