View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support.components;
14  
15  import java.awt.BorderLayout;
16  import java.awt.HeadlessException;
17  import java.awt.event.ActionEvent;
18  
19  import javax.swing.AbstractAction;
20  import javax.swing.BorderFactory;
21  import javax.swing.JButton;
22  import javax.swing.JDialog;
23  import javax.swing.JLabel;
24  import javax.swing.JPanel;
25  import javax.swing.JProgressBar;
26  
27  import com.eviware.soapui.support.UISupport;
28  import com.eviware.soapui.support.swing.SwingWorkerDelegator;
29  import com.eviware.x.dialogs.Worker;
30  import com.eviware.x.dialogs.XProgressDialog;
31  import com.eviware.x.dialogs.XProgressMonitor;
32  import com.jgoodies.forms.builder.ButtonBarBuilder;
33  
34  /***
35   * Dialog for creating progress-dialogs
36   * 
37   * @author Ole.Matzura
38   */
39  
40  public class ProgressDialog extends JDialog implements XProgressDialog, XProgressMonitor
41  {
42     private JProgressBar progressBar;
43     private JLabel progressLabel;
44  	private JButton cancelButton;
45  	private Worker worker;
46  
47     public ProgressDialog(String title, String label, int length, String initialValue, boolean allowCancel ) throws HeadlessException
48     {
49        super(UISupport.getMainFrame(), title, true);
50  
51        setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
52  
53        progressBar = new JProgressBar(0, length);
54        JPanel panel = UISupport.createProgressBarPanel( progressBar, 10, true );
55        progressBar.setString(initialValue );
56  
57        getContentPane().setLayout(new BorderLayout());
58        progressLabel = new JLabel(label);
59        progressLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
60  
61        getContentPane().add(progressLabel, BorderLayout.NORTH);
62        getContentPane().add(panel, BorderLayout.CENTER);
63        
64        if( allowCancel )
65        {
66            ButtonBarBuilder builder = ButtonBarBuilder.createLeftToRightBuilder();	
67            builder.addGlue();
68            cancelButton = new JButton( new CancelAction());
69  			 builder.addFixed( cancelButton );
70            builder.addGlue();
71            builder.setBorder( BorderFactory.createEmptyBorder(0, 10, 10, 10) );
72            getContentPane().add( builder.getPanel(), BorderLayout.SOUTH );
73        }
74        
75        pack();
76     }
77     
78  	public void run(Worker worker)
79  	{
80        this.worker = worker;
81  		SwingWorkerDelegator swingWorker = new SwingWorkerDelegator(this, this, worker);
82  		swingWorker.start();
83  		setVisible( true );
84  		worker = null;
85  	}
86  
87     /* (non-Javadoc)
88  	 * @see com.eviware.soapui.support.components.XProgressMonitor#setProgress(int, java.lang.String)
89  	 */
90     public void setProgress( int value, String string )
91     {
92        progressBar.setValue( value );
93        progressBar.setString( string );
94     }
95  
96     /* (non-Javadoc)
97  	 * @see com.eviware.soapui.support.components.XProgressMonitor#setVisible(boolean)
98  	 */
99     public void setVisible( boolean visible )
100    {
101       if( visible == true )
102       {
103          UISupport.centerDialog( this );
104       }
105       
106       super.setVisible( visible );
107    }
108    
109    private class CancelAction extends AbstractAction
110    {
111    	public CancelAction()
112    	{
113    		super( "Cancel" );
114    	}
115    	
116 		public void actionPerformed(ActionEvent e)
117 		{
118 			worker.onCancel();
119 		}
120    }
121 
122 	public void setCancelLabel(String label)
123 	{
124 		if( cancelButton != null )
125 			cancelButton.setText( label );
126 	}
127 }