View Javadoc

1   /*
2    *  UnBBayes
3    *  Copyright (C) 2002, 2009 Universidade de Brasilia - http://www.unb.br
4    *
5    *  This file is part of UnBBayes.
6    *
7    *  UnBBayes is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  UnBBayes is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with UnBBayes.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  package unbbayes.gui;
22  
23  import java.awt.BorderLayout;
24  import java.awt.Container;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  
28  import javax.swing.BorderFactory;
29  import javax.swing.JButton;
30  import javax.swing.JFrame;
31  import javax.swing.JProgressBar;
32  import javax.swing.border.Border;
33  
34  import unbbayes.util.longtask.ILongTaskProgressObserver;
35  import unbbayes.util.longtask.LongTaskProgressChangedEvent;
36  
37  public class LongTaskProgressBar implements ILongTaskProgressObserver {
38  
39  	private JButton cancelButton;
40  	
41  	public JButton getCancelButton() {
42  		return cancelButton;
43  	}
44  
45  	private JProgressBar progressBar;
46  	
47  	public JProgressBar getProgressBar() {
48  		return progressBar;
49  	}
50  
51  	private JFrame frm;
52  
53  	private Container content;
54  
55  	private Thread t;
56  
57  	public LongTaskProgressBar(boolean showProgressBar) {
58  		this("Reading...", showProgressBar);
59  	}
60  	
61  	public LongTaskProgressBar(String title, boolean showProgressBar) {
62  		frm = new JFrame();
63  		frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
64  		cancelButton = new JButton("Cancel");
65  		content = frm.getContentPane();
66  		content.add(cancelButton);
67  		cancelButton.setBounds(100, 35, 100, 25);
68  		cancelButton.addActionListener(new CancelActionListener()); // Add the button's action
69  		progressBar = new JProgressBar(0, 10000);
70  		progressBar.setValue(0);
71  		progressBar.setStringPainted(true);
72  		Border border = BorderFactory.createTitledBorder(title);
73  		progressBar.setBorder(border);
74  		content.add(progressBar, BorderLayout.NORTH);
75  		frm.setSize(300, 100);
76  		frm.setVisible(showProgressBar);
77  	}
78  
79  	public void setThread(Thread t) {
80  		this.t = t;
81  	}
82  
83  	public void setProgressBar(int n) {
84  		progressBar.setValue(n);
85  	}
86  
87  	public void hideProgressBar() {
88  		frm.setVisible(false);
89  	}
90  	
91  	public void showProgressBar() {
92  		frm.setVisible(true);
93  	}
94  	
95  	public void update(){
96  		progressBar.paintImmediately(0, 0, progressBar.getWidth(), progressBar.getHeight()); 
97  	}
98  	
99  	public void update(LongTaskProgressChangedEvent status) {
100 		progressBar.setValue(status.getPercentageDone()); 
101 		update(); 
102 	}
103 
104 	// The action
105 	protected class CancelActionListener implements ActionListener {
106 
107 		@SuppressWarnings("deprecation")
108 		public void actionPerformed(ActionEvent e) {
109 			frm.setVisible(false);
110 			frm.dispose();
111 			t.stop();
112 		}
113 	}
114 
115 }