KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > console > ConsoleManager


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.ui.internal.console;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.regex.PatternSyntaxException JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExtensionPoint;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.ISafeRunnable;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.ListenerList;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.core.runtime.SafeRunner;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.core.runtime.jobs.Job;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.ui.IViewPart;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.PartInitException;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.console.ConsolePlugin;
38 import org.eclipse.ui.console.IConsole;
39 import org.eclipse.ui.console.IConsoleConstants;
40 import org.eclipse.ui.console.IConsoleListener;
41 import org.eclipse.ui.console.IConsoleManager;
42 import org.eclipse.ui.console.IConsolePageParticipant;
43 import org.eclipse.ui.console.IConsoleView;
44 import org.eclipse.ui.console.IPatternMatchListener;
45 import org.eclipse.ui.console.TextConsole;
46 import org.eclipse.ui.progress.WorkbenchJob;
47
48 /**
49  * The singleton console manager.
50  *
51  * @since 3.0
52  */

53 public class ConsoleManager implements IConsoleManager {
54     
55     /**
56      * Console listeners
57      */

58     private ListenerList fListeners = null;
59     
60     /**
61      * List of registered consoles
62      */

63     private List JavaDoc fConsoles = new ArrayList JavaDoc(10);
64
65     
66     // change notification constants
67
private final static int ADDED = 1;
68     private final static int REMOVED = 2;
69
70     private List JavaDoc fPatternMatchListeners;
71
72     private List JavaDoc fPageParticipants;
73
74     private List JavaDoc fConsoleFactoryExtensions;
75     
76     private List JavaDoc fConsoleViews = new ArrayList JavaDoc();
77     
78     private boolean fWarnQueued = false;
79     
80     private RepaintJob fRepaintJob = new RepaintJob();
81     
82     private class RepaintJob extends WorkbenchJob {
83         private Set JavaDoc list = new HashSet JavaDoc();
84
85         public RepaintJob() {
86             super("schedule redraw() of viewers"); //$NON-NLS-1$
87
setSystem(true);
88         }
89         
90         void addConsole(IConsole console) {
91             synchronized (list) {
92                 list.add(console);
93             }
94         }
95         
96         public IStatus runInUIThread(IProgressMonitor monitor) {
97             synchronized (list) {
98                 if (list.isEmpty()) {
99                     return Status.OK_STATUS;
100                 }
101                 
102                 IWorkbenchWindow[] workbenchWindows = PlatformUI.getWorkbench().getWorkbenchWindows();
103                 for (int i = 0; i < workbenchWindows.length; i++) {
104                     IWorkbenchWindow window = workbenchWindows[i];
105                     if (window != null) {
106                         IWorkbenchPage page = window.getActivePage();
107                         if (page != null) {
108                             IViewPart part = page.findView(IConsoleConstants.ID_CONSOLE_VIEW);
109                             if (part != null && part instanceof IConsoleView) {
110                                 ConsoleView view = (ConsoleView) part;
111                                 if (list.contains(view.getConsole())) {
112                                     Control control = view.getCurrentPage().getControl();
113                                     if (!control.isDisposed()) {
114                                         control.redraw();
115                                     }
116                                 }
117                             }
118
119                         }
120                     }
121                 }
122                 list.clear();
123             }
124             return Status.OK_STATUS;
125         }
126     }
127     
128     /**
129      * Notifies a console listener of additions or removals
130      */

131     class ConsoleNotifier implements ISafeRunnable {
132         
133         private IConsoleListener fListener;
134         private int fType;
135         private IConsole[] fChanged;
136         
137         /* (non-Javadoc)
138          * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
139          */

140         public void handleException(Throwable JavaDoc exception) {
141             IStatus status = new Status(IStatus.ERROR, ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.INTERNAL_ERROR, ConsoleMessages.ConsoleManager_0, exception);
142             ConsolePlugin.log(status);
143         }
144
145         /* (non-Javadoc)
146          * @see org.eclipse.core.runtime.ISafeRunnable#run()
147          */

148         public void run() throws Exception JavaDoc {
149             switch (fType) {
150                 case ADDED:
151                     fListener.consolesAdded(fChanged);
152                     break;
153                 case REMOVED:
154                     fListener.consolesRemoved(fChanged);
155                     break;
156             }
157         }
158
159         /**
160          * Notifies the given listener of the adds/removes
161          *
162          * @param consoles the consoles that changed
163          * @param update the type of change
164          */

165         public void notify(IConsole[] consoles, int update) {
166             if (fListeners == null) {
167                 return;
168             }
169             fChanged = consoles;
170             fType = update;
171             Object JavaDoc[] copiedListeners= fListeners.getListeners();
172             for (int i= 0; i < copiedListeners.length; i++) {
173                 fListener = (IConsoleListener)copiedListeners[i];
174                 SafeRunner.run(this);
175             }
176             fChanged = null;
177             fListener = null;
178         }
179     }
180         
181     public void registerConsoleView(ConsoleView view) {
182         synchronized (fConsoleViews) {
183             fConsoleViews.add(view);
184         }
185     }
186     public void unregisterConsoleView(ConsoleView view) {
187         synchronized (fConsoleViews) {
188             fConsoleViews.remove(view);
189         }
190     }
191     
192     /* (non-Javadoc)
193      * @see org.eclipse.ui.console.IConsoleManager#addConsoleListener(org.eclipse.ui.console.IConsoleListener)
194      */

195     public void addConsoleListener(IConsoleListener listener) {
196         if (fListeners == null) {
197             fListeners = new ListenerList();
198         }
199         fListeners.add(listener);
200     }
201
202     /* (non-Javadoc)
203      * @see org.eclipse.ui.console.IConsoleManager#removeConsoleListener(org.eclipse.ui.console.IConsoleListener)
204      */

205     public void removeConsoleListener(IConsoleListener listener) {
206         if (fListeners != null) {
207             fListeners.remove(listener);
208         }
209     }
210
211     /* (non-Javadoc)
212      * @see org.eclipse.ui.console.IConsoleManager#addConsoles(org.eclipse.ui.console.IConsole[])
213      */

214     public synchronized void addConsoles(IConsole[] consoles) {
215         List JavaDoc added = new ArrayList JavaDoc(consoles.length);
216         for (int i = 0; i < consoles.length; i++) {
217             IConsole console = consoles[i];
218             if(console instanceof TextConsole) {
219                 TextConsole ioconsole = (TextConsole)console;
220                 createPatternMatchListeners(ioconsole);
221             }
222             if (!fConsoles.contains(console)) {
223                 fConsoles.add(console);
224                 added.add(console);
225             }
226         }
227         if (!added.isEmpty()) {
228             fireUpdate((IConsole[])added.toArray(new IConsole[added.size()]), ADDED);
229         }
230     }
231
232     /* (non-Javadoc)
233      * @see org.eclipse.ui.console.IConsoleManager#removeConsoles(org.eclipse.ui.console.IConsole[])
234      */

235     public synchronized void removeConsoles(IConsole[] consoles) {
236         List JavaDoc removed = new ArrayList JavaDoc(consoles.length);
237         for (int i = 0; i < consoles.length; i++) {
238             IConsole console = consoles[i];
239             if (fConsoles.remove(console)) {
240                 removed.add(console);
241             }
242         }
243         if (!removed.isEmpty()) {
244             fireUpdate((IConsole[])removed.toArray(new IConsole[removed.size()]), REMOVED);
245         }
246     }
247
248     /* (non-Javadoc)
249      * @see org.eclipse.ui.console.IConsoleManager#getConsoles()
250      */

251     public synchronized IConsole[] getConsoles() {
252         return (IConsole[])fConsoles.toArray(new IConsole[fConsoles.size()]);
253     }
254
255     /**
256      * Fires notification.
257      *
258      * @param consoles consoles added/removed
259      * @param type ADD or REMOVE
260      */

261     private void fireUpdate(IConsole[] consoles, int type) {
262         new ConsoleNotifier().notify(consoles, type);
263     }
264     
265     
266     private class ShowConsoleViewJob extends WorkbenchJob {
267         private IConsole console;
268         
269         ShowConsoleViewJob() {
270             super("Show Console View"); //$NON-NLS-1$
271
setSystem(true);
272             setPriority(Job.SHORT);
273         }
274         
275         void setConsole(IConsole console) {
276             this.console = console;
277         }
278         
279         public IStatus runInUIThread(IProgressMonitor monitor) {
280             boolean consoleFound = false;
281             IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
282             if (window != null && console != null) {
283                 IWorkbenchPage page= window.getActivePage();
284                 if (page != null) {
285                     synchronized (fConsoleViews) {
286                         for (Iterator JavaDoc iter = fConsoleViews.iterator(); iter.hasNext();) {
287                             ConsoleView consoleView = (ConsoleView) iter.next();
288                             if (consoleView.getSite().getPage().equals(page)) {
289                                 boolean consoleVisible = page.isPartVisible(consoleView);
290                                 if (consoleVisible) {
291                                     consoleFound = true;
292                                     boolean bringToTop = shouldBringToTop(console, consoleView);
293                                     if (bringToTop) {
294                                         page.bringToTop(consoleView);
295                                     }
296                                     consoleView.display(console);
297                                 }
298                             }
299                         }
300                     }
301                     
302                     if (!consoleFound) {
303                         try {
304                             IConsoleView consoleView = (IConsoleView) page.showView(IConsoleConstants.ID_CONSOLE_VIEW, null, IWorkbenchPage.VIEW_CREATE);
305                             boolean bringToTop = shouldBringToTop(console, consoleView);
306                             if (bringToTop) {
307                                 page.bringToTop(consoleView);
308                             }
309                             consoleView.display(console);
310                         } catch (PartInitException pie) {
311                             ConsolePlugin.log(pie);
312                         }
313                     }
314                 }
315             }
316             console = null;
317             return Status.OK_STATUS;
318         }
319     }
320     
321     private ShowConsoleViewJob showJob = new ShowConsoleViewJob();
322     /**
323      * @see IConsoleManager#showConsoleView(IConsole)
324      */

325     public void showConsoleView(final IConsole console) {
326         showJob.setConsole(console);
327         showJob.schedule(100);
328     }
329     
330     /**
331      * Returns whether the given console view should be brought to the top.
332      * The view should not be brought to the top if the view is pinned on
333      * a console other than the given console.
334      */

335     private boolean shouldBringToTop(IConsole console, IViewPart consoleView) {
336         boolean bringToTop= true;
337         if (consoleView instanceof IConsoleView) {
338             IConsoleView cView= (IConsoleView)consoleView;
339             if (cView.isPinned()) {
340                 IConsole pinnedConsole= cView.getConsole();
341                 bringToTop = console.equals(pinnedConsole);
342             }
343         }
344         return bringToTop;
345     }
346     
347     /* (non-Javadoc)
348      * @see org.eclipse.ui.console.IConsoleManager#warnOfContentChange(org.eclipse.ui.console.IConsole)
349      */

350     public void warnOfContentChange(final IConsole console) {
351         if (!fWarnQueued) {
352             fWarnQueued = true;
353             ConsolePlugin.getStandardDisplay().asyncExec(new Runnable JavaDoc(){
354                 public void run() {
355                     IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
356                     if (window != null) {
357                         IWorkbenchPage page= window.getActivePage();
358                         if (page != null) {
359                             IConsoleView consoleView= (IConsoleView)page.findView(IConsoleConstants.ID_CONSOLE_VIEW);
360                             if (consoleView != null) {
361                                 consoleView.warnOfContentChange(console);
362                             }
363                         }
364                     }
365                     fWarnQueued = false;
366                 }
367             });
368         }
369     }
370
371     /* (non-Javadoc)
372      * @see org.eclipse.ui.console.IConsoleManager#getPatternMatchListenerDelegates(org.eclipse.ui.console.IConsole)
373      */

374     public IPatternMatchListener[] createPatternMatchListeners(IConsole console) {
375             if (fPatternMatchListeners == null) {
376                 fPatternMatchListeners = new ArrayList JavaDoc();
377                 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.EXTENSION_POINT_CONSOLE_PATTERN_MATCH_LISTENERS);
378                 IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
379                 for (int i = 0; i < elements.length; i++) {
380                     IConfigurationElement config = elements[i];
381                     PatternMatchListenerExtension extension = new PatternMatchListenerExtension(config);
382                     fPatternMatchListeners.add(extension);
383                 }
384             }
385             ArrayList JavaDoc list = new ArrayList JavaDoc();
386             for(Iterator JavaDoc i = fPatternMatchListeners.iterator(); i.hasNext(); ) {
387                 PatternMatchListenerExtension extension = (PatternMatchListenerExtension) i.next();
388                 try {
389                     if (extension.getEnablementExpression() == null) {
390                         i.remove();
391                         continue;
392                     }
393                 
394                     if (console instanceof TextConsole && extension.isEnabledFor(console)) {
395                         TextConsole textConsole = (TextConsole) console;
396                         PatternMatchListener patternMatchListener = new PatternMatchListener(extension);
397                         try {
398                             textConsole.addPatternMatchListener(patternMatchListener);
399                             list.add(patternMatchListener);
400                         } catch (PatternSyntaxException JavaDoc e) {
401                             ConsolePlugin.log(e);
402                             i.remove();
403                         }
404                     }
405                 } catch (CoreException e) {
406                     ConsolePlugin.log(e);
407                 }
408             }
409         return (PatternMatchListener[])list.toArray(new PatternMatchListener[0]);
410     }
411
412     /* (non-Javadoc)
413      * @see org.eclipse.ui.console.IConsoleManager#getPageParticipants(org.eclipse.ui.console.IConsole)
414      */

