1 11 package org.eclipse.team.internal.ccvs.ui.console; 12 13 import com.ibm.icu.text.DateFormat; 14 import com.ibm.icu.text.SimpleDateFormat; 15 import java.util.Date ; 16 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.jface.preference.IPreferenceStore; 19 import org.eclipse.jface.preference.PreferenceConverter; 20 import org.eclipse.jface.resource.FontRegistry; 21 import org.eclipse.jface.resource.JFaceResources; 22 import org.eclipse.jface.util.IPropertyChangeListener; 23 import org.eclipse.jface.util.PropertyChangeEvent; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.swt.graphics.*; 26 import org.eclipse.swt.widgets.Display; 27 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin; 28 import org.eclipse.team.internal.ccvs.core.CVSStatus; 29 import org.eclipse.team.internal.ccvs.core.client.Session; 30 import org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener; 31 import org.eclipse.team.internal.ccvs.ui.*; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.console.*; 34 35 42 public class CVSOutputConsole extends MessageConsole implements IConsoleListener, IPropertyChangeListener { 43 44 private Color commandColor; 46 private Color messageColor; 47 private Color errorColor; 48 49 private long commandStarted = 0; 51 52 private MessageConsoleStream commandStream; 54 private MessageConsoleStream messageStream; 55 private MessageConsoleStream errorStream; 56 57 private boolean showOnMessage; 59 60 private ConsoleDocument document; 61 private IConsoleManager consoleManager; 62 63 private static final DateFormat TIME_FORMAT; 65 66 static { 67 DateFormat format; 68 try { 69 format = new SimpleDateFormat(CVSUIMessages.Console_resultTimeFormat); 70 } catch (RuntimeException e) { 71 format = new SimpleDateFormat("'(took 'm:ss.SSS')')"); } 74 TIME_FORMAT = format; 75 } 76 77 private boolean visible = false; 79 private boolean initialized = false; 81 82 85 private static final String NESTING = " "; 87 91 public class MyLifecycle implements org.eclipse.ui.console.IConsoleListener { 92 public void consolesAdded(IConsole[] consoles) { 93 for (int i = 0; i < consoles.length; i++) { 94 IConsole console = consoles[i]; 95 if (console == CVSOutputConsole.this) { 96 init(); 97 } 98 } 99 100 } 101 public void consolesRemoved(IConsole[] consoles) { 102 for (int i = 0; i < consoles.length; i++) { 103 IConsole console = consoles[i]; 104 if (console == CVSOutputConsole.this) { 105 ConsolePlugin.getDefault().getConsoleManager().removeConsoleListener(this); 106 dispose(); 107 } 108 } 109 } 110 } 111 112 116 public CVSOutputConsole() { 117 super("CVS", CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_CVS_CONSOLE)); showOnMessage = CVSUIPlugin.getPlugin().getPreferenceStore().getBoolean(ICVSUIConstants.PREF_CONSOLE_SHOW_ON_MESSAGE); 119 document = new ConsoleDocument(); 120 consoleManager = ConsolePlugin.getDefault().getConsoleManager(); 121 CVSProviderPlugin.getPlugin().setConsoleListener(CVSOutputConsole.this); 122 CVSUIPlugin.getPlugin().getPreferenceStore().addPropertyChangeListener(CVSOutputConsole.this); 123 } 124 125 128 protected void init() { 129 super.init(); 131 132 initLimitOutput(); 133 initWrapSetting(); 134 135 CVSUIPlugin.getStandardDisplay().asyncExec(new Runnable () { 137 public void run() { 138 JFaceResources.getFontRegistry().addListener(CVSOutputConsole.this); 139 initializeStreams(); 140 dump(); 141 } 142 }); 143 } 144 145 private void initWrapSetting() { 146 IPreferenceStore store = CVSUIPlugin.getPlugin().getPreferenceStore(); 147 if(store.getBoolean(ICVSUIConstants.PREF_CONSOLE_WRAP)) { 148 setConsoleWidth(store.getInt(ICVSUIConstants.PREF_CONSOLE_WIDTH)); 149 } else { 150 setConsoleWidth(-1); 151 } 152 } 153 154 private void initLimitOutput() { 155 IPreferenceStore store = CVSUIPlugin.getPlugin().getPreferenceStore(); 156 if(store.getBoolean(ICVSUIConstants.PREF_CONSOLE_LIMIT_OUTPUT)) { 157 setWaterMarks(1000, store.getInt(ICVSUIConstants.PREF_CONSOLE_HIGH_WATER_MARK)); 158 } else { 159 setWaterMarks(-1, 0); 160 } 161 } 162 163 167 private void initializeStreams() { 168 synchronized(document) { 169 if (!initialized) { 170 commandStream = newMessageStream(); 171 errorStream = newMessageStream(); 172 messageStream = newMessageStream(); 173 commandColor = createColor(CVSUIPlugin.getStandardDisplay(), ICVSUIConstants.PREF_CONSOLE_COMMAND_COLOR); 175 commandStream.setColor(commandColor); 176 messageColor = createColor(CVSUIPlugin.getStandardDisplay(), ICVSUIConstants.PREF_CONSOLE_MESSAGE_COLOR); 177 messageStream.setColor(messageColor); 178 errorColor = createColor(CVSUIPlugin.getStandardDisplay(), ICVSUIConstants.PREF_CONSOLE_ERROR_COLOR); 179 errorStream.setColor(errorColor); 180 Font f = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getFontRegistry().get(ICVSUIConstants.PREF_CONSOLE_FONT); 182 setFont(f); 183 initialized = true; 184 } 185 } 186 } 187 188 private void dump() { 189 synchronized(document) { 190 visible = true; 191 ConsoleDocument.ConsoleLine[] lines = document.getLines(); 192 for (int i = 0; i < lines.length; i++) { 193 ConsoleDocument.ConsoleLine line = lines[i]; 194 appendLine(line.type, line.line); 195 } 196 document.clear(); 197 } 198 } 199 200 private void appendLine(int type, String line) { 201 showConsole(); 202 synchronized(document) { 203 if(visible) { 204 switch(type) { 205 case ConsoleDocument.COMMAND: 206 commandStream.println(line); 207 break; 208 case ConsoleDocument.MESSAGE: 209 messageStream.println(" " + line); break; 211 case ConsoleDocument.ERROR: 212 errorStream.println(" " + line); break; 214 } 215 } else { 216 document.appendConsoleLine(type, line); 217 } 218 } 219 } 220 221 private void showConsole() { 222 show(false); 223 } 224 225 228 protected void dispose() { 229 233 synchronized (document) { 235 visible = false; 236 JFaceResources.getFontRegistry().removeListener(this); 237 } 238 } 239 240 243 public void shutdown() { 244 super.dispose(); 247 if (commandColor != null) 248 commandColor.dispose(); 249 if (messageColor != null) 250 messageColor.dispose(); 251 if (errorColor != null) 252 errorColor.dispose(); 253 CVSUIPlugin.getPlugin().getPreferenceStore().removePropertyChangeListener(this); 254 } 255 256 259 public void commandInvoked(Session session, String line) { 260 if (!session.isOutputToConsole()) return; 261 commandStarted = System.currentTimeMillis(); 262 appendLine(ConsoleDocument.COMMAND, CVSUIMessages.Console_preExecutionDelimiter); 263 appendLine(ConsoleDocument.COMMAND, line); 264 } 265 266 269 public void messageLineReceived(Session session, String line, IStatus status) { 270 if (session.isOutputToConsole()) { 271 appendLine(ConsoleDocument.MESSAGE, " " + line); } 273 } 274 275 278 public void errorLineReceived(Session session, String line, IStatus status) { 279 if (session.isOutputToConsole()) { 280 appendLine(ConsoleDocument.ERROR, " " + line); } 282 } 283 284 287 public void commandCompleted(Session session, IStatus status, Exception exception) { 288 if (!session.isOutputToConsole()) return; 289 long commandRuntime = System.currentTimeMillis() - commandStarted; 290 String time; 291 try { 292 time = TIME_FORMAT.format(new Date (commandRuntime)); 293 } catch (RuntimeException e) { 294 CVSUIPlugin.log(IStatus.ERROR, CVSUIMessages.Console_couldNotFormatTime, e); 295 time = ""; } 297 String statusText; 298 if (status != null) { 299 boolean includeRoot = true; 300 if (status.getCode() == CVSStatus.SERVER_ERROR) { 301 statusText = NLS.bind(CVSUIMessages.Console_resultServerError, new String [] { status.getMessage(), time }); 302 includeRoot = false; 303 } else { 304 statusText = NLS.bind(CVSUIMessages.Console_resultOk, new String [] { time }); 305 } 306 appendLine(ConsoleDocument.COMMAND, statusText); 307 outputStatus(status, includeRoot, includeRoot ? 0 : 1); 308 } else if (exception != null) { 309 if (exception instanceof OperationCanceledException) { 310 statusText = NLS.bind(CVSUIMessages.Console_resultAborted, new String [] { time }); 311 } else { 312 statusText = NLS.bind(CVSUIMessages.Console_resultException, new String [] { time }); 313 } 314 appendLine(ConsoleDocument.COMMAND, statusText); 315 if (exception instanceof CoreException) { 316 outputStatus(((CoreException)exception).getStatus(), true, 1); 317 } 318 } else { 319 statusText = NLS.bind(CVSUIMessages.Console_resultOk, new String [] { time }); 320 } 321 appendLine(ConsoleDocument.COMMAND, CVSUIMessages.Console_postExecutionDelimiter); 322 appendLine(ConsoleDocument.COMMAND, ""); } 324 325 private void outputStatus(IStatus status, boolean includeParent, int nestingLevel) { 326 if (includeParent && !status.isOK()) { 327 outputStatusMessage(status, nestingLevel); 328 nestingLevel++; 329 } 330 331 Throwable t = status.getException(); 333 if (t instanceof CoreException) { 334 outputStatus(((CoreException)t).getStatus(), true, nestingLevel); 335 } 336 337 IStatus[] children = status.getChildren(); 339 for (int i = 0; i < children.length; i++) { 340 outputStatus(children[i], true, nestingLevel); 341 } 342 } 343 344 private void outputStatusMessage(IStatus status, int nesting) { 345 StringBuffer buffer = new StringBuffer (); 346 for (int i = 0; i < nesting; i++) { 347 buffer.append(NESTING); 348 } 349 buffer.append(messageLineForStatus(status)); 350 appendLine(ConsoleDocument.COMMAND, buffer.toString()); 351 } 352 353 356 public void propertyChange(PropertyChangeEvent event) { 357 String property = event.getProperty(); 358 if (visible) { 360 if (property.equals(ICVSUIConstants.PREF_CONSOLE_COMMAND_COLOR)) { 361 Color newColor = createColor(CVSUIPlugin.getStandardDisplay(), ICVSUIConstants.PREF_CONSOLE_COMMAND_COLOR); 362 commandStream.setColor(newColor); 363 commandColor.dispose(); 364 commandColor = newColor; 365 } else if (property.equals(ICVSUIConstants.PREF_CONSOLE_MESSAGE_COLOR)) { 366 Color newColor = createColor(CVSUIPlugin.getStandardDisplay(), ICVSUIConstants.PREF_CONSOLE_MESSAGE_COLOR); 367 messageStream.setColor(newColor); 368 messageColor.dispose(); 369 messageColor = newColor; 370 } else if (property.equals(ICVSUIConstants.PREF_CONSOLE_ERROR_COLOR)) { 371 Color newColor = createColor(CVSUIPlugin.getStandardDisplay(), ICVSUIConstants.PREF_CONSOLE_ERROR_COLOR); 372 errorStream.setColor(newColor); 373 errorColor.dispose(); 374 errorColor = newColor; 375 } else if (property.equals(ICVSUIConstants.PREF_CONSOLE_FONT)) { 377 setFont(((FontRegistry) event.getSource()).get(ICVSUIConstants.PREF_CONSOLE_FONT)); 378 } 379 } 380 if (property.equals(ICVSUIConstants.PREF_CONSOLE_SHOW_ON_MESSAGE)) { 381 Object value = event.getNewValue(); 382 if (value instanceof String ) { 383 showOnMessage = Boolean.valueOf((String ) value).booleanValue(); 384 } else { 385 showOnMessage = ((Boolean ) value).booleanValue(); 386 } 387 } else if(property.equals(ICVSUIConstants.PREF_CONSOLE_LIMIT_OUTPUT)) { 388 initLimitOutput(); 389 } else if(property.equals(ICVSUIConstants.PREF_CONSOLE_WRAP)) { 390 initWrapSetting(); 391 } 392 } 393 394 401 private String messageLineForStatus(IStatus status) { 402 if (status.getSeverity() == IStatus.ERROR) { 403 return NLS.bind(CVSUIMessages.Console_error, new String [] { status.getMessage() }); 404 } else if (status.getSeverity() == IStatus.WARNING) { 405 return NLS.bind(CVSUIMessages.Console_warning, new String [] { status.getMessage() }); 406 } else if (status.getSeverity() == IStatus.INFO) { 407 return NLS.bind(CVSUIMessages.Console_info, new String [] { status.getMessage() }); 408 } 409 return status.getMessage(); 410 } 411 412 415 private Color createColor(Display display, String preference) { 416 RGB rgb = PreferenceConverter.getColor(CVSUIPlugin.getPlugin().getPreferenceStore(), preference); 417 return new Color(display, rgb); 418 } 419 420 424 public void show(boolean showNoMatterWhat) { 425 if(showNoMatterWhat || showOnMessage) { 426 if(!visible) 427 CVSConsoleFactory.showConsole(); 428 else 429 consoleManager.showConsoleView(this); 430 } 431 } 432 433 public String getHelpContextId() { 434 return IHelpContextIds.CONSOLE_VIEW; 435 } 436 } 437 | Popular Tags |