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