View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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  		{
83  			@Override
84  			public void finished()
85  			{
86  				super.finished();
87  				ProgressDialog.this.worker = null;
88  			}
89  		};
90  		
91  		swingWorker.start();
92  		setVisible( true );
93  	}
94  
95     /* (non-Javadoc)
96  	 * @see com.eviware.soapui.support.components.XProgressMonitor#setProgress(int, java.lang.String)
97  	 */
98     public void setProgress( int value, String string )
99     {
100       progressBar.setValue( value );
101       progressBar.setString( string );
102       
103       pack();
104    }
105 
106    /* (non-Javadoc)
107 	 * @see com.eviware.soapui.support.components.XProgressMonitor#setVisible(boolean)
108 	 */
109    public void setVisible( boolean visible )
110    {
111       if( visible == true )
112       {
113          UISupport.centerDialog( this );
114       }
115       
116       super.setVisible( visible );
117    }
118    
119    private class CancelAction extends AbstractAction
120    {
121    	public CancelAction()
122    	{
123    		super( "Cancel" );
124    	}
125    	
126 		public void actionPerformed(ActionEvent e)
127 		{
128 			worker.onCancel();
129 		}
130    }
131 
132 	public void setCancelLabel(String label)
133 	{
134 		if( cancelButton != null )
135 			cancelButton.setText( label );
136 	}
137 }