View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    *
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.view.swing.tree;
12  
13  import com.jsql.util.LogLevelUtil;
14  import com.jsql.view.swing.util.UiUtil;
15  import org.apache.logging.log4j.LogManager;
16  import org.apache.logging.log4j.Logger;
17  
18  import javax.imageio.ImageIO;
19  import javax.swing.*;
20  import java.awt.*;
21  import java.awt.image.BufferedImage;
22  import java.io.IOException;
23  import java.util.Objects;
24  
25  /**
26   * A progress bar with a Pause icon over it.
27   */
28  public class ProgressBarPausable extends JProgressBar {
29      
30      /**
31       * Log4j logger sent to view.
32       */
33      private static final Logger LOGGER = LogManager.getRootLogger();
34  
35      /**
36       * True if icon should be displayed, false otherwise.
37       */
38      private boolean isIconDisplayed = false;
39  
40      @Override
41      public void paint(Graphics graphics) {
42          
43          // Fix #42285: InternalError on paint()
44          try {
45              super.paint(graphics);
46          } catch (InternalError e) {
47              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
48          }
49  
50          if (this.isIconDisplayed) {
51              try {
52                  BufferedImage bufferedImage = ImageIO.read(
53                      Objects.requireNonNull(ProgressBarPausable.class.getClassLoader().getResource(UiUtil.PATH_PAUSE))
54                  );
55                  graphics.drawImage(
56                      bufferedImage,
57                      (this.getWidth() - bufferedImage.getWidth()) / 2,
58                      (this.getHeight() - bufferedImage.getHeight()) / 2,
59                      null
60                  );
61              } catch (IOException e) {
62                  LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
63              }
64          }
65      }
66  
67      /**
68       * Activate pause state, hence display pause icon.
69       */
70      public void pause() {
71          this.isIconDisplayed = true;
72      }
73  }