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.impl.wsdl.actions.iface.tools.support;
    14
    15  import java.awt.BorderLayout;
    16  import java.awt.Color;
    17  import java.awt.Component;
    18  import java.awt.Dimension;
    19  import java.awt.HeadlessException;
    20  import java.awt.event.ActionEvent;
    21  import java.awt.event.WindowAdapter;
    22  import java.awt.event.WindowEvent;
    23
    24  import javax.swing.AbstractAction;
    25  import javax.swing.BorderFactory;
    26  import javax.swing.JButton;
    27  import javax.swing.JDialog;
    28  import javax.swing.JLabel;
    29  import javax.swing.JPanel;
    30  import javax.swing.JProgressBar;
    31  import javax.swing.JScrollPane;
    32  import javax.swing.JTextArea;
    33  import javax.swing.text.BadLocationException;
    34
    35  import org.apache.log4j.Logger;
    36
    37  import com.eviware.soapui.SoapUI;
    38  import com.eviware.soapui.support.UISupport;
    39  import com.jgoodies.forms.builder.ButtonBarBuilder;
    40
    41  /***
    42   * Dialog for creating progress-dialogs
    43   *
    44   * @author Ole.Matzura
    45   */
    46
    47  public class ProcessDialog extends JDialog implements RunnerContext
    48  {
    49     private JProgressBar progressBar;
    50     private JLabel progressLabel;
    51  	private JButton cancelButton;
    52  	private JTextArea logArea;
    53  	private JButton closeButton;
    54  	private ToolRunner runner;
    55  	private RunnerStatus status;
    56  	private final static Logger log = Logger.getLogger("toolLogger");
    57
    58     public ProcessDialog( String title, String description, boolean showLog, boolean allowCancel ) throws HeadlessException
    59     {
    60     	super( UISupport.getMainFrame() );
    61     	setTitle( title );
    62     	setModal( true );
    63
    64        setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    65        addWindowListener( new WindowAdapter() {
    66
    67  			public void windowClosing(WindowEvent e)
    68  			{
    69  				if( runner != null && !runner.isRunning() )
    70  					dispose();
    71  				else
    72  					UISupport.showErrorMessage( "Cannot close while task is running.." );
    73  			}} );
    74
    75        progressBar = new JProgressBar( 0, 1 );
    76        progressBar.setValue( 0 );
    77        progressBar.setIndeterminate(false);
    78
    79        getContentPane().setLayout(new BorderLayout());
    80
    81        if( description != null )
    82        {
    83        	progressBar.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
    84
    85  	      JPanel p = new JPanel( new BorderLayout() );
    86  	      p.add( new JLabel( description ), BorderLayout.NORTH );
    87  	      p.add( progressBar, BorderLayout.CENTER );
    88  	      p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    89
    90  	      getContentPane().add( p, BorderLayout.NORTH);
    91        }
    92        else
    93        {
    94        	progressBar.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    95
    96        	getContentPane().add(progressBar , BorderLayout.NORTH);
    97        }
    98
    99        if( showLog )
    100       	getContentPane().add( buildLog(), BorderLayout.CENTER );
    101
    102       if( allowCancel )
    103       {
    104 	      ButtonBarBuilder builder = ButtonBarBuilder.createLeftToRightBuilder();
    105 	      builder.addGlue();
    106 	      cancelButton = new JButton( new CancelAction());
    107 	      builder.addFixed( cancelButton );
    108 	      builder.addUnrelatedGap();
    109
    110 	      if( showLog )
    111 	      {
    112 		      closeButton = new JButton( new CloseAction() );
    113 				builder.addFixed( closeButton);
    114 	      }
    115
    116 	      builder.addGlue();
    117
    118 	      builder.setBorder( BorderFactory.createEmptyBorder(0, 10, 10, 10) );
    119 	      getContentPane().add( builder.getPanel(), BorderLayout.SOUTH );
    120       }
    121       else if( showLog )
    122       {
    123       	ButtonBarBuilder builder = ButtonBarBuilder.createLeftToRightBuilder();
    124 	      builder.addGlue();
    125
    126 		   closeButton = new JButton( new CloseAction() );
    127 			builder.addFixed( closeButton);
    128 	      builder.addGlue();
    129
    130 	      builder.setBorder( BorderFactory.createEmptyBorder(0, 10, 10, 10) );
    131 	      getContentPane().add( builder.getPanel(), BorderLayout.SOUTH );
    132       }
    133
    134       pack();
    135    }
    136
    137    private Component buildLog()
    138 	{
    139 		logArea = new JTextArea();
    140 		logArea.setEditable( false );
    141 		logArea.setBackground( Color.WHITE );
    142 		JScrollPane scrollPane = new JScrollPane( logArea );
    143 		scrollPane.setPreferredSize( new Dimension (500, 300 ));
    144
    145 		return UISupport.wrapInEmptyPanel( scrollPane, BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
    146 	}
    147
    148 	public void setProgress( String string )
    149    {
    150       progressBar.setString( string );
    151    }
    152
    153 	public void run( ToolRunner toolRunner )
    154 	{
    155 		if( !SoapUI.getLogMonitor().hasLogArea( "toolLogger"))
    156 			SoapUI.getLogMonitor().addLogArea( "tools", "toolLogger", false );
    157
    158 		this.runner = toolRunner;
    159 		runner.setContext( this );
    160 		Thread thread = new Thread( runner, toolRunner.getName() );
    161 		thread.start();
    162
    163 		UISupport.centerDialog( this );
    164 		setVisible( true );
    165 	}
    166
    167    private class CancelAction extends AbstractAction
    168    {
    169    	public CancelAction()
    170    	{
    171    		super( "Cancel" );
    172    	}
    173
    174 		public void actionPerformed(ActionEvent e)
    175 		{
    176 			if( runner.isRunning() )
    177 				runner.cancel();
    178 		}
    179    }
    180
    181    private final class CloseAction extends AbstractAction
    182    {
    183    	public CloseAction()
    184    	{
    185    		super( "Close" );
    186    		setEnabled( false );
    187    	}
    188
    189 		public void actionPerformed(ActionEvent e)
    190 		{
    191 			setVisible( false );
    192 		}
    193    }
    194
    195 	public void setCancelLabel(String label)
    196 	{
    197 		if( cancelButton != null )
    198 			cancelButton.setText( label );
    199 	}
    200
    201 	public void setStatus(RunnerStatus status)
    202 	{
    203 		this.status = status;
    204
    205 		if( status == RunnerStatus.RUNNING )
    206 		{
    207 			progressBar.setIndeterminate( true );
    208 			if( cancelButton != null )
    209 				cancelButton.setEnabled( true );
    210
    211 			if( closeButton != null )
    212 				closeButton.setEnabled( false );
    213 		}
    214 		else if( status == RunnerStatus.ERROR )
    215 		{
    216 			if( logArea == null )
    217 			{
    218 				setVisible( false );
    219 				return;
    220 			}
    221
    222 			progressBar.setIndeterminate( false );
    223 			progressBar.setValue( 0 );
    224 			if( cancelButton != null )
    225 				cancelButton.setEnabled( false );
    226
    227 			if( closeButton != null )
    228 				closeButton.setEnabled( true );
    229 		}
    230 		else if( status == RunnerStatus.FINISHED )
    231 		{
    232 			if( logArea == null )
    233 			{
    234 				setVisible( false );
    235 				return;
    236 			}
    237
    238 			progressBar.setIndeterminate( false );
    239 			progressBar.setValue( 1 );
    240 			if( cancelButton != null )
    241 				cancelButton.setEnabled( false );
    242
    243 			if( closeButton != null )
    244 				closeButton.setEnabled( true );
    245 		}
    246 	}
    247
    248    public void disposeContext()
    249    {
    250    }
    251
    252 	public void log(String msg )
    253 	{
    254 		if( logArea == null )
    255 			return;
    256
    257 		logArea.insert( msg, logArea.getText().length() );
    258 		log.info( msg );
    259 		try
    260 		{
    261 			logArea.setCaretPosition( logArea.getLineStartOffset( logArea.getLineCount()-1 ));
    262 		}
    263 		catch (BadLocationException e)
    264 		{
    265 			SoapUI.logError( e );
    266 			log.error( e.toString() );
    267 		}
    268 	}
    269
    270    public void logError( String msg )
    271    {
    272       log(msg);
    273    }
    274
    275 	public RunnerStatus getStatus()
    276 	{
    277 		return status;
    278 	}
    279 }