1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

2005-03-29 Chris Wiebe

fix PR#87328 by making class wizard page publicly accessible
	refactored wizard code and added comments
	* src/org/eclipse/cdt/internal/ui/wizards/AbstractMethodStub.java
	* src/org/eclipse/cdt/internal/ui/wizards/ConstructorMethodStub.java
	* src/org/eclipse/cdt/internal/ui/wizards/DestructorMethodStub.java
	* src/org/eclipse/cdt/internal/ui/wizards/IMethodStub.java
	* src/org/eclipse/cdt/internal/ui/wizards/NewBaseClassSelectionDialog.java
	* src/org/eclipse/cdt/internal/ui/wizards/NewClassCodeGenerator.java
	* src/org/eclipse/cdt/internal/ui/wizards/NewClassWizardMessages.properties
	* src/org/eclipse/cdt/internal/ui/wizards/DestructorMethodStub.java
	* src/org/eclipse/cdt/internal/ui/wizards/DestructorMethodStub.java
	* src/org/eclipse/cdt/ui/wizards/NewClassCreationWizardPage.java
	* src/org/eclipse/cdt/internal/ui/wizards/NewClassCreationWizard.java
	* src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardPrefs.java
	* src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardUtil.java
	* plugin.xml
	removed:
	* src/org/eclipse/cdt/ui/wizards/NewClassCreationWizard.java
	* src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCreationWizardPage.java
This commit is contained in:
Chris Wiebe 2005-03-30 00:17:45 +00:00
parent 1e4dae1a86
commit 6acaa7fee9
16 changed files with 2555 additions and 2290 deletions

View file

@ -1,3 +1,25 @@
2005-03-29 Chris Wiebe
fix PR#87328 by making class wizard page publicly accessible
refactored wizard code and added comments
* src/org/eclipse/cdt/internal/ui/wizards/AbstractMethodStub.java
* src/org/eclipse/cdt/internal/ui/wizards/ConstructorMethodStub.java
* src/org/eclipse/cdt/internal/ui/wizards/DestructorMethodStub.java
* src/org/eclipse/cdt/internal/ui/wizards/IMethodStub.java
* src/org/eclipse/cdt/internal/ui/wizards/NewBaseClassSelectionDialog.java
* src/org/eclipse/cdt/internal/ui/wizards/NewClassCodeGenerator.java
* src/org/eclipse/cdt/internal/ui/wizards/NewClassWizardMessages.properties
* src/org/eclipse/cdt/internal/ui/wizards/DestructorMethodStub.java
* src/org/eclipse/cdt/internal/ui/wizards/DestructorMethodStub.java
* src/org/eclipse/cdt/ui/wizards/NewClassCreationWizardPage.java
* src/org/eclipse/cdt/internal/ui/wizards/NewClassCreationWizard.java
* src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardPrefs.java
* src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardUtil.java
* plugin.xml
removed:
* src/org/eclipse/cdt/ui/wizards/NewClassCreationWizard.java
* src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCreationWizardPage.java
2005-03-29 Vladimir Hirsl
Removing indexer problem markers when problem reporting is deselected.

View file

@ -109,7 +109,7 @@ CEditor.name=C/C++ Editor
CPluginPreferencePage.name=C/C++
CPluginEditorPreferencePage.name=Editor
CPluginTemplatePreferencePage.name=Code Templates
CPluginTemplatePreferencePage.name=Templates
CPluginBuildConsolePreferencePage.name=Build Console
CPluginFileTypesPreferencePage.name=File Types
CodeFormatterPreferencePage.name=Code Formatter

View file

@ -388,7 +388,7 @@
category="org.eclipse.cdt.ui.newCCWizards"
finalPerspective="org.eclipse.cdt.ui.CPerspective"
id="org.eclipse.cdt.ui.wizards.NewClassCreationWizard">
<class class="org.eclipse.cdt.ui.wizards.NewClassCreationWizard">
<class class="org.eclipse.cdt.internal.ui.wizards.NewClassCreationWizard">
<parameter name="ctype" value="true"/>
<parameter targetId="org.eclipse.cdt.ui.actions.NewTypeDropDown"/>
</class>

View file

