Skip to main content
summaryrefslogtreecommitdiffstats
blob: fabf1d17da80aaa292fa26da865b5a0da05a0acb (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
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v0.5 
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *     IBM Corp. - Rational Software - initial implementation
 ******************************************************************************/

package org.eclipse.cdt.ui.dialogs;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICDescriptor;
import org.eclipse.cdt.internal.core.search.indexing.IndexManager;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.dialogs.PropertyPage;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


public class IndexerOptionPropertyPage extends PropertyPage {
	
	private IndexerOptionDialogPage optionPage;
	private boolean oldIndexerValue;
	private int oldIndexerProblemsValue;
	private boolean requestedIndexAll;
	
	public IndexerOptionPropertyPage(){
		super();
		optionPage = new IndexerOptionDialogPage();
		requestedIndexAll = false;
	}
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new FillLayout());

		optionPage.createControl(composite);
		//WorkbenchHelp.setHelp(composite, ICHelpContextIds.PROJECT_INDEXER_PROPERTIES);	
		initialize();
		
		return composite;
	}
	

	protected void performDefaults() {
		initialize();
		super.performDefaults();
	}
	
	private void initialize(){
		IProject project = getProject();
		
		try {
			oldIndexerValue = getIndexerEnabled(project);
		} catch (CoreException e) {
			e.printStackTrace();
		}
		
		optionPage.setIndexerValue(oldIndexerValue);
	}
	
	/*
	 * @see IPreferencePage#performOk()
	 */
	public boolean performOk() {
	
		boolean newIndexerValue = optionPage.getIndexerValue();
		
		boolean indexChanged = (oldIndexerValue != newIndexerValue);
		
		if ( indexChanged ){
			//persist new values
			IProject tempProject = getProject();
			optionPage.persistIndexerValues(tempProject);
		
						
			//if indexer is now on send a index all request 
			if( indexChanged && newIndexerValue && !requestedIndexAll ) {
				CCorePlugin.getDefault().getCoreModel().getIndexManager().indexAll(tempProject);
				requestedIndexAll = true;
			} else if( indexChanged && !newIndexerValue ) {
				CCorePlugin.getDefault().getCoreModel().getIndexManager().discardJobs( tempProject.getName() );
			}
		}
		return true;
	}
	
	public IProject getProject(){
		Object tempElement = getElement();
		IProject project = null;
		if (tempElement != null && tempElement instanceof IProject)
			project = (IProject) tempElement;
			
		return project;
	}
	
	public boolean getIndexerEnabled(IProject project) throws CoreException {
		// See if there's already one associated with the resource for this
		// session
		 Boolean indexValue = (Boolean) project.getSessionProperty(IndexManager.activationKey);

		// Try to load one for the project
		if (indexValue == null) {
			indexValue = loadIndexerEnabledFromCDescriptor(project);
		}
	
		// There is nothing persisted for the session, or saved in a file so
		// create a build info object
		if (indexValue != null) {
			project.setSessionProperty(IndexManager.activationKey, indexValue);
		}
		else{
			//Hmm, no persisted indexer value. Could be an old project - set to true and persist
			indexValue = new Boolean(true);
			optionPage.setIndexerValue(true);
			optionPage.persistIndexerValues(project);
		}
		
		return indexValue.booleanValue();
	}
	
	/**
	 * Loads dis from .cdtproject file
	 * @param project
	 * @param includes
	 * @param symbols
	 * @throws CoreException
	 */
	private Boolean loadIndexerEnabledFromCDescriptor(IProject project) throws CoreException {
		ICDescriptor descriptor = CCorePlugin.getDefault().getCProjectDescription(project);
		
		Node child = descriptor.getProjectData(IndexManager.CDT_INDEXER).getFirstChild();
		Boolean strBool = null;
		
		while (child != null) {
			if (child.getNodeName().equals(IndexManager.INDEXER_ENABLED)) 
				 strBool = Boolean.valueOf(((Element)child).getAttribute(IndexManager.INDEXER_VALUE));
			
			
			child = child.getNextSibling();
		}
		
		return strBool;
	}
	
	
}

Back to the top