001    /**
002     * RestaurantsConnector.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 23/06/2007
008     */
009    package jcolibri.test.test13.gui;
010    
011    import java.awt.Dimension;
012    import java.awt.Rectangle;
013    import java.awt.Toolkit;
014    
015    import javax.swing.JFrame;
016    import javax.swing.JLabel;
017    import javax.swing.JPanel;
018    import javax.swing.JProgressBar;
019    import javax.swing.SwingConstants;
020    
021    /**
022     * Swing progress barthat shows methods progress.
023     * <br>
024     * This class implements the ProgressListener interface and is 
025     * registered in the ProgressController class.
026     * 
027     * @author Juan Antonio Recio García
028     * @version 1.0
029     * @see jcolibri.util.ProgressListener
030     * @see jcolibri.util.ProgressController
031     */
032    public class SwingProgressBar extends JFrame implements jcolibri.util.ProgressListener {
033            private static final long serialVersionUID = 1L;
034    
035            JPanel jPanel1 = new JPanel();
036    
037            JProgressBar jProgressBar1 = new JProgressBar();
038    
039            JLabel jLabel1 = new JLabel();
040    
041            double stepPercentage;
042    
043            double progress;
044    
045            public SwingProgressBar() {
046                    try {
047                            init();
048                    } catch (Exception e) {
049                            e.printStackTrace();
050                    }
051            }
052    
053            private void init() throws Exception {
054                    jPanel1.setLayout(null);
055                    this.getContentPane().setLayout(null);
056                    jPanel1.setBounds(new Rectangle(5, 5, 333, 80));
057                    this.setResizable(false);
058                    this.setTitle("Method Progress");
059                    jProgressBar1.setStringPainted(true);
060                    jProgressBar1.setBounds(new Rectangle(30, 40, 274, 16));
061                    jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
062                    jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
063                    jLabel1.setText("");
064                    jLabel1.setBounds(new Rectangle(30, 9, 275, 23));
065                    this.getContentPane().add(jPanel1, null);
066                    jPanel1.add(jLabel1, null);
067                    jPanel1.add(jProgressBar1, null);
068                    int w = 350;
069                    int h = 100;
070                    this.setSize(w, h);
071                    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
072                    int x = (dim.width - w) / 2;
073                    int y = (dim.height - h) / 2;
074                    this.setLocation(x, y);
075            }
076    
077            /**
078             * Sets the label text.
079             * 
080             * @param text
081             *            label text.
082             */
083            void setText(String text) {
084                    jLabel1.setText(text);
085            }
086    
087            /**
088             * Sets the limit of the progress bar count.
089             * 
090             * @param limit
091             *            limit of the progress bar count.
092             */
093            void setLimit(int limit) {
094                    this.stepPercentage = 100.0 / (double) limit;
095            }
096    
097            /**
098             * Increase the progress bar count in 1 unit.
099             */
100            public void step() {
101            if(!jProgressBar1.isIndeterminate())
102            {
103                    progress += stepPercentage;
104                    jProgressBar1.setValue((int) progress);
105            }
106                    /*
107            Rectangle progressRect = getBounds();
108                    progressRect.x = 0;
109                    progressRect.y = 0;
110                    this.paintComponents(this.getGraphics());
111                    */
112            }
113    
114            /**
115             * Initializes the progress bar frame.
116             * 
117             * @param text
118             *            text of the label.
119             * @param limit
120             *            limit of the progress bar count.
121             */
122            public void init(String text, int limit) {
123                    if(limit>-1)
124                    {
125                    jProgressBar1.setIndeterminate(false);
126                    jProgressBar1.setStringPainted(true);
127                    this.setVisible(true);
128                            jProgressBar1.setValue(0);
129                            setText(text);
130                            setLimit(limit);
131                            progress = 0;
132                    }
133                    else
134                            init(text);
135            }
136        
137        /**
138         * Initializes the progress bar frame for an indeterminated length.
139         * 
140         * @param text
141         *            text of the label.
142         */
143        private void init(String text)
144        {
145            jProgressBar1.setIndeterminate(true);
146            jProgressBar1.setStringPainted(false);
147            this.setVisible(true);
148            setText(text);
149        }
150    
151            public void finish() {
152                    this.setVisible(false);
153                    
154            }
155    
156            
157    }