001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.selfextract;
017:
018: import java.awt.BorderLayout;
019: import java.awt.Cursor;
020: import java.awt.Dimension;
021: import java.awt.GraphicsConfiguration;
022: import java.awt.GridBagConstraints;
023: import java.awt.GridBagLayout;
024: import java.awt.Insets;
025: import java.io.ByteArrayOutputStream;
026: import java.io.InputStream;
027:
028: import javax.swing.BorderFactory;
029: import javax.swing.ImageIcon;
030: import javax.swing.JFrame;
031: import javax.swing.JLabel;
032: import javax.swing.JPanel;
033: import javax.swing.JProgressBar;
034: import javax.swing.border.BevelBorder;
035: import javax.swing.border.Border;
036:
037: import org.tp23.antinstaller.renderer.AIResourceBundle;
038:
039: /**
040: *
041: * <p>Frame to indicate progress of the extraction of a SelfExctracting archive </p>
042: * <p> </p>
043: * <p>Copyright: Copyright (c) 2004</p>
044: * <p>Company: tp23</p>
045: * @author Paul Hinds
046: * @version $Id: ProgressIndicator.java,v 1.3 2007/01/28 08:44:40 teknopaul Exp $
047: */
048: public class ProgressIndicator extends JFrame {
049:
050: public static final String IMAGE_RESOURCE = "/resources/extract-image.png";
051: private static final AIResourceBundle res = new AIResourceBundle();
052:
053: private JPanel jPanel1 = new JPanel();
054: private JProgressBar jProgressBar1 = new JProgressBar();
055: private JLabel textLabel = new JLabel();
056: private Border border1;
057: private int max = 0;
058: private static int PAGE_WIDTH = 160;
059: private static int PAGE_HEIGHT = 110; // 35 is text + bar
060: private String title = res.getString("extracting");
061: private JLabel imagePanel = new JLabel();
062: GridBagLayout gridBagLayout1 = new GridBagLayout();
063: private boolean useIcon = true;
064:
065: public ProgressIndicator(int max) {
066: this .max = max;
067: jbInit();
068: }
069:
070: public ProgressIndicator(int max, String title) {
071: this .max = max;
072: this .title = title;
073: jbInit();
074: }
075:
076: private void setLocation() {
077: GraphicsConfiguration config = getGraphicsConfiguration();
078: int x = (int) config.getBounds().getCenterX()
079: - (PAGE_WIDTH / 2);
080: int y = (int) config.getBounds().getCenterY()
081: - (PAGE_HEIGHT / 2);
082: setLocation(x, y);
083: }
084:
085: private void jbInit() {
086: border1 = BorderFactory.createCompoundBorder(BorderFactory
087: .createBevelBorder(BevelBorder.RAISED), BorderFactory
088: .createEmptyBorder(4, 4, 4, 4));
089: jPanel1.setLayout(gridBagLayout1);
090: int row = 0;
091: this .getContentPane().add(jPanel1, BorderLayout.CENTER);
092: if (useIcon) {
093: PAGE_HEIGHT = 110;
094: setImage();
095: jPanel1.add(imagePanel, new GridBagConstraints(0, row++, 1,
096: 1, 0.1, 0.9, GridBagConstraints.CENTER,
097: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0,
098: 0), 0, 0));
099: this .setSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
100: } else {
101: PAGE_HEIGHT = 40;
102: this .setSize(new Dimension(PAGE_WIDTH, 35));
103: }
104: jPanel1.setBorder(border1);
105: jPanel1.setMaximumSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
106: jPanel1.setMinimumSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
107: jPanel1
108: .setPreferredSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
109: textLabel.setText(title);
110: this .setTitle(title);
111: jPanel1.add(textLabel, new GridBagConstraints(0, row++, 1, 1,
112: 0.1, 0.1, GridBagConstraints.WEST,
113: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
114: 0, 0));
115: jPanel1.add(jProgressBar1, new GridBagConstraints(0, row++, 1,
116: 1, 0.1, 0.1, GridBagConstraints.SOUTH,
117: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
118: 0, 0));
119: jProgressBar1.setMinimum(0);
120: jProgressBar1.setMaximum(max);
121: this .setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
122: this .setSize(new Dimension(PAGE_WIDTH, PAGE_HEIGHT));
123: this .setResizable(false);
124: this .setUndecorated(true);
125: setLocation();
126: }
127:
128: public void tick() {
129: jProgressBar1.setValue(jProgressBar1.getValue() + 1);
130: }
131:
132: private void setImage() {
133: try {
134: ByteArrayOutputStream baos = new ByteArrayOutputStream();
135: InputStream in = this .getClass().getResourceAsStream(
136: IMAGE_RESOURCE);
137: byte[] buffer = new byte[2048];
138: int read = -1;
139: while ((read = in.read(buffer)) != -1) {
140: baos.write(buffer, 0, read);
141: }
142: ImageIcon icon = new ImageIcon(baos.toByteArray());
143: imagePanel.setHorizontalAlignment(JLabel.CENTER);
144: imagePanel.setIcon(icon);
145: } catch (Exception ex) {
146: }
147: }
148:
149: }
|