001 /* $Id: SimpleProgressMonitor.java 12 2007-07-27 09:48:53Z ringler $
002 * This class is part of the de.mospace.swing library.
003 * Copyright (C) 2005-2006 Moritz Ringler
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019 package de.mospace.swing;
020
021 import java.awt.BorderLayout;
022 import java.awt.Container;
023 import java.awt.Dimension;
024 import java.awt.FlowLayout;
025 import java.awt.Frame;
026 import java.awt.event.ActionEvent;
027 import java.awt.event.ActionListener;
028 import java.awt.event.WindowEvent;
029 import javax.swing.BorderFactory;
030 import javax.swing.JButton;
031 import javax.swing.JComponent;
032 import javax.swing.JDialog;
033 import javax.swing.JLabel;
034 import javax.swing.JPanel;
035 import javax.swing.JProgressBar;
036 import javax.swing.UIManager;
037
038 /** A simplified progress monitor. It calls "close()" whenever it is
039 * terminated. By overriding the close() method you can thus handle any form of
040 * termination (e.g. for re-enabling another window).
041 * @author Moritz Ringler
042 * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
043 */
044 public class SimpleProgressMonitor{
045 private JDialog dialog;
046 private JLabel progressText;
047 private JProgressBar progressBar;
048 private boolean canceled;
049
050 /** Constructs a new ProgressMonitor dialog and makes it visible.
051 * @param f parent window
052 * @param title title of the dialog
053 * @param note initial text to display
054 * @param min integer corresponding to 0% progress
055 * @param max integer corresponding to 100% progress
056 * @deprecated use SimpleProgressMonitor(Frame f, String title, String note) instead
057 **/
058 public SimpleProgressMonitor(Frame f, String title, String note,
059 int min, int max){
060 reset(f,title,note,min,max);
061 }
062
063 public SimpleProgressMonitor(Frame f, String title, String note){
064 reset(f,title,note);
065 }
066
067 public void reset(Frame f, String title, String note){
068 reset(f,title,note,0,100);
069 }
070
071 /** Resets an existing progress monitor.
072 * @param f parent window
073 * @param title title of the dialog
074 * @param note initial text to display
075 * @param min integer corresponding to 0% progress
076 * @param max integer corresponding to 100% progress
077 **/
078 private final void reset(Frame f, String title, String note,
079 int min, int max){
080 canceled = false;
081 progressText = new JLabel((note == null)? "": note);
082 progressText.setPreferredSize(new Dimension(300,20));
083 progressBar = new JProgressBar(min, max);
084 progressBar.setPreferredSize(new Dimension(290,20));
085 JButton cancel = new JButton(UIManager.getString("OptionPane.cancelButtonText"));
086 cancel.addActionListener(new ActionListener(){
087 public void actionPerformed(ActionEvent e){
088 cancel();
089 }
090 });
091 dialog = new JDialog(f, title, false){
092 protected void processWindowEvent(WindowEvent e){
093 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
094 cancel();
095 }
096 super.processWindowEvent(e);
097 }
098 };
099 Container dcp = dialog.getContentPane();
100 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
101 dcp.setLayout(new BorderLayout());
102 ((JComponent) dcp).setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
103 if(note != null){
104 dcp.add(progressText, BorderLayout.NORTH);
105 }
106 dcp.add(progressBar, BorderLayout.CENTER);
107 JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
108 buttons.add(cancel);
109 dcp.add(buttons, BorderLayout.SOUTH);
110 dialog.pack();
111 dialog.setLocationRelativeTo(f);
112 //dialog.setVisible(true);
113 }
114
115 public void setVisible(boolean visible){
116 dialog.setVisible(visible);
117 }
118
119 public boolean isVisible(){
120 return dialog.isVisible();
121 }
122
123 /** Updates the progress bar with the indicated value.
124 * @see javax.swing.JProgressBar#setValue(int)
125 */
126 public void setProgress(int i){
127 if(i >= progressBar.getMaximum()){
128 close();
129 }
130 if(i > progressBar.getValue()){
131 progressBar.setValue(i);
132 }
133 }
134
135 /** Returns the current value of the progress bar.
136 * @see javax.swing.JProgressBar#getValue()
137 */
138 public int getProgress(){
139 return progressBar.getValue();
140 }
141
142 /** Changes the text displayed in the dialog window. **/
143 public void setNote(String s){
144 progressText.setText(s);
145 }
146
147 public void setMaximum(int max){
148 progressBar.setMaximum(max);
149 }
150
151 public void setMinimum(int min){
152 progressBar.setMinimum(min);
153 }
154
155 /** Returns whether the dialog has been cancelled.
156 * @see #cancel()
157 **/
158 public boolean isCanceled(){
159 return canceled;
160 }
161
162 /** Cancels the progress monitor dialog. */
163 public void cancel(){
164 close();
165 canceled = true;
166 }
167
168 /** Closes the progress monitor dialog. */
169 public void close(){
170 dialog.setVisible(false);
171 dialog.dispose();
172 }
173 }