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;
27
28import javax.swing.BorderFactory;
29import javax.swing.ImageIcon;
30import javax.swing.JFrame;
31import javax.swing.JLabel;
32import javax.swing.JPanel;
33import javax.swing.JProgressBar;
34import javax.swing.border.BevelBorder;
35import javax.swing.border.Border;
36
37import org.tp23.antinstaller.renderer.AIResourceBundle;
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 AIResourceBundle res = new AIResourceBundle();
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    private void jbInit() {
89        border1 = BorderFactory.createCompoundBorder(
90            BorderFactory.createBevelBorder(BevelBorder.RAISED),
91            BorderFactory.createEmptyBorder(4, 4, 4, 4));
92        jPanel1.setLayout(gridBagLayout1);
93        int row = 0;
94        this.getContentPane().add(jPanel1, BorderLayout.CENTER);
95        if (useIcon) {
96            PAGE_HEIGHT = 110;
97            setImage();
98            jPanel1.add(imagePanel, new GridBagConstraints(0, row++, 1, 1, 0.1, 0.9
99                , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
00            this.setSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
01        }
02        else {
03            PAGE_HEIGHT = 40;
04            this.setSize(new Dimension(PAGE_WIDTH, 35));
05        }
06        jPanel1.setBorder(border1);
07        jPanel1.setMaximumSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
08        jPanel1.setMinimumSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
09        jPanel1.setPreferredSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
10        textLabel.setText(title);
11        this.setTitle(title);
12        jPanel1.add(textLabel, new GridBagConstraints(0, row++, 1, 1, 0.1, 0.1
13            , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
14        jPanel1.add(jProgressBar1, new GridBagConstraints(0, row++, 1, 1, 0.1, 0.1
15            , GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
16        jProgressBar1.setMinimum(0);
17        jProgressBar1.setMaximum(max);
18        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
19        this.setSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
20        this.setResizable(false);
21        this.setUndecorated(true);
22        setLocation();
23    }
24
25    public void tick() {
26        jProgressBar1.setValue(jProgressBar1.getValue() + 1);
27    }
28
29    private void setImage() {
30        try {
31            ByteArrayOutputStream baos = new ByteArrayOutputStream();
32            InputStream in = this.getClass().getResourceAsStream(IMAGE_RESOURCE);
33            byte[] buffer = new byte[2048];
34            int read = -1;
35            while ( (read = in.read(buffer)) != -1) {
36                baos.write(buffer, 0, read);
37            }
38            ImageIcon icon = new ImageIcon(baos.toByteArray());
39            imagePanel.setHorizontalAlignment(JLabel.CENTER);
40            imagePanel.setIcon(icon);
41        }
42        catch (Exception ex) {
43        }
44    }
45
46}
47