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