From 31b748e7b9ce00dd4e5286f4a12236f263e3ec5f Mon Sep 17 00:00:00 2001 From: Marco Stornelli Date: Sat, 4 Apr 2020 09:16:28 +0200 Subject: Bug 510789 - Added final field to class wizard Change-Id: Ib2f0168b897e665f3577511144692bb446d5ab84 --- core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF | 2 +- .../wizards/classwizard/NewClassCodeGenerator.java | 20 +++++ .../classwizard/NewClassWizardMessages.java | 1 + .../classwizard/NewClassWizardMessages.properties | 2 + .../cdt/ui/wizards/NewClassCreationWizardPage.java | 90 ++++++++++++++++++++-- 5 files changed, 109 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF index 4e8d1774fed..9a8ae8f8b8a 100644 --- a/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.cdt.ui; singleton:=true -Bundle-Version: 6.7.100.qualifier +Bundle-Version: 7.0.0.qualifier Bundle-Activator: org.eclipse.cdt.ui.CUIPlugin Bundle-Vendor: %providerName Bundle-Localization: plugin diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCodeGenerator.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCodeGenerator.java index b5bbb7e4933..0be392ef967 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCodeGenerator.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCodeGenerator.java @@ -91,6 +91,7 @@ public class NewClassCodeGenerator { private ICElement fCreatedClass; private String fFullyQualifiedClassName; private boolean fForceSourceFileCreation; + private final boolean fIsFinal; /** * When set to true, the source file is created, even if no stubs have @@ -126,6 +127,21 @@ public class NewClassCodeGenerator { */ public NewClassCodeGenerator(IPath headerPath, IPath sourcePath, IPath testPath, String className, String namespace, IBaseClassInfo[] baseClasses, IMethodStub[] methodStubs) { + this(headerPath, sourcePath, testPath, className, namespace, baseClasses, methodStubs, false); + } + + /** + * @param headerPath the header file path + * @param sourcePath the source file path + * @param testPath the test file path, can be {@code null} + * @param className the class name + * @param namespace the namespace name + * @param baseClasses the base classes + * @param methodStubs the method stubs + * @param isFinal True if final, false otherwise + */ + public NewClassCodeGenerator(IPath headerPath, IPath sourcePath, IPath testPath, String className, String namespace, + IBaseClassInfo[] baseClasses, IMethodStub[] methodStubs, boolean isFinal) { fHeaderPath = headerPath; fSourcePath = sourcePath; fTestPath = testPath; @@ -142,6 +158,7 @@ public class NewClassCodeGenerator { } fBaseClasses = baseClasses; fMethodStubs = methodStubs; + fIsFinal = isFinal; } public ICElement getCreatedClass() { @@ -466,6 +483,9 @@ public class NewClassCodeGenerator { } code.append("class "); //$NON-NLS-1$ code.append(fClassName); + if (fIsFinal) { + code.append(" final "); //$NON-NLS-1$ + } code.append(constructBaseClassInheritance()); code.append(" {"); //$NON-NLS-1$ code.append(lineDelimiter); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardMessages.java index 9ce8764e9d2..a5cc3daf353 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardMessages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardMessages.java @@ -30,6 +30,7 @@ public final class NewClassWizardMessages extends NLS { public static String NewClassCreationWizardPage_warning_NotACProject; public static String NewClassCreationWizardPage_warning_NotInACProject; public static String NewClassCreationWizardPage_namespace_label; + public static String NewClassCreationWizardPage_final_label; public static String NewClassCreationWizardPage_namespace_button; public static String NewClassCreationWizardPage_error_EnterNamespace; public static String NewClassCreationWizardPage_error_NamespaceExistsDifferentCase; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardMessages.properties index d0d4b1b366d..2f5f6d457c5 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassWizardMessages.properties @@ -36,6 +36,8 @@ NewClassCreationWizardPage_warning_NotInACProject=Folder is not in a C/C++ proje NewClassCreationWizardPage_namespace_label=&Namespace: NewClassCreationWizardPage_namespace_button=Bro&wse... +NewClassCreationWizardPage_final_label=Final class + NewClassCreationWizardPage_error_EnterNamespace=Namespace is empty. NewClassCreationWizardPage_error_NamespaceExistsDifferentCase=Namespace with the same name exists in a different scope. NewClassCreationWizardPage_error_TypeMatchingNamespaceExists=Another type with the same name as specified namespace exists. diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/wizards/NewClassCreationWizardPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/wizards/NewClassCreationWizardPage.java index 2365b4055e6..a6f7716abc0 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/wizards/NewClassCreationWizardPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/wizards/NewClassCreationWizardPage.java @@ -122,6 +122,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { private static final String KEY_STUB_SELECTED = "stubSelected"; //$NON-NLS-1$ private static final String KEY_STUB_VIRTUAL = "stubVirtual"; //$NON-NLS-1$ private static final String KEY_STUB_IMPL = "stubImpl"; //$NON-NLS-1$ + private static final String KEY_FINAL_SELECTED = "finalSelected"; //$NON-NLS-1$ // Field IDs protected static final int SOURCE_FOLDER_ID = 1; @@ -133,8 +134,10 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { protected static final int SOURCE_FILE_ID = 64; /** @since 5.3 */ protected static final int TEST_FILE_ID = 128; + /** @since 7.0*/ + protected static final int FINAL_ID = 256; protected static final int ALL_FIELDS = SOURCE_FOLDER_ID | NAMESPACE_ID | CLASS_NAME_ID | BASE_CLASSES_ID - | METHOD_STUBS_ID | HEADER_FILE_ID | SOURCE_FILE_ID | TEST_FILE_ID; + | METHOD_STUBS_ID | HEADER_FILE_ID | SOURCE_FILE_ID | TEST_FILE_ID | FINAL_ID; protected int fLastFocusedField = 0; protected StringButtonDialogField fSourceFolderDialogField; @@ -145,6 +148,10 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { protected MethodStubsListDialogField fMethodStubsDialogField; protected StringButtonDialogField fHeaderFileDialogField; protected StringButtonDialogField fSourceFileDialogField; + /** + * @since 7.0 + */ + protected SelectionButtonDialogField fIsFinalClassField; /** @since 5.3 */ protected StringButtonDialogField fTestFileDialogField; /** @since 5.3 */ @@ -165,6 +172,10 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { protected IStatus fSourceFileStatus; /** @since 5.3 */ protected IStatus fTestFileStatus; + /** + * @since 7.0 + */ + protected IStatus fIsFinalStatus; protected final IStatus STATUS_OK = new StatusInfo(); protected IFile fCreatedHeaderFile; @@ -216,6 +227,11 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { fMethodStubsDialogField = new MethodStubsListDialogField( NewClassWizardMessages.NewClassCreationWizardPage_methodStubs_label, methodStubsAdapter); + FinalFieldAdapter finalAdapter = new FinalFieldAdapter(); + fIsFinalClassField = new SelectionButtonDialogField(SWT.CHECK); + fIsFinalClassField.setDialogFieldListener(finalAdapter); + fIsFinalClassField.setLabelText(NewClassWizardMessages.NewClassCreationWizardPage_final_label); + FileGroupFieldAdapter fileGroupAdapter = new FileGroupFieldAdapter(); fHeaderFileDialogField = new StringButtonDialogField(fileGroupAdapter); fHeaderFileDialogField.setDialogFieldListener(fileGroupAdapter); @@ -244,6 +260,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { fHeaderFileStatus = STATUS_OK; fSourceFileStatus = STATUS_OK; fTestFileStatus = STATUS_OK; + fIsFinalStatus = STATUS_OK; fLastFocusedField = 0; isFirstTime = true; @@ -275,6 +292,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { createClassNameControls(composite, nColumns); createBaseClassesControls(composite, nColumns); createMethodStubsControls(composite, nColumns); + createFinalClassControls(composite, nColumns); createSeparator(composite, nColumns); @@ -359,6 +377,16 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { textControl.addFocusListener(new StatusFocusListener(CLASS_NAME_ID)); } + /** + * Creates the controls for the final class field. + * + * @param composite the parent composite + * @since 7.0 + */ + protected void createFinalClassControls(Composite composite, int nColumns) { + fIsFinalClassField.doFillIntoGrid(composite, nColumns - 1); + } + /** * Creates the controls for the base classes field. Expects a GridLayout with * at least 3 columns. @@ -499,6 +527,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { addMethodStub(stub, getBooleanSettingWithDefault(KEY_STUB_SELECTED + i, stub.isEnabledByDefault())); } + fIsFinalClassField.setSelection(fDialogSettings.getBoolean(KEY_FINAL_SELECTED)); setTestFileSelection(fDialogSettings.getBoolean(KEY_TEST_FILE_SELECTED), true); handleFieldChanged(ALL_FIELDS); } @@ -716,6 +745,16 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { return fClassNameDialogField.getText().trim(); } + /** + * Check if the class is final + * + * @return true if final, false otherwise + * @since 7.0 + */ + public boolean isFinalClass() { + return fIsFinalClassField.isSelected(); + } + /** * Sets the text of the class name input field. * @@ -1085,6 +1124,21 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { return null; } + /** + * handles changes to the final field + */ + private final class FinalFieldAdapter implements IStringButtonAdapter, IDialogFieldListener { + @Override + public void changeControlPressed(DialogField field) { + handleFieldChanged(FINAL_ID); + } + + @Override + public void dialogFieldChanged(DialogField field) { + handleFieldChanged(FINAL_ID); + } + } + /** * handles changes to the namespace field */ @@ -1471,6 +1525,9 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { if (fieldChanged(fields, TEST_FILE_ID)) { fTestFileStatus = testFileChanged(); } + if (fieldChanged(fields, FINAL_ID)) { + fIsFinalStatus = finalChanged(); + } doStatusUpdate(); } @@ -1503,7 +1560,8 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { (fMethodStubsStatus != lastStatus) ? fMethodStubsStatus : STATUS_OK, (fHeaderFileStatus != lastStatus) ? fHeaderFileStatus : STATUS_OK, (fSourceFileStatus != lastStatus) ? fSourceFileStatus : STATUS_OK, - (fTestFileStatus != lastStatus) ? fTestFileStatus : STATUS_OK, }; + (fTestFileStatus != lastStatus) ? fTestFileStatus : STATUS_OK, + (fIsFinalStatus != lastStatus) ? fIsFinalStatus : STATUS_OK, }; // the mode severe status will be displayed and the ok button enabled/disabled. updateStatus(status); @@ -1532,6 +1590,8 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { return fSourceFileStatus; case TEST_FILE_ID: return fTestFileStatus; + case FINAL_ID: + return fIsFinalStatus; default: return STATUS_OK; } @@ -1726,6 +1786,15 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { return status; } + /** + * Final class field changed + * @return the status of the validation + * @since 7.0 + */ + protected IStatus finalChanged() { + return Status.OK_STATUS; + } + /** * Hook method that gets called when the list of base classes has changed. The method * validates the base classes and returns the status of the validation. @@ -2080,6 +2149,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { // Update dialog settings. fDialogSettings.put(KEY_NAMESPACE_SELECTED, fNamespaceSelection.isSelected()); fDialogSettings.put(KEY_TEST_FILE_SELECTED, fTestFileSelection.isSelected()); + fDialogSettings.put(KEY_FINAL_SELECTED, fIsFinalClassField.isSelected()); String namespace = fNamespaceSelection.isSelected() ? getNamespaceText() : null; fDialogSettings.put(KEY_NAMESPACE, namespace); IMethodStub[] stubs = fMethodStubsDialogField.getMethodStubs(); @@ -2103,7 +2173,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { IPath sourcePath = getSourceFileFullPath(); IPath testPath = getTestFileFullPath(); createClass(headerPath, sourcePath, testPath, getClassName(), namespace, getBaseClasses(), - getSelectedMethodStubs(), monitor); + getSelectedMethodStubs(), isFinalClass(), monitor); } /** @@ -2135,9 +2205,10 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { * @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 { + IBaseClassInfo[] baseClasses, IMethodStub[] methodStubs, boolean isFinal, IProgressMonitor monitor) + throws CoreException { NewClassCodeGenerator generator = new NewClassCodeGenerator(headerPath, sourcePath, testPath, className, - namespace, baseClasses, methodStubs); + namespace, baseClasses, methodStubs, isFinal); generator.setForceSourceFileCreation(true); generator.createClass(monitor); @@ -2156,6 +2227,15 @@ public class NewClassCreationWizardPage extends NewElementWizardPage { createClass(headerPath, sourcePath, null, className, namespace, baseClasses, methodStubs, monitor); } + /** + * @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 { + createClass(headerPath, sourcePath, null, className, namespace, baseClasses, methodStubs, false, monitor); + } + /** * Returns the created class. The method only returns a valid class * after {@link #createClass} has been called. -- cgit v1.2.3