1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }