1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-12 19:45:22 +02:00

Initial commit of the Import Executable Wizard.

This commit is contained in:
Ken Ryall 2006-04-28 15:16:43 +00:00
parent 8fb351caba
commit 8bd344b11f
10 changed files with 1408 additions and 1 deletions

View file

@ -19,6 +19,7 @@ Export-Package:
org.eclipse.cdt.debug.internal.ui.views.modules,
org.eclipse.cdt.debug.internal.ui.views.signals,
org.eclipse.cdt.debug.ui,
org.eclipse.cdt.debug.ui.importexecutable,
org.eclipse.cdt.debug.ui.sourcelookup
Require-Bundle: org.eclipse.ui.ide,
org.eclipse.jface.text,

Binary file not shown.

After

Width:  |  Height:  |  Size: 606 B

View file

@ -131,3 +131,8 @@ EditRegisterGroupAction.label=Edit Register Group
EditRegisterGroupAction.tooltip=Edit Register Group
RestoredefaultRegisterGroupsAction.label=Restore Default Register Groups
RestoredefaultRegisterGroupsAction.tooltip=Restore Default Register Groups
# Import Executable
importCPPCategory.name=C/C++
importExecutableWizard.name=C/C++ Executable
importExecutableWizard.description=Imports a C/C++ executable file. Will create a project and launch configuration for debugging.

View file

@ -1224,5 +1224,22 @@
class="org.eclipse.cdt.debug.internal.ui.DebugMarkerAnnotationModelFactory"
contentTypeId="org.eclipse.cdt.core.cSource"/>
</extension>
<!-- Import Executable Wizard -->
<extension
point="org.eclipse.ui.importWizards">
<wizard
category="org.eclipse.cdt.ui.import"
icon="icons/obj16/c_app.gif"
name="%importExecutableWizard.name"
class="org.eclipse.cdt.debug.ui.importexecutable.ImportExecutableWizard"
id="org.eclipse.cdt.ui.wizardse.ImportExecutableWizard">
<description>
%importExecutableWizard.description
</description>
</wizard>
<category
id="org.eclipse.cdt.ui.import"
name="%importCPPCategory.name"/>
</extension>
</plugin>

View file

