KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > client > ConsoleListeners


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.core.client;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener;
20
21 /**
22  * Class which forwards CVS console output to 1 or more
23  * registered console listeners.
24  */

25 public class ConsoleListeners implements IConsoleListener {
26
27     private static ConsoleListeners instance = new ConsoleListeners();
28     private Set JavaDoc listeners = new HashSet JavaDoc();
29     
30     /**
31      * Return the console listeners
32      * @return the console listeners
33      */

34     public static ConsoleListeners getInstance() {
35         return instance;
36     }
37     
38     public void addListener(IConsoleListener listener) {
39         synchronized(listeners) {
40             listeners.add(listener);
41         }
42     }
43     
44     public void removeListener(IConsoleListener listener) {
45         synchronized(listeners) {
46             listeners.remove(listener);
47         }
48     }
49
50     private IConsoleListener[] getListeners() {
51         synchronized(listeners) {
52             return (IConsoleListener[]) listeners.toArray(new IConsoleListener[listeners.size()]);
53         }
54     }
55     
56     /* (non-Javadoc)
57      * @see org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener#commandInvoked(java.lang.String)
58      */

59     public void commandInvoked(final Session session, final String JavaDoc line) {
60         if (listeners.isEmpty()) return;
61         IConsoleListener[] listeners = getListeners();
62         for (int i = 0; i < listeners.length; i++) {
63             final IConsoleListener listener = listeners[i];
64             Platform.run(new ISafeRunnable() {
65                 public void handleException(Throwable JavaDoc exception) {
66                     // Exception logged by Platform
67
}
68                 public void run() throws Exception JavaDoc {
69                     listener.commandInvoked(session, line);
70                 }
71             });
72         }
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener#messageLineReceived(java.lang.String)
77      */

78     public void messageLineReceived(final Session session, final String JavaDoc line, final IStatus status) {
79         if (listeners.isEmpty()) return;
80         IConsoleListener[] listeners = getListeners();
81         for (int i = 0; i < listeners.length; i++) {
82             final IConsoleListener listener = listeners[i];
83             Platform.run(new ISafeRunnable() {
84                 public void handleException(Throwable JavaDoc exception) {
85                     // Exception logged by Platform
86
}
87                 public void run() throws Exception JavaDoc {
88                     listener.messageLineReceived(session, line, status);
89                 }
90             });
91         }
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener#errorLineReceived(java.lang.String)
96      */

97     public void errorLineReceived(final Session session, final String JavaDoc line, final IStatus status) {
98         if (listeners.isEmpty()) return;
99         IConsoleListener[] listeners = getListeners();
100         for (int i = 0; i < listeners.length; i++) {
101             final IConsoleListener listener = listeners[i];
102             Platform.run(new ISafeRunnable() {
103                 public void handleException(Throwable JavaDoc exception) {
104                     // Exception logged by Platform
105
}
106                 public void run() throws Exception JavaDoc {
107                     listener.errorLineReceived(session, line, status);
108                 }
109             });
110         }
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener#commandCompleted(org.eclipse.core.runtime.IStatus, java.lang.Exception)
115      */

116     public void commandCompleted(final Session session, final IStatus status, final Exception JavaDoc exception) {
117         if (listeners.isEmpty()) return;
118         IConsoleListener[] listeners = getListeners();
119         for (int i = 0; i < listeners.length; i++) {
120             final IConsoleListener listener = listeners[i];
121             Platform.run(new ISafeRunnable() {
122                 public void handleException(Throwable JavaDoc exception) {
123                     // Exception logged by Platform
124
}
125                 public void run() throws Exception JavaDoc {
126                     listener.commandCompleted(session, status, exception);
127                 }
128             });
129         }
130     }
131 }
132
Popular Tags