1 /* 
2  * Copyright 2005 Paul Hinds
3  *
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  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 */
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/**
42 *
43 * <p>Frame to indicate progress of the extraction of a SelfExctracting archive </p>
44 * <p> </p>
45 * <p>Copyright: Copyright (c) 2004</p>
46 * <p>Company: tp23</p>
47 * @author Paul Hinds
48 * @version $Id: ProgressIndicator.java,v 1.3 2007/01/28 08:44:40 teknopaul Exp $
49 */
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; // 35 is text + bar
63    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    /**
48     * TODO move to JUnit
49     * @param args
50     */
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            // not concerned about Look and Feel
60        }
61
62    }
63}
64