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.impl.wsdl.support;
14  
15  import java.util.concurrent.Future;
16  
17  import javax.swing.ImageIcon;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.model.support.AbstractAnimatableModelItem;
21  import com.eviware.soapui.support.UISupport;
22  
23  /***
24   * Class to animate the icon of a ModelItem
25   * 
26   * @author ole.matzura
27   */
28  
29  public class ModelItemIconAnimator<T extends AbstractAnimatableModelItem<?>> implements Runnable
30  {
31  	private final T target;
32  	private int index = 0;
33  	private boolean stopped = true;
34  	private boolean enabled = true;
35  	private ImageIcon baseIcon;
36  	private ImageIcon[] animateIcons;
37  	private Future<?> future;
38  
39  	public ModelItemIconAnimator( T target, String baseIcon, String animationBaseIcon, int num, String type )
40  	{
41  		this.target = target;
42  		this.baseIcon = UISupport.createImageIcon( baseIcon );
43  
44  		animateIcons = new ImageIcon[num];
45  
46  		for( int c = 0; c < animateIcons.length; c++ )
47  			animateIcons[c] = UISupport.createImageIcon( animationBaseIcon + "_" + ( c + 1 ) + "." + type );
48  	}
49  
50  	public void stop()
51  	{
52  		stopped = true;
53  	}
54  
55  	public int getIndex()
56  	{
57  		return index;
58  	}
59  
60  	public boolean isStopped()
61  	{
62  		return stopped;
63  	}
64  
65  	public void start()
66  	{
67  		if( !enabled || future != null )
68  			return;
69  
70  		stopped = false;
71  		future = SoapUI.getThreadPool().submit( this );
72  	}
73  
74  	public ImageIcon getBaseIcon()
75  	{
76  		return baseIcon;
77  	}
78  
79  	public ImageIcon getIcon()
80  	{
81  		if( !isStopped() )
82  		{
83  			return animateIcons[getIndex()];
84  		}
85  
86  		return baseIcon;
87  	}
88  
89  	public void run()
90  	{
91  		if( future != null )
92  		{
93  			if( System.getProperty( "soapui.enablenamedthreads" ) != null )
94  				Thread.currentThread().setName( "ModelItemIconAnimator for " + target.getName() );
95  		}
96  
97  		while( !stopped )
98  		{
99  			try
100 			{
101 				if( stopped )
102 					break;
103 
104 				index = index >= animateIcons.length - 1 ? 0 : index + 1;
105 				target.setIcon( getIcon() );
106 				Thread.sleep( 500 );
107 			}
108 			catch( InterruptedException e )
109 			{
110 				SoapUI.logError( e );
111 			}
112 		}
113 
114 		target.setIcon( getIcon() );
115 		future = null;
116 		// iconAnimationThread = null;
117 	}
118 
119 	public T getTarget()
120 	{
121 		return target;
122 	}
123 
124 	public boolean isEnabled()
125 	{
126 		return enabled;
127 	}
128 
129 	public void setEnabled( boolean enabled )
130 	{
131 		this.enabled = enabled;
132 		if( !stopped )
133 			stopped = enabled;
134 	}
135 }