|
@@ -1,33 +1,29 @@ |
1 |
1 |
|
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
2 |
|
package org.litesoft.GWT.client.command.queue; |
3 |
3 |
|
|
4 |
|
- |
import java.util.*; |
5 |
|
- |
|
6 |
4 |
|
import com.google.gwt.user.client.*; |
7 |
5 |
|
|
|
6 |
+ |
import java.util.*; |
|
7 |
+ |
|
8 |
8 |
|
/** |
9 |
9 |
|
* The subclass is responsible for starting the queue once a command has been |
10 |
10 |
|
* added and determining how each run cycle is scheduled. |
11 |
11 |
|
*/ |
12 |
|
- |
public abstract class CommandQueueBase implements SourcesCommandQueueEvents |
13 |
|
- |
{ |
|
12 |
+ |
public abstract class CommandQueueBase implements SourcesCommandQueueEvents { |
14 |
13 |
|
private final ArrayList<CommandQueueListener> mListeners = new ArrayList<CommandQueueListener>(); |
15 |
14 |
|
private final ArrayList<Command> mQueue = new ArrayList<Command>(); |
16 |
15 |
|
|
17 |
16 |
|
@Override |
18 |
|
- |
public void addListener( CommandQueueListener pListener ) |
19 |
|
- |
{ |
|
17 |
+ |
public void addListener( CommandQueueListener pListener ) { |
20 |
18 |
|
mListeners.add( pListener ); |
21 |
19 |
|
} |
22 |
20 |
|
|
23 |
21 |
|
@Override |
24 |
|
- |
public void removeListener( CommandQueueListener pListener ) |
25 |
|
- |
{ |
|
22 |
+ |
public void removeListener( CommandQueueListener pListener ) { |
26 |
23 |
|
mListeners.remove( pListener ); |
27 |
24 |
|
} |
28 |
25 |
|
|
29 |
|
- |
public boolean hasNext() |
30 |
|
- |
{ |
|
26 |
+ |
public boolean hasNext() { |
31 |
27 |
|
return mQueue.size() > 0; |
32 |
28 |
|
} |
33 |
29 |
|
|
|
@@ -41,33 +37,27 @@ |
41 |
37 |
|
* |
42 |
38 |
|
* @throws IndexOutOfBoundsException if <code>index < 0 || index > {@link #getSize()}</code> |
43 |
39 |
|
*/ |
44 |
|
- |
public Command get( int pIndex ) |
45 |
|
- |
{ |
|
40 |
+ |
public Command get( int pIndex ) { |
46 |
41 |
|
return mQueue.get( pIndex ); |
47 |
42 |
|
} |
48 |
43 |
|
|
49 |
|
- |
public Command remove( int pIndex ) |
50 |
|
- |
{ |
|
44 |
+ |
public Command remove( int pIndex ) { |
51 |
45 |
|
return mQueue.remove( pIndex ); |
52 |
46 |
|
} |
53 |
47 |
|
|
54 |
|
- |
public void clear() |
55 |
|
- |
{ |
|
48 |
+ |
public void clear() { |
56 |
49 |
|
mQueue.clear(); |
57 |
50 |
|
} |
58 |
51 |
|
|
59 |
|
- |
public int getSize() |
60 |
|
- |
{ |
|
52 |
+ |
public int getSize() { |
61 |
53 |
|
return mQueue.size(); |
62 |
54 |
|
} |
63 |
55 |
|
|
64 |
56 |
|
public abstract void add( Command pCommand ); |
65 |
57 |
|
|
66 |
|
- |
protected boolean addImpl( Command pCommand ) |
67 |
|
- |
{ |
|
58 |
+ |
protected boolean addImpl( Command pCommand ) { |
68 |
59 |
|
// Debug.println("CommandQueue.addImpl(" + command + ')'); |
69 |
|
- |
if ( !fireBeforeCommandAdded( pCommand ) ) |
70 |
|
- |
{ |
|
60 |
+ |
if ( !fireBeforeCommandAdded( pCommand ) ) { |
71 |
61 |
|
return false; |
72 |
62 |
|
} |
73 |
63 |
|
|
|
@@ -78,12 +68,10 @@ |
78 |
68 |
|
|
79 |
69 |
|
protected abstract void scheduleRunIteration(); |
80 |
70 |
|
|
81 |
|
- |
protected void run() |
82 |
|
- |
{ |
|
71 |
+ |
protected void run() { |
83 |
72 |
|
// Debug.println("CommandQueue.run()"); |
84 |
73 |
|
executeCommand( mQueue.remove( 0 ) ); |
85 |
|
- |
if ( hasNext() ) |
86 |
|
- |
{ |
|
74 |
+ |
if ( hasNext() ) { |
87 |
75 |
|
scheduleRunIteration(); |
88 |
76 |
|
} |
89 |
77 |
|
} |
|
@@ -97,11 +85,9 @@ |
97 |
85 |
|
* |
98 |
86 |
|
* @param pCommand |
99 |
87 |
|
*/ |
100 |
|
- |
protected void executeCommand( Command pCommand ) |
101 |
|
- |
{ |
|
88 |
+ |
protected void executeCommand( Command pCommand ) { |
102 |
89 |
|
// Debug.println("CommandQueue.executeCommand(" + command + ")"); |
103 |
|
- |
if ( !fireBeforeCommandExecuted( pCommand ) ) |
104 |
|
- |
{ |
|
90 |
+ |
if ( !fireBeforeCommandExecuted( pCommand ) ) { |
105 |
91 |
|
return; |
106 |
92 |
|
} |
107 |
93 |
|
|
|
@@ -109,76 +95,62 @@ |
109 |
95 |
|
fireAfterCommandExecuted( pCommand ); |
110 |
96 |
|
} |
111 |
97 |
|
|
112 |
|
- |
protected void executeCommandImpl( Command pCommand ) |
113 |
|
- |
{ |
|
98 |
+ |
protected void executeCommandImpl( Command pCommand ) { |
114 |
99 |
|
pCommand.execute(); |
115 |
100 |
|
} |
116 |
101 |
|
|
117 |
|
- |
protected boolean fireBeforeCommandAdded( Command pCommand ) |
118 |
|
- |
{ |
119 |
|
- |
if ( mListeners.size() == 0 ) |
120 |
|
- |
{ |
|
102 |
+ |
protected boolean fireBeforeCommandAdded( Command pCommand ) { |
|
103 |
+ |
if ( mListeners.size() == 0 ) { |
121 |
104 |
|
return true; |
122 |
105 |
|
} |
123 |
106 |
|
|
124 |
107 |
|
boolean result = true; |
125 |
108 |
|
|
126 |
109 |
|
Object[] listeners = mListeners.toArray(); |
127 |
|
- |
for ( Object listener : listeners ) |
128 |
|
- |
{ |
|
110 |
+ |
for ( Object listener : listeners ) { |
129 |
111 |
|
result &= ((CommandQueueListener) listener).beforeCommandAdded( this, pCommand ); |
130 |
112 |
|
} |
131 |
113 |
|
return result; |
132 |
114 |
|
} |
133 |
115 |
|
|
134 |
|
- |
protected void fireAfterCommandAdded( Command pCommand ) |
135 |
|
- |
{ |
136 |
|
- |
if ( mListeners.size() == 0 ) |
137 |
|
- |
{ |
|
116 |
+ |
protected void fireAfterCommandAdded( Command pCommand ) { |
|
117 |
+ |
if ( mListeners.size() == 0 ) { |
138 |
118 |
|
return; |
139 |
119 |
|
} |
140 |
120 |
|
|
141 |
121 |
|
Object[] listeners = mListeners.toArray(); |
142 |
|
- |
for ( Object listener : listeners ) |
143 |
|
- |
{ |
|
122 |
+ |
for ( Object listener : listeners ) { |
144 |
123 |
|
((CommandQueueListener) listener).afterCommandAdded( this, pCommand ); |
145 |
124 |
|
} |
146 |
125 |
|
} |
147 |
126 |
|
|
148 |
|
- |
protected boolean fireBeforeCommandExecuted( Command pCommand ) |
149 |
|
- |
{ |
150 |
|
- |
if ( mListeners.size() == 0 ) |
151 |
|
- |
{ |
|
127 |
+ |
protected boolean fireBeforeCommandExecuted( Command pCommand ) { |
|
128 |
+ |
if ( mListeners.size() == 0 ) { |
152 |
129 |
|
return true; |
153 |
130 |
|
} |
154 |
131 |
|
|
155 |
132 |
|
boolean result = true; |
156 |
133 |
|
|
157 |
134 |
|
Object[] listeners = mListeners.toArray(); |
158 |
|
- |
for ( Object listener : listeners ) |
159 |
|
- |
{ |
|
135 |
+ |
for ( Object listener : listeners ) { |
160 |
136 |
|
result &= ((CommandQueueListener) listener).beforeCommandExecuted( this, pCommand ); |
161 |
137 |
|
} |
162 |
138 |
|
return result; |
163 |
139 |
|
} |
164 |
140 |
|
|
165 |
|
- |
protected void fireAfterCommandExecuted( Command pCommand ) |
166 |
|
- |
{ |
167 |
|
- |
if ( mListeners.size() == 0 ) |
168 |
|
- |
{ |
|
141 |
+ |
protected void fireAfterCommandExecuted( Command pCommand ) { |
|
142 |
+ |
if ( mListeners.size() == 0 ) { |
169 |
143 |
|
return; |
170 |
144 |
|
} |
171 |
145 |
|
|
172 |
146 |
|
Object[] listeners = mListeners.toArray(); |
173 |
|
- |
for ( Object listener : listeners ) |
174 |
|
- |
{ |
|
147 |
+ |
for ( Object listener : listeners ) { |
175 |
148 |
|
((CommandQueueListener) listener).afterCommandExecuted( this, pCommand ); |
176 |
149 |
|
} |
177 |
150 |
|
} |
178 |
151 |
|
|
179 |
152 |
|
@Override |
180 |
|
- |
public String toString() |
181 |
|
- |
{ |
|
153 |
+ |
public String toString() { |
182 |
154 |
|
return "CommandQueueBase" + mQueue.toString(); |
183 |
155 |
|
} |
184 |
156 |
|
} |