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  /*
19   * @author Paul Smith <psmith@apache.org>
20   *
21   */
22  package org.apache.log4j.chainsaw;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Color;
26  import java.awt.Point;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  
30  import javax.swing.JButton;
31  import javax.swing.JDialog;
32  import javax.swing.JEditorPane;
33  import javax.swing.JFrame;
34  import javax.swing.JScrollPane;
35  import javax.swing.ScrollPaneConstants;
36  import javax.swing.SwingUtilities;
37  import javax.swing.event.HyperlinkEvent;
38  import javax.swing.event.HyperlinkListener;
39  
40  import org.apache.log4j.Logger;
41  import org.apache.log4j.chainsaw.help.HelpManager;
42  
43  /***
44   * A simple About box telling people stuff about this project
45   * 
46   * @author Paul Smith <psmith@apache.org>
47   * 
48   */
49  class ChainsawAbout extends JDialog {
50      private static final Logger LOG = Logger.getLogger(ChainsawAbout.class);
51  
52      private final JEditorPane editPane = new JEditorPane("text/html", "");
53  
54      private final JScrollPane scrollPane = new JScrollPane(editPane,
55              ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
56              ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
57  
58      private final String url = ChainsawAbout.class.getName().replace('.', '/')
59              + ".html";
60  
61      private boolean sleep = false;
62  
63      private final Object guard = new Object();
64  
65      ChainsawAbout(JFrame parent) {
66          super(parent, "About Chainsaw v2", true);
67          // setResizable(false);
68          setBackground(Color.white);
69          getContentPane().setLayout(new BorderLayout());
70  
71          JButton closeButton = new JButton("Close");
72          closeButton.addActionListener(new ActionListener() {
73              public void actionPerformed(ActionEvent e) {
74                  setVisible(false);
75              }
76          });
77          closeButton.setDefaultCapable(true);
78  
79          try {
80              editPane.setPage(this.getClass().getClassLoader().getResource(url));
81          } catch (Exception e) {
82              throw new RuntimeException("Failed to find the About panel HTML", e);
83          }
84          getContentPane().add(scrollPane, BorderLayout.CENTER);
85          getContentPane().add(closeButton, BorderLayout.SOUTH);
86  
87          editPane.setEditable(false);
88          editPane.addHyperlinkListener(
89                  new HyperlinkListener() {
90                      public void hyperlinkUpdate(HyperlinkEvent e) {
91                        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
92                            HelpManager.getInstance().setHelpURL(e.getURL());
93                        }
94                      }
95                    });
96          
97          setSize(320, 240);
98          new Thread(new Scroller()).start();
99          scrollPane.getViewport().setViewPosition(new Point(0, 0));
100 
101         setLocationRelativeTo(parent);
102     }
103 
104     private class Scroller implements Runnable {
105 
106         public void run() {
107             while (true) {
108                 try {
109                     if (sleep) {
110                         synchronized (guard) {
111                             guard.wait();
112                         }
113                             SwingUtilities.invokeLater(new Runnable() {
114                                 public void run() {
115                                     scrollPane.getViewport().setViewPosition(
116                                             new Point(0, 0));
117                                 }
118                             });
119                         continue;
120                     }
121                     SwingUtilities.invokeLater(new Runnable() {
122                         public void run() {
123                             scrollPane.getViewport().setViewPosition(
124                                     new Point(0, scrollPane.getViewport()
125                                             .getViewPosition().y + 1));
126                         }
127                     });
128                     Thread.sleep(100);
129                 } catch (Exception e) {
130                     LOG.error("Error during scrolling", e);
131                 }
132 
133             }
134         }
135     }
136 
137     public void setVisible(boolean visible) {
138         super.setVisible(visible);
139         sleep = !visible;
140         synchronized (guard) {
141             guard.notifyAll();
142         }
143     }
144 }