@ -8,16 +8,13 @@
* Contributors:
* QNX Software Systems - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.ui.wizards;
package org.eclipse.cdt.internal.ui.wizards;
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.internal.ui.wizards.classwizard.NewClassCreationWizardPage;
import org.eclipse.cdt.internal.ui.wizards.classwizard.NewClassWizardMessages;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.wizards.NewClassCreationWizardPage;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
@ -48,7 +45,7 @@ public class NewClassCreationWizard extends NewElementWizard {
* @see org.eclipse.cdt.internal.ui.wizards.NewElementWizard#canRunForked()
*/
protected boolean canRunForked() {
return !fPage.isEnclosingTypeSelected();
return !fPage.isNamespaceSelected();
}
/*
@ -66,28 +63,21 @@ public class NewClassCreationWizard extends NewElementWizard {
* @see org.eclipse.jface.wizard.IWizard#performFinish()
*/
public boolean performFinish() {
boolean res = super.performFinish();
if (res) {
//TODO need prefs option for opening editor
boolean openInEditor = true;
ITranslationUnit bodyTU = fPage.getCreatedSourceTU();
if (bodyTU != null) {
IResource resource= bodyTU.getResource();
selectAndReveal(resource);
if (openInEditor) {
openResource((IFile) resource);
boolean finished = super.performFinish();
if (finished) {
if (fPage.openClassInEditor()) {
IFile source = fPage.getCreatedSourceFile();
if (source != null) {
selectAndReveal(source);
openResource(source);
}
}
ITranslationUnit headerTU = fPage.getCreatedHeaderTU();
if (headerTU != null) {
IResource resource = headerTU.getResource();
selectAndReveal(resource);
if (openInEditor) {
openResource((IFile) resource);
IFile header = fPage.getCreatedHeaderFile();
if (header != null) {
selectAndReveal(header);
openResource(header);
}
}
}
return res;
return finished;
}
}

View file

@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.classwizard;
import org.eclipse.cdt.core.browser.IQualifiedTypeName;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
public abstract class AbstractMethodStub implements IMethodStub {
@ -79,7 +78,7 @@ public abstract class AbstractMethodStub implements IMethodStub {
return false;
}
public abstract String createMethodDeclaration(IQualifiedTypeName className, IBaseClassInfo[] baseClasses, String lineDelimiter);
public abstract String createMethodDeclaration(String className, IBaseClassInfo[] baseClasses, String lineDelimiter);
public abstract String createMethodImplementation(IQualifiedTypeName className, IBaseClassInfo[] baseClasses, String lineDelimiter);
public abstract String createMethodImplementation(String className, IBaseClassInfo[] baseClasses, String lineDelimiter);
}

View file

@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.classwizard;
import org.eclipse.cdt.core.browser.IQualifiedTypeName;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
@ -26,10 +25,10 @@ public final class ConstructorMethodStub extends AbstractMethodStub {
super(NAME, access, false, isInline);
}
public String createMethodDeclaration(IQualifiedTypeName className, IBaseClassInfo[] baseClasses, String lineDelimiter) {
public String createMethodDeclaration(String className, IBaseClassInfo[] baseClasses, String lineDelimiter) {
//TODO should use code templates
StringBuffer buf = new StringBuffer();
buf.append(className.toString());
buf.append(className);
buf.append("()"); //$NON-NLS-1$
if (fIsInline) {
buf.append(" {}"); //$NON-NLS-1$
@ -39,13 +38,13 @@ public final class ConstructorMethodStub extends AbstractMethodStub {
return buf.toString();
}
public String createMethodImplementation(IQualifiedTypeName className, IBaseClassInfo[] baseClasses, String lineDelimiter) {
public String createMethodImplementation(String className, IBaseClassInfo[] baseClasses, String lineDelimiter) {
//TODO should use code templates
if (fIsInline) {
return ""; //$NON-NLS-1$
}
StringBuffer buf = new StringBuffer();
buf.append(className.toString());
buf.append(className);
buf.append("::"); //$NON-NLS-1$
buf.append(className.toString());
buf.append("()"); //$NON-NLS-1$

View file

@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.classwizard;
import org.eclipse.cdt.core.browser.IQualifiedTypeName;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
@ -26,14 +25,14 @@ public final class DestructorMethodStub extends AbstractMethodStub {
super(NAME, access, isVirtual, isInline);
}
public String createMethodDeclaration(IQualifiedTypeName className, IBaseClassInfo[] baseClasses, String lineDelimiter) {
public String createMethodDeclaration(String className, IBaseClassInfo[] baseClasses, String lineDelimiter) {
//TODO should use code templates
StringBuffer buf = new StringBuffer();
if (fIsVirtual){
buf.append("virtual "); //$NON-NLS-1$
}
buf.append("~"); //$NON-NLS-1$
buf.append(className.toString());
buf.append(className);
buf.append("()"); //$NON-NLS-1$
if (fIsInline) {
buf.append(" {}"); //$NON-NLS-1$
@ -43,15 +42,15 @@ public final class DestructorMethodStub extends AbstractMethodStub {
return buf.toString();
}
public String createMethodImplementation(IQualifiedTypeName className, IBaseClassInfo[] baseClasses, String lineDelimiter) {
public String createMethodImplementation(String className, IBaseClassInfo[] baseClasses, String lineDelimiter) {
//TODO should use code templates
if (fIsInline) {
return ""; //$NON-NLS-1$
}
StringBuffer buf = new StringBuffer();
buf.append(className.toString());
buf.append(className);
buf.append("::~"); //$NON-NLS-1$
buf.append(className.toString());
buf.append(className);
buf.append("()"); //$NON-NLS-1$
buf.append(lineDelimiter);
buf.append('{');

View file

@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.classwizard;
import org.eclipse.cdt.core.browser.IQualifiedTypeName;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
@ -33,6 +32,6 @@ public interface IMethodStub {
public boolean isConstructor();
public boolean isDestructor();
public String createMethodDeclaration(IQualifiedTypeName className, IBaseClassInfo[] baseClasses, String lineDelimiter);
public String createMethodImplementation(IQualifiedTypeName className, IBaseClassInfo[] baseClasses, String lineDelimiter);
public String createMethodDeclaration(String className, IBaseClassInfo[] baseClasses, String lineDelimiter);
public String createMethodImplementation(String className, IBaseClassInfo[] baseClasses, String lineDelimiter);
}

View file

@ -10,21 +10,16 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.classwizard;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.cdt.core.browser.AllTypesCache;
import org.eclipse.cdt.core.browser.ITypeInfo;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.internal.ui.dialogs.StatusInfo;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
import org.eclipse.cdt.ui.browser.typeinfo.TypeSelectionDialog;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
@ -105,36 +100,47 @@ public class NewBaseClassSelectionDialog extends TypeSelectionDialog {
private void addType(Object elem) {
if (elem instanceof ITypeInfo) {
ITypeInfo type = (ITypeInfo)elem;
if (!fTypeList.contains(type)) {
if (fTypeList.contains(type)) {
String qualifiedName = type.getQualifiedTypeName().getFullyQualifiedName();
String message = NewClassWizardMessages.getFormattedString("NewBaseClassSelectionDialog.classalreadyadded.info", qualifiedName); //$NON-NLS-1$
updateStatus(new StatusInfo(IStatus.INFO, message));
} else {
String qualifiedName = type.getQualifiedTypeName().getFullyQualifiedName();
String message = NewClassWizardMessages.getFormattedString("NewBaseClassSelectionDialog.addingclass.info", qualifiedName); //$NON-NLS-1$
updateStatus(new StatusInfo(IStatus.INFO, message));
// resolve location of base class
if (type.getResolvedReference() == null) {
final ITypeInfo[] typesToResolve = new ITypeInfo[] { type };
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException {
AllTypesCache.resolveTypeLocation(typesToResolve[0], progressMonitor);
if (progressMonitor.isCanceled()) {
throw new InterruptedException();
}
}
};
boolean canAdd = true;
if (verifyBaseClasses()) {
IProgressService service = PlatformUI.getWorkbench().getProgressService();
try {
service.busyCursorWhile(runnable);
} catch (InvocationTargetException e) {
String title= NewClassWizardMessages.getString("NewBaseClassSelectionDialog.getClasses.exception.title"); //$NON-NLS-1$
String errorMessage= NewClassWizardMessages.getString("NewBaseClassSelectionDialog.getClasses.exception.message"); //$NON-NLS-1$
ExceptionHandler.handle(e, title, errorMessage);
} catch (InterruptedException e) {
// cancelled by user
}
NewClassWizardUtil.resolveClassLocation(type, service);
canAdd = (type.getResolvedReference() != null);
}
if (type.getResolvedReference() != null) {
// // resolve location of base class
// if (type.getResolvedReference() == null) {
// final ITypeInfo[] typesToResolve = new ITypeInfo[] { type };
// IRunnableWithProgress runnable = new IRunnableWithProgress() {
// public void run(IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException {
// AllTypesCache.resolveTypeLocation(typesToResolve[0], progressMonitor);
// if (progressMonitor.isCanceled()) {
// throw new InterruptedException();
// }
// }
// };
//
// IProgressService service = PlatformUI.getWorkbench().getProgressService();
// try {
// service.busyCursorWhile(runnable);
// } catch (InvocationTargetException e) {
// String title= NewClassWizardMessages.getString("NewBaseClassSelectionDialog.getClasses.exception.title"); //$NON-NLS-1$
// String errorMessage= NewClassWizardMessages.getString("NewBaseClassSelectionDialog.getClasses.exception.message"); //$NON-NLS-1$
// ExceptionHandler.handle(e, title, errorMessage);
// } catch (InterruptedException e) {
// // cancelled by user
// }
// }
if (canAdd) {
fTypeList.add(type);
message = NewClassWizardMessages.getFormattedString("NewBaseClassSelectionDialog.classadded.info", qualifiedName); //$NON-NLS-1$
@ -145,14 +151,19 @@ public class NewBaseClassSelectionDialog extends TypeSelectionDialog {
message = NewClassWizardMessages.getFormattedString("NewBaseClassSelectionDialog.error.classnotadded", qualifiedName); //$NON-NLS-1$
updateStatus(new StatusInfo(IStatus.ERROR, message));
}
} else {
String qualifiedName = type.getQualifiedTypeName().getFullyQualifiedName();
String message = NewClassWizardMessages.getFormattedString("NewBaseClassSelectionDialog.classalreadyadded.info", qualifiedName); //$NON-NLS-1$
updateStatus(new StatusInfo(IStatus.INFO, message));
}
}
}
/**
* Checks if the base classes need to be verified (ie they must exist in the project)
*
* @return <code>true</code> if the base classes should be verified
*/
public boolean verifyBaseClasses() {
return NewClassWizardPrefs.verifyBaseClasses();
}
/*
* @see AbstractElementListSelectionDialog#handleDefaultSelected()
*/

View file

@ -18,6 +18,7 @@ import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.browser.IQualifiedTypeName;
import org.eclipse.cdt.core.browser.ITypeReference;
import org.eclipse.cdt.core.browser.PathUtil;
import org.eclipse.cdt.core.browser.QualifiedTypeName;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICContainer;
@ -32,20 +33,23 @@ import org.eclipse.cdt.core.parser.IScannerInfoProvider;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
import org.eclipse.cdt.internal.corext.util.CModelUtil;
import org.eclipse.cdt.internal.ui.wizards.filewizard.NewSourceFileGenerator;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
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;
public class NewClassCodeGenerator {
private IPath fHeaderPath = null;
private IPath fSourcePath = null;
private IQualifiedTypeName fClassName = null;
private String fClassName = null;
private IQualifiedTypeName fNamespace = null;
private String fLineDelimiter;
private IBaseClassInfo[] fBaseClasses = null;
@ -54,25 +58,30 @@ public class NewClassCodeGenerator {
private ITranslationUnit fCreatedSourceTU = null;
private ICElement fCreatedClass = null;
//TODO this should be a prefs option
private boolean fCreateIncludePaths = true;
public static class CodeGeneratorException extends Exception {
public static class CodeGeneratorException extends CoreException {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
public CodeGeneratorException(String message) {
super(new Status(Status.ERROR, CUIPlugin.getPluginId(), IStatus.OK, message, null));
}
public CodeGeneratorException(Throwable e) {
super(new Status(Status.ERROR, CUIPlugin.getPluginId(), IStatus.OK, e.getMessage(), e));
}
}
public NewClassCodeGenerator(IPath headerPath, IPath sourcePath, IQualifiedTypeName className, IQualifiedTypeName namespace, IBaseClassInfo[] baseClasses, IMethodStub[] methodStubs) {
public NewClassCodeGenerator(IPath headerPath, IPath sourcePath, String className, String namespace, IBaseClassInfo[] baseClasses, IMethodStub[] methodStubs) {
fHeaderPath = headerPath;
fSourcePath = sourcePath;
if (className != null && className.length() > 0) {
fClassName = className;
fNamespace = namespace;
}
if (namespace != null && namespace.length() > 0) {
fNamespace = new QualifiedTypeName(namespace);
}
fBaseClasses = baseClasses;
fMethodStubs = methodStubs;
fLineDelimiter = NewSourceFileGenerator.getLineDelimiter();
@ -86,10 +95,24 @@ public class NewClassCodeGenerator {
return fCreatedHeaderTU;
}
public IFile getCreatedHeaderFile() {
if (fCreatedHeaderTU != null) {
return (IFile)fCreatedHeaderTU.getResource();
}
return null;
}
public ITranslationUnit getCreatedSourceTU() {
return fCreatedSourceTU;
}
public IFile getCreatedSourceFile() {
if (fCreatedSourceTU != null) {
return (IFile)fCreatedSourceTU.getResource();
}
return null;
}
/**
* Creates the new class.
*
@ -393,10 +416,10 @@ public class NewClassCodeGenerator {
IPath headerLocation = headerTU.getResource().getLocation();
List includePaths = getIncludePaths(headerTU);
List baseClassPaths = getBaseClassPaths();
List baseClassPaths = getBaseClassPaths(verifyBaseClasses());
// add the missing include paths to the project
if (fCreateIncludePaths) {
if (createIncludePaths()) {
List newIncludePaths = getMissingIncludePaths(projectLocation, includePaths, baseClassPaths);
if (!newIncludePaths.isEmpty()) {
addIncludePaths(cProject, newIncludePaths, monitor);
@ -450,6 +473,24 @@ public class NewClassCodeGenerator {
monitor.done();
}
/**
* Checks if the base classes need to be verified (ie they must exist in the project)
*
* @return <code>true</code> if the base classes should be verified
*/
private boolean verifyBaseClasses() {
return NewClassWizardPrefs.verifyBaseClasses();
}
/**
* Checks if include paths can be added to the project as needed.
*
* @return <code>true</code> if the include paths should be added
*/
private boolean createIncludePaths() {
return NewClassWizardPrefs.createIncludePaths();
}
private void addIncludePaths(ICProject cProject, List newIncludePaths, IProgressMonitor monitor) throws CodeGeneratorException {
monitor.beginTask(NewClassWizardMessages.getString("NewClassCodeGeneration.createType.task.header.addIncludePaths"), 100); //$NON-NLS-1$
@ -562,19 +603,24 @@ public class NewClassCodeGenerator {
return null;
}
private List getBaseClassPaths() throws CodeGeneratorException {
private List getBaseClassPaths(boolean verifyLocation) throws CodeGeneratorException {
List list = new ArrayList();
for (int i = 0; i < fBaseClasses.length; ++i) {
IBaseClassInfo baseClass = fBaseClasses[i];
ITypeReference ref = baseClass.getType().getResolvedReference();
IPath baseClassLocation = null;
if (ref != null)
if (ref != null) {
baseClassLocation = ref.getLocation();
}
if (baseClassLocation == null) {
if (verifyLocation) {
throw new CodeGeneratorException("Could not find base class " + baseClass.toString()); //$NON-NLS-1$
}
} else {
list.add(baseClassLocation);
}
}
return list;
}

View file

@ -35,14 +35,8 @@ NewClassCreationWizardPage.error.ProjectClosed=Project ''{0}'' must be accessibl
NewClassCreationWizardPage.warning.NotACProject=Folder is not a C/C++ project.
NewClassCreationWizardPage.warning.NotInACProject=Folder is not in a C/C++ project.
# NewClassCreationWizardPage.enclosingType.label=&Enclosing Type:
NewClassCreationWizardPage.enclosingType.label=&Namespace:
NewClassCreationWizardPage.enclosingType.namespace=Namespace
NewClassCreationWizardPage.enclosingType.class=Class
NewClassCreationWizardPage.enclosingType.button=Bro&wse...
NewClassCreationWizardPage.enclosingClassAccess.public=public
NewClassCreationWizardPage.enclosingClassAccess.protected=protected
NewClassCreationWizardPage.enclosingClassAccess.private=private
NewClassCreationWizardPage.namespace.label=&Namespace:
NewClassCreationWizardPage.namespace.button=Bro&wse...
NewClassCreationWizardPage.error.EnterNamespace=Namespace is empty.
NewClassCreationWizardPage.error.EnclosingNamespaceNotExists=Enclosing namespace does not exist.

View file

@ -0,0 +1,37 @@
package org.eclipse.cdt.internal.ui.wizards.classwizard;
public class NewClassWizardPrefs {
/**
* Checks if the base classes need to be verified (ie they must exist in the project)
*
* @return <code>true</code> if the base classes should be verified
*/
public static boolean verifyBaseClasses() {
//TODO this should be a prefs option
return true;
}
/**
* Checks if include paths can be added to the project as needed.
*
* @return <code>true</code> if the include paths should be added
*/
public static boolean createIncludePaths() {
//TODO this should be a prefs option
return true;
}
/**
* Returns whether the generated header and source files should be
* opened in editors after the finish button is pressed.
*
* @return <code>true</code> if the header and source file should be
* displayed
*/
public static boolean openClassInEditor() {
//TODO this should be a prefs option
return true;
}
}

View file

@ -0,0 +1,398 @@
package org.eclipse.cdt.internal.ui.wizards.classwizard;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.browser.AllTypesCache;
import org.eclipse.cdt.core.browser.ITypeInfo;
import org.eclipse.cdt.core.browser.ITypeReference;
import org.eclipse.cdt.core.browser.ITypeSearchScope;
import org.eclipse.cdt.core.browser.TypeSearchScope;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICContainer;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ISourceRoot;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IScannerInfoProvider;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
import org.eclipse.cdt.internal.ui.viewsupport.IViewPartInputProvider;
import org.eclipse.cdt.internal.ui.wizards.filewizard.NewSourceFileGenerator;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.contentoutline.ContentOutline;
public class NewClassWizardUtil {
/**
* Returns the parent source folder of the given element. If the given
* element is already a source folder, the element itself is returned.
*
* @param element the C Element
* @return the source folder
*/
public static ICContainer getSourceFolder(ICElement element) {
ICContainer folder = null;
boolean foundSourceRoot = false;
ICElement curr = element;
while (curr != null && !foundSourceRoot) {
if (curr instanceof ICContainer && folder == null) {
folder = (ICContainer)curr;
}
foundSourceRoot = (curr instanceof ISourceRoot);
curr = curr.getParent();
}
if (folder == null) {
ICProject cproject = element.getCProject();
folder = cproject.findSourceRoot(cproject.getProject());
}
return folder;
}
/**
* Returns the parent source folder for the given path. If the given
* path is already a source folder, the corresponding C element is returned.
*
* @param path the path
* @return the source folder
*/
public static ICContainer getSourceFolder(IPath path) {
if (path == null)
return null;
while (!path.isEmpty()) {
IResource res = getWorkspaceRoot().findMember(path);
if (res != null && res.exists()) {
int resType = res.getType();
if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
ICElement elem = CoreModel.getDefault().create(res.getFullPath());
if (elem != null) {
ICContainer sourceFolder = getSourceFolder(elem);
if (sourceFolder != null)
return sourceFolder;
if (resType == IResource.PROJECT) {
return (ICContainer)elem;
}
}
}
}
path = path.removeLastSegments(1);
}
return null;
}
/**
* Returns the parent source folder for the given resource. If the given
* resource is already a source folder, the corresponding C element is returned.
*
* @param resource the resource
* @return the source folder
*/
public static ICContainer getSourceFolder(IResource resource) {
if (resource != null && resource.exists()) {
int resType = resource.getType();
if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
ICElement elem = CoreModel.getDefault().create(resource.getFullPath());
if (elem != null) {
ICContainer sourceFolder = getSourceFolder(elem);
if (sourceFolder != null)
return sourceFolder;
if (resType == IResource.PROJECT) {
return (ICContainer)elem;
}
}
} else {
return getSourceFolder(resource.getParent());
}
}
return null;
}
/**
* Returns the first source root in the given project. If the project has
* no source roots as children, the project itself is returned.
*
* @param cproject
* @return the source root
*/
public static ISourceRoot getFirstSourceRoot(ICProject cproject) {
ISourceRoot folder = null;
try {
if (cproject.exists()) {
ISourceRoot[] roots = cproject.getSourceRoots();
if (roots != null && roots.length > 0)
folder = roots[0];
}
} catch (CModelException e) {
CUIPlugin.getDefault().log(e);
}
if (folder == null) {
folder = cproject.findSourceRoot(cproject.getResource());
}
return folder;
}
/**
* Returns the C Element which corresponds to the given selection.
*
* @param selection the selection to be inspected
* @return a C element matching the selection, or <code>null</code>
* if no C element exists in the given selection
*/
public static ICElement getCElementFromSelection(IStructuredSelection selection) {
ICElement celem = null;
if (selection != null && !selection.isEmpty()) {
Object selectedElement = selection.getFirstElement();
if (selectedElement instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) selectedElement;
celem = (ICElement) adaptable.getAdapter(ICElement.class);
if (celem == null) {
IResource resource = (IResource) adaptable.getAdapter(IResource.class);
if (resource != null && resource.getType() != IResource.ROOT) {
while (celem == null && resource.getType() != IResource.PROJECT) {
resource = resource.getParent();
celem = (ICElement) resource.getAdapter(ICElement.class);
}
if (celem == null) {
celem = CoreModel.getDefault().create(resource); // c project
}
}
}
}
}
return celem;
}
/**
* Returns the C Element which corresponds to the active editor.
*
* @return a C element matching the active editor, or <code>null</code>
* if no C element can be found
*/
public static ICElement getCElementFromEditor() {
ICElement celem = null;
IWorkbenchPart part = CUIPlugin.getActivePage().getActivePart();
if (part instanceof ContentOutline) {
part = CUIPlugin.getActivePage().getActiveEditor();
}
if (part instanceof IViewPartInputProvider) {
Object elem = ((IViewPartInputProvider)part).getViewPartInput();
if (elem instanceof ICElement) {
celem = (ICElement) elem;
}
}
if (celem == null && part instanceof CEditor) {
IEditorInput input = ((IEditorPart)part).getEditorInput();
if (input != null) {
final IResource res = (IResource) input.getAdapter(IResource.class);
if (res != null && res instanceof IFile) {
celem = CoreModel.getDefault().create((IFile)res);
}
}
}
return celem;
}
/**
* Returns the parent namespace for the given element. If the given element is
* already a namespace, the element itself is returned.
*
* @param element the given C Element
* @return the C Element for the namespace, or <code>null</code> if not found
*/
public static ICElement getNamespace(ICElement element) {
ICElement curr = element;
while (curr != null) {
int type = curr.getElementType();
if (type == ICElement.C_UNIT) {
break;
}
if (type == ICElement.C_NAMESPACE) {
return curr;
}
curr = curr.getParent();
}
return null;
}
/**
* Creates a header file name from the given class name. This is the file name
* to be used when the class is created. eg. "MyClass" -> "MyClass.h"
*
* @param className the class name
* @return the header file name for the given class
*/
public static String createHeaderFileName(String className) {
return NewSourceFileGenerator.generateHeaderFileNameFromClass(className);
}
/**
* Creates a source file name from the given class name. This is the file name
* to be used when the class is created. eg. "MyClass" -> "MyClass.cpp"
*
* @param className the class name
* @return the source file name for the given class
*/
public static String createSourceFileName(String className) {
return NewSourceFileGenerator.generateSourceFileNameFromClass(className);
}
/**
* Returns the workspace root.
*
* @return the workspace root
*/
public static IWorkspaceRoot getWorkspaceRoot() {
return ResourcesPlugin.getWorkspace().getRoot();
}
/**
* Ensures the type cache is up to date.
*
* @param context the runnable context
*/
public static void prepareTypeCache(IRunnableContext context) {
final ITypeSearchScope scope = new TypeSearchScope(true);
if (!AllTypesCache.isCacheUpToDate(scope)) {
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
AllTypesCache.updateCache(scope, monitor);
if (monitor.isCanceled()) {
throw new InterruptedException();
}
}
};
try {
context.run(true, true, runnable);
} catch (InvocationTargetException e) {
String title = NewClassWizardMessages.getString("NewClassCreationWizardPage.getTypes.exception.title"); //$NON-NLS-1$
String message = NewClassWizardMessages.getString("NewClassCreationWizardPage.getTypes.exception.message"); //$NON-NLS-1$
ExceptionHandler.handle(e, title, message);
} catch (InterruptedException e) {
// cancelled by user
}
}
}
/**
* Resolve the location of the given class.
*
* @param type the class to resolve
* @param context the runnable context
* @return the class location, or <code>null</code> if not found
*/
public static ITypeReference resolveClassLocation(ITypeInfo type, IRunnableContext context) {
prepareTypeCache(context);
// resolve location of base class
if (type.getResolvedReference() == null) {
final ITypeInfo[] typesToResolve = new ITypeInfo[] { type };
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException {
AllTypesCache.resolveTypeLocation(typesToResolve[0], progressMonitor);
if (progressMonitor.isCanceled()) {
throw new InterruptedException();
}
}
};
try {
context.run(true, true, runnable);
} catch (InvocationTargetException e) {
String title = NewClassWizardMessages.getString("NewClassCreationWizardPage.getTypes.exception.title"); //$NON-NLS-1$
String message = NewClassWizardMessages.getString("NewClassCreationWizardPage.getTypes.exception.message"); //$NON-NLS-1$
ExceptionHandler.handle(e, title, message);
return null;
} catch (InterruptedException e) {
// cancelled by user
return null;
}
}
return type.getResolvedReference();
}
private static final int[] CLASS_TYPES = { ICElement.C_CLASS, ICElement.C_STRUCT };
/**
* Returns all classes/structs which are accessible from the include
* paths of the given project.
*
* @param project the given project
* @return array of classes/structs
*/
public static ITypeInfo[] getReachableClasses(IProject project) {
ITypeInfo[] elements = AllTypesCache.getTypes(new TypeSearchScope(true), CLASS_TYPES);
if (elements != null && elements.length > 0) {
if (project != null) {
IScannerInfoProvider provider = CCorePlugin.getDefault().getScannerInfoProvider(project);
if (provider != null) {
//TODO get the scanner info for the actual source folder
IScannerInfo info = provider.getScannerInformation(project);
if (info != null) {
String[] includePaths = info.getIncludePaths();
List filteredTypes = new ArrayList();
for (int i = 0; i < elements.length; ++i) {
ITypeInfo baseType = elements[i];
if (isTypeReachable(baseType, project, includePaths)) {
filteredTypes.add(baseType);
}
}
return (ITypeInfo[]) filteredTypes.toArray(new ITypeInfo[filteredTypes.size()]);
}
}
}
}
return elements;
}
/**
* Checks whether the given type can be found in the given project or the
* given include paths.
*
* @param type the type
* @param project the project
* @param includePaths the include paths
* @return <code>true</code> if the given type is found
*/
public static boolean isTypeReachable(ITypeInfo type, IProject project, String[] includePaths) {
IProject baseProject = type.getEnclosingProject();
if (baseProject != null) {
if (baseProject.equals(project)) {
return true;
}
ITypeReference ref = type.getResolvedReference();
for (int i = 0; i < includePaths.length; ++i) {
IPath includePath = new Path(includePaths[i]);
if (ref != null) {
if (includePath.isPrefixOf(ref.getLocation()))
return true;
} else {
// we don't have the real location, so just check the project path
if (baseProject.getLocation().isPrefixOf(includePath))
return true;
}
}
}
return false;
}
}

View file

@ -61,8 +61,9 @@ public class NewSourceFileGenerator {
buf.append(includeGuardSymbol);
buf.append(fLineDelimiter);
buf.append(fLineDelimiter);
buf.append("#endif //"); //$NON-NLS-1$
buf.append("#endif /*"); //$NON-NLS-1$
buf.append(includeGuardSymbol);
buf.append("*/"); //$NON-NLS-1$
buf.append(fLineDelimiter);
stream = new ByteArrayInputStream(buf.toString().getBytes());
} else {