1
16package org.tp23.antinstaller.selfextract;
17
18import java.awt.BorderLayout;
19import java.awt.Cursor;
20import java.awt.Dimension;
21import java.awt.GraphicsConfiguration;
22import java.awt.GridBagConstraints;
23import java.awt.GridBagLayout;
24import java.awt.Insets;
25import java.io.ByteArrayOutputStream;
26import java.io.InputStream;
27import java.util.ResourceBundle;
28
29import javax.swing.BorderFactory;
30import javax.swing.ImageIcon;
31import javax.swing.JFrame;
32import javax.swing.JLabel;
33import javax.swing.JPanel;
34import javax.swing.JProgressBar;
35import javax.swing.UIManager;
36import javax.swing.border.BevelBorder;
37import javax.swing.border.Border;
38
39
40
41
50public class ProgressIndicator
51 extends JFrame {
52
53 public static final String IMAGE_RESOURCE = "/resources/extract-image.png";
54 private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
55
56 private JPanel jPanel1 = new JPanel();
57 private JProgressBar jProgressBar1 = new JProgressBar();
58 private JLabel textLabel = new JLabel();
59 private Border border1;
60 private int max = 0;
61 private static int PAGE_WIDTH = 160;
62 private static int PAGE_HEIGHT = 110; private String title = res.getString("extracting");
64 private JLabel imagePanel = new JLabel();
65 GridBagLayout gridBagLayout1 = new GridBagLayout();
66 private boolean useIcon = true;
67
68
69
70 public ProgressIndicator(int max) {
71 this.max = max;
72 jbInit();
73 }
74
75 public ProgressIndicator(int max, String title) {
76 this.max = max;
77 this.title = title;
78 jbInit();
79 }
80
81 private void setLocation() {
82 GraphicsConfiguration config = getGraphicsConfiguration();
83 int x = (int) config.getBounds().getCenterX() - (PAGE_WIDTH / 2);
84 int y = (int) config.getBounds().getCenterY() - (PAGE_HEIGHT / 2);
85 setLocation(x, y);
86 }
87
88
89 private void jbInit() {
90 border1 = BorderFactory.createCompoundBorder(
91 BorderFactory.createBevelBorder(BevelBorder.RAISED),
92 BorderFactory.createEmptyBorder(4, 4, 4, 4));
93 jPanel1.setLayout(gridBagLayout1);
94 int row = 0;
95 this.getContentPane().add(jPanel1, BorderLayout.CENTER);
96 if (useIcon) {
97 PAGE_HEIGHT = 110;
98 setImage();
99 jPanel1.add(imagePanel, new GridBagConstraints(0, row++, 1, 1, 0.1, 0.9
00 , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
01 this.setSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
02 }
03 else {
04 PAGE_HEIGHT = 40;
05 this.setSize(new Dimension(PAGE_WIDTH, 35));
06 }
07 jPanel1.setBorder(border1);
08 jPanel1.setMaximumSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
09 jPanel1.setMinimumSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
10 jPanel1.setPreferredSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
11 textLabel.setText(title);
12 this.setTitle(title);
13 jPanel1.add(textLabel, new GridBagConstraints(0, row++, 1, 1, 0.1, 0.1
14 , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
15 jPanel1.add(jProgressBar1, new GridBagConstraints(0, row++, 1, 1, 0.1, 0.1
16 , GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
17 jProgressBar1.setMinimum(0);
18 jProgressBar1.setMaximum(max);
19 this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
20 this.setSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
21 this.setResizable(false);
22 this.setUndecorated(true);
23 setLocation();
24 }
25
26 public void tick() {
27 jProgressBar1.setValue(jProgressBar1.getValue() + 1);
28 }
29
30 private void setImage() {
31 try {
32 ByteArrayOutputStream baos = new ByteArrayOutputStream();
33 InputStream in = this.getClass().getResourceAsStream(IMAGE_RESOURCE);
34 byte[] buffer = new byte[2048];
35 int read = -1;
36 while ( (read = in.read(buffer)) != -1) {
37 baos.write(buffer, 0, read);
38 }
39 ImageIcon icon = new ImageIcon(baos.toByteArray());
40 imagePanel.setHorizontalAlignment(JLabel.CENTER);
41 imagePanel.setIcon(icon);
42 }
43 catch (Exception ex) {
44 }
45 }
46
47
51 public static void main(String[] args) {
52 try {
53 ProgressIndicator indicator = null;
54 indicator = new ProgressIndicator(200);
55 indicator.show();
56 UIManager.setLookAndFeel("org.tp23.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
57 }
58 catch (Exception ex) {
59 }
61
62 }
63}
64