KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > console > CVSOutputConsole


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc;
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 /**
36  * Console that shows the output of CVS commands. It is shown as a page in the generic
37  * console view. It supports coloring for message, command, and error lines in addition
38  * the font can be configured.
39  *
40  * @since 3.0
41  */

42 public class CVSOutputConsole extends MessageConsole implements IConsoleListener, IPropertyChangeListener {
43     
44     // created colors for each line type - must be disposed at shutdown
45
private Color commandColor;
46     private Color messageColor;
47     private Color errorColor;
48     
49     // used to time the commands
50
private long commandStarted = 0;
51     
52     // streams for each command type - each stream has its own color
53
private MessageConsoleStream commandStream;
54     private MessageConsoleStream messageStream;
55     private MessageConsoleStream errorStream;
56     
57     // preferences for showing the cvs console when cvs output is provided
58
private boolean showOnMessage;
59     
60     private ConsoleDocument document;
61     private IConsoleManager consoleManager;
62     
63     // format for timings printed to console
64
private static final DateFormat TIME_FORMAT;
65     
66     static {
67         DateFormat format;
68         try {
69             format = new SimpleDateFormat(CVSUIMessages.Console_resultTimeFormat);
70         } catch (RuntimeException JavaDoc e) {
71             // This can happen if the bundle contains an invalid format
72
format = new SimpleDateFormat("'(took 'm:ss.SSS')')"); //$NON-NLS-1$
73
}
74         TIME_FORMAT = format;
75     }
76
77     // Indicates whether the console is visible in the Console view
78
private boolean visible = false;
79     // Indicates whether the console's streams have been initialized
80
private boolean initialized = false;
81     
82     /*
83      * Constant used for indenting error status printing
84      */

85     private static final String JavaDoc NESTING = " "; //$NON-NLS-1$
86

87     /**
88      * Used to notify this console of lifecycle methods <code>init()</code>
89      * and <code>dispose()</code>.
90      */

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     /**
113      * Constructor initializes preferences and colors but doesn't create the console
114      * page yet.
115      */

116     public CVSOutputConsole() {
117         super("CVS", CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_CVS_CONSOLE)); //$NON-NLS-1$
118
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     /* (non-Javadoc)
126      * @see org.eclipse.ui.console.AbstractConsole#init()
127      */

128     protected void init() {
129         // Called when console is added to the console view
130
super.init();
131         
132         initLimitOutput();
133         initWrapSetting();
134         
135         // Ensure that initialization occurs in the ui thread
136
CVSUIPlugin.getStandardDisplay().asyncExec(new Runnable JavaDoc() {
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     /*
164      * Initialize thre streams of the console. Must be
165      * called from the UI thread.
166      */

167     private void initializeStreams() {
168         synchronized(document) {
169             if (!initialized) {
170                 commandStream = newMessageStream();
171                 errorStream = newMessageStream();
172                 messageStream = newMessageStream();
173                 // install colors
174
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                 // install font
181
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 JavaDoc 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); //$NON-NLS-1$
210
break;
211                     case ConsoleDocument.ERROR:
212                         errorStream.println(" " + line); //$NON-NLS-1$
213
break;
214                 }
215             } else {
216                 document.appendConsoleLine(type, line);
217             }
218         }
219     }
220
221     private void showConsole() {
222         show(false);
223     }
224     
225     /* (non-Javadoc)
226      * @see org.eclipse.ui.console.MessageConsole#dispose()
227      */

228     protected void dispose() {
229         // Here we can't call super.dispose() because we actually want the partitioner to remain
230
// connected, but we won't show lines until the console is added to the console manager
231
// again.
232

233         // Called when console is removed from the console view
234
synchronized (document) {
235             visible = false;
236             JFaceResources.getFontRegistry().removeListener(this);
237         }
238     }
239     
240     /**
241      * Clean-up created fonts.
242      */

243     public void shutdown() {
244         // Call super dispose because we want the partitioner to be
245
// disconnected.
246
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     /* (non-Javadoc)
257      * @see org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener#commandInvoked(java.lang.String)
258      */

259     public void commandInvoked(Session session, String JavaDoc 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     /* (non-Javadoc)
267      * @see org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener#messageLineReceived(java.lang.String)
268      */

269     public void messageLineReceived(Session session, String JavaDoc line, IStatus status) {
270         if (session.isOutputToConsole()) {
271             appendLine(ConsoleDocument.MESSAGE, " " + line); //$NON-NLS-1$
272
}
273     }
274
275     /* (non-Javadoc)
276      * @see org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener#errorLineReceived(java.lang.String)
277      */

278     public void errorLineReceived(Session session, String JavaDoc line, IStatus status) {
279         if (session.isOutputToConsole()) {
280             appendLine(ConsoleDocument.ERROR, " " + line); //$NON-NLS-1$
281
}
282     }
283     
284     /* (non-Javadoc)
285      * @see org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener#commandCompleted(org.eclipse.core.runtime.IStatus, java.lang.Exception)
286      */

287     public void commandCompleted(Session session, IStatus status, Exception JavaDoc exception) {
288         if (!session.isOutputToConsole()) return;
289         long commandRuntime = System.currentTimeMillis() - commandStarted;
290         String JavaDoc time;
291         try {
292             time = TIME_FORMAT.format(new Date JavaDoc(commandRuntime));
293         } catch (RuntimeException JavaDoc e) {
294             CVSUIPlugin.log(IStatus.ERROR, CVSUIMessages.Console_couldNotFormatTime, e);
295             time = ""; //$NON-NLS-1$
296
}
297         String JavaDoc 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 JavaDoc[] { status.getMessage(), time });
302                 includeRoot = false;
303             } else {
304                 statusText = NLS.bind(CVSUIMessages.Console_resultOk, new String JavaDoc[] { 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 JavaDoc[] { time });
311             } else {
312                 statusText = NLS.bind(CVSUIMessages.Console_resultException, new String JavaDoc[] { 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 JavaDoc[] { time });
320         }
321         appendLine(ConsoleDocument.COMMAND, CVSUIMessages.Console_postExecutionDelimiter);
322         appendLine(ConsoleDocument.COMMAND, ""); //$NON-NLS-1$
323
}
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         // Include a CoreException in the status
332
Throwable JavaDoc t = status.getException();
333         if (t instanceof CoreException) {
334             outputStatus(((CoreException)t).getStatus(), true, nestingLevel);
335         }
336         
337         // Include child status
338
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 JavaDoc buffer = new StringBuffer JavaDoc();
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     /* (non-Javadoc)
354      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
355      */

356     public void propertyChange(PropertyChangeEvent event) {
357         String JavaDoc property = event.getProperty();
358         // colors
359
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                 // font
376
} 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 JavaDoc value = event.getNewValue();
382             if (value instanceof String JavaDoc) {
383                 showOnMessage = Boolean.valueOf((String JavaDoc) value).booleanValue();
384             } else {
385                 showOnMessage = ((Boolean JavaDoc) 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     /**
395      * Returns the NLSd message based on the status returned from the CVS
396      * command.
397      *
398      * @param status an NLSd message based on the status returned from the CVS
399      * command.
400      */

401     private String JavaDoc messageLineForStatus(IStatus status) {
402         if (status.getSeverity() == IStatus.ERROR) {
403             return NLS.bind(CVSUIMessages.Console_error, new String JavaDoc[] { status.getMessage() });
404         } else if (status.getSeverity() == IStatus.WARNING) {
405             return NLS.bind(CVSUIMessages.Console_warning, new String JavaDoc[] { status.getMessage() });
406         } else if (status.getSeverity() == IStatus.INFO) {
407             return NLS.bind(CVSUIMessages.Console_info, new String JavaDoc[] { status.getMessage() });
408         }
409         return status.getMessage();
410     }
411     
412     /**
413      * Returns a color instance based on data from a preference field.
414      */

415     private Color createColor(Display display, String JavaDoc preference) {
416         RGB rgb = PreferenceConverter.getColor(CVSUIPlugin.getPlugin().getPreferenceStore(), preference);
417         return new Color(display, rgb);
418     }
419
420     /**
421      * Show the console.
422      * @param showNoMatterWhat ignore preferences if <code>true</code>
423      */

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 JavaDoc getHelpContextId() {
434         return IHelpContextIds.CONSOLE_VIEW;
435     }
436 }
437
Popular Tags