Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1ef4b98309cc2ee7eeb1257710d9f41ee8aef123 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Andrey Loskutov <loskutov@gmx.de> - bug 489546
 *******************************************************************************/
package org.eclipse.ui.internal.console;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.PatternSyntaxException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleListener;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IConsolePageParticipant;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.IPatternMatchListener;
import org.eclipse.ui.console.TextConsole;
import org.eclipse.ui.progress.UIJob;
import org.eclipse.ui.progress.WorkbenchJob;

/**
 * The singleton console manager.
 *
 * @since 3.0
 */
public class ConsoleManager implements IConsoleManager {

	/**
	 * Console listeners
	 */
	private ListenerList<IConsoleListener> fListeners = null;

	/**
	 * List of registered consoles
	 */
	private List<IConsole> fConsoles = new ArrayList<IConsole>(10);


	// change notification constants
	private final static int ADDED = 1;
	private final static int REMOVED = 2;

	private List<PatternMatchListenerExtension> fPatternMatchListeners;

	private List<ConsolePageParticipantExtension> fPageParticipants;

	private List<ConsoleFactoryExtension> fConsoleFactoryExtensions;

	private List<IConsoleView> fConsoleViews = new ArrayList<IConsoleView>();

    private boolean fWarnQueued = false;

    private RepaintJob fRepaintJob = new RepaintJob();

    private class RepaintJob extends WorkbenchJob {
		private Set<IConsole> list = new HashSet<IConsole>();

        public RepaintJob() {
            super("schedule redraw() of viewers"); //$NON-NLS-1$
            setSystem(true);
        }

        void addConsole(IConsole console) {
        	synchronized (list) {
        		list.add(console);
			}
        }

        @Override
		public IStatus runInUIThread(IProgressMonitor monitor) {
            synchronized (list) {
                if (list.isEmpty()) {
                    return Status.OK_STATUS;
                }

                IWorkbenchWindow[] workbenchWindows = PlatformUI.getWorkbench().getWorkbenchWindows();
                for (int i = 0; i < workbenchWindows.length; i++) {
                    IWorkbenchWindow window = workbenchWindows[i];
                    if (window != null) {
                        IWorkbenchPage page = window.getActivePage();
                        if (page != null) {
                            IViewPart part = page.findView(IConsoleConstants.ID_CONSOLE_VIEW);
                            if (part != null && part instanceof IConsoleView) {
                                ConsoleView view = (ConsoleView) part;
                                if (list.contains(view.getConsole())) {
                                    Control control = view.getCurrentPage().getControl();
                                    if (!control.isDisposed()) {
                                        control.redraw();
                                    }
                                }
                            }

                        }
                    }
                }
                list.clear();
            }
            return Status.OK_STATUS;
        }
    }

	/**
	 * Notifies a console listener of additions or removals
	 */
	class ConsoleNotifier implements ISafeRunnable {

		private IConsoleListener fListener;
		private int fType;
		private IConsole[] fChanged;

		/* (non-Javadoc)
		 * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
		 */
		@Override
		public void handleException(Throwable exception) {
			IStatus status = new Status(IStatus.ERROR, ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.INTERNAL_ERROR, ConsoleMessages.ConsoleManager_0, exception);
			ConsolePlugin.log(status);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.core.runtime.ISafeRunnable#run()
		 */
		@Override
		public void run() throws Exception {
			switch (fType) {
				case ADDED:
					fListener.consolesAdded(fChanged);
					break;
				case REMOVED:
					fListener.consolesRemoved(fChanged);
					break;
				default:
					break;
			}
		}

		/**
		 * Notifies the given listener of the adds/removes
		 *
		 * @param consoles the consoles that changed
		 * @param update the type of change
		 */
		public void notify(IConsole[] consoles, int update) {
			if (fListeners == null) {
				return;
			}
			fChanged = consoles;
			fType = update;
			for (IConsoleListener iConsoleListener : fListeners) {
				fListener = iConsoleListener;
                SafeRunner.run(this);
			}
			fChanged = null;
			fListener = null;
		}
	}

	public void registerConsoleView(ConsoleView view) {
	    synchronized (fConsoleViews) {
	        fConsoleViews.add(view);
	    }
	}
    public void unregisterConsoleView(ConsoleView view) {
        synchronized (fConsoleViews) {
            fConsoleViews.remove(view);
        }
    }

