1
/*******************************************************************************
2
* Copyright (c) 2008, 2010 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.ui.wizards.settingswizards;
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.Collections;
16
import java.util.Comparator;
17
import java.util.List;
19
import org.eclipse.core.resources.IProject;
20
import org.eclipse.core.runtime.IStatus;
21
import org.eclipse.core.runtime.Status;
22
import org.eclipse.jface.dialogs.ErrorDialog;
23
import org.eclipse.jface.viewers.CheckboxTableViewer;
24
import org.eclipse.jface.viewers.ISelectionChangedListener;
25
import org.eclipse.jface.viewers.LabelProvider;
26
import org.eclipse.jface.viewers.SelectionChangedEvent;
27
import org.eclipse.jface.viewers.TableViewer;
28
import org.eclipse.jface.wizard.WizardPage;
29
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.events.ModifyEvent;
31
import org.eclipse.swt.events.ModifyListener;
32
import org.eclipse.swt.events.SelectionAdapter;
33
import org.eclipse.swt.events.SelectionEvent;
34
import org.eclipse.swt.graphics.Image;
35
import org.eclipse.swt.layout.GridData;
36
import org.eclipse.swt.layout.GridLayout;
37
import org.eclipse.swt.widgets.Button;
38
import org.eclipse.swt.widgets.Composite;
39
import org.eclipse.swt.widgets.Display;
40
import org.eclipse.swt.widgets.Event;
41
import org.eclipse.swt.widgets.FileDialog;
42
import org.eclipse.swt.widgets.Label;
43
import org.eclipse.swt.widgets.Shell;
44
import org.eclipse.swt.widgets.Table;
45
import org.eclipse.swt.widgets.TableItem;
46
import org.eclipse.swt.widgets.Text;
48
import org.eclipse.cdt.core.CCorePlugin;
49
import org.eclipse.cdt.core.model.CModelException;
50
import org.eclipse.cdt.core.model.CoreModel;
51
import org.eclipse.cdt.core.model.ICProject;
52
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
53
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
54
import org.eclipse.cdt.ui.CDTSharedImages;
55
import org.eclipse.cdt.ui.CElementLabelProvider;
56
import org.eclipse.cdt.ui.CUIPlugin;
58
import org.eclipse.cdt.internal.ui.viewsupport.ListContentProvider;
59
import org.eclipse.cdt.internal.ui.wizards.settingswizards.IProjectSettingsWizardPageStrategy.MessageType;
64
abstract public class ProjectSettingsWizardPage extends WizardPage implements IProjectSettingsWizardPage {
66
public static final String FILENAME_EXTENSION = "xml"; //$NON-NLS-1$
69
private final IProjectSettingsWizardPageStrategy strategy;
70
private final List<ISettingsProcessor> processors;
72
private ICProject selectedProject;
73
private ICConfigurationDescription selectedConfiguration;
75
private Text filePathText;
76
private CheckboxTableViewer settingsViewer;
78
private IProject initialProject;
83
* @param initialProject the initial project to be selected, may be null
85
private ProjectSettingsWizardPage(IProjectSettingsWizardPageStrategy strategy) {
86
super(""); //$NON-NLS-1$
87
this.strategy = strategy;
89
// This could be replaced with an extension point
90
this.processors = Arrays.<ISettingsProcessor>asList(
91
new IncludePathsSettingsProcessor(),
92
new MacroSettingsProcessor()
100
protected abstract void layoutPage(Composite parent);
103
public static ProjectSettingsWizardPage createExportWizardPage() {
104
return new ProjectSettingsWizardPage(new ProjectSettingsExportStrategy()) {
106
public void layoutPage(Composite parent) {
107
createProjectSelectionGroup(parent);
108
createSettingsSelectionGroup(parent);
109
//createCheckboxSelectionGroup(parent);
110
createFileSelectionGroup(parent);
116
public static ProjectSettingsWizardPage createImportWizardPage() {
117
return new ProjectSettingsWizardPage(new ProjectSettingsImportStrategy()) {
119
public void layoutPage(Composite parent) {
120
createFileSelectionGroup(parent);
121
createProjectSelectionGroup(parent);
122
createSettingsSelectionGroup(parent);
123
//createCheckboxSelectionGroup(parent);
129
public boolean finish() {
130
return strategy.finish(this);
134
public void setInitialProject(IProject project) {
135
this.initialProject = project;
139
public List<ISettingsProcessor> getSettingsProcessors() {
140
return Collections.unmodifiableList(processors);
144
public List<ISettingsProcessor> getSelectedSettingsProcessors() {
145
List<ISettingsProcessor> selected = new ArrayList<ISettingsProcessor>();
146
for(Object element : settingsViewer.getCheckedElements()) {
147
selected.add((ISettingsProcessor)element);
152
public String getDestinationFilePath() {
153
return filePathText.getText();
157
public ICProject getSelectedProject() {
158
return selectedProject;
162
public ICConfigurationDescription getSelectedConfiguration() {
163
return selectedConfiguration;
166
public void setDisplayedSettingsProcessors(List<ISettingsProcessor> processors) {
167
settingsViewer.setInput(processors);
168
settingsViewer.refresh();
169
settingsViewer.setAllChecked(true);
170
updateWidgetEnablements();
174
public void showErrorDialog(String dialogTitle, String message) {
175
Shell shell = Display.getCurrent().getActiveShell();
176
Status status = new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0, message, null);
177
ErrorDialog.openError(shell, dialogTitle, null, status);
181
public void createControl(Composite parent) {
182
setTitle(strategy.getMessage(MessageType.TITLE));
183
setMessage(strategy.getMessage(MessageType.MESSAGE));
185
Composite composite = new Composite(parent, SWT.NULL);
186
composite.setLayout(new GridLayout());
187
composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
188
composite.setFont(parent.getFont());
190
layoutPage(composite);
192
setControl(composite);
194
strategy.pageCreated(this);
196
updateWidgetEnablements();
200
private void updateWidgetEnablements() {
201
boolean enableFinishButton = selectedProject != null
202
&& selectedConfiguration != null
203
&& settingsViewer.getCheckedElements().length > 0
204
&& filePathText.getText().length() > 0;
206
// since this wizard has only one page we can toggle the finish button using the setPageComplete() method
207
setPageComplete(enableFinishButton);
212
protected void createProjectSelectionGroup(Composite parent) {
213
Composite projectSelectionGroup = new Composite(parent, SWT.NONE);
214
projectSelectionGroup.setLayout(new GridLayout(2, true));
215
projectSelectionGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
216
projectSelectionGroup.setFont(parent.getFont());
218
Label projectLabel = new Label(projectSelectionGroup, SWT.NONE);
219
projectLabel.setText(Messages.ProjectSettingsWizardPage_selectProject);
220
projectLabel.setLayoutData(new GridData());
222
Label configLabel = new Label(projectSelectionGroup, SWT.NONE);
223
configLabel.setText(Messages.ProjectSettingsWizardPage_selectConfiguration);
224
configLabel.setLayoutData(new GridData());
226
final Table projectTable = new Table(projectSelectionGroup, SWT.SINGLE | SWT.BORDER);
227
projectTable.setLayoutData(new GridData(GridData.FILL_BOTH));
229
TableViewer projectViewer = new TableViewer(projectTable);
230
projectViewer.setContentProvider(new ListContentProvider());
231
projectViewer.setLabelProvider(new CElementLabelProvider());
232
List<ICProject> openProjects = getAllOpenCProjects();
233
Collections.sort(openProjects, new Comparator<ICProject>() {
234
public int compare(ICProject o1, ICProject o2) {
235
return o1.getProject().getName().compareTo(o2.getProject().getName());
238
projectViewer.setInput(openProjects);
240
final Table configTable = new Table(projectSelectionGroup, SWT.SINGLE | SWT.BORDER);
241
configTable.setLayoutData(new GridData(GridData.FILL_BOTH));
243
final TableViewer configViewer = new TableViewer(configTable);
244
configViewer.setContentProvider(new ListContentProvider());
245
configViewer.setLabelProvider(new LabelProvider() {
246
@Override public Image getImage(Object element) {
247
return CDTSharedImages.getImage(CDTSharedImages.IMG_OBJS_CONFIG);
249
@Override public String getText(Object element) {
250
ICConfigurationDescription config = (ICConfigurationDescription)element;
251
String label = config.getName();
252
if(config.isActive())
253
label += " (" + Messages.ProjectSettingsWizardPage_active + ")"; //$NON-NLS-1$ //$NON-NLS-2$
259
// TODO what if nothing is selected?
260
projectTable.addSelectionListener(new SelectionAdapter() {
261
@Override public void widgetSelected(SelectionEvent e) {
262
TableItem[] items = projectTable.getSelection();
263
selectedProject = (ICProject)items[0].getData(); // its a single select so this is ok
264
configViewer.setInput(getConfigurations(selectedProject));
265
configViewer.refresh();
266
configTable.select(0);
267
configTable.notifyListeners(SWT.Selection, new Event());
272
configTable.addSelectionListener(new SelectionAdapter() {
273
@Override public void widgetSelected(SelectionEvent e) {
274
TableItem[] items = configTable.getSelection();
275
selectedConfiguration = (ICConfigurationDescription)items[0].getData();
276
updateWidgetEnablements();
280
if(openProjects.isEmpty()) {
281
setErrorMessage(Messages.ProjectSettingsWizardPage_noOpenProjects);
285
if((initialProject == null || !initialProject.isOpen()) && !openProjects.isEmpty()) {
286
initialProject = openProjects.get(0).getProject();
289
if(initialProject != null) {
290
String initialProjectName = initialProject.getName();
291
for(int i = 0; i < openProjects.size(); i++) {
292
ICProject tableProject = openProjects.get(i);
293
if(tableProject.getElementName().equals(initialProjectName)) {
294
projectTable.select(i);
295
configViewer.setInput(getConfigurations(tableProject));
296
configViewer.refresh();
297
configTable.select(0);
298
selectedProject = tableProject;
299
selectedConfiguration = (ICConfigurationDescription)configTable.getSelection()[0].getData();
307
private static List<ICConfigurationDescription> getConfigurations(ICProject project) {
308
// get a read-only project description, no need to waste memory
309
ICProjectDescription description = CCorePlugin.getDefault().getProjectDescription(project.getProject(), false);
310
return Arrays.asList(description.getConfigurations());
314
private static List<ICProject> getAllOpenCProjects() {
315
List<ICProject> projects = new ArrayList<ICProject>();
317
for(ICProject project : CoreModel.getDefault().getCModel().getCProjects()) {
318
if(project.getProject().isOpen()) {
319
projects.add(project);
322
} catch(CModelException e) {
329
protected void createSettingsSelectionGroup(Composite parent) {
330
Composite settingsSelectionGroup = new Composite(parent, SWT.NONE);
331
settingsSelectionGroup.setLayout(new GridLayout());
332
settingsSelectionGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
333
settingsSelectionGroup.setFont(parent.getFont());
335
Label label = new Label(settingsSelectionGroup, SWT.NONE);
336
label.setText(strategy.getMessage(MessageType.SETTINGS));
338
Table table = new Table(settingsSelectionGroup, SWT.CHECK | SWT.BORDER);
339
table.setLayoutData(new GridData(GridData.FILL_BOTH));
342
settingsViewer = new CheckboxTableViewer(table);
343
settingsViewer.setContentProvider(new ListContentProvider());
345
settingsViewer.addSelectionChangedListener(new ISelectionChangedListener() {
346
public void selectionChanged(SelectionChangedEvent event) {
347
updateWidgetEnablements();
351
LabelProvider settingsProcessorLabelProvider = new LabelProvider() {
352
@Override public Image getImage(Object element) {
353
return ((ISettingsProcessor)element).getIcon();
355
@Override public String getText(Object element) {
356
return ((ISettingsProcessor)element).getDisplayName();
360
settingsViewer.setLabelProvider(settingsProcessorLabelProvider);
361
settingsViewer.setInput(processors);
362
settingsViewer.setAllChecked(true);
365
Composite buttonComposite = new Composite(settingsSelectionGroup, SWT.NONE);
366
GridLayout layout = new GridLayout(2, true);
367
layout.marginHeight= layout.marginWidth= 0;
368
buttonComposite.setLayout(layout);
369
buttonComposite.setLayoutData(new GridData());
371
Button selectButton = new Button(buttonComposite, SWT.PUSH);
372
selectButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
373
selectButton.setText(Messages.ProjectSettingsWizardPage_selectAll);
375
selectButton.addSelectionListener(new SelectionAdapter() {
376
@Override public void widgetSelected(SelectionEvent e) {
377
settingsViewer.setAllChecked(true);
378
updateWidgetEnablements();
382
Button deselectButton = new Button(buttonComposite, SWT.PUSH);
383
deselectButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
384
deselectButton.setText(Messages.ProjectSettingsWizardPage_deselectAll);
386
deselectButton.addSelectionListener(new SelectionAdapter() {
387
@Override public void widgetSelected(SelectionEvent e) {
388
settingsViewer.setAllChecked(false);
389
updateWidgetEnablements();
395
protected void createFileSelectionGroup(Composite parent) {
396
Composite fileSelectionGroup = new Composite(parent, SWT.NONE);
397
fileSelectionGroup.setLayout(new GridLayout(2, false));
398
fileSelectionGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
399
fileSelectionGroup.setFont(parent.getFont());
401
Label label = new Label(fileSelectionGroup, SWT.NONE);
402
label.setText(strategy.getMessage(MessageType.FILE));
403
GridData gridData = new GridData();
404
gridData.horizontalSpan = 2;
405
label.setLayoutData(gridData);
407
filePathText = new Text(fileSelectionGroup, SWT.SINGLE | SWT.BORDER);
408
filePathText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
409
filePathText.addModifyListener(new ModifyListener() {
410
public void modifyText(ModifyEvent e) {
411
updateWidgetEnablements();
412
strategy.fileSelected(ProjectSettingsWizardPage.this);
416
Button browseButton = new Button(fileSelectionGroup, SWT.PUSH);
417
browseButton.setText(Messages.ProjectSettingsWizardPage_browse);
418
browseButton.setLayoutData(new GridData());
419
browseButton.addSelectionListener(new SelectionAdapter() {
420
@Override public void widgetSelected(SelectionEvent e) {
421
int type = (strategy instanceof ProjectSettingsImportStrategy) ? SWT.OPEN : SWT.SAVE;
422
FileDialog fileDialog = new FileDialog(getShell(), type);
423
fileDialog.setFilterExtensions(new String[] {"*." + FILENAME_EXTENSION}); //$NON-NLS-1$
424
String filePath = fileDialog.open();
426
filePathText.setText(filePath);
434
// protected void createCheckboxSelectionGroup(Composite parent) {
435
// Composite checkboxSelectGroup = new Composite(parent, SWT.NONE);
436
// checkboxSelectGroup.setLayout(new GridLayout());
437
// checkboxSelectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
438
// checkboxSelectGroup.setFont(parent.getFont());
440
// final Button checkBox = new Button(checkboxSelectGroup, SWT.CHECK);
441
// checkBox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
442
// checkBox.setText(strategy.getMessage(CHECKBOX));
444
// checkBox.addSelectionListener(new SelectionAdapter() {
445
// @Override public void widgetSelected(SelectionEvent e) {
446
// strategy.handleCheckboxClick(ProjectSettingsWizardPage.this, checkBox.getSelection());