@ -0,0 +1,317 @@
/*******************************************************************************
* Copyright (c) 2006 Nokia 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:
* Nokia - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.debug.ui.importexecutable;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.debug.core.sourcelookup.ICSourceLocator;
import org.eclipse.cdt.debug.internal.core.sourcelookup.CSourceLookupDirector;
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.model.ISourceLocator;
import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.containers.DirectorySourceContainer;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.progress.UIJob;
public abstract class AbstractImportExecutableWizard extends Wizard implements INewWizard {
// The ImportExecutableWizard lets you select one or more executables and
// import them into the workspace. You can bring the executables into an
// existing project or have the wizard create a new project that
// will contains the executables and allow you to debug them. The wizard can
// also create a default launch configuration for you that's pre configured
// to debug the executables.
public static final String DEBUG_PROJECT_ID = "org.eclipse.cdt.debug"; //$NON-NLS-1$
private ImportExecutablePageOne pageOne;
private ImportExecutablePageTwo pageTwo;
/**
* Override this method to add the correct binary parsers to the project.
* @param newProject - the project created by the wizard
* @throws CoreException
*/
public abstract void addBinaryParsers(IProject newProject) throws CoreException;
/**
* Adds the executables to a new or existing project. The executables are
* added as external links.
*
* @param project -
* project receiving the executables
* @throws CoreException
*/
private void addExecutables(IProject project) throws CoreException {
String[] executables = pageOne.getSelectedExecutables();
for (int i = 0; i < executables.length; i++) {
IPath location = Path.fromOSString(executables[i]);
String executableName = location.toFile().getName();
IFile exeFile = project.getFile(executableName);
if (!exeFile.exists())
exeFile.createLink(location, 0, null);
}
}
public void addPages() {
super.addPages();
pageOne = new ImportExecutablePageOne(this);
addPage(pageOne);
pageTwo = new ImportExecutablePageTwo(this);
addPage(pageTwo);
}
private void addSourceLocation(ISourceLocator locator, AbstractSourceLookupDirector director, IPath unitLocation)
{
if (unitLocation.toFile().exists()) {
boolean found = false;
String unitLocationPathString = unitLocation.toOSString();
if (locator instanceof ICSourceLocator)
found = (((ICSourceLocator) locator).findSourceElement(unitLocationPathString) != null);
else if (locator instanceof CSourceLookupDirector)
found = ((CSourceLookupDirector) locator).contains(unitLocationPathString);
if (!found) {
DirectorySourceContainer directoryContainer = new DirectorySourceContainer(
unitLocation.removeLastSegments(1), false);
ArrayList containerList = new ArrayList(Arrays.asList(director
.getSourceContainers()));
containerList.add(directoryContainer);
director.setSourceContainers((ISourceContainer[]) containerList
.toArray(new ISourceContainer[containerList.size()]));
}
}
}
protected void addSourceLocations(IBinary[] binaries, ILaunchConfigurationWorkingCopy configuration) {
String memento = null;
String type = null;
try {
memento = configuration.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String) null);
type = configuration.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, (String) null);
if (type == null) {
type = configuration.getType().getSourceLocatorId();
}
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
ISourceLocator locator = launchManager.newSourceLocator(type);
if (locator instanceof AbstractSourceLookupDirector) {
AbstractSourceLookupDirector director = (AbstractSourceLookupDirector) locator;
if (memento == null) {
director.initializeDefaults(configuration);
} else {
director.initializeFromMemento(memento, configuration);
}
for (int i = 0; i < binaries.length; i++) {
IBinary binary = binaries[i];
if (!binary.getPath().lastSegment().startsWith(".")) {
addSourceLocation(locator, director, binary.getUnderlyingResource().getLocation());
List sourceFiles;
sourceFiles = binary.getChildrenOfType(ICElement.C_UNIT);
if (sourceFiles.size() == 0)
{
sourceFiles = binary.getChildrenOfType(ICElement.C_UNIT);
}
for (Iterator iter = sourceFiles.iterator(); iter.hasNext();) {
Object element = (Object) iter.next();
if (element instanceof ExternalTranslationUnit) {
ExternalTranslationUnit unit = (ExternalTranslationUnit) element;
IPath unitLocation = unit.getLocation();
addSourceLocation(locator, director, unitLocation);
}
}
}
}
configuration.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, director.getMemento());
configuration.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, director.getId());
}
} catch (CoreException e) {
return;
}
}
public IProject createCProjectForExecutable(String projectName) throws OperationCanceledException, CoreException {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject newProjectHandle = workspace.getRoot().getProject(projectName);
IProjectDescription description = workspace.newProjectDescription(newProjectHandle.getName());
description.setLocation(null);
IProject newProject = CCorePlugin.getDefault().createCProject(description, newProjectHandle, null,
DEBUG_PROJECT_ID);
return newProject;
}
public void createLaunchConfiguration(ICProject targetProject) throws CoreException {
ILaunchConfigurationWorkingCopy wc = this.getSelectedLaunchConfigurationType().newInstance(null,
this.getImportExecutablePage2().getNewConfigurationName());
wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, targetProject.getProject().getName());
wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, new File(getImportExecutablePage()
.getSelectedExecutables()[0]).getName());
addSourceLocations(targetProject.getBinaryContainer().getBinaries(), wc);
setConfigurationDefaults(wc);
final IStructuredSelection selection = new StructuredSelection(wc.doSave());
final String identifier = new String("org.eclipse.debug.ui.launchGroup.debug");
UIJob openLaunchConfigJob = new UIJob(Messages.AbstractImportExecutableWizard_CreateLaunchConfiguration) {
public IStatus runInUIThread(IProgressMonitor monitor) {
DebugUITools.openLaunchConfigurationDialogOnGroup(CUIPlugin.getActiveWorkbenchShell(), selection, identifier);
return Status.OK_STATUS;
}};
openLaunchConfigJob.schedule();
}
public abstract String getExecutableListLabel();
public ImportExecutablePageOne getImportExecutablePage() {
return pageOne;
}
public ImportExecutablePageTwo getImportExecutablePage2() {
return pageTwo;
}
public IWizardPage getNextPage(IWizardPage page) {
if (page == pageOne) {
pageTwo.checkExecutableSettings();
}
return super.getNextPage(page);
}
public abstract String getPageOneDescription();
public abstract String getPageOneTitle();
public ILaunchConfigurationType getSelectedLaunchConfigurationType() {
return pageTwo.getSelectedLaunchConfigurationType();
}
public String getDefaultWindowTitle() {
return Messages.AbstractImportExecutableWizard_windowTitle;
}
public void init(IWorkbench workbench, IStructuredSelection selection) {
setWindowTitle(getDefaultWindowTitle());
setNeedsProgressMonitor(true);
}
public abstract boolean isExecutableFile(File file);
public boolean performFinish() {
ICProject targetProject = null;
try {
if (pageTwo.isCreateNewProjectSelected()) {
// Create a new project and add the executables and binary
// parsers.
IProject newProject = createCProjectForExecutable(pageTwo
.getNewProjectName());
setupProject(newProject);
addExecutables(newProject);
addBinaryParsers(newProject);
targetProject = CCorePlugin.getDefault().getCoreModel().create(
newProject);
} else {
// Assume the existing project already has binary parsers setup,
// just add the executables.
ICProject existingProject = pageTwo.getExistingCProject();
addExecutables(existingProject.getProject());
targetProject = existingProject;
}
if (pageTwo.isCreateLaunchConfigurationSelected()) {
createLaunchConfiguration(targetProject);
}
} catch (OperationCanceledException e) {
} catch (CoreException e) {
}
return true;
}
/**
* Subclasses should override this method to modify the launch configuration
* created by the wizard. The default implementation does nothing.
* @param config the launch configuration created by the wizard
*/
public void setConfigurationDefaults(ILaunchConfigurationWorkingCopy config) {
}
public abstract void setupFileDialog(FileDialog dialog);
public void setupProject(IProject newProject) throws CoreException {
}
/**
* The wizard will only display launch configuration types that you support.
* This method will be called for each available type.
*
* @param type -
* the type of launch configuration
* @return - if the wizard supports this launch configuration type
*/
public abstract boolean supportsConfigurationType(
ILaunchConfigurationType type);
}

