Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d5238778c3c5e1133ca2b4639a08ccaee0b77a2f (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
/*******************************************************************************
 * Copyright (c) 2000, 2004, 2009 QNX Software Systems 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:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.autotools.ui.wizards;

 
import org.eclipse.cdt.autotools.core.AutotoolsNewProjectNature;
import org.eclipse.cdt.autotools.ui.AutotoolsUIPlugin;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.internal.autotools.core.AutotoolsPropertyConstants;
import org.eclipse.cdt.internal.autotools.core.configure.AutotoolsConfigurationManager;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.core.IProjectType;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedCProjectNature;
import org.eclipse.cdt.ui.wizards.conversion.ConvertProjectWizardPage;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;


/**
 *
 * ConvertToAutotoolsProjectWizardPage
 * Standard main page for a wizard that adds a Managed Make C project Nature to a project with no nature associated with it.
 * This conversion is one way in that the project cannot be converted back (i.e have the nature removed).
 *
 * @author Jeff Johnston
 * @since Feb 8, 2006
 *<p>
 * Example useage:
 * <pre>
 * mainPage = new ConvertToAutotoolsProjectWizardPage("ConvertProjectPage");
 * mainPage.setTitle("Project Conversion");
 * mainPage.setDescription("Add C or C++ Managed Make Nature to a project.");
 * </pre>
 * </p>
 */
public class ConvertToAutotoolsProjectWizardPage extends ConvertProjectWizardPage {
    
    private static final String WZ_TITLE = "WizardAutotoolsProjectConversion.title"; //$NON-NLS-1$
    private static final String WZ_DESC = "WizardAutotoolsProjectConversion.description"; //$NON-NLS-1$
    private static final String PREFIX = "WizardAutotoolsProjectConversion";
	protected static final String MSG_ADD_NATURE = PREFIX + ".message.add_nature";	//$NON-NLS-1$
	protected static final String MSG_ADD_BUILDER = PREFIX + ".message.add_builder";	//$NON-NLS-1$
	protected static final String MSG_SAVE = PREFIX + ".message.save";	//$NON-NLS-1$
    
	/**
	 * Constructor for ConvertToStdMakeProjectWizardPage.
	 * @param pageName
	 */
	public ConvertToAutotoolsProjectWizardPage(String pageName, ConvertToAutotoolsProjectWizard wizard) {
		super(pageName);
		setWizard(wizard);
	}
    
    /**
     * Method getWzTitleResource returns the correct Title Label for this class
     * overriding the default in the superclass.
     */
    @Override
	protected String getWzTitleResource(){
        return AutotoolsUIPlugin.getResourceString(WZ_TITLE);
    }
    
    /**
     * Method getWzDescriptionResource returns the correct description
     * Label for this class overriding the default in the superclass.
     */
    @Override
	protected String getWzDescriptionResource(){
        return AutotoolsUIPlugin.getResourceString(WZ_DESC);
    }
       
    /**
     * Method isCandidate returns true for all projects.
     * 
     * @param project
     * @return boolean
     */
    @Override
	public boolean isCandidate(IProject project) { 
		return true; // all 
    }    
    
    protected IProjectType getProjectType() {
    	return ((ConvertToAutotoolsProjectWizard)getWizard()).getProjectType();
    }
    
    protected IConfiguration[] getSelectedConfigurations() {
    	return ((ConvertToAutotoolsProjectWizard)getWizard()).getSelectedConfigurations();
    }
    
    protected void applyOptions(IProject project, IProgressMonitor monitor) {
    	((ConvertToAutotoolsProjectWizard)getWizard()).applyOptions(project, monitor);
    }
    
	@Override
	public void convertProject(IProject project, IProgressMonitor monitor, String projectID) throws CoreException {
		monitor.beginTask(AutotoolsUIPlugin.getResourceString("WizardMakeProjectConversion.monitor.convertingToMakeProject"), 7); //$NON-NLS-1$
		IConfiguration defaultCfg = null;
		Boolean convertingNewAutotoolsProject = false;
		try {
			super.convertProject(project, SubMonitor.convert(monitor, 1), projectID);
			// Bug 289834 - Converting C Autotools project to C++ loses configurations
			if (project.hasNature(AutotoolsNewProjectNature.AUTOTOOLS_NATURE_ID)) {
				convertingNewAutotoolsProject = true;  // set this for finally clause
				return; // We have converted C to C++ or vice-versa for an existing Autotools project and are done
			}
			// Otherwise, we must scrap existing configurations as they will have tool settings we cannot use
			monitor.subTask(AutotoolsUIPlugin.getResourceString(MSG_ADD_NATURE));
			ManagedCProjectNature.addManagedNature(project, SubMonitor.convert(monitor, 1));
			AutotoolsNewProjectNature.addAutotoolsNature(project, SubMonitor.convert(monitor, 1));
			// We need to remove any old Autotools nature, if one exists.
			AutotoolsNewProjectNature.removeOldAutotoolsNature(project, SubMonitor.convert(monitor, 1));
			monitor.subTask(AutotoolsUIPlugin.getResourceString(MSG_ADD_BUILDER));
			AutotoolsNewProjectNature.addAutotoolsBuilder(project, SubMonitor.convert(monitor, 1));
			// FIXME: Default scanner property: make -w - eventually we want to use Make core's build scanner
			project.setPersistentProperty(AutotoolsPropertyConstants.SCANNER_USE_MAKE_W, AutotoolsPropertyConstants.TRUE);
			// Specify false for override in next call as override can cause the method to throw an
			// exception.
			CCorePlugin.getDefault().mapCProjectOwner(project, projectID, false);
			// Add the ManagedProject to the project
			IManagedProject newManagedProject = null;
			IManagedBuildInfo info = null;
			try {
				info = ManagedBuildManager.createBuildInfo(project);
				IProjectType parent = getProjectType();
				newManagedProject = ManagedBuildManager.createManagedProject(project, parent);
				if (newManagedProject != null) {
					IConfiguration [] selectedConfigs = getSelectedConfigurations();
					for (int i = 0; i < selectedConfigs.length; i++) {
						IConfiguration config = selectedConfigs[i];
						int id = ManagedBuildManager.getRandomNumber();
						IConfiguration newConfig = newManagedProject.createConfiguration(config, config.getId() + "." + id); //$NON-NLS-1$
						newConfig.setArtifactName(newManagedProject.getDefaultArtifactName());
					}
					// Now add the first supported config in the list as the default
					IConfiguration[] newConfigs = newManagedProject.getConfigurations();
					for(int i = 0; i < newConfigs.length; i++) {
						if(newConfigs[i].isSupported()){
							defaultCfg = newConfigs[i];
							break;
						}
					}
					
					if(defaultCfg == null && newConfigs.length > 0)
						defaultCfg = newConfigs[0];
					
					
					if(defaultCfg != null) {
						ManagedBuildManager.setDefaultConfiguration(project, defaultCfg);
						ManagedBuildManager.setSelectedConfiguration(project, defaultCfg);
					}
					ManagedBuildManager.setNewProjectVersion(project);
				}
			} catch (BuildException e) {
				AutotoolsUIPlugin.log(e);
			}

			// Modify the project settings
			if (project != null) {
				applyOptions(project, SubMonitor.convert(monitor, 2));
			}

			// Save the build options
			monitor.subTask(AutotoolsUIPlugin.getResourceString(MSG_SAVE));
			if (info != null) {
				info.setValid(true);
				ManagedBuildManager.saveBuildInfo(project, true);
			}
		} finally {
			if (!convertingNewAutotoolsProject) { // Bug 289834 - don't create new config if switching between C and C++
				// Create a default Autotools configuration and save it.
				// We must do this after the ManagedBuildManager does a save because
				// we might not yet have a Configuration Description set up for the
				// default configuration and we need the id to create our own form
				// of configuration.
				ICConfigurationDescription cfgd = ManagedBuildManager.getDescriptionForConfiguration(defaultCfg);
				String id = cfgd.getId();
				AutotoolsConfigurationManager.getInstance().getConfiguration(project, id, true);
				AutotoolsConfigurationManager.getInstance().saveConfigs(project);
				IStatus initResult = ManagedBuildManager.initBuildInfoContainer(project);
				if (initResult.getCode() != IStatus.OK) {
					// At this point, I can live with a failure
					AutotoolsUIPlugin.log(initResult);
				}
			}
			monitor.done();
		}
	}

	@Override
	public void createControl(Composite parent) {
		super.createControl(parent);
		IStructuredSelection sel = ((BasicNewResourceWizard)getWizard()).getSelection();
		if ( sel != null) {
			tableViewer.setCheckedElements(sel.toArray());
			setPageComplete(validatePage());
		}
	}

}

Back to the top