1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.mindtree.techworks.insight.gui.widgets;
25
26 import java.awt.Component;
27 import java.awt.Dimension;
28 import java.awt.GridBagConstraints;
29 import java.awt.GridBagLayout;
30 import java.awt.Toolkit;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.util.ArrayList;
34
35 import javax.swing.JPanel;
36 import javax.swing.JProgressBar;
37 import javax.swing.JSeparator;
38 import javax.swing.Timer;
39
40 import com.mindtree.techworks.insight.InsightConstants;
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class StatusBar extends JPanel {
55
56
57
58
59 private static final long serialVersionUID = -6654725016958741987L;
60
61
62
63
64 private static final int WIDTH = 900;
65 private static final int HEIGHT = 20;
66 private static final int SEPARATOR_WIDTH = 2;
67
68
69
70
71 private static final int MAX_TEXT_LENGTH = 100;
72
73
74
75
76 private static StatusBar instance;
77
78
79
80
81 private ArrayList progressBarList;
82
83
84
85
86 private GridBagLayout gl;
87 private GridBagConstraints gc;
88
89
90
91
92 private int componentCount = -1;
93
94
95
96
97
98 private StatusBar(int partitionCount) {
99 gl = new GridBagLayout();
100 gc = new GridBagConstraints();
101 setLayout(gl);
102
103 progressBarList = new ArrayList(partitionCount);
104
105 for (int i = 0; i < partitionCount; i++) {
106 JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL);
107 initializeProgressBar(progressBar);
108 progressBarList.add(progressBar);
109 }
110 setPreferredSize(getIdealPreferredSize());
111 }
112
113
114
115
116
117 public synchronized static final StatusBar getInstance() {
118 if (instance == null) {
119 instance = new StatusBar(InsightConstants.SB_PARTITION_COUNT + 1);
120 }
121 return instance;
122 }
123
124
125
126
127
128
129
130
131
132 public void setDisplayText(int partitionIndex, String text, boolean animate) {
133 if (partitionIndex >= InsightConstants.SB_PARTITION_COUNT) {
134 return;
135 }
136 this.setDisplayTextPrivate(partitionIndex, text, animate, true);
137 }
138
139
140
141
142
143
144 public void setStatisticsDisplayText(String text) {
145 this.setDisplayTextPrivate(InsightConstants.SB_PARTITION_COUNT, text, false, false);
146 }
147
148
149
150
151
152 public void clearStatisticsDisplayText() {
153 this.clearDisplayPrivate(InsightConstants.SB_PARTITION_COUNT);
154 }
155
156
157
158
159
160 public void clearDisplay(int partitionIndex) {
161 if (partitionIndex >= InsightConstants.SB_PARTITION_COUNT) {
162 return;
163 }
164 this.clearDisplayPrivate(partitionIndex);
165 }
166
167
168
169
170
171
172 private Dimension getIdealPreferredSize() {
173 Dimension dimension = null;
174 Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
175 dimension = new Dimension((int)Math.min(screenDim.getWidth() - 50, WIDTH),
176 HEIGHT);
177 return dimension;
178 }
179
180
181
182
183
184 private void clearDisplayPrivate(int partitionIndex) {
185 JProgressBar progressBar = (JProgressBar)progressBarList.get(partitionIndex);
186 progressBar.setString("");
187 progressBar.setIndeterminate(false);
188 }
189
190
191
192
193
194
195
196
197
198
199 private void setDisplayTextPrivate(int partitionIndex, String text, boolean animate, boolean enableDisplayLease) {
200 JProgressBar progressBar = (JProgressBar)progressBarList.get(partitionIndex);
201 text = text.substring(0,Math.min(text.length(), MAX_TEXT_LENGTH));
202 progressBar.setString(text);
203 int stringSizeInPixels = progressBar.getFontMetrics(progressBar.getFont()).stringWidth(text);
204 if (stringSizeInPixels > progressBar.getWidth()) {
205
206
207 progressBar.invalidate();
208 this.validate();
209 }
210 progressBar.setIndeterminate(animate);
211 if (!animate && enableDisplayLease) {
212 Timer timer = new Timer(InsightConstants.DISPLAY_INTERVAL, new DisplayLease(partitionIndex));
213 timer.setRepeats(false);
214 timer.start();
215 }
216 }
217
218
219
220
221
222 private void initializeProgressBar(JProgressBar progressBar) {
223 JSeparator separator = new JSeparator(JSeparator.VERTICAL);
224 separator.setPreferredSize(new Dimension(SEPARATOR_WIDTH,HEIGHT));
225 addComponent(separator,++componentCount,0,0,0);
226 progressBar.setBorderPainted(false);
227 progressBar.setStringPainted(true);
228 progressBar.setString("");
229 addComponent(progressBar,++componentCount,0,1,0);
230 }
231
232
233
234
235 private final void addComponent(Component c, int gridx, int gridy, int weightx,
236 int weighty) {
237 gc.fill = GridBagConstraints.HORIZONTAL;
238 gc.anchor = GridBagConstraints.NORTHWEST;
239 gc.gridx = gridx;
240 gc.gridy = gridy;
241 gc.weightx = weightx;
242 gc.weighty = weighty;
243 gl.setConstraints( c, gc);
244 add(c);
245 }
246
247
248
249
250
251 private class DisplayLease implements ActionListener {
252
253
254
255 private int partitionIndex;
256
257
258
259
260
261 public DisplayLease(int partitionIndex) {
262 this.partitionIndex = partitionIndex;
263 }
264
265
266
267
268
269 public void actionPerformed(ActionEvent e) {
270 clearDisplay(this.partitionIndex);
271 }
272 }
273
274 }