Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 350f1f4d0d8f41a2f4aa196be708e179e5877a87 (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
/*******************************************************************************
 * Copyright (c) 2005, 2012 Intel 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:
 *     Intel Corporation - initial API and implementation
 *     Anna Dushistova (MontaVista) - [366771] Converter fails to convert a CDT makefile project
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.ui.commands;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.managedbuilder.core.IBuildObject;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IConvertManagedBuildObject;
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.IToolChain;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.ui.properties.ManagedBuilderUIPlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class ConvertTargetDialog extends Dialog {
	final private String title;
	protected org.eclipse.swt.widgets.List convertersList;
	private IProject project;
	private Map<String, IConfigurationElement> conversionElements;
	private IConfigurationElement selectedConversionElement;
	private static boolean isConversionSuccessful = false;

	public static final String PREFIX = "ProjectConvert";	//$NON-NLS-1$
	public static final String CONVERTERS_LIST = PREFIX + ".convertersList";	//$NON-NLS-1$

	/**
	 * @param parentShell the parent shell
	 * @param project the project to convert
	 * @param title the title of the dialog
	 */
	protected ConvertTargetDialog(Shell parentShell, IProject project, String title) {
		super(parentShell);
		this.title = title;
		setProject(project);

		IProjectType projectType = getProjectType();
		if (projectType != null) {
			conversionElements = ManagedBuildManager.getConversionElements(projectType);
		}
		for (IBuildObject tc : getProjectToolchains()) {
			Map<String, IConfigurationElement> converters = ManagedBuildManager.getConversionElements(tc);
			if (converters != null) {
				if (conversionElements == null) {
					conversionElements = converters;
				} else {
					conversionElements.putAll(converters);
				}
			}
		}

		setShellStyle(getShellStyle() | SWT.RESIZE);
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			handleConverterSelection();
			IConvertManagedBuildObject convertBuildObject = null;
			try {
				convertBuildObject =
						(IConvertManagedBuildObject) getSelectedConversionElement().createExecutableExtension("class");	//$NON-NLS-1$
			} catch (CoreException e) {
				ManagedBuilderUIPlugin.log(e);
			}
			if (convertBuildObject != null) {
				String fromId = getSelectedConversionElement().getAttribute("fromId");	//$NON-NLS-1$
				String toId = getSelectedConversionElement().getAttribute("toId");	//$NON-NLS-1$

				IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(getProject());
				if (info != null) {
					IManagedProject managedProject = info.getManagedProject();
					if (managedProject != null) {
						if (convertBuildObject.convert(managedProject, fromId, toId, true) == null) {
							setConversionSuccessful(false);
						} else {
							setConversionSuccessful(true);
						}
					} else {
						setConversionSuccessful(false);
					}
				} else {
					setConversionSuccessful(false);
				}
			} else {
				setConversionSuccessful(false);
			}
		}
		super.buttonPressed(buttonId);
	}


	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		if (title != null)
			shell.setText(title);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite comp = new Composite(parent, SWT.NULL);
		comp.setFont(parent.getFont());
		comp.setLayout(new GridLayout(1, true));
		comp.setLayoutData(new GridData(GridData.FILL_BOTH));

		// Create the converters list group area
		final Group convertersListGroup = new Group(comp, SWT.NONE);
		convertersListGroup.setFont(parent.getFont());
		convertersListGroup.setText(Messages.ProjectConvert_convertersList);
		convertersListGroup.setLayout(new GridLayout(1, false));
		convertersListGroup.setLayoutData(new GridData(GridData.FILL_BOTH));

		// Create the current config List
		convertersList = new org.eclipse.swt.widgets.List(convertersListGroup, SWT.SINGLE|SWT.V_SCROLL|SWT.H_SCROLL|SWT.BORDER);
		convertersList.setFont(convertersListGroup.getFont());
		GridData data = new GridData(GridData.FILL_BOTH);
		convertersList.setLayoutData(data);
		convertersList.addDisposeListener(new DisposeListener() {
			@Override
			public void widgetDisposed(DisposeEvent event) {
				convertersList = null;
			}
		});
		convertersList.addListener (SWT.Selection, new Listener () {
			@Override
			public void handleEvent(Event e) {
				validateState();
			}
		});
		Object [] objs = getConversionElements().keySet().toArray();
		String [] names = new String[objs.length];
		for (int i = 0; i < objs.length; i++) {
			Object object = objs[i];
			names[i] = (String)object;
		}
	    convertersList.setItems(names);
	    validateState();
		return comp;
	}

	private void handleConverterSelection() {
		// Determine which configuration was selected
		int selectionIndex = convertersList.getSelectionIndex();

		String selectedConverterName = convertersList
				.getItem(selectionIndex);

		IConfigurationElement selectedElement = getConversionElements().get(selectedConverterName);
		setSelectedConversionElement(selectedElement);
		return;
	}

	private void validateState() {
		Button b = getButton(IDialogConstants.OK_ID);
		if (b != null)
			b.setEnabled(convertersList.getSelectionIndex() != -1);
	}

	private Map<String, IConfigurationElement> getConversionElements() {
		if (conversionElements == null) {
			conversionElements = new HashMap<String, IConfigurationElement>();
		}
		return conversionElements;
	}

	private IProjectType getProjectType() {
		IProjectType projectType = null;

		// Get the projectType from project.
		IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(getProject());
		if (info != null) {
			IManagedProject managedProject = info.getManagedProject();
			if ( managedProject != null) {
				projectType = managedProject.getProjectType();
			}
		}
		return projectType;
	}

	public IProject getProject() {
		return project;
	}

	public void setProject(IProject project) {
		this.project = project;
	}

	public IConfigurationElement getSelectedConversionElement() {
		return selectedConversionElement;
	}

	public void setSelectedConversionElement(
			IConfigurationElement selectedConversionElement) {
		this.selectedConversionElement = selectedConversionElement;
	}

	public static boolean isConversionSuccessful() {
		return isConversionSuccessful;
	}

	public void setConversionSuccessful(boolean isConversionSuccessful) {
		ConvertTargetDialog.isConversionSuccessful = isConversionSuccessful;
	}
	
	private List<IBuildObject> getProjectToolchains() {
		List<IBuildObject> projectToolchains = new ArrayList<>();

		// Get the projectType from project.
		IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(getProject());
		if (info != null) {
			IConfiguration[] configs = info.getManagedProject().getConfigurations();
			for (IConfiguration config : configs) {
				IToolChain toolchain = config.getToolChain();
				if (toolchain != null) {
					projectToolchains.add(toolchain);
				}
			}
		}
		return projectToolchains;
	}
}

Back to the top