View file

@ -0,0 +1,459 @@
/*******************************************************************************
* Copyright (c) 2006 Nokia 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:
* Nokia - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.debug.ui.importexecutable;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
public class ImportExecutablePageOne extends WizardPage {
// Keep track of the directory that we browsed the last time
// the wizard was invoked.
private static String previouslyBrowsedDirectory = ""; //$NON-NLS-1$
private Text multipleExecutablePathField;
private CheckboxTreeViewer executablesViewer;
private File[] executables = new File[0];
private String previouslySearchedDirectory;
private Text singleExecutablePathField;
private boolean selectSingleFile = true;
private Button selectSingleButton;
private Button selectSingleBrowseButton;
private Button selectMultipleButton;
private Button selectMultipleBrowseButton;
private Button selectAll;
private Button deselectAll;
private Label selectMultipleTitle;
private AbstractImportExecutableWizard wizard;
public ImportExecutablePageOne(AbstractImportExecutableWizard wizard) {
super("ImportApplicationPageOne");
this.wizard = wizard;
setPageComplete(false);
setTitle(wizard.getPageOneTitle());
setDescription(wizard.getPageOneDescription());
}
private void checkControlState() {
selectSingleFile = selectSingleButton.getSelection();
singleExecutablePathField.setEnabled(selectSingleFile);
selectSingleBrowseButton.setEnabled(selectSingleFile);
multipleExecutablePathField.setEnabled(!selectSingleFile);
selectMultipleBrowseButton.setEnabled(!selectSingleFile);
selectAll.setEnabled(!selectSingleFile);
deselectAll.setEnabled(!selectSingleFile);
selectMultipleTitle.setEnabled(!selectSingleFile);
}
private boolean collectExecutableFiles(Collection files, File directory,
IProgressMonitor monitor) {
if (monitor.isCanceled())
return false;
monitor.subTask(directory.getPath());
File[] contents = directory.listFiles();
// first look for project description files
for (int i = 0; i < contents.length; i++) {
File file = contents[i];
if (file.isFile() && wizard.isExecutableFile(file)) {
files.add(file);
}
}
// no project description found, so recurse into sub-directories
for (int i = 0; i < contents.length; i++) {
if (contents[i].isDirectory())
collectExecutableFiles(files, contents[i], monitor);
}
return true;
}
public void createControl(Composite parent) {
initializeDialogUnits(parent);
Composite workArea = new Composite(parent, SWT.NONE);
setControl(workArea);
workArea.setLayout(new GridLayout());
workArea.setLayoutData(new GridData(GridData.FILL_BOTH
| GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
Composite selectExecutableGroup = new Composite(workArea, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
layout.makeColumnsEqualWidth = false;
layout.marginWidth = 0;
selectExecutableGroup.setLayout(layout);
selectExecutableGroup.setLayoutData(new GridData(
GridData.FILL_HORIZONTAL));
createSelectExecutable(selectExecutableGroup);
createExecutablesRoot(selectExecutableGroup);
createExecutablesList(workArea);
Dialog.applyDialogFont(workArea);
selectSingleButton.setSelection(true);
checkControlState();
}
private void createExecutablesList(Composite workArea) {
selectMultipleTitle = new Label(workArea, SWT.NONE);
selectMultipleTitle.setText(wizard.getExecutableListLabel());
Composite listComposite = new Composite(workArea, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.marginWidth = 0;
layout.makeColumnsEqualWidth = false;
listComposite.setLayout(layout);
listComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
| GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
executablesViewer = new CheckboxTreeViewer(listComposite, SWT.BORDER);
GridData listData = new GridData(GridData.GRAB_HORIZONTAL
| GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
executablesViewer.getControl().setLayoutData(listData);
executablesViewer.setContentProvider(new ITreeContentProvider() {
public void dispose() {
}
public Object[] getChildren(Object parentElement) {
return null;
}
public Object[] getElements(Object inputElement) {
return executables;
}
public Object getParent(Object element) {
return null;
}
public boolean hasChildren(Object element) {
return false;
}
public void inputChanged(Viewer viewer, Object oldInput,
Object newInput) {
}
});
executablesViewer.setLabelProvider(new LabelProvider() {
public String getText(Object element) {
return ((File) element).getName();
}
});
executablesViewer.addCheckStateListener(new ICheckStateListener() {
public void checkStateChanged(CheckStateChangedEvent event) {
setPageComplete(executablesViewer.getCheckedElements().length > 0);
}
});
executablesViewer.setInput(this);
createSelectionButtons(listComposite);
}
private void createExecutablesRoot(Composite workArea) {
selectMultipleButton = new Button(workArea, SWT.RADIO);
selectMultipleButton.setText(Messages.ImportExecutablePageOne_SearchDirectory);
selectMultipleButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
checkControlState();
if (!selectSingleFile) {
singleExecutablePathField.setText("");
noFilesSelected();
}
}
});
// project location entry field
this.multipleExecutablePathField = new Text(workArea, SWT.BORDER);
this.multipleExecutablePathField.setLayoutData(new GridData(
GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
selectMultipleBrowseButton = new Button(workArea, SWT.PUSH);
selectMultipleBrowseButton.setText(Messages.ImportExecutablePageOne_Browse);
setButtonLayoutData(selectMultipleBrowseButton);
selectMultipleBrowseButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
handleLocationBrowseButtonPressed();
}
});
multipleExecutablePathField.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
updateExecutablesList(multipleExecutablePathField.getText()
.trim());
}
});
}
private void createSelectExecutable(Composite workArea) {
// project specification group
selectSingleButton = new Button(workArea, SWT.RADIO);
selectSingleButton.setText(Messages.ImportExecutablePageOne_SelectExecutable);
selectSingleButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
checkControlState();
if (selectSingleFile) {
multipleExecutablePathField.setText("");
noFilesSelected();
}
}
});
// project location entry field
this.singleExecutablePathField = new Text(workArea, SWT.BORDER);
// Set the data name field so Abbot based tests can find it.
singleExecutablePathField.setData("name", "singleExecutablePathField");
singleExecutablePathField.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
setErrorMessage(null);
setPageComplete(false);
String path = singleExecutablePathField.getText();
if (path.length() > 0) {
File testFile = new File(path);
if (testFile.exists()) {
if (wizard.isExecutableFile(testFile))
{
executables = new File[1];
executables[0] = testFile;
setPageComplete(true);
}
else
setErrorMessage(Messages.ImportExecutablePageOne_NoteAnEXE);
} else {
setErrorMessage(Messages.ImportExecutablePageOne_NoSuchFile);
}
}
}
});
this.singleExecutablePathField.setLayoutData(new GridData(
GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
selectSingleBrowseButton = new Button(workArea, SWT.PUSH);
selectSingleBrowseButton.setText(Messages.ImportExecutablePageOne_Browse);
setButtonLayoutData(selectSingleBrowseButton);
selectSingleBrowseButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(getShell(), SWT.NONE);
wizard.setupFileDialog(dialog);
String res = dialog.open();
if (res != null) {
singleExecutablePathField.setText(res);
}
}
});
}
private void createSelectionButtons(Composite listComposite) {
Composite buttonsComposite = new Composite(listComposite, SWT.NONE);
GridLayout layout = new GridLayout();
layout.marginWidth = 0;
layout.marginHeight = 0;
buttonsComposite.setLayout(layout);
buttonsComposite.setLayoutData(new GridData(
GridData.VERTICAL_ALIGN_BEGINNING));
selectAll = new Button(buttonsComposite, SWT.PUSH);
selectAll.setText(Messages.ImportExecutablePageOne_SelectAll);
selectAll.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
executablesViewer.setCheckedElements(executables);
setPageComplete(executables.length > 0);
}
});
setButtonLayoutData(selectAll);
deselectAll = new Button(buttonsComposite, SWT.PUSH);
deselectAll.setText(Messages.ImportExecutablePageOne_DeselectAll);
deselectAll.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
executablesViewer.setCheckedElements(new Object[0]);
setPageComplete(false);
}
});
setButtonLayoutData(deselectAll);
}
public String[] getSelectedExecutables() {
String[] selectedExecutablePaths = new String[0];
if (selectSingleFile) {
if (executables.length > 0) {
selectedExecutablePaths = new String[1];
selectedExecutablePaths[0] = executables[0].getAbsolutePath();
}
} else {
Object[] checkedFiles = executablesViewer.getCheckedElements();
selectedExecutablePaths = new String[checkedFiles.length];
for (int i = 0; i < checkedFiles.length; i++) {
selectedExecutablePaths[i] = ((File) checkedFiles[i])
.getAbsolutePath();
}
}
return selectedExecutablePaths;
}
protected void handleLocationBrowseButtonPressed() {
DirectoryDialog dialog = new DirectoryDialog(
multipleExecutablePathField.getShell());
dialog
.setMessage(Messages.ImportExecutablePageOne_SelectADirectory);
String dirName = multipleExecutablePathField.getText().trim();
if (dirName.length() == 0)
dirName = previouslyBrowsedDirectory;
if (dirName.length() > 0) {
File path = new File(dirName);
if (path.exists())
dialog.setFilterPath(new Path(dirName).toOSString());
}
String selectedDirectory = dialog.open();
if (selectedDirectory != null) {
previouslyBrowsedDirectory = selectedDirectory;
multipleExecutablePathField.setText(previouslyBrowsedDirectory);
updateExecutablesList(selectedDirectory);
}
}
protected void noFilesSelected() {
executables = new File[0];
executablesViewer.refresh(true);
executablesViewer.setCheckedElements(executables);
previouslySearchedDirectory = "";
setPageComplete(false);
}
protected void updateExecutablesList(final String path) {
// don't search on empty path
if (path == null || path.length() == 0)
return;
// don't repeat the same search - the user might just be tabbing to
// traverse
if (previouslySearchedDirectory != null
&& previouslySearchedDirectory.equals(path))
return;
previouslySearchedDirectory = path;
try {
getContainer().run(true, true, new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) {
monitor.beginTask(Messages.ImportExecutablePageOne_Searching, 100);
File directory = new File(path);
executables = new File[0];
monitor.worked(10);
if (directory.isDirectory()) {
Collection files = new ArrayList();
if (!collectExecutableFiles(files, directory, monitor))
return;
Iterator filesIterator = files.iterator();
executables = new File[files.size()];
int index = 0;
monitor.worked(50);
monitor.subTask(Messages.ImportExecutablePageOne_ProcessingResults);
while (filesIterator.hasNext()) {
File file = (File) filesIterator.next();
executables[index] = file;
index++;
}
} else
monitor.worked(60);
monitor.done();
}
});
} catch (InvocationTargetException e) {
} catch (InterruptedException e) {
// Nothing to do if the user interrupts.
}
executablesViewer.refresh(true);
executablesViewer.setCheckedElements(executables);
setPageComplete(executables.length > 0);
}
}

View file

@ -0,0 +1,423 @@
/*******************************************************************************
* Copyright (c) 2006 Nokia 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:
* Nokia - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.debug.ui.importexecutable;
import java.io.File;
import java.util.ArrayList;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICDescriptor;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.ui.CElementLabelProvider;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
public class ImportExecutablePageTwo extends WizardPage {
private Combo configTypes;
private Text configurationName;
private Label configurationNameLabel;
private Button createLaunch;
private Button existingProjectButton;
private Text existingProjectName;
private String filterPlatform;
private boolean isCreateLaunchConfigurationSelected = true;
private boolean isCreateNewProjectSelected = true;
private Button newProjectButton;
private Label newProjectLabel;
private Text newProjectName;
private Button searchButton;
private AbstractImportExecutableWizard wizard;
public ImportExecutablePageTwo(AbstractImportExecutableWizard wizard) {
super("ImportExecutablePageTwo");
this.wizard = wizard;
setTitle(Messages.ImportExecutablePageTwo_ChooseProject);
setDescription(Messages.ImportExecutablePageTwo_ChooseExisting);
filterPlatform = "*";
}
private void addLaunchConfigTypes() {
ILaunchConfigurationType[] configTypeList = DebugPlugin.getDefault()
.getLaunchManager().getLaunchConfigurationTypes();
for (int i = 0; i < configTypeList.length; i++) {
String configTypeName = configTypeList[i].getName();
if (configTypeList[i].isPublic()
&& configTypeList[i]
.supportsMode(ILaunchManager.DEBUG_MODE)) {
if (wizard.supportsConfigurationType(configTypeList[i])) {
configTypes.add(configTypeName);
}
}
}
configTypes.select(0);
}
public void checkExecutableSettings() {
if (isCreateNewProjectSelected) {
String defaultName = getDefaultProjectName();
if (defaultName.length() > 0) {
ICProject cProject = CoreModel.getDefault().getCModel()
.getCProject(defaultName);
if (cProject.exists()) {
isCreateNewProjectSelected = false;
existingProjectName.setText(defaultName);
existingProjectButton.setSelection(true);
newProjectButton.setSelection(false);
checkExistingProjectName();
} else {
newProjectName.setText(defaultName);
checkNewProjectName();
}
setLaunchConfigurationName(defaultName);
}
}
}
protected void checkExistingProjectName() {
ICProject project = getExistingCProject();
setErrorMessage(null);
setPageComplete(project != null);
if (project == null) {
setErrorMessage(Messages.ImportExecutablePageTwo_BadProjectName);
}
}
protected void checkLaunchConfigurationName() {
String newName = configurationName.getText();
setErrorMessage(null);
if (isCreateLaunchConfigurationSelected) {
setPageComplete(newName.length() > 0);
if (newName.length() == 0) {
setErrorMessage(Messages.ImportExecutablePageTwo_EnterLaunchConfig);
}
}
}
protected void checkNewProjectName() {
String newName = newProjectName.getText().trim();
setErrorMessage(null);
setPageComplete(newName.length() > 0);
if (newName.length() == 0) {
setErrorMessage(Messages.ImportExecutablePageTwo_EnterProjectName);
}
ICProject cProject = CoreModel.getDefault().getCModel().getCProject(
newName);
if (cProject.exists()) {
setErrorMessage(Messages.ImportExecutablePageTwo_ProjectAlreadyExists);
}
}
protected ICProject chooseCProject() {
try {
ICProject[] projects = getCProjects();
ILabelProvider labelProvider = new CElementLabelProvider();
ElementListSelectionDialog dialog = new ElementListSelectionDialog(
getShell(), labelProvider);
dialog.setTitle("Select a Project"); //$NON-NLS-1$
dialog.setMessage("Choose a project for the executable."); //$NON-NLS-1$
dialog.setElements(projects);
ICProject cProject = getExistingCProject();
if (cProject != null) {
dialog.setInitialSelections(new Object[] { cProject });
}
if (dialog.open() == Window.OK) {
return (ICProject) dialog.getFirstResult();
}
} catch (CModelException e) {
}
return null;
}
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
container.setLayout(new GridLayout());
//
setControl(container);
final Composite composite = new Composite(container, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 4;
composite.setLayout(gridLayout);
newProjectButton = new Button(composite, SWT.RADIO);
newProjectButton.setText(Messages.ImportExecutablePageTwo_NewProjectName);
newProjectButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
isCreateNewProjectSelected = newProjectButton.getSelection();
checkNewProjectName();
updateControls();
}
});
newProjectName = new Text(composite, SWT.BORDER);
final GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 3;
newProjectName.setLayoutData(gridData);
newProjectName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
checkNewProjectName();
}
});
new Label(composite, SWT.NONE);
newProjectLabel = new Label(composite, SWT.NONE);
final GridData gridData_1 = new GridData();
gridData_1.horizontalSpan = 3;
newProjectLabel.setLayoutData(gridData_1);
newProjectLabel
.setText(Messages.ImportExecutablePageTwo_ProjectLabel);
final Label dummy2 = new Label(composite, SWT.NONE);
final GridData gridData_2 = new GridData();
gridData_2.horizontalSpan = 4;
dummy2.setLayoutData(gridData_2);
existingProjectButton = new Button(composite, SWT.RADIO);
existingProjectButton.setText(Messages.ImportExecutablePageTwo_ExistingProject);
existingProjectButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
isCreateNewProjectSelected = !newProjectButton.getSelection();
checkExistingProjectName();
updateControls();
}
});
existingProjectName = new Text(composite, SWT.BORDER);
final GridData gridData_3 = new GridData(GridData.FILL_HORIZONTAL);
gridData_3.horizontalSpan = 2;
existingProjectName.setLayoutData(gridData_3);
existingProjectName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
checkExistingProjectName();
}
});
searchButton = new Button(composite, SWT.NONE);
searchButton.setLayoutData(new GridData());
searchButton.setText(Messages.ImportExecutablePageTwo_Search);
searchButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
ICProject project = chooseCProject();
if (project == null) {
return;
}
String projectName = project.getElementName();
existingProjectName.setText(projectName);
updateControls();
}
});
newProjectButton.setSelection(true);
final Label label = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
final GridData gridData_4 = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData_4.horizontalSpan = 4;
label.setLayoutData(gridData_4);
final Composite composite_1 = new Composite(composite, SWT.NONE);
final GridData gridData_5 = new GridData();
gridData_5.horizontalSpan = 4;
composite_1.setLayoutData(gridData_5);
final GridLayout gridLayout_1 = new GridLayout();
gridLayout_1.numColumns = 3;
composite_1.setLayout(gridLayout_1);
createLaunch = new Button(composite_1, SWT.CHECK);
createLaunch.setText(Messages.ImportExecutablePageTwo_CreateLaunch);
createLaunch.setSelection(true);
createLaunch.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
isCreateLaunchConfigurationSelected = createLaunch
.getSelection();
setLaunchConfigurationName(configurationName.getText().trim());
checkLaunchConfigurationName();
updateControls();
}
});
configTypes = new Combo(composite_1, SWT.READ_ONLY);
final GridData gridData_6 = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData_6.horizontalSpan = 2;
configTypes.setLayoutData(gridData_6);
configTypes.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
setLaunchConfigurationName(configurationName.getText().trim());
}
});
configurationNameLabel = new Label(composite, SWT.NONE);
configurationNameLabel.setLayoutData(new GridData(
GridData.HORIZONTAL_ALIGN_END));
configurationNameLabel.setText(Messages.ImportExecutablePageTwo_Name);
configurationName = new Text(composite, SWT.BORDER);
final GridData gridData_7 = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData_7.horizontalSpan = 3;
configurationName.setLayoutData(gridData_7);
configurationName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
checkLaunchConfigurationName();
}
});
addLaunchConfigTypes();
updateControls();
}
private ICProject[] getCProjects() throws CModelException {
ICProject cproject[] = CoreModel.getDefault().getCModel()
.getCProjects();
ArrayList list = new ArrayList(cproject.length);
for (int i = 0; i < cproject.length; i++) {
ICDescriptor cdesciptor = null;
try {
cdesciptor = CCorePlugin.getDefault().getCProjectDescription(
(IProject) cproject[i].getResource(), false);
if (cdesciptor != null) {
String projectPlatform = cdesciptor.getPlatform();
if (filterPlatform.equals("*") //$NON-NLS-1$
|| projectPlatform.equals("*") //$NON-NLS-1$
|| filterPlatform.equalsIgnoreCase(projectPlatform) == true) {
list.add(cproject[i]);
}
} else {
list.add(cproject[i]);
}
} catch (CoreException e) {
list.add(cproject[i]);
}
}
return (ICProject[]) list.toArray(new ICProject[list.size()]);
}
protected String getDefaultProjectName() {
String defaultName = new String();
String[] executables = wizard.getImportExecutablePage()
.getSelectedExecutables();
if (executables.length > 0) {
String fileName = new File(executables[0]).getName();
defaultName = new String(Messages.ImportExecutablePageTwo_DefaultProjectPrefix + fileName);
}
return defaultName;
}
protected ICProject getExistingCProject() {
String projectName = existingProjectName.getText().trim();
if (projectName.length() < 1) {
return null;
}
ICProject cProject = CoreModel.getDefault().getCModel().getCProject(
projectName);
if (!cProject.exists())
return null;
return cProject;
}
public String getExistingProjectName() {
return existingProjectName.getText().trim();
}
public String getNewConfigurationName() {
return configurationName.getText().trim();
}
public String getNewProjectName() {
return newProjectName.getText().trim();
}
protected ILaunchConfigurationType getSelectedLaunchConfigurationType() {
ILaunchConfigurationType result = null;
String selectedTypeName = configTypes.getText();
ILaunchConfigurationType[] configTypeList = DebugPlugin.getDefault()
.getLaunchManager().getLaunchConfigurationTypes();
for (int i = 0; i < configTypeList.length; i++) {
if (selectedTypeName.equals(configTypeList[i].getName())) {
result = configTypeList[i];
break;
}
}
return result;
}
public boolean isCreateLaunchConfigurationSelected() {
return isCreateLaunchConfigurationSelected;
}
public boolean isCreateNewProjectSelected() {
return isCreateNewProjectSelected;
}
private void setLaunchConfigurationName(String defaultName) {
configurationName.setText(DebugPlugin.getDefault().getLaunchManager()
.generateUniqueLaunchConfigurationNameFrom(defaultName));
checkLaunchConfigurationName();
}
protected void updateControls() {
isCreateNewProjectSelected = newProjectButton.getSelection();
searchButton.setEnabled(!isCreateNewProjectSelected);
newProjectName.setEnabled(isCreateNewProjectSelected);
existingProjectName.setEnabled(!isCreateNewProjectSelected);
newProjectLabel.setEnabled(isCreateNewProjectSelected);
configTypes.setEnabled(isCreateLaunchConfigurationSelected);
configurationName.setEnabled(isCreateLaunchConfigurationSelected);
configurationNameLabel.setEnabled(isCreateLaunchConfigurationSelected);
}
}

View file

@ -0,0 +1,74 @@
/*******************************************************************************
* Copyright (c) 2006 Nokia 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:
* Nokia - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.debug.ui.importexecutable;
import java.io.File;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICDescriptor;
import org.eclipse.cdt.core.ICDescriptorOperation;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.swt.widgets.FileDialog;
/**
* Reference implementation of a wizard that imports executables.
* Create your own version to import specific kinds of executables
* with product specific messages and launch configuration types.
*
*
*/
public class ImportExecutableWizard extends AbstractImportExecutableWizard {
public String getPageOneTitle() {
return Messages.ImportExecutableWizard_pageOneTitle;
}
public String getPageOneDescription() {
return Messages.ImportExecutableWizard_pageOneDescription;
}
public String getExecutableListLabel() {
return Messages.ImportExecutableWizard_executableListLabel;
}
public void setupFileDialog(FileDialog dialog) {
dialog.setText(Messages.ImportExecutableWizard_fileDialogTitle);
dialog.setFilterExtensions(new String[] { "*.*", "*.exe", "*.dll" });
dialog.setFilterNames(new String[] { Messages.ImportExecutableWizard_AllFiles, Messages.ImportExecutableWizard_Applications, Messages.ImportExecutableWizard_LIbaries });
}
public void addBinaryParsers(IProject newProject) throws CoreException {
ICDescriptorOperation op = new ICDescriptorOperation() {
public void execute(ICDescriptor descriptor, IProgressMonitor monitor) throws CoreException {
descriptor.remove(CCorePlugin.BINARY_PARSER_UNIQ_ID);
descriptor.create(CCorePlugin.BINARY_PARSER_UNIQ_ID, "org.eclipse.cdt.core.PE");
}
};
CCorePlugin.getDefault().getCDescriptorManager().runDescriptorOperation(newProject.getProject(), op, null);
}
public boolean supportsConfigurationType(ILaunchConfigurationType type) {
return type.getIdentifier().startsWith("org.eclipse.cdt.launch");
}
public boolean isExecutableFile(File file) {
String filename = file.getName().toLowerCase();
if (filename.endsWith(".exe") || filename.endsWith(".dll")
|| filename.endsWith(".elf"))
return true;
return false;
}
}

