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