View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer.gui;
22  
23  import java.lang.reflect.InvocationTargetException;
24  
25  import javax.swing.ImageIcon;
26  import javax.swing.JTabbedPane;
27  import javax.swing.SwingUtilities;
28  
29  import org.apache.log4j.Logger;
30  import org.bounce.image.ImageLoader;
31  
32  public class AnimatedThread extends Thread {
33      static final ImageIcon[] RUN_ICONS = new ImageIcon[] {
34          ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/running1.gif"),
35          ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/running2.gif"),
36          ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/running3.gif"),
37          ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/running4.gif"),
38          ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/running5.gif"),
39          ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/running6.gif"),
40          ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/running7.gif"),
41          ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/running8.gif")
42      };
43  
44      private ProjectView view = null;
45      private JTabbedPane tabs = null;
46      boolean running = true;
47      private int iconIndex = 0;
48      
49      public AnimatedThread(JTabbedPane tabs, ProjectView view) {
50          this.tabs = tabs;
51          this.view = view;
52      }
53  
54      @Override
55      public void run() {
56          
57          while (running) {
58              try {
59                  SwingUtilities.invokeAndWait( new Runnable() {
60                      public void run() {
61                          int index = getTabIndex();
62                          if (index != -1) {
63                              tabs.setIconAt(index, RUN_ICONS[iconIndex]);
64                          }
65                      }
66                  });
67              } catch (InterruptedException e) {
68                  Logger.getLogger(this.getClass()).warn("Interrupted Error", e);
69              } catch (InvocationTargetException e) {
70                  Logger.getLogger(this.getClass()).warn("Invocation Target Error", e);
71              }
72               
73              
74              iconIndex++;
75              
76              if (iconIndex == RUN_ICONS.length) {
77                  iconIndex = 0;
78              }
79              
80              try {
81                  sleep(150);
82              } catch (InterruptedException e) {
83                  Logger.getLogger(this.getClass()).debug(e);
84              }
85          }
86          
87          int index = getTabIndex();
88          if (index != -1) {
89              tabs.setIconAt(index, view.getIcon());        
90          }
91      }
92      
93      private int getTabIndex() {
94          for (int i = 0; i < tabs.getTabCount(); i++) {
95              if (tabs.getComponentAt(i) == view) {
96                  return i;
97              }
98          }
99          
100         return -1;
101     }
102     
103     public void setRunning(boolean running) {
104         this.running = running;
105     }
106 }