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