View file

@ -0,0 +1,79 @@
package org.eclipse.cdt.debug.ui.importexecutable;
import org.eclipse.osgi.util.NLS;
public class Messages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.cdt.debug.ui.importexecutable.messages"; //$NON-NLS-1$
private Messages() {
}
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}
public static String ImportExecutableWizard_pageOneTitle;
public static String ImportExecutableWizard_pageOneDescription;
public static String ImportExecutableWizard_executableListLabel;
public static String ImportExecutableWizard_fileDialogTitle;
public static String ImportExecutableWizard_AllFiles;
public static String ImportExecutableWizard_Applications;
public static String ImportExecutableWizard_LIbaries;
public static String ImportExecutablePageOne_SearchDirectory;
public static String ImportExecutablePageOne_SelectExecutable;
public static String ImportExecutablePageOne_NoteAnEXE;
public static String ImportExecutablePageOne_NoSuchFile;
public static String ImportExecutablePageOne_Browse;
public static String ImportExecutablePageOne_SelectAll;
public static String ImportExecutablePageOne_DeselectAll;
public static String ImportExecutablePageOne_SelectADirectory;
public static String ImportExecutablePageOne_Searching;
public static String ImportExecutablePageOne_ProcessingResults;
public static String ImportExecutablePageTwo_ChooseProject;
public static String ImportExecutablePageTwo_ChooseExisting;
public static String ImportExecutablePageTwo_BadProjectName;
public static String ImportExecutablePageTwo_EnterLaunchConfig;
public static String ImportExecutablePageTwo_EnterProjectName;
public static String ImportExecutablePageTwo_ProjectAlreadyExists;
public static String ImportExecutablePageTwo_NewProjectName;
public static String ImportExecutablePageTwo_ProjectLabel;
public static String ImportExecutablePageTwo_ExistingProject;
public static String ImportExecutablePageTwo_Search;
public static String ImportExecutablePageTwo_CreateLaunch;
public static String ImportExecutablePageTwo_Name;
public static String ImportExecutablePageTwo_DefaultProjectPrefix;
public static String AbstractImportExecutableWizard_windowTitle;
public static String AbstractImportExecutableWizard_CreateLaunchConfiguration;
}

