1 19 20 package org.netbeans.modules.junit; 21 22 46 public final class MessageStack { 47 48 51 52 53 public static final int LAYER_VOLATILE = -1; 54 55 58 private final String [] messageLayers; 59 63 private int visibleLayerIndex = -1; 64 65 private String volatileMsg; 66 67 72 public MessageStack(int size) { 73 if (size <= 0) { 74 throw new IllegalArgumentException ( 75 "number of layers must be positive"); } 77 messageLayers = new String [size]; 78 } 79 80 87 public String getDisplayedMessage() { 88 return volatileMsg != null ? volatileMsg 89 : getTopmostStackMessage(); 90 } 91 92 98 private String getTopmostStackMessage() { 99 return visibleLayerIndex != -1 ? messageLayers[visibleLayerIndex] 100 : null; 101 } 102 103 108 public void clear() { 109 volatileMsg = null; 110 if (visibleLayerIndex != -1) { 111 for (int i = visibleLayerIndex; i < messageLayers.length; i++) { 112 messageLayers[i] = null; 113 } 114 visibleLayerIndex = -1; 115 } 116 } 117 118 133 public String setMessage(final int layer, String message) 134 throws IllegalArgumentException { 135 136 137 if (layer == LAYER_VOLATILE) { 138 return setVolatileMessage(message); 139 } 140 if (layer < 0 || layer >= messageLayers.length) { 141 throw new IllegalArgumentException ( 142 java.text.MessageFormat.format( 143 "Message type out of bounds (0 .. {1}): {0}", new Object [] { new Integer (layer), 145 new Integer (messageLayers.length) })); 146 } 147 148 149 if ((message != null) && (message.trim().length() == 0)) { 150 message = null; 151 } 152 153 final String oldDisplayed = getDisplayedMessage(); 154 155 156 volatileMsg = null; 157 messageLayers[layer] = message; 158 159 160 if (message != null) { 161 if ((visibleLayerIndex == -1) || (layer < visibleLayerIndex)) { 162 visibleLayerIndex = layer; 163 } 164 } else if (layer == visibleLayerIndex) { 165 for (int i = layer + 1; i < messageLayers.length; i++) { 166 if (messageLayers[i] != null) { 167 visibleLayerIndex = i; 168 break; 169 } 170 } 171 if (visibleLayerIndex == layer) { visibleLayerIndex = -1; 173 } 174 } 175 176 177 return checkDisplayModified(oldDisplayed, getDisplayedMessage()); 178 } 179 180 191 public String clearMessage(final int layer) { 192 return setMessage(layer, null); 193 } 194 195 204 public String setVolatileMessage(String message) { 205 206 207 if ((message != null) && (message.trim().length() == 0)) { 208 message = null; 209 } 210 211 final String oldDisplayed = getDisplayedMessage(); 212 213 214 volatileMsg = message; 215 216 217 return checkDisplayModified(oldDisplayed, getDisplayedMessage()); 218 } 219 220 230 public String clearVolatileMessage() { 231 return setVolatileMessage(null); 232 } 233 234 243 private String checkDisplayModified(String oldDisplay, String newDisplay) { 244 if ((newDisplay == null) && (oldDisplay != null)) { 245 return ""; } 247 if ((newDisplay != null) && !newDisplay.equals(oldDisplay)) { 248 return newDisplay; 249 } 250 return null; 251 } 252 253 } 254 | Popular Tags |