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.lang.reflect.InvocationTargetException;
23  
24  import javax.swing.BorderFactory;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  import javax.swing.JProgressBar;
28  import javax.swing.SwingConstants;
29  import javax.swing.SwingUtilities;
30  
31  /***
32   * A simple ProgressPanel that can be used, a little more flexible
33   * than ProgressMonitor when you want it to be shown REGARDLESS
34   * of any timeouts etc.
35   * 
36   * @author Paul Smith <psmith@apache.org>
37   *
38   */
39  public class ProgressPanel extends JPanel {
40    private final JLabel messageLabel = new JLabel();
41    private final JProgressBar progressBar;
42  
43    ProgressPanel(int min, int max, String msg) {
44      this.progressBar = new JProgressBar(min, max);
45      setBorder(BorderFactory.createLineBorder(Color.black, 1));
46      messageLabel.setHorizontalAlignment(SwingConstants.HORIZONTAL);
47      messageLabel.setText(msg);
48      setLayout(new BorderLayout());
49  
50      add(progressBar, BorderLayout.CENTER);
51      add(messageLabel, BorderLayout.SOUTH);
52    }
53  
54    public void setMessage(final String string) {
55      SwingUtilities.invokeLater(
56        new Runnable() {
57          public void run() {
58            messageLabel.setText(string);
59          }
60        });
61    }
62  
63    public void setProgress(final int progress) {
64      try {
65        Runnable runnable = new Runnable() {
66            public void run() {
67              progressBar.setValue(progress);
68            }
69          };
70      if (!SwingUtilities.isEventDispatchThread()) {
71          SwingUtilities.invokeAndWait(runnable);
72      }else {
73          runnable.run();
74      }
75      } catch (InterruptedException e) {
76        // TODO Auto-generated catch block
77        e.printStackTrace();
78      } catch (InvocationTargetException e) {
79        // TODO Auto-generated catch block
80        e.printStackTrace();
81      }
82    }
83  }