View file

@ -0,0 +1,32 @@
ImportExecutableWizard_pageOneTitle=Import C/C++ Executable Files
ImportExecutableWizard_pageOneDescription=Select a file or a directory to search for C/C++ executable files.
ImportExecutablePageOne_SelectExecutable=Select Executable:
ImportExecutablePageOne_SelectADirectory=Select a directory to search for C/C++ executable files.
ImportExecutablePageTwo_EnterProjectName=Enter a project name.
ImportExecutablePageOne_ProcessingResults=Processing results
ImportExecutablePageTwo_EnterLaunchConfig=Enter a launch configuration name.
ImportExecutablePageTwo_ProjectAlreadyExists=That project already exists, enter a new name.
ImportExecutablePageTwo_DefaultProjectPrefix=Debug
ImportExecutableWizard_executableListLabel=C/C++ Executable Files
ImportExecutableWizard_fileDialogTitle=Choose an executable file
ImportExecutablePageOne_SearchDirectory=Search Directory:
ImportExecutablePageTwo_ChooseExisting=Choose an existing project or create a new one.
ImportExecutablePageTwo_BadProjectName=Invalid project name.
ImportExecutablePageTwo_NewProjectName=New Project Name:
ImportExecutablePageTwo_ExistingProject=Existing Project:
ImportExecutableWizard_AllFiles=All Files
ImportExecutableWizard_Applications=Applications
ImportExecutablePageOne_NoSuchFile=That file does not exist.
ImportExecutablePageOne_DeselectAll=Deselect All
ImportExecutablePageTwo_ChooseProject=Choose Project
ImportExecutablePageTwo_ProjectLabel=The new project will let you debug but not build the executable.
ImportExecutablePageTwo_CreateLaunch=Create a Launch Configuration:
ImportExecutableWizard_LIbaries=Libraries
ImportExecutablePageOne_Browse=Browse...
ImportExecutablePageTwo_Search=Search...
ImportExecutablePageTwo_Name=Name:
ImportExecutablePageOne_NoteAnEXE=Not an executable file.
ImportExecutablePageOne_SelectAll=Select All
ImportExecutablePageOne_Searching=Searching:
AbstractImportExecutableWizard_windowTitle=Import Executable
AbstractImportExecutableWizard_CreateLaunchConfiguration=Create Launch Configuration