1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35
36 public class ProgressBar extends JFrame {
37
38
39
40 private static final Logger LOG = LogManager.getLogger(ProgressBar.class);
41
42
43
44
45 private static final int DEFAULT_UI_DELAY = 200;
46
47
48
49
50 private JLabel textInfo = null;
51
52
53
54 private JProgressBar progressBar = null;
55
56
57
58
59
60
61 private int delayInUI = -1;
62
63
64
65
66 private final AtomicInteger currentState;
67
68
69
70
71
72
73 public ProgressBar(final int maxCapacity) {
74 currentState = new AtomicInteger(0);
75 init(maxCapacity);
76 }
77
78
79
80
81
82
83
84 private void init(final int maxCapacity) {
85
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
99
100
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 }
112
113
114
115
116
117
118 public final void updateUI() {
119 this.pack();
120 ComponentGaugeUtil.makeCentered(this);
121
122
123
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
134
135
136 public final void setProgress() {
137 this.progressBar.setValue(currentState.getAndIncrement());
138 }
139
140
141
142
143
144
145 public final void setText(final String textContent) {
146 this.textInfo.setText(textContent);
147 }
148
149
150
151
152
153
154 public final String getText() {
155 return textInfo.getText();
156 }
157
158
159
160
161
162
163 public final int getProgress() {
164 return progressBar.getValue();
165 }
166 }