    /* (non-Javadoc)
	 * @see org.eclipse.ui.console.IConsoleManager#addConsoleListener(org.eclipse.ui.console.IConsoleListener)
	 */
	@Override
	public void addConsoleListener(IConsoleListener listener) {
		if (fListeners == null) {
			fListeners = new ListenerList<>();
		}
		fListeners.add(listener);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.console.IConsoleManager#removeConsoleListener(org.eclipse.ui.console.IConsoleListener)
	 */
	@Override
	public void removeConsoleListener(IConsoleListener listener) {
		if (fListeners != null) {
			fListeners.remove(listener);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.console.IConsoleManager#addConsoles(org.eclipse.ui.console.IConsole[])
	 */
	@Override
	public void addConsoles(IConsole[] consoles) {
		List<IConsole> added = new ArrayList<IConsole>(consoles.length);
		synchronized (fConsoles) {
			for (int i = 0; i < consoles.length; i++) {
			    IConsole console = consoles[i];
			    if(console instanceof TextConsole) {
			        TextConsole ioconsole = (TextConsole)console;
			        createPatternMatchListeners(ioconsole);
			    }
				if (!fConsoles.contains(console)) {
					fConsoles.add(console);
					added.add(console);
				}
			}
		}
		if (!added.isEmpty()) {
			fireUpdate(added.toArray(new IConsole[added.size()]), ADDED);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.console.IConsoleManager#removeConsoles(org.eclipse.ui.console.IConsole[])
	 */
	@Override
	public void removeConsoles(IConsole[] consoles) {
		List<IConsole> removed = new ArrayList<IConsole>(consoles.length);
		synchronized (fConsoles) {
			for (int i = 0; i < consoles.length; i++) {
				IConsole console = consoles[i];
				if (fConsoles.remove(console)) {
					removed.add(console);
				}
			}
		}
		if (!removed.isEmpty()) {
			fireUpdate(removed.toArray(new IConsole[removed.size()]), REMOVED);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.console.IConsoleManager#getConsoles()
	 */
	@Override
	public IConsole[] getConsoles() {
		synchronized (fConsoles) {
			return fConsoles.toArray(new IConsole[fConsoles.size()]);
		}
	}

	/**
	 * Fires notification.
	 *
	 * @param consoles consoles added/removed
	 * @param type ADD or REMOVE
	 */
	private void fireUpdate(IConsole[] consoles, int type) {
		new ConsoleNotifier().notify(consoles, type);
	}


	private class ShowConsoleViewJob extends WorkbenchJob {
		private Set<IConsole> queue = new LinkedHashSet<IConsole>();

		ShowConsoleViewJob() {
			super("Show Console View"); //$NON-NLS-1$
			setSystem(true);
			setPriority(Job.SHORT);
		}

		void addConsole(IConsole console) {
			synchronized (queue) {
				queue.add(console);
			}
		}

		@Override
		public IStatus runInUIThread(IProgressMonitor monitor) {
			Set<IConsole> consolesToShow;
			synchronized (queue) {
				consolesToShow = new LinkedHashSet<>(queue);
				queue.clear();
			}
			for (IConsole c : consolesToShow) {
				showConsole(c);
			}
			synchronized (queue) {
				if (!queue.isEmpty()) {
					schedule();
				}
			}
			return Status.OK_STATUS;
		}

		private void showConsole(IConsole c) {
			boolean consoleFound = false;
			IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
			if (window != null && c != null) {
				IWorkbenchPage page = window.getActivePage();
				if (page != null) {
					synchronized (fConsoleViews) {
						for (IConsoleView consoleView : fConsoleViews) {
							if (consoleView.getSite().getPage().equals(page)) {
								boolean consoleVisible = page.isPartVisible(consoleView);
								if (consoleVisible) {
									consoleFound = true;
									boolean bringToTop = shouldBringToTop(c, consoleView);
									if (bringToTop) {
										page.bringToTop(consoleView);
									}
									consoleView.display(c);
								}
							}
						}
					}

					if (!consoleFound) {
						try {
							IConsoleView consoleView = (IConsoleView) page.showView(IConsoleConstants.ID_CONSOLE_VIEW, null, IWorkbenchPage.VIEW_CREATE);
							boolean bringToTop = shouldBringToTop(c, consoleView);
							if (bringToTop) {
								page.bringToTop(consoleView);
							}
							consoleView.display(c);
						} catch (PartInitException pie) {
							ConsolePlugin.log(pie);
						}
					}
				}
			}
		}
	}

	private ShowConsoleViewJob showJob = new ShowConsoleViewJob();
	/**
	 * @see IConsoleManager#showConsoleView(IConsole)
	 */
	@Override
	public void showConsoleView(final IConsole console) {
		showJob.addConsole(console);
		showJob.schedule(100);
	}

	/**
	 * Returns whether the given console view should be brought to the top.
	 * The view should not be brought to the top if the view is pinned on
	 * a console other than the given console.
	 */
	private boolean shouldBringToTop(IConsole console, IViewPart consoleView) {
		boolean bringToTop= true;
		if (consoleView instanceof IConsoleView) {
			IConsoleView cView= (IConsoleView)consoleView;
			if (cView.isPinned()) {
				IConsole pinnedConsole= cView.getConsole();
				bringToTop = console.equals(pinnedConsole);
			}
		}
		return bringToTop;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.console.IConsoleManager#warnOfContentChange(org.eclipse.ui.console.IConsole)
	 */
	@Override
	public void warnOfContentChange(final IConsole console) {
		if (!fWarnQueued) {
			fWarnQueued = true;
			Job job = new UIJob(ConsolePlugin.getStandardDisplay(), ConsoleMessages.ConsoleManager_consoleContentChangeJob) {
				@Override
				public IStatus runInUIThread(IProgressMonitor monitor) {
					IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
					if (window != null) {
						IWorkbenchPage page= window.getActivePage();
						if (page != null) {
							IConsoleView consoleView= (IConsoleView)page.findView(IConsoleConstants.ID_CONSOLE_VIEW);
							if (consoleView != null) {
								consoleView.warnOfContentChange(console);
							}
						}
					}
					fWarnQueued = false;
					return Status.OK_STATUS;
				}
			};
			job.setSystem(true);
			job.schedule();
		}
	}

    /* (non-Javadoc)
     * @see org.eclipse.ui.console.IConsoleManager#getPatternMatchListenerDelegates(org.eclipse.ui.console.IConsole)
     */
    @Override
	public IPatternMatchListener[] createPatternMatchListeners(IConsole console) {
    		if (fPatternMatchListeners == null) {
			fPatternMatchListeners = new ArrayList<PatternMatchListenerExtension>();
    			IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.EXTENSION_POINT_CONSOLE_PATTERN_MATCH_LISTENERS);
    			IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
    			for (int i = 0; i < elements.length; i++) {
    				IConfigurationElement config = elements[i];
    				PatternMatchListenerExtension extension = new PatternMatchListenerExtension(config);
    				fPatternMatchListeners.add(extension);
    			}
    		}
		ArrayList<PatternMatchListener> list = new ArrayList<PatternMatchListener>();
		for (Iterator<PatternMatchListenerExtension> i = fPatternMatchListeners.iterator(); i.hasNext();) {
    		    PatternMatchListenerExtension extension = i.next();
                try {
                    if (extension.getEnablementExpression() == null) {
                        i.remove();
                        continue;
                    }

    		        if (console instanceof TextConsole && extension.isEnabledFor(console)) {
                        TextConsole textConsole = (TextConsole) console;
    		            PatternMatchListener patternMatchListener = new PatternMatchListener(extension);
                        try {
                            textConsole.addPatternMatchListener(patternMatchListener);
                            list.add(patternMatchListener);
                        } catch (PatternSyntaxException e) {
                            ConsolePlugin.log(e);
                            i.remove();
                        }
    		        }
    		    } catch (CoreException e) {
    		        ConsolePlugin.log(e);
    		    }
    		}
        return list.toArray(new PatternMatchListener[0]);
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.console.IConsoleManager#getPageParticipants(org.eclipse.ui.console.IConsole)
     */
    public IConsolePageParticipant[] getPageParticipants(IConsole console) {
        if(fPageParticipants == null) {
			fPageParticipants = new ArrayList<ConsolePageParticipantExtension>();
            IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.EXTENSION_POINT_CONSOLE_PAGE_PARTICIPANTS);
            IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
            for(int i = 0; i < elements.length; i++) {
                IConfigurationElement config = elements[i];
                ConsolePageParticipantExtension extension = new ConsolePageParticipantExtension(config);
                fPageParticipants.add(extension);
            }
        }
		ArrayList<IConsolePageParticipant> list = new ArrayList<IConsolePageParticipant>();
		for (Iterator<ConsolePageParticipantExtension> i = fPageParticipants.iterator(); i.hasNext();) {
            ConsolePageParticipantExtension extension = i.next();
            try {
                if (extension.isEnabledFor(console)) {
                    list.add(extension.createDelegate());
                }
            } catch (CoreException e) {
                ConsolePlugin.log(e);
            }
        }
        return list.toArray(new IConsolePageParticipant[0]);
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.console.IConsoleManager#getConsoleFactories()
     */
    public ConsoleFactoryExtension[] getConsoleFactoryExtensions() {
        if (fConsoleFactoryExtensions == null) {
			fConsoleFactoryExtensions = new ArrayList<ConsoleFactoryExtension>();
            IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.EXTENSION_POINT_CONSOLE_FACTORIES);
            IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements();
            for (int i = 0; i < configurationElements.length; i++) {
                fConsoleFactoryExtensions.add(new ConsoleFactoryExtension(configurationElements[i]));
            }
        }
        return fConsoleFactoryExtensions.toArray(new ConsoleFactoryExtension[0]);
    }


    @Override
	public void refresh(final IConsole console) {
        fRepaintJob.addConsole(console);
        fRepaintJob.schedule(50);
    }

}

Back to the top