Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fa26b709672602a1425bfa250090ee000b1fd47c (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 Alena Laskavaia 
 * 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:
 *    Alena Laskavaia  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.ui.cxx;

import org.eclipse.cdt.codan.core.CodanRuntime;
import org.eclipse.cdt.codan.core.model.CheckerLaunchMode;
import org.eclipse.cdt.codan.internal.core.CodanBuilder;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * @author Alena Laskavaia
 */
public class Startup implements IStartup {
	private static final IProgressMonitor NULL_PROGRESS_MONITOR = new NullProgressMonitor();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IStartup#earlyStartup()
	 */
	public void earlyStartup() {
		registerListeners();
	}

	/**
	 * Register part listener for editor to install c ast reconcile listener
	 */
	private void registerListeners() {
		final IWorkbench workbench = PlatformUI.getWorkbench();
		workbench.getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow active = workbench.getActiveWorkbenchWindow();
				final IWorkbenchPage page = active.getActivePage();
				IPartListener2 partListener = new IPartListener2() {
					CodanCReconciler reconciler = new CodanCReconciler();

					public void partActivated(IWorkbenchPartReference partRef) {
					}

					public void partDeactivated(IWorkbenchPartReference partRef) {
					}

					public void partOpened(IWorkbenchPartReference partRef) {
						IWorkbenchPart part = partRef.getPart(false);
						if (part instanceof CEditor) {
							CEditor editor = (CEditor) part;
							reconciler.install(editor);
							IResource resource = (IResource) editor.getEditorInput().getAdapter(IResource.class);
							if (resource != null) {
								processResource(resource);
							}
						}
					}
					
					private void processResource(IResource resource) {
						CodanBuilder builder = (CodanBuilder) CodanRuntime.getInstance().getBuilder();
						builder.processResource(resource, NULL_PROGRESS_MONITOR, CheckerLaunchMode.RUN_ON_FILE_OPEN);
					}

					public void partHidden(IWorkbenchPartReference partRef) {
					}

					public void partVisible(IWorkbenchPartReference partRef) {
					}

					public void partClosed(IWorkbenchPartReference partRef) {
						IWorkbenchPart part = partRef.getPart(false);
						if (part instanceof CEditor) {
							reconciler.uninstall((CEditor) part);
						}
					}

					public void partBroughtToTop(IWorkbenchPartReference partRef) {
					}

					public void partInputChanged(IWorkbenchPartReference partRef) {
					}
				};
				page.addPartListener(partListener);
				// check current open editors
				IEditorReference[] editorReferences = page.getEditorReferences();
				for (int i = 0; i < editorReferences.length; i++) {
					IEditorReference ref = editorReferences[i];
					partListener.partOpened(ref);
				}
			}
		});
	}
}

Back to the top