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      private static final Logger LOGGER = LogManager.getRootLogger();
31  
32      /**
33       * True if icon should be displayed, false otherwise.
34       */
35      private boolean isIconDisplayed = false;
36  
37      @Override
38      public void paint(Graphics graphics) {
39          // Fix #42285: InternalError on paint()
40          try {
41              super.paint(graphics);
42          } catch (InternalError e) {
43              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
44          }
45  
46          if (this.isIconDisplayed) {
47              try {
48                  BufferedImage bufferedImage = ImageIO.read(
49                      Objects.requireNonNull(ProgressBarPausable.class.getClassLoader().getResource(UiUtil.PATH_PAUSE))
50                  );
51                  graphics.drawImage(
52                      bufferedImage,
53                      (this.getWidth() - bufferedImage.getWidth()) / 2,
54                      (this.getHeight() - bufferedImage.getHeight()) / 2,
55                      null
56                  );
57              } catch (IOException e) {
58                  LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
59              }
60          }
61      }
62  
63      /**
64       * Activate pause state, hence display pause icon.
65       */
66      public void pause() {
67          this.isIconDisplayed = true;
68      }
69  }