|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| package com.gargoylesoftware.base.gui; |
|
39 |
| |
|
40 |
| import com.gargoylesoftware.base.util.DetailedIllegalArgumentException; |
|
41 |
| import com.gargoylesoftware.base.util.DetailedNullPointerException; |
|
42 |
| import java.awt.BorderLayout; |
|
43 |
| import java.awt.Component; |
|
44 |
| import java.util.Collections; |
|
45 |
| import java.util.HashSet; |
|
46 |
| import java.util.Set; |
|
47 |
| import javax.swing.JComponent; |
|
48 |
| import javax.swing.JLabel; |
|
49 |
| import javax.swing.SwingUtilities; |
|
50 |
| import javax.swing.UIManager; |
|
51 |
| import javax.swing.JScrollPane; |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| public class DelayedComponentLoaderPanel extends JComponent { |
|
77 |
| private static final long serialVersionUID = 1; |
|
78 |
| private static final int LOADER_STARTED = 1; |
|
79 |
| private static final int LOADER_FINISHED = 2; |
|
80 |
| |
|
81 |
| private Set listeners_; |
|
82 |
| private Component waitingComponent_; |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
0
| public DelayedComponentLoaderPanel() {
|
|
89 |
0
| this( new JLabel("Loading ... Please wait.") );
|
|
90 |
0
| JLabel label = (JLabel)waitingComponent_;
|
|
91 |
0
| label.setIcon( UIManager.getIcon("OptionPane.informationIcon") );
|
|
92 |
0
| label.setHorizontalAlignment( JLabel.CENTER );
|
|
93 |
0
| label.setOpaque(true);
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
0
| public DelayedComponentLoaderPanel( final Component waitingComponent ) {
|
|
103 |
0
| assertNotNull("waitingComponent", waitingComponent);
|
|
104 |
| |
|
105 |
0
| setLayout( new BorderLayout() );
|
|
106 |
0
| waitingComponent_ = waitingComponent;
|
|
107 |
0
| add( BorderLayout.CENTER, waitingComponent_ );
|
|
108 |
| } |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
0
| public void setComponentLoader( final ComponentLoader loader ) {
|
|
119 |
| |
|
120 |
0
| replaceComponent( waitingComponent_ );
|
|
121 |
| |
|
122 |
0
| if( loader != null ) {
|
|
123 |
0
| fireComponentLoadingEvent(LOADER_STARTED, loader, null);
|
|
124 |
0
| new Thread() {
|
|
125 |
0
| public void run() {
|
|
126 |
0
| try {
|
|
127 |
0
| sleep(500);
|
|
128 |
| } |
|
129 |
| catch( final InterruptedException e ) { |
|
130 |
| |
|
131 |
| } |
|
132 |
| |
|
133 |
0
| Component loadedComponent;
|
|
134 |
| |
|
135 |
0
| try {
|
|
136 |
0
| loadedComponent = loader.loadComponent();
|
|
137 |
| } |
|
138 |
| catch( final Throwable t ) { |
|
139 |
0
| loadedComponent = new JScrollPane(new ThrowablePanel(t));
|
|
140 |
0
| t.printStackTrace();
|
|
141 |
| } |
|
142 |
| |
|
143 |
0
| final Component finalLoadedComponent = loadedComponent;
|
|
144 |
0
| SwingUtilities.invokeLater( new Runnable() {
|
|
145 |
0
| public void run() {
|
|
146 |
0
| replaceComponent( finalLoadedComponent );
|
|
147 |
0
| fireComponentLoadingEvent(LOADER_FINISHED, loader,
|
|
148 |
| finalLoadedComponent ); |
|
149 |
| } |
|
150 |
| } ); |
|
151 |
| } |
|
152 |
| }.start(); |
|
153 |
| } |
|
154 |
| } |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
0
| private void replaceComponent( final Component component ) {
|
|
161 |
0
| removeAll();
|
|
162 |
0
| add( BorderLayout.CENTER, component );
|
|
163 |
0
| revalidate();
|
|
164 |
| } |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
0
| public void addDelayedComponentLoaderListener( final DelayedComponentLoaderListener listener ) {
|
|
172 |
| |
|
173 |
0
| assertNotNull("listener", listener);
|
|
174 |
| |
|
175 |
| |
|
176 |
0
| if( listeners_ == null ) {
|
|
177 |
0
| synchronized(this) {
|
|
178 |
0
| if( listeners_ == null ) {
|
|
179 |
0
| listeners_ = Collections.synchronizedSet(new HashSet());
|
|
180 |
| } |
|
181 |
| } |
|
182 |
| } |
|
183 |
0
| listeners_.add(listener);
|
|
184 |
| } |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
0
| public void removeDelayedComponentLoaderListener(final DelayedComponentLoaderListener listener ) {
|
|
191 |
| |
|
192 |
0
| assertNotNull("listener", listener);
|
|
193 |
| |
|
194 |
0
| if( listeners_ != null ) {
|
|
195 |
0
| listeners_.remove(listener);
|
|
196 |
| } |
|
197 |
| } |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
0
| private void fireComponentLoadingEvent(
|
|
206 |
| final int action, |
|
207 |
| final ComponentLoader loader, |
|
208 |
| final Component loadedComponent ) { |
|
209 |
| |
|
210 |
0
| if( listeners_ != null && listeners_.isEmpty() == false ) {
|
|
211 |
0
| DelayedComponentLoaderListener listenerArray[];
|
|
212 |
0
| synchronized(listeners_) {
|
|
213 |
0
| listenerArray = new DelayedComponentLoaderListener[listeners_.size()];
|
|
214 |
0
| listeners_.toArray(listenerArray);
|
|
215 |
| } |
|
216 |
| |
|
217 |
0
| final DelayedComponentLoaderEvent event
|
|
218 |
| = new DelayedComponentLoaderEvent(this, loader, loadedComponent); |
|
219 |
| |
|
220 |
0
| int i;
|
|
221 |
0
| for( i=0; i<listenerArray.length; i++ ) {
|
|
222 |
0
| switch( action ) {
|
|
223 |
0
| case LOADER_STARTED:
|
|
224 |
0
| listenerArray[i].componentLoadingStarted(event);
|
|
225 |
0
| break;
|
|
226 |
| |
|
227 |
0
| case LOADER_FINISHED:
|
|
228 |
0
| listenerArray[i].componentLoadingFinished(event);
|
|
229 |
0
| break;
|
|
230 |
| |
|
231 |
0
| default:
|
|
232 |
0
| throw new DetailedIllegalArgumentException("action", new Integer(action), "Unexpected value");
|
|
233 |
| } |
|
234 |
| } |
|
235 |
| } |
|
236 |
| } |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
0
| protected final void assertNotNull( final String fieldName, final Object object ) {
|
|
245 |
0
| if( object == null ) {
|
|
246 |
0
| throw new DetailedNullPointerException(fieldName);
|
|
247 |
| } |
|
248 |
| } |
|
249 |
| } |