|
@@ -1,186 +1,185 @@ |
1 |
|
- |
package org.litesoft.GWT.client.util; |
2 |
|
- |
|
3 |
|
- |
import org.litesoft.commonfoundation.base.*; |
4 |
|
- |
import org.litesoft.commonfoundation.typeutils.*; |
5 |
|
- |
|
6 |
|
- |
import java.util.*; |
7 |
|
- |
|
8 |
|
- |
public class StyledSafeTextMessageUserSink implements MessageUserSink { |
9 |
|
- |
private final Manager mManager = new Manager(); |
10 |
|
- |
private final StyledSafeTextSink mSafeTextSink; |
11 |
|
- |
private PriorityMessage mCurrentMessage; |
12 |
|
- |
|
13 |
|
- |
public StyledSafeTextMessageUserSink( StyledSafeTextSink pSafeTextSink ) { |
14 |
|
- |
mSafeTextSink = pSafeTextSink; |
15 |
|
- |
} |
16 |
|
- |
|
17 |
|
- |
@Override |
18 |
|
- |
public void clearMessage() { |
19 |
|
- |
Priority zNextPriority = mManager.getNextPriority(); |
20 |
|
- |
if ( !optionallyUpdateCurrentMessage( zNextPriority ) ) { |
21 |
|
- |
removeCurrentMessageStyle(); |
22 |
|
- |
mCurrentMessage = null; |
23 |
|
- |
if ( !optionallyUpdateCurrentMessage( zNextPriority ) ) { |
24 |
|
- |
setMessageAndStyle( "", Priority.Info ); |
25 |
|
- |
} |
26 |
|
- |
} |
27 |
|
- |
} |
28 |
|
- |
|
29 |
|
- |
@Override |
30 |
|
- |
public void clearMessage( String pMessage ) { |
31 |
|
- |
if ( mCurrentMessage != null && mCurrentMessage.hasMessage( pMessage ) ) { |
32 |
|
- |
clearMessage(); |
33 |
|
- |
return; |
34 |
|
- |
} |
35 |
|
- |
mManager.removeMessage( pMessage ); |
36 |
|
- |
} |
37 |
|
- |
|
38 |
|
- |
@Override |
39 |
|
- |
public void setInfoMessage( String pMessage ) { |
40 |
|
- |
optionallyUpdateCurrentMessage( mManager.addMessage( Priority.Info, pMessage ) ); |
41 |
|
- |
} |
42 |
|
- |
|
43 |
|
- |
@Override |
44 |
|
- |
public void setWarningMessage( String pMessage ) { |
45 |
|
- |
optionallyUpdateCurrentMessage( mManager.addMessage( Priority.Warning, pMessage ) ); |
46 |
|
- |
} |
47 |
|
- |
|
48 |
|
- |
@Override |
49 |
|
- |
public void setErrorMessage( String pMessage ) { |
50 |
|
- |
optionallyUpdateCurrentMessage( mManager.addMessage( Priority.Error, pMessage ) ); |
51 |
|
- |
} |
52 |
|
- |
|
53 |
|
- |
@Override |
54 |
|
- |
public void setErrorMessage( Throwable pThrowable, String pMessage ) { |
55 |
|
- |
if ( pThrowable != null ) { |
56 |
|
- |
System.err.println( "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" ); |
57 |
|
- |
System.err.println( pMessage ); |
58 |
|
- |
pThrowable.printStackTrace(); |
59 |
|
- |
System.err.println( "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" ); |
60 |
|
- |
} |
61 |
|
- |
setErrorMessage( pMessage ); |
62 |
|
- |
} |
63 |
|
- |
|
64 |
|
- |
/** |
65 |
|
- |
* @param pNextHighestPriority null OK - means no more messages - Don't update |
66 |
|
- |
* |
67 |
|
- |
* @return true if updated the the Current Message |
68 |
|
- |
*/ |
69 |
|
- |
private boolean optionallyUpdateCurrentMessage( Priority pNextHighestPriority ) { |
70 |
|
- |
if ( pNextHighestPriority != null ) { |
71 |
|
- |
Priority zCurrentPriority = getCurrentMessagePriority(); |
72 |
|
- |
if ( zCurrentPriority == null || zCurrentPriority.isLowerPriorityThan( pNextHighestPriority ) ) { |
73 |
|
- |
removeCurrentMessageStyle(); |
74 |
|
- |
mCurrentMessage = mManager.getNextMessage(); |
75 |
|
- |
setMessageAndStyle( mCurrentMessage.getMessage(), mCurrentMessage.getPriority() ); |
76 |
|
- |
return true; |
77 |
|
- |
} |
78 |
|
- |
} |
79 |
|
- |
return false; |
80 |
|
- |
} |
81 |
|
- |
|
82 |
|
- |
private void removeCurrentMessageStyle() { |
83 |
|
- |
Priority zCurrentStyle = Priority.deNull( getCurrentMessagePriority() ); |
84 |
|
- |
mSafeTextSink.removeStyleName( zCurrentStyle.name() ); |
85 |
|
- |
} |
86 |
|
- |
|
87 |
|
- |
private void setMessageAndStyle( String pMessage, Priority pPriority ) { |
88 |
|
- |
mSafeTextSink.addStyleName( pPriority.name() ); |
89 |
|
- |
mSafeTextSink.setText( pMessage ); |
90 |
|
- |
} |
91 |
|
- |
|
92 |
|
- |
private Priority getCurrentMessagePriority() { |
93 |
|
- |
return (mCurrentMessage != null) ? mCurrentMessage.getPriority() : null; |
94 |
|
- |
} |
95 |
|
- |
|
96 |
|
- |
private static class PriorityMessage { |
97 |
|
- |
private Priority mPriority; |
98 |
|
- |
private String mMessage; |
99 |
|
- |
|
100 |
|
- |
public PriorityMessage( Priority pPriority, String pMessage ) { |
101 |
|
- |
mPriority = pPriority; |
102 |
|
- |
mMessage = pMessage; |
103 |
|
- |
} |
104 |
|
- |
|
105 |
|
- |
public Priority getPriority() { |
106 |
|
- |
return mPriority; |
107 |
|
- |
} |
108 |
|
- |
|
109 |
|
- |
public String getMessage() { |
110 |
|
- |
return mMessage; |
111 |
|
- |
} |
112 |
|
- |
|
113 |
|
- |
public boolean isLowerPriorityThan( Priority pPriority ) { |
114 |
|
- |
return mPriority.isLowerPriorityThan( pPriority ); |
115 |
|
- |
} |
116 |
|
- |
|
117 |
|
- |
public boolean hasMessage( String pMessage ) { |
118 |
|
- |
return mMessage.equals( pMessage ); |
119 |
|
- |
} |
120 |
|
- |
} |
121 |
|
- |
|
122 |
|
- |
private static class Manager { |
123 |
|
- |
private final List<String>[] mMessagesByPriority = Cast.it( new List[Priority.values().length] ); |
124 |
|
- |
|
125 |
|
- |
{ |
126 |
|
- |
for ( int i = 0; i < mMessagesByPriority.length; i++ ) { |
127 |
|
- |
mMessagesByPriority[i] = new ArrayList<String>(); |
128 |
|
- |
} |
129 |
|
- |
} |
130 |
|
- |
|
131 |
|
- |
/** |
132 |
|
- |
* Get the Next Message (with the Highest Priority) |
133 |
|
- |
* |
134 |
|
- |
* @return null means no more messages |
135 |
|
- |
*/ |
136 |
|
- |
public Priority getNextPriority() { |
137 |
|
- |
for ( Priority zPriority : Priority.values() ) { |
138 |
|
- |
if ( !mMessagesByPriority[zPriority.ordinal()].isEmpty() ) { |
139 |
|
- |
return zPriority; |
140 |
|
- |
} |
141 |
|
- |
} |
142 |
|
- |
return null; |
143 |
|
- |
} |
144 |
|
- |
|
145 |
|
- |
/** |
146 |
|
- |
* Get the Next Message (with the Highest Priority) |
147 |
|
- |
* |
148 |
|
- |
* @return null means no more messages |
149 |
|
- |
*/ |
150 |
|
- |
public PriorityMessage getNextMessage() { |
151 |
|
- |
for ( Priority zPriority : Priority.values() ) { |
152 |
|
- |
List<String> zMessages = mMessagesByPriority[zPriority.ordinal()]; |
153 |
|
- |
if ( !zMessages.isEmpty() ) { |
154 |
|
- |
return new PriorityMessage( zPriority, zMessages.remove( 0 ) ); |
155 |
|
- |
} |
156 |
|
- |
} |
157 |
|
- |
return null; |
158 |
|
- |
} |
159 |
|
- |
|
160 |
|
- |
/** |
161 |
|
- |
* Add a new Message. |
162 |
|
- |
* |
163 |
|
- |
* @return The Highest Priority Message's Priority OR null if no Messages waiting |
164 |
|
- |
*/ |
165 |
|
- |
public Priority addMessage( Priority pPriority, String pMessage ) { |
166 |
|
- |
if ( null != (pMessage = Strings.noEmpty( pMessage )) ) { |
167 |
|
- |
mMessagesByPriority[Priority.deNull( pPriority ).ordinal()].add( pMessage ); |
168 |
|
- |
} |
169 |
|
- |
for ( Priority zPriority : Priority.values() ) { |
170 |
|
- |
if ( !mMessagesByPriority[zPriority.ordinal()].isEmpty() ) { |
171 |
|
- |
return zPriority; |
172 |
|
- |
} |
173 |
|
- |
} |
174 |
|
- |
return null; |
175 |
|
- |
} |
176 |
|
- |
|
177 |
|
- |
public void removeMessage( String pMessage ) { |
178 |
|
- |
for ( Priority zPriority : Priority.values() ) { |
179 |
|
- |
List<String> zMessages = mMessagesByPriority[zPriority.ordinal()]; |
180 |
|
- |
if ( zMessages.remove( pMessage ) ) { |
181 |
|
- |
return; |
182 |
|
- |
} |
183 |
|
- |
} |
184 |
|
- |
} |
185 |
|
- |
} |
186 |
|
- |
} |
|
1 |
+ |
package org.litesoft.GWT.client.util; |
|
2 |
+ |
|
|
3 |
+ |
import org.litesoft.commonfoundation.base.*; |
|
4 |
+ |
|
|
5 |
+ |
import java.util.*; |
|
6 |
+ |
|
|
7 |
+ |
public class StyledSafeTextMessageUserSink implements MessageUserSink { |
|
8 |
+ |
private final Manager mManager = new Manager(); |
|
9 |
+ |
private final StyledSafeTextSink mSafeTextSink; |
|
10 |
+ |
private PriorityMessage mCurrentMessage; |
|
11 |
+ |
|
|
12 |
+ |
public StyledSafeTextMessageUserSink( StyledSafeTextSink pSafeTextSink ) { |
|
13 |
+ |
mSafeTextSink = pSafeTextSink; |
|
14 |
+ |
} |
|
15 |
+ |
|
|
16 |
+ |
@Override |
|
17 |
+ |
public void clearMessage() { |
|
18 |
+ |
Priority zNextPriority = mManager.getNextPriority(); |
|
19 |
+ |
if ( !optionallyUpdateCurrentMessage( zNextPriority ) ) { |
|
20 |
+ |
removeCurrentMessageStyle(); |
|
21 |
+ |
mCurrentMessage = null; |
|
22 |
+ |
if ( !optionallyUpdateCurrentMessage( zNextPriority ) ) { |
|
23 |
+ |
setMessageAndStyle( "", Priority.Info ); |
|
24 |
+ |
} |
|
25 |
+ |
} |
|
26 |
+ |
} |
|
27 |
+ |
|
|
28 |
+ |
@Override |
|
29 |
+ |
public void clearMessage( String pMessage ) { |
|
30 |
+ |
if ( mCurrentMessage != null && mCurrentMessage.hasMessage( pMessage ) ) { |
|
31 |
+ |
clearMessage(); |
|
32 |
+ |
return; |
|
33 |
+ |
} |
|
34 |
+ |
mManager.removeMessage( pMessage ); |
|
35 |
+ |
} |
|
36 |
+ |
|
|
37 |
+ |
@Override |
|
38 |
+ |
public void setInfoMessage( String pMessage ) { |
|
39 |
+ |
optionallyUpdateCurrentMessage( mManager.addMessage( Priority.Info, pMessage ) ); |
|
40 |
+ |
} |
|
41 |
+ |
|
|
42 |
+ |
@Override |
|
43 |
+ |
public void setWarningMessage( String pMessage ) { |
|
44 |
+ |
optionallyUpdateCurrentMessage( mManager.addMessage( Priority.Warning, pMessage ) ); |
|
45 |
+ |
} |
|
46 |
+ |
|
|
47 |
+ |
@Override |
|
48 |
+ |
public void setErrorMessage( String pMessage ) { |
|
49 |
+ |
optionallyUpdateCurrentMessage( mManager.addMessage( Priority.Error, pMessage ) ); |
|
50 |
+ |
} |
|
51 |
+ |
|
|
52 |
+ |
@Override |
|
53 |
+ |
public void setErrorMessage( Throwable pThrowable, String pMessage ) { |
|
54 |
+ |
if ( pThrowable != null ) { |
|
55 |
+ |
System.err.println( "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" ); |
|
56 |
+ |
System.err.println( pMessage ); |
|
57 |
+ |
pThrowable.printStackTrace(); |
|
58 |
+ |
System.err.println( "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" ); |
|
59 |
+ |
} |
|
60 |
+ |
setErrorMessage( pMessage ); |
|
61 |
+ |
} |
|
62 |
+ |
|
|
63 |
+ |
/** |
|
64 |
+ |
* @param pNextHighestPriority null OK - means no more messages - Don't update |
|
65 |
+ |
* |
|
66 |
+ |
* @return true if updated the the Current Message |
|
67 |
+ |
*/ |
|
68 |
+ |
private boolean optionallyUpdateCurrentMessage( Priority pNextHighestPriority ) { |
|
69 |
+ |
if ( pNextHighestPriority != null ) { |
|
70 |
+ |
Priority zCurrentPriority = getCurrentMessagePriority(); |
|
71 |
+ |
if ( zCurrentPriority == null || zCurrentPriority.isLowerPriorityThan( pNextHighestPriority ) ) { |
|
72 |
+ |
removeCurrentMessageStyle(); |
|
73 |
+ |
mCurrentMessage = mManager.getNextMessage(); |
|
74 |
+ |
setMessageAndStyle( mCurrentMessage.getMessage(), mCurrentMessage.getPriority() ); |
|
75 |
+ |
return true; |
|
76 |
+ |
} |
|
77 |
+ |
} |
|
78 |
+ |
return false; |
|
79 |
+ |
} |
|
80 |
+ |
|
|
81 |
+ |
private void removeCurrentMessageStyle() { |
|
82 |
+ |
Priority zCurrentStyle = Priority.deNull( getCurrentMessagePriority() ); |
|
83 |
+ |
mSafeTextSink.removeStyleName( zCurrentStyle.name() ); |
|
84 |
+ |
} |
|
85 |
+ |
|
|
86 |
+ |
private void setMessageAndStyle( String pMessage, Priority pPriority ) { |
|
87 |
+ |
mSafeTextSink.addStyleName( pPriority.name() ); |
|
88 |
+ |
mSafeTextSink.setText( pMessage ); |
|
89 |
+ |
} |
|
90 |
+ |
|
|
91 |
+ |
private Priority getCurrentMessagePriority() { |
|
92 |
+ |
return (mCurrentMessage != null) ? mCurrentMessage.getPriority() : null; |
|
93 |
+ |
} |
|
94 |
+ |
|
|
95 |
+ |
private static class PriorityMessage { |
|
96 |
+ |
private Priority mPriority; |
|
97 |
+ |
private String mMessage; |
|
98 |
+ |
|
|
99 |
+ |
public PriorityMessage( Priority pPriority, String pMessage ) { |
|
100 |
+ |
mPriority = pPriority; |
|
101 |
+ |
mMessage = pMessage; |
|
102 |
+ |
} |
|
103 |
+ |
|
|
104 |
+ |
public Priority getPriority() { |
|
105 |
+ |
return mPriority; |
|
106 |
+ |
} |
|
107 |
+ |
|
|
108 |
+ |
public String getMessage() { |
|
109 |
+ |
return mMessage; |
|
110 |
+ |
} |
|
111 |
+ |
|
|
112 |
+ |
public boolean isLowerPriorityThan( Priority pPriority ) { |
|
113 |
+ |
return mPriority.isLowerPriorityThan( pPriority ); |
|
114 |
+ |
} |
|
115 |
+ |
|
|
116 |
+ |
public boolean hasMessage( String pMessage ) { |
|
117 |
+ |
return mMessage.equals( pMessage ); |
|
118 |
+ |
} |
|
119 |
+ |
} |
|
120 |
+ |
|
|
121 |
+ |
private static class Manager { |
|
122 |
+ |
private final List<String>[] mMessagesByPriority = Cast.it( new List[Priority.values().length] ); |
|
123 |
+ |
|
|
124 |
+ |
{ |
|
125 |
+ |
for ( int i = 0; i < mMessagesByPriority.length; i++ ) { |
|
126 |
+ |
mMessagesByPriority[i] = new ArrayList<String>(); |
|
127 |
+ |
} |
|
128 |
+ |
} |
|
129 |
+ |
|
|
130 |
+ |
/** |
|
131 |
+ |
* Get the Next Message (with the Highest Priority) |
|
132 |
+ |
* |
|
133 |
+ |
* @return null means no more messages |
|
134 |
+ |
*/ |
|
135 |
+ |
public Priority getNextPriority() { |
|
136 |
+ |
for ( Priority zPriority : Priority.values() ) { |
|
137 |
+ |
if ( !mMessagesByPriority[zPriority.ordinal()].isEmpty() ) { |
|
138 |
+ |
return zPriority; |
|
139 |
+ |
} |
|
140 |
+ |
} |
|
141 |
+ |
return null; |
|
142 |
+ |
} |
|
143 |
+ |
|
|
144 |
+ |
/** |
|
145 |
+ |
* Get the Next Message (with the Highest Priority) |
|
146 |
+ |
* |
|
147 |
+ |
* @return null means no more messages |
|
148 |
+ |
*/ |
|
149 |
+ |
public PriorityMessage getNextMessage() { |
|
150 |
+ |
for ( Priority zPriority : Priority.values() ) { |
|
151 |
+ |
List<String> zMessages = mMessagesByPriority[zPriority.ordinal()]; |
|
152 |
+ |
if ( !zMessages.isEmpty() ) { |
|
153 |
+ |
return new PriorityMessage( zPriority, zMessages.remove( 0 ) ); |
|
154 |
+ |
} |
|
155 |
+ |
} |
|
156 |
+ |
return null; |
|
157 |
+ |
} |
|
158 |
+ |
|
|
159 |
+ |
/** |
|
160 |
+ |
* Add a new Message. |
|
161 |
+ |
* |
|
162 |
+ |
* @return The Highest Priority Message's Priority OR null if no Messages waiting |
|
163 |
+ |
*/ |
|
164 |
+ |
public Priority addMessage( Priority pPriority, String pMessage ) { |
|
165 |
+ |
if ( null != (pMessage = ConstrainTo.significantOrNull( pMessage )) ) { |
|
166 |
+ |
mMessagesByPriority[Priority.deNull( pPriority ).ordinal()].add( pMessage ); |
|
167 |
+ |
} |
|
168 |
+ |
for ( Priority zPriority : Priority.values() ) { |
|
169 |
+ |
if ( !mMessagesByPriority[zPriority.ordinal()].isEmpty() ) { |
|
170 |
+ |
return zPriority; |
|
171 |
+ |
} |
|
172 |
+ |
} |
|
173 |
+ |
return null; |
|
174 |
+ |
} |
|
175 |
+ |
|
|
176 |
+ |
public void removeMessage( String pMessage ) { |
|
177 |
+ |
for ( Priority zPriority : Priority.values() ) { |
|
178 |
+ |
List<String> zMessages = mMessagesByPriority[zPriority.ordinal()]; |
|
179 |
+ |
if ( zMessages.remove( pMessage ) ) { |
|
180 |
+ |
return; |
|
181 |
+ |
} |
|
182 |
+ |
} |
|
183 |
+ |
} |
|
184 |
+ |
} |
|
185 |
+ |
} |