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 java.awt.BorderLayout;
21  import java.awt.Color;
22  import java.awt.Dimension;
23  import java.awt.event.ActionEvent;
24  import java.io.IOException;
25  import java.net.URL;
26  import java.util.Stack;
27  
28  import javax.swing.AbstractAction;
29  import javax.swing.Action;
30  import javax.swing.BorderFactory;
31  import javax.swing.ImageIcon;
32  import javax.swing.JButton;
33  import javax.swing.JEditorPane;
34  import javax.swing.JPanel;
35  import javax.swing.JScrollPane;
36  import javax.swing.JToolBar;
37  import javax.swing.SwingUtilities;
38  import javax.swing.event.HyperlinkEvent;
39  import javax.swing.event.HyperlinkListener;
40  
41  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
42  
43  
44  /**
45   * An initial Welcome Panel that is used when Chainsaw starts up, can displays
46   * a HTML pages based on URLs.
47   *
48   * @author Paul Smith
49   * @author Scott Deboy <sdeboy@apache.org>
50   */
51  public class WelcomePanel extends JPanel {
52    private Stack urlStack = new Stack();
53    private final JEditorPane textInfo = new JEditorPane();
54    private final URLToolbar urlToolbar = new URLToolbar();
55  
56    public WelcomePanel() {
57      super(new BorderLayout());
58      setBackground(Color.white);
59      add(urlToolbar, BorderLayout.NORTH);
60  
61  	URL helpURL = ChainsawConstants.WELCOME_URL;
62  
63      if (helpURL != null) {
64        textInfo.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
65  
66        JScrollPane pane = new JScrollPane(textInfo);
67        pane.setBorder(null);
68        add(pane, BorderLayout.CENTER);
69  
70        try {
71          textInfo.setEditable(false);
72          textInfo.setPreferredSize(new Dimension(320, 240));
73          textInfo.setPage(helpURL);
74          JTextComponentFormatter.applySystemFontAndSize(textInfo);
75          textInfo.addHyperlinkListener(
76            new HyperlinkListener() {
77              public void hyperlinkUpdate(HyperlinkEvent e) {
78                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
79                  urlStack.add(textInfo.getPage());
80  
81                  try {
82                    textInfo.setPage(e.getURL());
83                    urlToolbar.updateToolbar();
84                  } catch (IOException e1) {
85                    e1.printStackTrace();
86                  }
87                }
88              }
89            });
90        } catch (Exception e) {
91          e.printStackTrace();
92        }
93      }
94    }
95  
96    void setURL(final URL url) {
97      SwingUtilities.invokeLater(
98        new Runnable() {
99          public void run() {
100           try {
101             urlStack.push(textInfo.getPage());
102             textInfo.setPage(url);
103             //not all pages displayed in the Welcome Panel are html-based (example receiver config is an xml file)..
104             JTextComponentFormatter.applySystemFontAndSize(textInfo);
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 }