415     public IConsolePageParticipant[] getPageParticipants(IConsole console) {
416         if(fPageParticipants == null) {
417             fPageParticipants = new ArrayList JavaDoc();
418             IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.EXTENSION_POINT_CONSOLE_PAGE_PARTICIPANTS);
419             IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
420             for(int i = 0; i < elements.length; i++) {
421                 IConfigurationElement config = elements[i];
422                 ConsolePageParticipantExtension extension = new ConsolePageParticipantExtension(config);
423                 fPageParticipants.add(extension);
424             }
425         }
426         ArrayList JavaDoc list = new ArrayList JavaDoc();
427         for(Iterator JavaDoc i = fPageParticipants.iterator(); i.hasNext(); ) {
428             ConsolePageParticipantExtension extension = (ConsolePageParticipantExtension) i.next();
429             try {
430                 if (extension.isEnabledFor(console)) {
431                     list.add(extension.createDelegate());
432                 }
433             } catch (CoreException e) {
434                 ConsolePlugin.log(e);
435             }
436         }
437         return (IConsolePageParticipant[]) list.toArray(new IConsolePageParticipant[0]);
438     }
439
440     /* (non-Javadoc)
441      * @see org.eclipse.ui.console.IConsoleManager#getConsoleFactories()
442      */

443     public ConsoleFactoryExtension[] getConsoleFactoryExtensions() {
444         if (fConsoleFactoryExtensions == null) {
445             fConsoleFactoryExtensions = new ArrayList JavaDoc();
446             IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.EXTENSION_POINT_CONSOLE_FACTORIES);
447             IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements();
448             for (int i = 0; i < configurationElements.length; i++) {
449                 fConsoleFactoryExtensions.add(new ConsoleFactoryExtension(configurationElements[i]));
450             }
451         }
452         return (ConsoleFactoryExtension[]) fConsoleFactoryExtensions.toArray(new ConsoleFactoryExtension[0]);
453     }
454     
455     
456     public void refresh(final IConsole console) {
457         fRepaintJob.addConsole(console);
458         fRepaintJob.schedule(50);
459     }
460
461 }
462
Popular Tags