View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.impl.wsdl.AbstractWsdlModelItem;
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 AbstractWsdlModelItem<?>> 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  			Thread.currentThread().setName( "ModelItemIconAnimator for " + target.getName() );
94  		}
95  
96  		while( !stopped )
97  		{
98  			try
99  			{
100 				if( stopped )
101 					break;
102 
103 				index = index >= animateIcons.length - 1 ? 0 : index + 1;
104 				target.setIcon( getIcon() );
105 				Thread.sleep( 500 );
106 			}
107 			catch( InterruptedException e )
108 			{
109 				SoapUI.logError( e );
110 			}
111 		}
112 
113 		target.setIcon( getIcon() );
114 		future = null;
115 		// iconAnimationThread = null;
116 	}
117 
118 	public T getTarget()
119 	{
120 		return target;
121 	}
122 
123 	public boolean isEnabled()
124 	{
125 		return enabled;
126 	}
127 
128 	public void setEnabled( boolean enabled )
129 	{
130 		this.enabled = enabled;
131 		if( !stopped )
132 			stopped = enabled;
133 	}
134 }