View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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 )
48  			throws HeadlessException
49  	{
50  		super( UISupport.getMainFrame(), title, true );
51  
52  		setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE );
53  
54  		progressBar = new JProgressBar( 0, length );
55  		JPanel panel = UISupport.createProgressBarPanel( progressBar, 10, true );
56  		progressBar.setString( initialValue );
57  
58  		getContentPane().setLayout( new BorderLayout() );
59  		progressLabel = new JLabel( label );
60  		progressLabel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 0, 10 ) );
61  
62  		getContentPane().add( progressLabel, BorderLayout.NORTH );
63  		getContentPane().add( panel, BorderLayout.CENTER );
64  
65  		if( allowCancel )
66  		{
67  			ButtonBarBuilder builder = ButtonBarBuilder.createLeftToRightBuilder();
68  			builder.addGlue();
69  			cancelButton = new JButton( new CancelAction() );
70  			builder.addFixed( cancelButton );
71  			builder.addGlue();
72  			builder.setBorder( BorderFactory.createEmptyBorder( 0, 10, 10, 10 ) );
73  			getContentPane().add( builder.getPanel(), BorderLayout.SOUTH );
74  		}
75  
76  		pack();
77  	}
78  
79  	public void run( Worker worker )
80  	{
81  		this.worker = worker;
82  		SwingWorkerDelegator swingWorker = new SwingWorkerDelegator( this, this, worker )
83  		{
84  			@Override
85  			public void finished()
86  			{
87  				super.finished();
88  				ProgressDialog.this.worker = null;
89  			}
90  		};
91  
92  		swingWorker.start();
93  		setVisible( true );
94  	}
95  
96  	/*
97  	 * (non-Javadoc)
98  	 * 
99  	 * @see
100 	 * com.eviware.soapui.support.components.XProgressMonitor#setProgress(int,
101 	 * java.lang.String)
102 	 */
103 	public void setProgress( int value, String string )
104 	{
105 		progressBar.setValue( value );
106 		progressBar.setString( string );
107 
108 		pack();
109 	}
110 
111 	/*
112 	 * (non-Javadoc)
113 	 * 
114 	 * @see
115 	 * com.eviware.soapui.support.components.XProgressMonitor#setVisible(boolean)
116 	 */
117 	public void setVisible( boolean visible )
118 	{
119 		if( visible == true )
120 		{
121 			UISupport.centerDialog( this );
122 		}
123 
124 		super.setVisible( visible );
125 	}
126 
127 	private class CancelAction extends AbstractAction
128 	{
129 		public CancelAction()
130 		{
131 			super( "Cancel" );
132 		}
133 
134 		public void actionPerformed( ActionEvent e )
135 		{
136 			worker.onCancel();
137 		}
138 	}
139 
140 	public void setCancelLabel( String label )
141 	{
142 		if( cancelButton != null )
143 			cancelButton.setText( label );
144 	}
145 }