1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Code cleanup.

Change-Id: Id1f1d6c0c098d94a4275188a3b4f6443ddc4843d
This commit is contained in:
Sergey Prigogin 2016-05-25 17:21:20 -07:00 committed by Gerrit Code Review @ Eclipse.org
parent 290c8dfe23
commit 1bdfc29020
7 changed files with 217 additions and 285 deletions

View file

@ -62,7 +62,7 @@ public class NewClassCreationWizard extends NewElementWizard {
}
@Override
protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException {
protected void finishPage(IProgressMonitor monitor) throws CoreException {
fPage.createClass(monitor); // use the full progress monitor
}

View file

@ -12,9 +12,6 @@ package org.eclipse.cdt.internal.ui.wizards;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.cdt.internal.ui.actions.WorkbenchRunnableAdapter;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
@ -34,8 +31,12 @@ import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
public abstract class NewElementWizard extends Wizard implements INewWizard {
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.actions.WorkbenchRunnableAdapter;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
public abstract class NewElementWizard extends Wizard implements INewWizard {
private IWorkbench fWorkbench;
private IStructuredSelection fSelection;
@ -65,11 +66,8 @@ public abstract class NewElementWizard extends Wizard implements INewWizard {
/**
* Subclasses should override to perform the actions of the wizard.
* This method is run in the wizard container's context as a workspace runnable.
* @param monitor
* @throws InterruptedException
* @throws CoreException
*/
protected abstract void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException;
protected abstract void finishPage(IProgressMonitor monitor) throws CoreException;
/**
* Returns the scheduling rule for creating the element.
@ -78,31 +76,22 @@ public abstract class NewElementWizard extends Wizard implements INewWizard {
return ResourcesPlugin.getWorkspace().getRoot(); // look all by default
}
protected boolean canRunForked() {
return true;
}
protected void handleFinishException(Shell shell, InvocationTargetException e) {
String title= NewWizardMessages.NewElementWizard_op_error_title;
String message= NewWizardMessages.NewElementWizard_op_error_message;
ExceptionHandler.handle(e, shell, title, message);
}
/*
* @see Wizard#performFinish
*/
@Override
public boolean performFinish() {
IWorkspaceRunnable op= new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
try {
finishPage(monitor);
} catch (InterruptedException e) {
throw new OperationCanceledException(e.getMessage());
}
finishPage(monitor);
}
};
try {
@ -110,15 +99,12 @@ public abstract class NewElementWizard extends Wizard implements INewWizard {
} catch (InvocationTargetException e) {
handleFinishException(getShell(), e);
return false;
} catch (InterruptedException e) {
} catch (InterruptedException e) {
return false;
}
return true;
}
/* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
*/
@Override
public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
fWorkbench= workbench;
@ -136,5 +122,4 @@ public abstract class NewElementWizard extends Wizard implements INewWizard {
protected void selectAndReveal(IResource newResource) {
BasicNewResourceWizard.selectAndReveal(newResource, fWorkbench.getActiveWorkbenchWindow());
}
}

View file

@ -29,10 +29,9 @@ 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.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
@ -181,19 +180,13 @@ public class NewClassCodeGenerator {
/**
* Creates the new class.
*
* @param monitor
* a progress monitor to report progress.
* @throws CoreException
* Thrown when the creation failed.
* @throws InterruptedException
* Thrown when the operation was cancelled.
* @param monitor a progress monitor to report progress
* @throws CoreException if the creation failed
*/
public ICElement createClass(IProgressMonitor monitor)
throws CodeGeneratorException, CoreException, InterruptedException {
if (monitor == null)
monitor = new NullProgressMonitor();
monitor.beginTask(NewClassWizardMessages.NewClassCodeGeneration_createType_mainTask, 400);
public ICElement createClass(IProgressMonitor monitor) throws CoreException {
SubMonitor progress = SubMonitor.convert(monitor,
NewClassWizardMessages.NewClassCodeGeneration_createType_mainTask,
(fHeaderPath != null ? 3 : 0) + (fSourcePath != null ? 3 : 0)+ (fTestPath != null ? 3 : 0));
ITranslationUnit headerTU = null;
ITranslationUnit sourceTU = null;
@ -205,26 +198,25 @@ public class NewClassCodeGenerator {
IWorkingCopy testWorkingCopy = null;
try {
if (fHeaderPath != null) {
// Get method stubs
// Get method stubs.
List<IMethodStub> publicMethods = getStubs(ASTAccessVisibility.PUBLIC, false);
List<IMethodStub> protectedMethods = getStubs(ASTAccessVisibility.PROTECTED, false);
List<IMethodStub> privateMethods = getStubs(ASTAccessVisibility.PRIVATE, false);
IFile headerFile = NewSourceFileGenerator.createHeaderFile(fHeaderPath, true,
new SubProgressMonitor(monitor, 50));
IFile headerFile =
NewSourceFileGenerator.createHeaderFile(fHeaderPath, true, progress.split(1));
if (headerFile != null) {
headerTU = (ITranslationUnit) CoreModel.getDefault().create(headerFile);
if (headerTU == null) {
throw new CodeGeneratorException("Failed to create " + headerFile); //$NON-NLS-1$
}
// Create a working copy with a new owner
// Create a working copy with a new owner.
headerWorkingCopy = headerTU.getWorkingCopy();
// headerWorkingCopy = headerTU.getSharedWorkingCopy(null, CUIPlugin.getDefault().getBufferFactory());
String headerContent = constructHeaderFileContent(headerTU, publicMethods,
protectedMethods, privateMethods, headerWorkingCopy.getBuffer().getContents(),
new SubProgressMonitor(monitor, 100));
progress.split(1));
if (headerContent != null) {
headerContent= formatSource(headerContent, headerTU);
} else {
@ -232,13 +224,8 @@ public class NewClassCodeGenerator {
}
headerWorkingCopy.getBuffer().setContents(headerContent);
if (monitor.isCanceled()) {
throw new InterruptedException();
}
headerWorkingCopy.reconcile();
headerWorkingCopy.commit(true, monitor);
monitor.worked(50);
headerWorkingCopy.commit(true, progress.split(1));
createdClass = headerWorkingCopy.getElement(fFullyQualifiedClassName);
}
@ -247,30 +234,27 @@ public class NewClassCodeGenerator {
}
if (fSourcePath != null) {
// Get method stubs
// Get method stubs.
List<IMethodStub> publicMethods = getStubs(ASTAccessVisibility.PUBLIC, true);
List<IMethodStub> protectedMethods = getStubs(ASTAccessVisibility.PROTECTED, true);
List<IMethodStub> privateMethods = getStubs(ASTAccessVisibility.PRIVATE, true);
if (!fForceSourceFileCreation && publicMethods.isEmpty() &&
protectedMethods.isEmpty() && privateMethods.isEmpty()) {
monitor.worked(100);
} else {
IFile sourceFile = NewSourceFileGenerator.createSourceFile(fSourcePath, true,
new SubProgressMonitor(monitor, 50));
if (fForceSourceFileCreation || !publicMethods.isEmpty() ||
!protectedMethods.isEmpty() || !privateMethods.isEmpty()) {
IFile sourceFile =
NewSourceFileGenerator.createSourceFile(fSourcePath, true, progress.split(1));
if (sourceFile != null) {
sourceTU = (ITranslationUnit) CoreModel.getDefault().create(sourceFile);
if (sourceTU == null) {
throw new CodeGeneratorException("Failed to create " + sourceFile); //$NON-NLS-1$
}
monitor.worked(50);
// Create a working copy with a new owner
// Create a working copy with a new owner.
sourceWorkingCopy = sourceTU.getWorkingCopy();
String sourceContent = constructSourceFileContent(sourceTU, headerTU,
publicMethods, protectedMethods, privateMethods,
sourceWorkingCopy.getBuffer().getContents(), new SubProgressMonitor(monitor, 100));
sourceWorkingCopy.getBuffer().getContents(), progress.split(1));
if (sourceContent != null) {
sourceContent = formatSource(sourceContent, sourceTU);
} else {
@ -278,13 +262,8 @@ public class NewClassCodeGenerator {
}
sourceWorkingCopy.getBuffer().setContents(sourceContent);
if (monitor.isCanceled()) {
throw new InterruptedException();
}
sourceWorkingCopy.reconcile();
sourceWorkingCopy.commit(true, monitor);
monitor.worked(50);
sourceWorkingCopy.commit(true, progress.split(1));
}
fCreatedSourceTU = sourceTU;
@ -292,30 +271,23 @@ public class NewClassCodeGenerator {
}
if (fTestPath != null) {
IFile testFile = NewSourceFileGenerator.createTestFile(fTestPath, true,
new SubProgressMonitor(monitor, 50));
IFile testFile = NewSourceFileGenerator.createTestFile(fTestPath, true, progress.split(1));
if (testFile != null) {
testTU = (ITranslationUnit) CoreModel.getDefault().create(testFile);
if (testTU == null) {
throw new CodeGeneratorException("Failed to create " + testFile); //$NON-NLS-1$
}
monitor.worked(50);
// Create a working copy with a new owner
testWorkingCopy = testTU.getWorkingCopy();
String testContent = constructTestFileContent(testTU, headerTU,
testWorkingCopy.getBuffer().getContents(), new SubProgressMonitor(monitor, 100));
testWorkingCopy.getBuffer().getContents(), progress.split(1));
testContent= formatSource(testContent, testTU);
testWorkingCopy.getBuffer().setContents(testContent);
if (monitor.isCanceled()) {
throw new InterruptedException();
}
testWorkingCopy.reconcile();
testWorkingCopy.commit(true, monitor);
monitor.worked(50);
testWorkingCopy.commit(true, progress.split(1));
}
fCreatedTestTU = testTU;
@ -332,7 +304,6 @@ public class NewClassCodeGenerator {
if (testWorkingCopy != null) {
testWorkingCopy.destroy();
}
monitor.done();
}
return fCreatedClass;
@ -382,7 +353,8 @@ public class NewClassCodeGenerator {
public String constructHeaderFileContent(ITranslationUnit headerTU, List<IMethodStub> publicMethods,
List<IMethodStub> protectedMethods, List<IMethodStub> privateMethods, String oldContents,
IProgressMonitor monitor) throws CoreException {
monitor.beginTask(NewClassWizardMessages.NewClassCodeGeneration_createType_task_header, 100);
SubMonitor progress = SubMonitor.convert(monitor,
NewClassWizardMessages.NewClassCodeGeneration_createType_task_header, 1);
String lineDelimiter= StubUtility.getLineDelimiterUsed(headerTU);
@ -394,13 +366,12 @@ public class NewClassCodeGenerator {
privateMethods, lineDelimiter);
String includes = null;
if (fBaseClasses != null && fBaseClasses.length > 0) {
includes = constructBaseClassIncludes(headerTU, lineDelimiter,
new SubProgressMonitor(monitor, 50));
if (fBaseClasses != null && fBaseClasses.length != 0) {
includes = constructBaseClassIncludes(headerTU, lineDelimiter, progress.split(1));
}
if (oldContents != null) {
if (oldContents.length() == 0) {
if (oldContents.isEmpty()) {
oldContents = null;
} else if (!oldContents.endsWith(lineDelimiter)) {
oldContents += lineDelimiter;
@ -466,8 +437,6 @@ public class NewClassCodeGenerator {
namespaceEnd, namespaceName, classComment, classDefinition, fClassName,
lineDelimiter);
}
monitor.done();
return fileContent;
}
@ -710,7 +679,8 @@ public class NewClassCodeGenerator {
private String constructBaseClassIncludes(ITranslationUnit headerTU, String lineDelimiter,
IProgressMonitor monitor) throws CodeGeneratorException {
monitor.beginTask(NewClassWizardMessages.NewClassCodeGeneration_createType_task_header_includePaths, 100);
SubMonitor progress = SubMonitor.convert(monitor,
NewClassWizardMessages.NewClassCodeGeneration_createType_task_header_includePaths, 1);
ICProject cProject = headerTU.getCProject();
IProject project = cProject.getProject();
@ -723,7 +693,7 @@ public class NewClassCodeGenerator {
if (createIncludePaths()) {
List<IPath> newIncludePaths = getMissingIncludePaths(projectLocation, includePaths, baseClassPaths);
if (!newIncludePaths.isEmpty()) {
addIncludePaths(cProject, newIncludePaths, monitor);
addIncludePaths(cProject, newIncludePaths, progress.split(1));
}
}
@ -749,8 +719,6 @@ public class NewClassCodeGenerator {
text.append(lineDelimiter);
previousStyle = style;
}
monitor.done();
return text.toString();
}
@ -772,8 +740,10 @@ public class NewClassCodeGenerator {
return NewClassWizardPrefs.createIncludePaths();
}
private void addIncludePaths(ICProject cProject, List<IPath> newIncludePaths, IProgressMonitor monitor) throws CodeGeneratorException {
monitor.beginTask(NewClassWizardMessages.NewClassCodeGeneration_createType_task_header_addIncludePaths, 100);
private void addIncludePaths(ICProject cProject, List<IPath> newIncludePaths, IProgressMonitor monitor)
throws CodeGeneratorException {
SubMonitor progress = SubMonitor.convert(monitor,
NewClassWizardMessages.NewClassCodeGeneration_createType_task_header_addIncludePaths, 1);
//TODO prefs option whether to add to project or parent source folder?
IPath addToResourcePath = cProject.getPath();
@ -802,18 +772,19 @@ public class NewClassCodeGenerator {
if (includeProject != null) {
// Make sure that the include is made the same way that build properties for
// projects makes them, so .contains below is a valid check
IIncludeEntry entry = CoreModel.newIncludeEntry(addToResourcePath, null, new Path(includeProject.getProject().getLocationURI().getPath()), true);
IIncludeEntry entry = CoreModel.newIncludeEntry(addToResourcePath, null,
new Path(includeProject.getProject().getLocationURI().getPath()), true);
if (!checkEntryList.contains(entry)) // if the path already exists in the #includes then don't add it
// If the path already exists in the #includes then don't add it.
if (!checkEntryList.contains(entry))
pathEntryList.add(entry);
}
}
pathEntries = pathEntryList.toArray(new IPathEntry[pathEntryList.size()]);
cProject.setRawPathEntries(pathEntries, new SubProgressMonitor(monitor, 80));
cProject.setRawPathEntries(pathEntries, progress.split(1));
} catch (CModelException e) {
throw new CodeGeneratorException(e);
}
monitor.done();
}
private ICProject toCProject(IProject enclosingProject) {
@ -925,12 +896,13 @@ public class NewClassCodeGenerator {
public String constructSourceFileContent(ITranslationUnit sourceTU, ITranslationUnit headerTU,
List<IMethodStub> publicMethods, List<IMethodStub> protectedMethods,
List<IMethodStub> privateMethods, String oldContents, IProgressMonitor monitor) throws CoreException {
monitor.beginTask(NewClassWizardMessages.NewClassCodeGeneration_createType_task_source, 150);
SubMonitor progress = SubMonitor.convert(monitor,
NewClassWizardMessages.NewClassCodeGeneration_createType_task_source, 2);
String lineDelimiter= StubUtility.getLineDelimiterUsed(sourceTU);
String includeString = null;
if (headerTU != null) {
includeString = getHeaderIncludeString(sourceTU, headerTU, new SubProgressMonitor(monitor, 50));
includeString = getHeaderIncludeString(sourceTU, headerTU, progress.split(1));
if (includeString != null) {
// Check if file already has the include.
if (oldContents != null && hasInclude(oldContents, includeString)) {
@ -944,7 +916,7 @@ public class NewClassCodeGenerator {
if (!publicMethods.isEmpty() || !protectedMethods.isEmpty() || !privateMethods.isEmpty()) {
// TODO sort methods (e.g. constructor always first?)
methodBodies = constructMethodBodies(sourceTU, publicMethods, protectedMethods,
privateMethods, lineDelimiter, new SubProgressMonitor(monitor, 50));
privateMethods, lineDelimiter, progress.split(1));
}
String namespaceBegin = fNamespace == null ?
@ -1007,19 +979,19 @@ public class NewClassCodeGenerator {
fileContent= CodeGeneration.getBodyFileContent(sourceTU, includeString, namespaceBegin,
namespaceEnd, namespaceName, null, methodBodies, fClassName, lineDelimiter);
}
monitor.done();
return fileContent;
}
public String constructTestFileContent(ITranslationUnit testTU, ITranslationUnit headerTU,
String oldContents, IProgressMonitor monitor) throws CoreException {
monitor.beginTask(NewClassWizardMessages.NewClassCodeGeneration_createType_task_source, 150);
SubMonitor progress = SubMonitor.convert(monitor,
NewClassWizardMessages.NewClassCodeGeneration_createType_task_source, 1);
String lineDelimiter= StubUtility.getLineDelimiterUsed(testTU);
String includeString = null;
if (headerTU != null) {
includeString = getHeaderIncludeString(testTU, headerTU, new SubProgressMonitor(monitor, 50));
includeString = getHeaderIncludeString(testTU, headerTU, progress.split(1));
if (includeString != null) {
// Check if file already has the include.
if (oldContents != null && hasInclude(oldContents, includeString)) {
@ -1071,7 +1043,6 @@ public class NewClassCodeGenerator {
fileContent= CodeGeneration.getTestFileContent(testTU, includeString, namespaceBegin,
namespaceEnd, namespaceName, null, fClassName, lineDelimiter);
}
monitor.done();
return fileContent;
}

View file

@ -11,18 +11,19 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.filewizard;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.ui.CPluginImages;
import org.eclipse.cdt.internal.ui.wizards.NewElementWizard;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.CPluginImages;
import org.eclipse.cdt.internal.ui.wizards.NewElementWizard;
public abstract class AbstractFileCreationWizard extends NewElementWizard {
protected AbstractFileCreationWizardPage fPage = null;
protected AbstractFileCreationWizardPage fPage;
public AbstractFileCreationWizard() {
super();
@ -31,35 +32,20 @@ public abstract class AbstractFileCreationWizard extends NewElementWizard {
setWindowTitle(NewFileWizardMessages.AbstractFileCreationWizard_title);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.ui.wizards.NewElementWizard#canRunForked()
*/
@Override
protected boolean canRunForked() {
return true;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.ui.wizards.NewElementWizard#finishPage(org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException {
fPage.createFile(monitor); // use the full progress monitor
protected void finishPage(IProgressMonitor monitor) throws CoreException {
fPage.createFile(monitor); // Use the full progress monitor.
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.wizard.IWizard#performFinish()
*/
@Override
public boolean performFinish() {
boolean res = super.performFinish();
if (res) {
boolean result = super.performFinish();
if (result) {
//TODO need prefs option for opening editor
boolean openInEditor = true;
@ -72,6 +58,6 @@ public abstract class AbstractFileCreationWizard extends NewElementWizard {
}
}
}
return res;
return result;
}
}

View file

@ -23,9 +23,8 @@ 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.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
@ -79,78 +78,78 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
private StringButtonDialogField fProjectField;
private StatusInfo fProjectStatus;
private StringButtonDialogField fRootDialogField;
private StatusInfo fRootStatus;
private SelectionButtonDialogField fExcludeInOthersFields;
private IWorkspaceRoot fWorkspaceRoot;
private ICProject fCurrCProject;
private IPathEntry[] fEntries;
private IPathEntry[] fNewEntries;
private boolean fIsProjectAsSourceFolder;
private ISourceRoot fCreatedRoot;
public NewSourceFolderWizardPage() {
super(PAGE_NAME);
setTitle(NewFolderWizardMessages.NewSourceFolderWizardPage_title);
setDescription(NewFolderWizardMessages.NewSourceFolderWizardPage_description);
setTitle(NewFolderWizardMessages.NewSourceFolderWizardPage_title);
setDescription(NewFolderWizardMessages.NewSourceFolderWizardPage_description);
fWorkspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
RootFieldAdapter adapter= new RootFieldAdapter();
fProjectField= new StringButtonDialogField(adapter);
fProjectField.setDialogFieldListener(adapter);
fProjectField.setLabelText(NewFolderWizardMessages.NewSourceFolderWizardPage_project_label);
fProjectField.setButtonLabel(NewFolderWizardMessages.NewSourceFolderWizardPage_project_button);
fProjectField.setLabelText(NewFolderWizardMessages.NewSourceFolderWizardPage_project_label);
fProjectField.setButtonLabel(NewFolderWizardMessages.NewSourceFolderWizardPage_project_button);
fRootDialogField= new StringButtonDialogField(adapter);
fRootDialogField.setDialogFieldListener(adapter);
fRootDialogField.setLabelText(NewFolderWizardMessages.NewSourceFolderWizardPage_root_label);
fRootDialogField.setButtonLabel(NewFolderWizardMessages.NewSourceFolderWizardPage_root_button);
fRootDialogField.setLabelText(NewFolderWizardMessages.NewSourceFolderWizardPage_root_label);
fRootDialogField.setButtonLabel(NewFolderWizardMessages.NewSourceFolderWizardPage_root_button);
fExcludeInOthersFields= new SelectionButtonDialogField(SWT.CHECK);
fExcludeInOthersFields.setDialogFieldListener(adapter);
fExcludeInOthersFields.setLabelText(NewFolderWizardMessages.NewSourceFolderWizardPage_exclude_label);
fExcludeInOthersFields.setLabelText(NewFolderWizardMessages.NewSourceFolderWizardPage_exclude_label);
fRootStatus= new StatusInfo();
fProjectStatus= new StatusInfo();
}
// -------- Initialization ---------
public void init(IStructuredSelection selection) {
if (selection == null || selection.isEmpty()) {
setDefaultAttributes();
return;
}
Object selectedElement= selection.getFirstElement();
if (selectedElement == null) {
selectedElement= EditorUtility.getActiveEditorCInput();
}
}
String projPath= null;
if (selectedElement instanceof IResource) {
IProject proj= ((IResource) selectedElement).getProject();
if (proj != null) {
projPath= proj.getFullPath().makeRelative().toString();
}
}
} else if (selectedElement instanceof ICElement) {
ICProject jproject= ((ICElement) selectedElement).getCProject();
if (jproject != null) {
projPath= jproject.getProject().getFullPath().makeRelative().toString();
}
}
}
if (projPath != null) {
fProjectField.setText(projPath);
fRootDialogField.setText(""); //$NON-NLS-1$
@ -158,10 +157,10 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
setDefaultAttributes();
}
}
private void setDefaultAttributes() {
String projPath= ""; //$NON-NLS-1$
try {
// find the first C project
IProject[] projects= fWorkspaceRoot.getProjects();
@ -170,7 +169,7 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
projPath= proj.getFullPath().makeRelative().toString();
break;
}
}
}
} catch (CoreException e) {
// ignore here
}
@ -180,60 +179,51 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
// -------- UI Creation ---------
/*
* @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
*/
@Override
public void createControl(Composite parent) {
initializeDialogUnits(parent);
Composite composite= new Composite(parent, SWT.NONE);
GridLayout layout= new GridLayout();
layout.marginWidth= 0;
layout.marginHeight= 0;
layout.marginHeight= 0;
layout.numColumns= 3;
composite.setLayout(layout);
fProjectField.doFillIntoGrid(composite, 3);
fProjectField.doFillIntoGrid(composite, 3);
fRootDialogField.doFillIntoGrid(composite, 3);
fExcludeInOthersFields.doFillIntoGrid(composite, 3);
int maxFieldWidth= convertWidthInCharsToPixels(40);
LayoutUtil.setWidthHint(fProjectField.getTextControl(null), maxFieldWidth);
LayoutUtil.setHorizontalGrabbing(fProjectField.getTextControl(null), true);
LayoutUtil.setWidthHint(fRootDialogField.getTextControl(null), maxFieldWidth);
LayoutUtil.setHorizontalGrabbing(fProjectField.getTextControl(null), true);
LayoutUtil.setWidthHint(fRootDialogField.getTextControl(null), maxFieldWidth);
// Bug #220003 : consistency between New Source Folder dialog and Source Location Property tab.
fExcludeInOthersFields.setSelection(true);
setControl(composite);
Dialog.applyDialogFont(composite);
PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, ICHelpContextIds.NEW_SRCFLDER_WIZARD_PAGE);
PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, ICHelpContextIds.NEW_SRCFLDER_WIZARD_PAGE);
}
/*
* @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
*/
@Override
public void setVisible(boolean visible) {
super.setVisible(visible);
if (visible) {
fRootDialogField.setFocus();
}
}
}
// -------- ContainerFieldAdapter --------
private class RootFieldAdapter implements IStringButtonAdapter, IDialogFieldListener {
// -------- IStringButtonAdapter
@Override
public void changeControlPressed(DialogField field) {
packRootChangeControlPressed(field);
}
// -------- IDialogFieldListener
@Override
public void dialogFieldChanged(DialogField field) {
packRootDialogFieldChanged(field);
@ -243,8 +233,8 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
protected void packRootChangeControlPressed(DialogField field) {
if (field == fRootDialogField) {
IPath initialPath= new Path(fRootDialogField.getText());
String title= NewFolderWizardMessages.NewSourceFolderWizardPage_ChooseExistingRootDialog_title;
String message= NewFolderWizardMessages.NewSourceFolderWizardPage_ChooseExistingRootDialog_description;
String title= NewFolderWizardMessages.NewSourceFolderWizardPage_ChooseExistingRootDialog_title;
String message= NewFolderWizardMessages.NewSourceFolderWizardPage_ChooseExistingRootDialog_description;
IFolder folder= chooseFolder(title, message, initialPath);
if (folder != null) {
IPath path= folder.getFullPath().removeFirstSegments(1);
@ -256,9 +246,9 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
IPath path= jproject.getProject().getFullPath().makeRelative();
fProjectField.setText(path.toString());
}
}
}
}
}
protected void packRootDialogFieldChanged(DialogField field) {
if (field == fRootDialogField) {
updateRootStatus();
@ -274,20 +264,20 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
private void updateProjectStatus() {
fCurrCProject= null;
fIsProjectAsSourceFolder= false;
String str= fProjectField.getText();
if (str.length() == 0) {
fProjectStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_EnterProjectName);
if (str.isEmpty()) {
fProjectStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_EnterProjectName);
return;
}
IPath path= new Path(str);
if (path.segmentCount() != 1) {
fProjectStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_InvalidProjectPath);
fProjectStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_InvalidProjectPath);
return;
}
IProject project= fWorkspaceRoot.getProject(path.toString());
if (!project.exists()) {
fProjectStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_ProjectNotExists);
fProjectStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_ProjectNotExists);
return;
}
try {
@ -300,8 +290,8 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
} catch (CoreException e) {
CUIPlugin.log(e);
fCurrCProject= null;
}
fProjectStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_NotACProject);
}
fProjectStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_NotACProject);
}
private void updateRootStatus() {
@ -311,44 +301,44 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
return;
}
fRootStatus.setOK();
IPath projPath= fCurrCProject.getProject().getFullPath();
String str= fRootDialogField.getText();
if (str.length() == 0) {
fRootStatus.setError(NLS.bind(NewFolderWizardMessages.NewSourceFolderWizardPage_error_EnterRootName, fCurrCProject.getProject().getFullPath().toString()));
fRootStatus.setError(NLS.bind(NewFolderWizardMessages.NewSourceFolderWizardPage_error_EnterRootName, fCurrCProject.getProject().getFullPath().toString()));
} else {
IPath path= projPath.append(str);
IStatus validate= fWorkspaceRoot.getWorkspace().validatePath(path.toString(), IResource.FOLDER);
if (validate.matches(IStatus.ERROR)) {
fRootStatus.setError(NLS.bind(NewFolderWizardMessages.NewSourceFolderWizardPage_error_InvalidRootName, validate.getMessage()));
fRootStatus.setError(NLS.bind(NewFolderWizardMessages.NewSourceFolderWizardPage_error_InvalidRootName, validate.getMessage()));
} else {
IResource res= fWorkspaceRoot.findMember(path);
if (res != null) {
if (res.getType() != IResource.FOLDER) {
fRootStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_NotAFolder);
fRootStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_NotAFolder);
return;
}
}
ArrayList<IPathEntry> newEntries= new ArrayList<>(fEntries.length + 1);
int projectEntryIndex= -1;
for (int i= 0; i < fEntries.length; i++) {
IPathEntry curr= fEntries[i];
if (curr.getEntryKind() == IPathEntry.CDT_SOURCE) {
if (path.equals(curr.getPath())) {
fRootStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_AlreadyExisting);
fRootStatus.setError(NewFolderWizardMessages.NewSourceFolderWizardPage_error_AlreadyExisting);
return;
}
if (projPath.equals(curr.getPath())) {
projectEntryIndex= i;
}
}
}
newEntries.add(curr);
}
IPathEntry newEntry= CoreModel.newSourceEntry(path);
Set<IPathEntry> modified= new HashSet<>();
Set<IPathEntry> modified= new HashSet<>();
if (fExcludeInOthersFields.isSelected()) {
InternalCoreModelUtil.addExclusionPatterns(newEntry, newEntries, modified);
newEntries.add(CoreModel.newSourceEntry(path));
@ -360,7 +350,7 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
newEntries.add(CoreModel.newSourceEntry(path));
}
}
fNewEntries= newEntries.toArray(new IPathEntry[newEntries.size()]);
ICModelStatus status= PathEntryManager.getDefault().validatePathEntry(fCurrCProject, fNewEntries);
@ -372,64 +362,54 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
fRootStatus.setError(status.getMessage());
return;
} else if (fIsProjectAsSourceFolder) {
fRootStatus.setInfo(NewFolderWizardMessages.NewSourceFolderWizardPage_warning_ReplaceSF);
fRootStatus.setInfo(NewFolderWizardMessages.NewSourceFolderWizardPage_warning_ReplaceSF);
return;
}
if (!modified.isEmpty()) {
fRootStatus.setInfo(NLS.bind(NewFolderWizardMessages.NewSourceFolderWizardPage_warning_AddedExclusions, String.valueOf(modified.size())));
fRootStatus.setInfo(NLS.bind(NewFolderWizardMessages.NewSourceFolderWizardPage_warning_AddedExclusions, String.valueOf(modified.size())));
return;
}
}
}
}
// ---- creation ----------------
public ISourceRoot getNewSourceRoot() {
return fCreatedRoot;
}
public IResource getCorrespondingResource() {
return fCurrCProject.getProject().getFolder(fRootDialogField.getText());
}
public void createSourceRoot(IProgressMonitor monitor) throws CoreException, InterruptedException {
if (monitor == null) {
monitor= new NullProgressMonitor();
public void createSourceRoot(IProgressMonitor monitor) throws CoreException {
SubMonitor progress = SubMonitor.convert(monitor,
NewFolderWizardMessages.NewSourceFolderWizardPage_operation, 3);
String relPath= fRootDialogField.getText();
IProject project = fCurrCProject.getProject();
IFolder folder= project.getFolder(relPath);
if (!folder.exists()) {
CoreUtility.createFolder(folder, true, true, progress.split(1));
}
monitor.beginTask(NewFolderWizardMessages.NewSourceFolderWizardPage_operation, 3);
try {
String relPath= fRootDialogField.getText();
IProject project = fCurrCProject.getProject();
IFolder folder= project.getFolder(relPath);
if (!folder.exists()) {
CoreUtility.createFolder(folder, true, true, new SubProgressMonitor(monitor, 1));
}
if (monitor.isCanceled()) {
throw new InterruptedException();
}
if (CCorePlugin.getDefault().isNewStyleProject(project)) {
InternalCoreModelUtil.addSourceEntry(project, folder, fIsProjectAsSourceFolder,
new SubProgressMonitor(monitor, 2));
} else {
fCurrCProject.setRawPathEntries(fNewEntries, new SubProgressMonitor(monitor, 2));
}
fCreatedRoot= fCurrCProject.findSourceRoot(folder);
} finally {
monitor.done();
if (CCorePlugin.getDefault().isNewStyleProject(project)) {
InternalCoreModelUtil.addSourceEntry(project, folder, fIsProjectAsSourceFolder, progress.split(2));
} else {
fCurrCProject.setRawPathEntries(fNewEntries, progress.split(2));
}
fCreatedRoot= fCurrCProject.findSourceRoot(folder);
}
// ------------- choose dialogs
private IFolder chooseFolder(String title, String message, IPath initialPath) {
private IFolder chooseFolder(String title, String message, IPath initialPath) {
Class<?>[] acceptedClasses= new Class<?>[] { IFolder.class };
ISelectionStatusValidator validator= new TypedElementSelectionValidator(acceptedClasses, false);
ViewerFilter filter= new TypedViewerFilter(acceptedClasses, null);
ViewerFilter filter= new TypedViewerFilter(acceptedClasses, null);
ILabelProvider lp= new WorkbenchLabelProvider();
ITreeContentProvider cp= new WorkbenchContentProvider();
@ -449,10 +429,10 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
if (dialog.open() == Window.OK) {
return (IFolder) dialog.getFirstResult();
}
return null;
}
return null;
}
private ICProject chooseProject() {
ICProject[] projects;
try {
@ -461,16 +441,16 @@ public class NewSourceFolderWizardPage extends NewElementWizardPage {
CUIPlugin.log(e);
projects= new ICProject[0];
}
ILabelProvider labelProvider= new CElementLabelProvider(CElementLabelProvider.SHOW_DEFAULT);
ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider);
dialog.setTitle(NewFolderWizardMessages.NewSourceFolderWizardPage_ChooseProjectDialog_title);
dialog.setMessage(NewFolderWizardMessages.NewSourceFolderWizardPage_ChooseProjectDialog_description);
dialog.setTitle(NewFolderWizardMessages.NewSourceFolderWizardPage_ChooseProjectDialog_title);
dialog.setMessage(NewFolderWizardMessages.NewSourceFolderWizardPage_ChooseProjectDialog_description);
dialog.setElements(projects);
dialog.setInitialSelections(new Object[] { fCurrCProject });
if (dialog.open() == Window.OK) {
if (dialog.open() == Window.OK) {
return (ICProject) dialog.getFirstResult();
}
return null;
}
return null;
}
}

View file

@ -568,6 +568,9 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
* Returns the method stubs to display in the wizard.
*
* @return array of method stubs
*
* @noreference This method is not intended to be referenced by clients.
* @nooverride This method is not intended to be re-implemented or extended by clients.
*/
protected IMethodStub[] getDefaultMethodStubs() {
return new IMethodStub[] {
@ -717,6 +720,9 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
* Returns the currently selected (checked) method stubs.
*
* @return array of <code>IMethodStub</code> or empty array if none selected.
*
* @noreference This method is not intended to be referenced by clients.
* @nooverride This method is not intended to be re-implemented or extended by clients.
*/
protected IMethodStub[] getSelectedMethodStubs() {
return fMethodStubsDialogField.getCheckedMethodStubs();
@ -724,8 +730,12 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
/**
* Adds a method stub to the method stubs field.
*
* @param methodStub the method stub to add
* @param selected <code>true</code> if the stub is initially selected (checked)
*
* @noreference This method is not intended to be referenced by clients.
* @nooverride This method is not intended to be re-implemented or extended by clients.
*/
protected void addMethodStub(IMethodStub methodStub, boolean selected) {
fMethodStubsDialogField.addMethodStub(methodStub, selected);
@ -735,6 +745,9 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
* Returns the contents of the base classes field.
*
* @return array of <code>IBaseClassInfo</code>
*
* @noreference This method is not intended to be referenced by clients.
* @nooverride This method is not intended to be re-implemented or extended by clients.
*/
protected IBaseClassInfo[] getBaseClasses() {
List<IBaseClassInfo> classesList = fBaseClassesDialogField.getElements();
@ -748,7 +761,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
* @param isVirtual <code>true</code> if the inheritance is virtual
*/
protected void addBaseClass(ITypeInfo newBaseClass, ASTAccessVisibility access, boolean isVirtual) {
// check if already exists
// Check if already exists.
List<IBaseClassInfo> baseClasses = fBaseClassesDialogField.getElements();
if (baseClasses != null) {
for (IBaseClassInfo baseClassInfo : baseClasses) {
@ -771,8 +784,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
* Sets the use test file creation checkbox's selection state.
*
* @param isSelected the checkbox's selection state
* @param canBeModified if <code>true</code> the checkbox is
* modifiable; otherwise it is read-only.
* @param canBeModified if <code>true</code> the checkbox is modifiable; otherwise it is read-only.
* @since 5.3
*/
public void setTestFileSelection(boolean isSelected, boolean canBeModified) {
@ -1725,6 +1737,9 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
* @param baseClasses an array of base classes
*
* @return the status of the validation
*
* @noreference This method is not intended to be referenced by clients.
* @nooverride This method is not intended to be re-implemented or extended by clients.
*/
protected IStatus baseClassesChanged(ICProject project, IPath sourceFolder, IBaseClassInfo[] baseClasses) {
MultiStatus status = new MultiStatus(CUIPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
@ -2020,9 +2035,8 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
*
* @param monitor a progress monitor to report progress.
* @throws CoreException Thrown when the creation failed.
* @throws InterruptedException Thrown when the operation was cancelled.
*/
public void createClass(IProgressMonitor monitor) throws CoreException, InterruptedException {
public void createClass(IProgressMonitor monitor) throws CoreException {
// Update dialog settings.
fDialogSettings.put(KEY_NAMESPACE_SELECTED, fNamespaceSelection.isSelected());
fDialogSettings.put(KEY_TEST_FILE_SELECTED, fTestFileSelection.isSelected());
@ -2075,12 +2089,14 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
* @param methodStubs array of method stubs
* @param monitor a progress monitor
* @throws CoreException if the creation failed
* @throws InterruptedException if the operation was cancelled
* @since 5.3
*
* @noreference This method is not intended to be referenced by clients.
* @nooverride This method is not intended to be re-implemented or extended by clients.
*/
protected void createClass(IPath headerPath, IPath sourcePath, IPath testPath, String className,
String namespace, IBaseClassInfo[] baseClasses, IMethodStub[] methodStubs, IProgressMonitor monitor)
throws CoreException, InterruptedException {
throws CoreException {
NewClassCodeGenerator generator = new NewClassCodeGenerator(
headerPath,
sourcePath,
@ -2098,16 +2114,19 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
fCreatedTestFile = generator.getCreatedTestFile();
}
/**
* @noreference This method is not intended to be referenced by clients.
* @nooverride This method is not intended to be re-implemented or extended by clients.
*/
protected void createClass(IPath headerPath, IPath sourcePath, String className, String namespace,
IBaseClassInfo[] baseClasses, IMethodStub[] methodStubs, IProgressMonitor monitor)
throws CoreException, InterruptedException {
createClass(headerPath, sourcePath, null, className, namespace, baseClasses, methodStubs,
monitor);
throws CoreException {
createClass(headerPath, sourcePath, null, className, namespace, baseClasses, methodStubs, monitor);
}
/**
* Returns the created class. The method only returns a valid class
* after <code>createClass</code> has been called.
* after {@link #createClass} has been called.
*
* @return the created class
* @see #createClass(IProgressMonitor)
@ -2118,7 +2137,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
/**
* Returns the created header file. The method only returns a valid file
* after <code>createClass</code> has been called.
* after {@link #createClass} has been called.
*
* @return the created header file
* @see #createClass(IProgressMonitor)
@ -2129,7 +2148,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
/**
* Returns the created source file. The method only returns a valid file
* after <code>createClass</code> has been called.
* after {@link #createClass} has been called.
*
* @return the created source file
* @see #createClass(IProgressMonitor)
@ -2140,7 +2159,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
/**
* Returns the created test file. The method only returns a valid file
* after <code>createClass</code> has been called.
* after {@link #createClass} has been called.
*
* @return the created test file
* @see #createClass(IProgressMonitor)

View file

@ -10,16 +10,17 @@
*******************************************************************************/
package org.eclipse.cdt.ui.wizards;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.CPluginImages;
import org.eclipse.cdt.internal.ui.wizards.NewElementWizard;
import org.eclipse.cdt.internal.ui.wizards.folderwizard.NewFolderWizardMessages;
import org.eclipse.cdt.internal.ui.wizards.folderwizard.NewSourceFolderWizardPage;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
public class NewSourceFolderCreationWizard extends NewElementWizard {
private NewSourceFolderWizardPage fPage;
public NewSourceFolderCreationWizard() {
@ -29,9 +30,6 @@ public class NewSourceFolderCreationWizard extends NewElementWizard {
setWindowTitle(NewFolderWizardMessages.NewSourceFolderCreationWizard_title);
}
/*
* @see Wizard#addPages
*/
@Override
public void addPages() {
super.addPages();
@ -40,17 +38,11 @@ public class NewSourceFolderCreationWizard extends NewElementWizard {
fPage.init(getSelection());
}
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.wizards.NewElementWizard#finishPage(org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException {
fPage.createSourceRoot(monitor); // use the full progress monitor
protected void finishPage(IProgressMonitor monitor) throws CoreException {
fPage.createSourceRoot(monitor); // Use the full progress monitor.
}
/* (non-Javadoc)
* @see org.eclipse.jface.wizard.IWizard#performFinish()
*/
@Override
public boolean performFinish() {
boolean res= super.performFinish();
@ -59,5 +51,4 @@ public class NewSourceFolderCreationWizard extends NewElementWizard {
}
return res;
}
}