View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.theplugin.idea;
18  
19  import com.intellij.util.ui.AsyncProcessIcon;
20  import com.intellij.openapi.util.IconLoader;
21  
22  import javax.swing.*;
23  import java.awt.*;
24  
25  /**
26   * Created by IntelliJ IDEA.
27   * User: Jacek
28   * Date: 2008-04-02
29   * Time: 13:35:13
30   * To change this template use File | Settings | File Templates.
31   */
32  public class ProgressAnimationProvider extends JPanel {
33  
34  	private AnimatedProgressIcon progressIcon = new AnimatedProgressIcon("Progress Indicator");
35  	private JComponent replacedComponent;
36  	private Object addConstraint;
37  	private JComponent parentComponent;
38  
39  	/**
40  	 * @param rootComponent place where to put animation (parent of the replaceComponent). CANNOT BE NULL
41  	 * @param replaceComponent component to hide (to replace by animated progress icon).
42  	 * Can be null if rootComponent doesn't contain child
43  	 * @param constraint constraint used to add animation icon (and replaceComponent). 
44  	 */
45  	public void configure(JComponent rootComponent, JComponent replaceComponent, Object constraint) {
46  		this.parentComponent = rootComponent;
47  		this.replacedComponent = replaceComponent;
48  		this.addConstraint = constraint;
49  	}
50  
51  	public void startProgressAnimation() {
52  		EventQueue.invokeLater(new ProgressAnimation(true));
53  	}
54  
55  	public void stopProgressAnimation() {
56  		EventQueue.invokeLater(new ProgressAnimation(false));
57  	}
58  
59  	private class ProgressAnimation implements Runnable {
60  		private boolean start;
61  
62  		public ProgressAnimation(boolean start) {
63  			this.start = start;
64  		}
65  
66  		public void run() {
67  			if (start) {
68  				if (replacedComponent != null) {
69  					parentComponent.remove(replacedComponent);
70  				}
71  				parentComponent.add(progressIcon, addConstraint);
72  				progressIcon.resume();
73  				parentComponent.repaint();
74  				parentComponent.validate();
75  				parentComponent.requestFocus();
76  			} else {
77  
78  				progressIcon.suspend();
79  				parentComponent.remove(progressIcon);
80  				if (replacedComponent != null) {
81  					parentComponent.add(replacedComponent, addConstraint);
82  				}
83  				parentComponent.repaint();
84  				parentComponent.validate();
85  				parentComponent.requestFocus();
86  			}
87  
88  		}
89  	}
90  
91  	private static class AnimatedProgressIcon extends AsyncProcessIcon {
92  		private Icon[] icons;
93  		private Icon passiveIcon;
94  		private static final int CYCLE_LENGTH = 640; // whole animation single cycle lenght
95  		private static final int CYCLE_GAP = 80; // break after every single cycle (best 'cycleLenght / number of frames')
96  
97  		public AnimatedProgressIcon(@org.jetbrains.annotations.NonNls String name) {
98  			super(name);
99  
100 			// comment that line if you want to use standard IDEA small progress circle
101 			initCustomLook();
102 		}
103 
104 		private void initCustomLook() {
105 			loadIcons();
106 			init(icons, passiveIcon, CYCLE_LENGTH, CYCLE_GAP, -1);
107 		}
108 
109 		private void loadIcons() {
110 			icons = new Icon[]{
111 				IconLoader.getIcon("/icons/progress/roller_1.png"),
112 				IconLoader.getIcon("/icons/progress/roller_2.png"),
113 				IconLoader.getIcon("/icons/progress/roller_3.png"),
114 				IconLoader.getIcon("/icons/progress/roller_4.png"),
115 				IconLoader.getIcon("/icons/progress/roller_5.png"),
116 				IconLoader.getIcon("/icons/progress/roller_6.png"),
117 				IconLoader.getIcon("/icons/progress/roller_7.png"),
118 				IconLoader.getIcon("/icons/progress/roller_8.png"),
119 			};
120 
121 			passiveIcon = IconLoader.getIcon("/icons/progress/roller_0.png");
122 
123 		}
124 	}
125 }