View Javadoc
1   /**
2    * Copyright 2011, Aiki IT, FotoRenamer
3    * <p/>
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * <p/>
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * <p/>
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.aikiit.fotorenamer.gui;
17  
18  import de.aikiit.fotorenamer.util.ComponentGaugeUtil;
19  import org.apache.logging.log4j.LogManager;
20  import org.apache.logging.log4j.Logger;
21  
22  import javax.swing.*;
23  import java.awt.*;
24  import java.util.concurrent.TimeUnit;
25  import java.util.concurrent.atomic.AtomicInteger;
26  
27  import static de.aikiit.fotorenamer.util.LocalizationHelper.getBundleString;
28  
29  /**
30   * This class provides a progressbar with 2 lines. One is the progress bar
31   * itself, the other is a String for text messages.
32   *
33   * @author hirsch, 13.10.2003
34   * @version 2004-01-08
35   */
36  public class ProgressBar extends JFrame {
37      /**
38       * Logger for this class.
39       */
40      private static final Logger LOG = LogManager.getLogger(ProgressBar.class);
41      /**
42       * By default the UI sleeps for 200 ms to be able to read the file names
43       * during operation.
44       */
45      private static final int DEFAULT_UI_DELAY = 200;
46  
47      /**
48       * Text field to show current file or other information during run.
49       */
50      private JLabel textInfo = null;
51      /**
52       * The visual progress bar.
53       */
54      private JProgressBar progressBar = null;
55      /**
56       * Amount of seconds the UI sleeps in order to give a
57       * user a chance to read the screen,
58       * this value is calculated dynamically
59       * depending on the amount of pictures to be processed.
60       */
61      private int delayInUI = -1;
62  
63      /**
64      * Current success rate/counter.
65      */
66      private final AtomicInteger currentState;
67  
68      /**
69       * Creates a progress bar with the given amount as 100 percent.
70       *
71       * @param maxCapacity Defines the 100%-scale for this progress bar.
72       */
73      public ProgressBar(final int maxCapacity) {
74          currentState = new AtomicInteger(0);
75          init(maxCapacity);
76      }
77  
78      /**
79       * Perform internal initialization of UI components.
80       *
81       * @param maxCapacity Sets the 100% value of this component to this absolute
82       *                    value.
83       */
84      private void init(final int maxCapacity) {
85          // set window title
86          this.setTitle(
87                  getBundleString("fotorenamer.ui.progress"));
88  
89          this.setResizable(false);
90          this.progressBar = new JProgressBar(0, maxCapacity);
91  
92          JLabel info = new JLabel(
93                  getBundleString(
94                          "fotorenamer.ui.progress.title"));
95          this.textInfo = new JLabel();
96          this.progressBar.setValue(0);
97          this.progressBar.setStringPainted(true);
98          // default value: 200 ms
99          // calculate delay in UI depending on maximum capacity of the bar
100         // itself; the more files the lower the delay
101         this.delayInUI = maxCapacity < 35
102                 ? DEFAULT_UI_DELAY : DEFAULT_UI_DELAY / 3;
103 
104         this.getContentPane().setLayout(new GridLayout(3, 1));
105         this.getContentPane().add(info);
106         this.getContentPane().add(textInfo);
107         this.getContentPane().add(progressBar);
108         ComponentGaugeUtil.makeCentered(this);
109         this.pack();
110         this.setVisible(true);
111     } // end of init
112 
113     /**
114      * After any changes the UI needs to be repainted and recentered which is
115      * done by that method. This method waits 200ms in order to make the UI more
116      * human readable.
117      */
118     public final void updateUI() {
119         this.pack();
120         ComponentGaugeUtil.makeCentered(this);
121 
122         // TODO replace with a Timer - see
123         // http://download.oracle.com/javase/tutorial/uiswing/misc/timer.html
124         try {
125             TimeUnit.MILLISECONDS.sleep(this.delayInUI);
126         } catch (InterruptedException e) {
127             Thread.currentThread().interrupt();
128             LOG.error("Thread sleep error during repaint of ProgressBar, " + e.getMessage());
129         }
130     }
131 
132     /**
133      * Set progress by setting the amount of items that are processed
134      * successfully.
135      */
136     public final void setProgress() {
137         this.progressBar.setValue(currentState.getAndIncrement());
138     }
139 
140     /**
141      * Sets the part of this component that shows above the graphical bar.
142      *
143      * @param textContent Content to show under the graphical progress bar.
144      */
145     public final void setText(final String textContent) {
146         this.textInfo.setText(textContent);
147     }
148 
149     /**
150      * Getter for text value.
151      *
152      * @return Current text value.
153      */
154     public final String getText() {
155         return textInfo.getText();
156     }
157 
158     /**
159      * Getter for current step/progress.
160      *
161      * @return Current absolute progress value (step).
162      */
163     public final int getProgress() {
164         return progressBar.getValue();
165     }
166 }