1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-06 07:45:50 +02:00

Fix for PR 60650

* src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java
This commit is contained in:
Alain Magloire 2004-06-04 21:57:38 +00:00
parent a206e32d2a
commit 1e489f25e0
2 changed files with 117 additions and 82 deletions

View file

@ -1,3 +1,8 @@
2004-06-03 Alain Magloire
Fix for PR 60650
* src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java
2004-06-03 Alain Magloire 2004-06-03 Alain Magloire
Fix for PR 64197 60906 Fix for PR 64197 60906

View file

@ -196,7 +196,7 @@ public class NewClassWizardPage extends WizardPage implements Listener {
IResource resource = getSelectionResourceElement(currentSelection); IResource resource = getSelectionResourceElement(currentSelection);
if (resource != null) if (resource != null)
defaultSourceFolder = resource.getLocation().makeAbsolute(); defaultSourceFolder = resource.getFullPath();
if (fSelectedProject != null && hasCppNature && defaultSourceFolder != null) { if (fSelectedProject != null && hasCppNature && defaultSourceFolder != null) {
fAccessButtons.setEnabled(false); fAccessButtons.setEnabled(false);
setPageComplete(false); setPageComplete(false);
@ -577,83 +577,87 @@ public class NewClassWizardPage extends WizardPage implements Listener {
monitor.beginTask(NewWizardMessages.getString("NewTypeWizardPage.operationdesc"), 10); //$NON-NLS-1$ monitor.beginTask(NewWizardMessages.getString("NewTypeWizardPage.operationdesc"), 10); //$NON-NLS-1$
try { // resolve location of base class
// resolve location of base class String baseClassName = getBaseClassName();
String baseClassName = getBaseClassName(); ITypeInfo baseClass = null;
ITypeInfo baseClass = null; if ((baseClassName != null) && (baseClassName.length() > 0))
if ((baseClassName != null) && (baseClassName.length() > 0)) {
{ ITypeInfo[] classElements = findClassElementsInProject();
ITypeInfo[] classElements = findClassElementsInProject(); baseClass = findInList(classElements, new QualifiedTypeName(baseClassName));
baseClass = findInList(classElements, new QualifiedTypeName(baseClassName)); if (baseClass != null && baseClass.getResolvedReference() == null) {
if (baseClass != null && baseClass.getResolvedReference() == null) { final ITypeInfo[] typesToResolve = new ITypeInfo[] { baseClass };
final ITypeInfo[] typesToResolve = new ITypeInfo[] { baseClass }; IRunnableWithProgress runnable = new IRunnableWithProgress() {
IRunnableWithProgress runnable = new IRunnableWithProgress() { public void run(IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException {
public void run(IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException { AllTypesCache.resolveTypeLocation(typesToResolve[0], progressMonitor);
AllTypesCache.resolveTypeLocation(typesToResolve[0], progressMonitor); if (progressMonitor.isCanceled()) {
if (progressMonitor.isCanceled()) { throw new InterruptedException();
throw new InterruptedException();
}
} }
};
try {
getContainer().run(true, true, runnable);
} catch (InvocationTargetException e) {
String title= NewWizardMessages.getString("NewClassWizardPage.getProjectClasses.exception.title"); //$NON-NLS-1$
String message= NewWizardMessages.getString("NewClassWizardPage.getProjectClasses.exception.message"); //$NON-NLS-1$
ExceptionHandler.handle(e, title, message);
return false;
} catch (InterruptedException e) {
// cancelled by user
return false;
} }
};
try {
getContainer().run(true, true, runnable);
} catch (InvocationTargetException e) {
String title= NewWizardMessages.getString("NewClassWizardPage.getProjectClasses.exception.title"); //$NON-NLS-1$
String message= NewWizardMessages.getString("NewClassWizardPage.getProjectClasses.exception.message"); //$NON-NLS-1$
ExceptionHandler.handle(e, title, message);
return false;
} catch (InterruptedException e) {
// cancelled by user
return false;
} }
} }
String lineDelimiter= null;
lineDelimiter= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
parentHeaderTU = createTranslationUnit(linkedResourceGroupForHeader);
parentBodyTU = createTranslationUnit(linkedResourceGroupForBody);
monitor.worked(1);
if(parentHeaderTU != null){
String header = constructHeaderFileContent(parentHeaderTU, lineDelimiter, baseClass);
IWorkingCopy headerWC = parentHeaderTU.getSharedWorkingCopy(null, CUIPlugin.getDefault().getBufferFactory());
headerWC.getBuffer().append(header);
synchronized(headerWC) {
headerWC.reconcile();
headerWC.commit(true, monitor);
}
//createdClass= (IStructure)headerWC.getElement(getNewClassName());
createdClass= headerWC.getElement(getNewClassName());
headerWC.destroy();
}
if(parentBodyTU != null){
String body = constructBodyFileContent(lineDelimiter);
IWorkingCopy bodyWC = parentBodyTU.getSharedWorkingCopy(null, CUIPlugin.getDefault().getBufferFactory());
bodyWC.getBuffer().append(body);
synchronized(bodyWC){
bodyWC.reconcile();
bodyWC.commit(true, monitor);
}
bodyWC.destroy();
}
return true;
}catch(CModelException e){
MessageDialog.openError(getContainer().getShell(), WorkbenchMessages.getString("WizardNewFileCreationPage.internalErrorTitle"), WorkbenchMessages.format("WizardNewFileCreationPage.internalErrorMessage", new Object[] {e.getMessage()})); //$NON-NLS-2$ //$NON-NLS-1$
return false;
}finally{
monitor.done();
} }
String lineDelimiter= null;
lineDelimiter= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
parentHeaderTU = createTranslationUnit(linkedResourceGroupForHeader, true);
parentBodyTU = createTranslationUnit(linkedResourceGroupForBody, false);
monitor.worked(1);
if(parentHeaderTU != null && !parentHeaderTU.isReadOnly()){
String header = constructHeaderFileContent(parentHeaderTU, lineDelimiter, baseClass);
IWorkingCopy headerWC = null;
try {
headerWC = parentHeaderTU.getSharedWorkingCopy(null, CUIPlugin.getDefault().getBufferFactory());
headerWC.getBuffer().append(header);
headerWC.reconcile();
headerWC.commit(true, monitor);
//createdClass= (IStructure)headerWC.getElement(getNewClassName());
createdClass= headerWC.getElement(getNewClassName());
} catch (CModelException cme) {
MessageDialog.openError(getContainer().getShell(), WorkbenchMessages.getString("WizardNewFileCreationPage.internalErrorTitle"), cme.getMessage()); //$NON-NLS-2$ //$NON-NLS-1$
} finally {
if (headerWC != null) {
headerWC.destroy();
}
}
}
if(parentBodyTU != null && !parentBodyTU.isReadOnly()){
String body = constructBodyFileContent(lineDelimiter);
IWorkingCopy bodyWC = null;
try {
bodyWC = parentBodyTU.getSharedWorkingCopy(null, CUIPlugin.getDefault().getBufferFactory());
bodyWC.getBuffer().append(body);
bodyWC.reconcile();
bodyWC.commit(true, monitor);
} catch (CModelException cme) {
MessageDialog.openError(getContainer().getShell(), WorkbenchMessages.getString("WizardNewFileCreationPage.internalErrorTitle"), cme.getMessage()); //$NON-NLS-2$ //$NON-NLS-1$
} finally {
if (bodyWC != null) {
bodyWC.destroy();
}
}
}
monitor.done();
return true;
} }
protected ITranslationUnit createTranslationUnit(LinkToFileGroup linkedGroup){ protected ITranslationUnit createTranslationUnit(LinkToFileGroup linkedGroup, boolean isHeader){
ITranslationUnit createdUnit = null; ITranslationUnit createdUnit = null;
IFile createdFile = null; IFile createdFile = null;
createdFile= createNewFile(linkedGroup); createdFile= createNewFile(linkedGroup, isHeader);
// turn the file into a translation unit // turn the file into a translation unit
if(createdFile != null){ if(createdFile != null){
Object element= CoreModel.getDefault().create(createdFile); Object element= CoreModel.getDefault().create(createdFile);
@ -664,16 +668,16 @@ public class NewClassWizardPage extends WizardPage implements Listener {
return createdUnit; return createdUnit;
} }
protected IFile createNewFile(LinkToFileGroup linkedGroup) { protected IFile createNewFile(LinkToFileGroup linkedGroup, boolean isHeader) {
final IPath newFilePath = getContainerFullPath(linkedGroup); final IFile newFileHandle = createFileHandle(linkedGroup, isHeader);
final IFile newFileHandle = createFileHandle(newFilePath);
if(newFileHandle.exists()){ if(newFileHandle.exists()){
return newFileHandle; return newFileHandle;
} }
// create the new file and cache it if successful // create the new file and cache it if successful
final boolean linkedFile = linkedGroup.linkCreated(); final IPath newFilePath = getContainerFullPath(linkedGroup);
final boolean isLinkedFile = linkedGroup.linkCreated();
final IPath containerPath = getContainerPath(linkedGroup); final IPath containerPath = getContainerPath(linkedGroup);
final InputStream initialContents = getInitialContents(); final InputStream initialContents = getInitialContents();
@ -687,7 +691,7 @@ public class NewClassWizardPage extends WizardPage implements Listener {
ContainerGenerator generator = new ContainerGenerator(containerPath); ContainerGenerator generator = new ContainerGenerator(containerPath);
generator.generateContainer(new SubProgressMonitor(monitor, 1000)); generator.generateContainer(new SubProgressMonitor(monitor, 1000));
} }
createFile(newFileHandle,initialContents, newFilePath, linkedFile, new SubProgressMonitor(monitor, 1000)); createFile(newFileHandle,initialContents, newFilePath, isLinkedFile, new SubProgressMonitor(monitor, 1000));
} finally { } finally {
monitor.done(); monitor.done();
} }
@ -716,22 +720,30 @@ public class NewClassWizardPage extends WizardPage implements Listener {
return newFileHandle; return newFileHandle;
} }
protected IFile createFileHandle(IPath filePath) {
IFile newFile = null; protected IFile createFileHandle(LinkToFileGroup linkedGroup, boolean isHeader) {
IWorkspaceRoot root= CUIPlugin.getWorkspace().getRoot(); IWorkspaceRoot root= CUIPlugin.getWorkspace().getRoot();
newFile = root.getFileForLocation(filePath); if (linkedGroup.linkCreated()) {
IPath path = (isHeader) ? getHeaderFullPath() : getBodyFullPath();
return root.getFile(path);
}
IPath filePath = getContainerFullPath(linkedGroup);
IFile newFile = root.getFileForLocation(filePath);
if(newFile == null) if(newFile == null)
newFile = root.getFile(filePath); newFile = root.getFile(filePath);
return newFile; return newFile;
} }
protected void createFile(IFile fileHandle, InputStream contents, IPath targetPath, boolean linkedFile, IProgressMonitor monitor) throws CoreException { protected void createFile(IFile fileHandle, InputStream contents, IPath linkTargetPath, boolean isLinkedFile, IProgressMonitor monitor) throws CoreException {
if (contents == null) if (contents == null)
contents = new ByteArrayInputStream(new byte[0]); contents = new ByteArrayInputStream(new byte[0]);
try { try {
fileHandle.create(contents, false, monitor); if (isLinkedFile) {
fileHandle.createLink(linkTargetPath, IResource.ALLOW_MISSING_LOCAL, monitor);
} else {
fileHandle.create(contents, false, monitor);
}
} }
catch (CoreException e) { catch (CoreException e) {
// If the file already existed locally, just refresh to get contents // If the file already existed locally, just refresh to get contents
@ -760,7 +772,7 @@ public class NewClassWizardPage extends WizardPage implements Listener {
return defaultSourceFolder; return defaultSourceFolder;
} }
} }
/* /*
* returns the path including the file name * returns the path including the file name
*/ */
@ -780,6 +792,24 @@ public class NewClassWizardPage extends WizardPage implements Listener {
} }
} }
/**
* return the path of the new ClassName
* @return
*/
protected IPath getHeaderFullPath() {
String pathName = getNewClassName() + HEADER_EXT;
IPath containerPath = defaultSourceFolder;
//containerPath.addTrailingSeparator();
return ((containerPath.append(pathName)).makeAbsolute());
}
protected IPath getBodyFullPath() {
String pathName = getNewClassName() + BODY_EXT;
IPath containerPath = defaultSourceFolder;
//containerPath.addTrailingSeparator();
return ((containerPath.append(pathName)).makeAbsolute());
}
protected boolean containerExists(IPath containerPath) { protected boolean containerExists(IPath containerPath) {
IContainer container = null; IContainer container = null;
IWorkspaceRoot root= CUIPlugin.getWorkspace().getRoot(); IWorkspaceRoot root= CUIPlugin.getWorkspace().getRoot();