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