diff options
author | Harald Albers | 2016-03-09 20:53:40 +0000 |
---|---|---|
committer | Noopur Gupta | 2016-04-18 08:06:19 +0000 |
commit | ae0f4733850d80765bd9b59df53774626d27df6d (patch) | |
tree | efcd05dfab865640e059334ea03fcbaeb6595359 | |
parent | 4526d5d5c48c642606c0ca129b684821eef8cc6f (diff) | |
download | eclipse.jdt.ui-ae0f4733850d80765bd9b59df53774626d27df6d.tar.gz eclipse.jdt.ui-ae0f4733850d80765bd9b59df53774626d27df6d.tar.xz eclipse.jdt.ui-ae0f4733850d80765bd9b59df53774626d27df6d.zip |
Bug 339292 - [type wizards] NewAnnotationWizard generates @Documented,
@Retention and @Target
Change-Id: Ie44869a21ea5242cc49762960acfece868f4fe64
Signed-off-by: Harald Albers <eclipse@albersweb.de>
6 files changed, 470 insertions, 10 deletions
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/dialogs/WizardsTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/dialogs/WizardsTest.java index a043175cc4..82263dd161 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/dialogs/WizardsTest.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/dialogs/WizardsTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,13 +7,10 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Harald Albers <eclipse@albersweb.de> - [type wizards] New Annotation dialog could allow generating @Documented, @Retention and @Target - https://bugs.eclipse.org/339292 *******************************************************************************/ package org.eclipse.jdt.ui.tests.dialogs; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - import org.eclipse.jdt.testplugin.JavaProjectHelper; import org.eclipse.jdt.testplugin.util.DialogCheck; @@ -32,11 +29,16 @@ import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.internal.ui.jarpackager.JarPackageWizard; import org.eclipse.jdt.internal.ui.wizards.JavaProjectWizard; +import org.eclipse.jdt.internal.ui.wizards.NewAnnotationCreationWizard; import org.eclipse.jdt.internal.ui.wizards.NewClassCreationWizard; import org.eclipse.jdt.internal.ui.wizards.NewInterfaceCreationWizard; import org.eclipse.jdt.internal.ui.wizards.NewPackageCreationWizard; import org.eclipse.jdt.internal.ui.wizards.NewSourceFolderCreationWizard; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + public class WizardsTest extends TestCase { private static final String PROJECT_NAME = "DummyProject"; @@ -45,6 +47,7 @@ public class WizardsTest extends TestCase { TestSuite suite= new TestSuite(WizardsTest.class.getName()); suite.addTest(new WizardsTest("testClassWizard")); suite.addTest(new WizardsTest("testInterfaceWizard")); + suite.addTest(new WizardsTest("testAnnotationWizard")); suite.addTest(new WizardsTest("testJarPackageWizard")); suite.addTest(new WizardsTest("testNewProjectWizard")); suite.addTest(new WizardsTest("testPackageWizard")); @@ -133,6 +136,22 @@ public class WizardsTest extends TestCase { JavaProjectHelper.delete(jproject); } + public void testAnnotationWizard() throws Exception { + IJavaProject jproject= JavaProjectHelper.createJavaProject(PROJECT_NAME, "bin"); + IPackageFragmentRoot root= JavaProjectHelper.addSourceContainer(jproject, "src1"); + IPackageFragment pack= root.createPackageFragment("org.eclipse.jdt.internal.ui.hello", true, null); + JavaProjectHelper.addRTJar(jproject); + + NewAnnotationCreationWizard wizard= new NewAnnotationCreationWizard(); + wizard.init(getWorkbench(), new StructuredSelection(pack)); + wizard.setForcePreviousAndNextButtons(true); + WizardDialog dialog= new WizardDialog(getShell(), wizard); + dialog.create(); + DialogCheck.assertDialog(dialog); + + JavaProjectHelper.delete(jproject); + } + public void testJarPackageWizard() throws Exception { IJavaProject jproject= JavaProjectHelper.createJavaProject(PROJECT_NAME, "bin"); IPackageFragmentRoot root= JavaProjectHelper.addSourceContainer(jproject, "src1"); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewAnnotationCreationWizard.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewAnnotationCreationWizard.java index 1ad6ea10aa..1f3354150b 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewAnnotationCreationWizard.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewAnnotationCreationWizard.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Harald Albers <eclipse@albersweb.de> - [type wizards] New Annotation dialog could allow generating @Documented, @Retention and @Target - https://bugs.eclipse.org/339292 *******************************************************************************/ package org.eclipse.jdt.internal.ui.wizards; @@ -49,6 +50,7 @@ public class NewAnnotationCreationWizard extends NewElementWizard { super.addPages(); if (fPage == null) { fPage= new NewAnnotationWizardPage(); + fPage.setWizard(this); fPage.init(getSelection()); } addPage(fPage); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewWizardMessages.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewWizardMessages.java index a2a4e099a5..b1251b9215 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewWizardMessages.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewWizardMessages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2015 IBM Corporation and others. + * Copyright (c) 2000, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -13,6 +13,7 @@ * Stephan Herrmann - Contributions for * Bug 463936 - ExternalAnnotationsAttachmentDialog should not allow virtual folders * Bug 465293 - External annotation path per container and project + * Harald Albers <eclipse@albersweb.de> - [type wizards] New Annotation dialog could allow generating @Documented, @Retention and @Target - https://bugs.eclipse.org/339292 *******************************************************************************/ package org.eclipse.jdt.internal.ui.wizards; @@ -191,6 +192,9 @@ public final class NewWizardMessages extends NLS { public static String NewEnumWizardPage_description; public static String NewAnnotationCreationWizard_title; + public static String NewAnnotationWizardPage_add_documented; + public static String NewAnnotationWizardPage_add_retention; + public static String NewAnnotationWizardPage_add_target; public static String NewAnnotationWizardPage_title; public static String NewAnnotationWizardPage_description; diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewWizardMessages.properties b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewWizardMessages.properties index 3bcc9da043..8635219db2 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewWizardMessages.properties +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/NewWizardMessages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2000, 2015 IBM Corporation and others. +# Copyright (c) 2000, 2016 IBM Corporation and others. # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 # which accompanies this distribution, and is available at @@ -13,6 +13,7 @@ # Stephan Herrmann - Contributions for # Bug 463936 - ExternalAnnotationsAttachmentDialog should not allow virtual folders # Bug 465293 - External annotation path per container and project +# Harald Albers <eclipse@albersweb.de> - [type wizards] New Annotation dialog could allow generating @Documented, @Retention and @Target - https://bugs.eclipse.org/339292 ############################################################################### # ------- AbstractOpenWizardAction ------- @@ -189,6 +190,9 @@ NewEnumWizardPage_description=Create a new enum type. NewAnnotationCreationWizard_title=New Annotation Type +NewAnnotationWizardPage_add_documented=Add @Doc&umented +NewAnnotationWizardPage_add_retention=Add @&Retention: +NewAnnotationWizardPage_add_target=Add @T&arget: NewAnnotationWizardPage_title=Annotation Type NewAnnotationWizardPage_description=Create a new annotation type. diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/dialogfields/SelectionButtonDialogFieldGroup.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/dialogfields/SelectionButtonDialogFieldGroup.java index 566afcad29..9695963011 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/dialogfields/SelectionButtonDialogFieldGroup.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/dialogfields/SelectionButtonDialogFieldGroup.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Harald Albers <eclipse@albersweb.de> - [type wizards] New Annotation dialog could allow generating @Documented, @Retention and @Target - https://bugs.eclipse.org/339292 *******************************************************************************/ package org.eclipse.jdt.internal.ui.wizards.dialogfields; @@ -207,6 +208,7 @@ public class SelectionButtonDialogFieldGroup extends DialogField { /** * Returns the selection state of a button contained in the group. * @param index The index of the button + * @return <code>true</code> if the button is selected, else <code>false</code> */ public boolean isSelected(int index) { if (index >= 0 && index < fButtonsSelected.length) { @@ -216,6 +218,18 @@ public class SelectionButtonDialogFieldGroup extends DialogField { } /** + * Returns the enabled state of a button contained in the group. + * @param index the index of the button + * @return <code>true</code> if the button is enabled, else <code>false</code> + */ + public boolean isEnabled(int index) { + if (index >= 0 && index < fButtonsEnabled.length) { + return fButtonsEnabled[index]; + } + return false; + } + + /** * Sets the selection state of a button contained in the group. */ public void setSelection(int index, boolean selected) { diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewAnnotationWizardPage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewAnnotationWizardPage.java index a1dcedf813..987a7c1bbc 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewAnnotationWizardPage.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewAnnotationWizardPage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2011 IBM Corporation and others. + * Copyright (c) 2004, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,24 +7,49 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Harald Albers <eclipse@albersweb.de> - [type wizards] New Annotation dialog could allow generating @Documented, @Retention and @Target - https://bugs.eclipse.org/339292 *******************************************************************************/ package org.eclipse.jdt.ui.wizards; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.ArrayList; +import java.util.List; + import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.ui.PlatformUI; +import org.eclipse.jdt.core.IBuffer; import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaModelException; + +import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; +import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; +import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; +import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; +import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; +import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup; /** * Wizard page to create a new annotation type. @@ -42,6 +67,14 @@ public class NewAnnotationWizardPage extends NewTypeWizardPage { private final static String PAGE_NAME= "NewAnnotationWizardPage"; //$NON-NLS-1$ private final static int TYPE = NewTypeWizardPage.ANNOTATION_TYPE; + private final static String SETTINGS_ADD_DOCUMENTED= "add_documented"; //$NON-NLS-1$ + + private AddRetentionControl fRetentionSelection; + + private AddTargetControl fTargetSelection; + + private SelectionButtonDialogField fDocumentedSelection; + /** * Create a new <code>NewAnnotationWizardPage</code> */ @@ -50,6 +83,11 @@ public class NewAnnotationWizardPage extends NewTypeWizardPage { setTitle(NewWizardMessages.NewAnnotationWizardPage_title); setDescription(NewWizardMessages.NewAnnotationWizardPage_description); + + fRetentionSelection= new AddRetentionControl(); + fTargetSelection= new AddTargetControl(); + fDocumentedSelection= new SelectionButtonDialogField(SWT.CHECK); + fDocumentedSelection.setLabelText(NewWizardMessages.NewAnnotationWizardPage_add_documented); } // -------- Initialization --------- @@ -66,11 +104,42 @@ public class NewAnnotationWizardPage extends NewTypeWizardPage { initContainerPage(jelem); initTypePage(jelem); + initAnnotationPage(); doStatusUpdate(); } // ------ validation -------- + private void initAnnotationPage() { + IDialogSettings dialogSettings= getDialogSettings(); + IDialogSettings section= null; + if (dialogSettings != null) { + section= dialogSettings.getSection(PAGE_NAME); + } + restoreSettings(section); + } + + private void restoreSettings(IDialogSettings section) { + if (section != null) { + boolean addDocumented= section.getBoolean(SETTINGS_ADD_DOCUMENTED); + fDocumentedSelection.setSelection(addDocumented); + } + + fRetentionSelection.init(section); + fTargetSelection.init(section); + } + + @Override + protected IStatus containerChanged() { + IStatus status= super.containerChanged(); + if (status.isOK()) { + IJavaProject javaProject= getJavaProject(); + fRetentionSelection.setProject(javaProject); + fTargetSelection.setProject(javaProject); + } + return status; + } + private void doStatusUpdate() { // all used component status IStatus[] status= new IStatus[] { @@ -98,6 +167,305 @@ public class NewAnnotationWizardPage extends NewTypeWizardPage { // ------ UI -------- + /** + * Control for adding an annotation with enum-based values to the generated source code. + * + * @param <A> the class to add as an annotation + * @param <E> the enum class that supplies values for the annotation + */ + private abstract static class AddAnnotationControl<A, E extends Enum<E>> { + private static final String SETTINGS_ENABLED= "enabled"; //$NON-NLS-1$ + private static final String SETTINGS_SELECTED_ENUMS= "selectedEnums"; //$NON-NLS-1$ + + /** + * A checkbox for enabling the addition of this annotation. + */ + protected final SelectionButtonDialogField fEnableButton; + + /** + * A group of buttons for choosing between the available enum values for the annotation. + */ + protected final SelectionButtonDialogFieldGroup fEnumButtons; + + /** + * The annotation whose addition this control represents. + */ + private final Class<A> fAnnotationClass; + + /** + * The enum from which one or more constants should be added as values to the annotation. + */ + private final Class<E> fEnumClass; + + private boolean fControlsAreCreated; + + /** + * The Java project in which the annotation is going to be created. The availability of enum + * constants may depend on the project, e.g. its source level. + */ + protected IJavaProject fJavaProject; + + public AddAnnotationControl(int style, String enableLabel, Class<A> annotationClass, Class<E> enumClass, int nColumns) { + fAnnotationClass= annotationClass; + fEnumClass= enumClass; + + String[] enumLabels= toStringArray(enumClass); + + fEnableButton= new SelectionButtonDialogField(SWT.CHECK); + fEnableButton.setLabelText(enableLabel); + + fEnumButtons= new SelectionButtonDialogFieldGroup(style, enumLabels, nColumns); + + fEnableButton.setDialogFieldListener(new IDialogFieldListener() { + @Override + public void dialogFieldChanged(DialogField field) { + fEnumButtons.setEnabled(fEnableButton.isSelected()); + } + }); + } + + private String[] toStringArray(Class<E> enumClass) { + E[] enums= enumClass.getEnumConstants(); + String[] strings= new String[enums.length]; + for (Enum<?> en : enums) { + strings[en.ordinal()]= labelFor(en); + } + return strings; + } + + protected String labelFor(Enum<?> en) { + String name= en.name(); + String first= name.substring(0, 1).toUpperCase(); + String rest= name.substring(1).toLowerCase().replace('_', ' '); + return first + rest; + } + + public void init(IDialogSettings settings) { + boolean enabled= false; + String[] selectedEnums= defaultSelectedEnums(); + + if (settings != null) { + IDialogSettings section= settings.getSection(dialogSettingsSectionName()); + if (section != null) { + enabled= section.getBoolean(SETTINGS_ENABLED); + selectedEnums= section.getArray(SETTINGS_SELECTED_ENUMS); + } + } + + applyInitialSettings(enabled, selectedEnums); + } + + protected String[] defaultSelectedEnums() { + return new String[] {}; + } + + private void applyInitialSettings(boolean enabled, String[] selectedEnumsAsStrings) { + fEnableButton.setSelection(enabled); + fEnumButtons.setEnabled(enabled); + + fEnumButtons.setSelection(0, false); + for (String string : selectedEnumsAsStrings) { + E en= Enum.valueOf(fEnumClass, string); + fEnumButtons.setSelection(en.ordinal(), true); + } + } + + /** + * @return the section name under which this control persists its settings. + */ + abstract protected String dialogSettingsSectionName(); + + public void persistSettings(IDialogSettings settings) { + String sectionName= dialogSettingsSectionName(); + IDialogSettings section= settings.getSection(sectionName); + if (section == null) { + section= settings.addNewSection(sectionName); + } + section.put(SETTINGS_ENABLED, isEnabled()); + section.put(SETTINGS_SELECTED_ENUMS, selectedEnumsAsStrings()); + } + + private String[] selectedEnumsAsStrings() { + List<String> resultList= new ArrayList<>(); + + for (E en : allEnums()) { + if (isSelected(en)) { + resultList.add(en.name()); + } + } + + String[] resultArray= resultList.toArray(new String[] {}); + return resultArray; + } + + private E[] allEnums() { + return fEnumClass.getEnumConstants(); + } + + public void doFillIntoGrid(Composite parent, int nColumns) { + Button button= fEnableButton.getSelectionButton(parent); + GridData gdButton= new GridData(GridData.VERTICAL_ALIGN_BEGINNING); + button.setLayoutData(gdButton); + + Composite buttonsGroup= fEnumButtons.getSelectionButtonsGroup(parent); + GridData gdButtonsGroup= new GridData(); + gdButtonsGroup.horizontalSpan= nColumns - 1; + buttonsGroup.setLayoutData(gdButtonsGroup); + + fControlsAreCreated= true; + updateButtons(); + } + + /** + * Sets or changes the target <code>IJavaProject</code>. The availability of enum constants + * may depend on the project, e.g. its source level. + * + * @param javaProject the new Java project in which the annotation will be created. + */ + public void setProject(IJavaProject javaProject) { + fJavaProject= javaProject; + updateButtons(); + } + + private void updateButtons() { + if (fControlsAreCreated && fJavaProject != null) { + updateAvailableButtons(); + } + } + + protected void updateAvailableButtons() { + // after changing the project, the availability of buttons might change. + } + + /** + * @return <code>true</code> if the control is enabled, else <code>false</code> + */ + public boolean isEnabled() { + return fEnableButton.isSelected(); + } + + public void addAnnotation(IType newType, ImportsManager imports, String lineDelimiter) throws JavaModelException { + if (isEnabled()) { + List<E> selectedEnums= availableSelectedEnums(); + if (selectedEnums.size() > 0) { + String annotation= createAnnotationAndImports(selectedEnums, imports, lineDelimiter); + + int start= newType.getSourceRange().getOffset(); + IBuffer buffer= newType.getCompilationUnit().getBuffer(); + buffer.replace(start, 0, annotation); + } + } + } + + private List<E> availableSelectedEnums() { + List<E> resultList= new ArrayList<>(); + for (E en : allEnums()) { + if (isEnabled(en) && isSelected(en)) { + resultList.add(en); + } + } + return resultList; + } + + private boolean isEnabled(E en) { + return fEnumButtons.isEnabled(en.ordinal()); + } + + private boolean isSelected(E en) { + return fEnumButtons.isSelected(en.ordinal()); + } + + private String createAnnotationAndImports(List<E> selectedEnums, ImportsManager imports, String lineDelimiter) { + StringBuffer buffer= new StringBuffer(); + + String annotationTypeName= imports.addImport(fAnnotationClass.getName()); + buffer.append("@"); //$NON-NLS-1$ + buffer.append(annotationTypeName); + buffer.append("("); //$NON-NLS-1$ + + if (selectedEnums.size() > 1) { + buffer.append("{"); //$NON-NLS-1$ + } + + for (Enum<?> en : selectedEnums) { + String enumTypeName= imports.addStaticImport(en.getClass().getName(), en.name(), true); + buffer.append(enumTypeName); + buffer.append(", "); //$NON-NLS-1$ + } + + buffer.delete(buffer.length() - 2, buffer.length()); + + if (selectedEnums.size() > 1) { + buffer.append("}"); //$NON-NLS-1$ + } + + buffer.append(")"); //$NON-NLS-1$ + buffer.append(lineDelimiter); + + return buffer.toString(); + } + } + + /** + * Control for adding <code>@Retention(RetentionPolicy)</code> + */ + private static class AddRetentionControl extends AddAnnotationControl<Retention, RetentionPolicy> { + private static final String SETTINGS_SECTION_NAME= "AddRetention"; //$NON-NLS-1$ + + private static final String[] MNEMONICS= { "S", "l", "n" }; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ + + public AddRetentionControl() { + super(SWT.RADIO, NewWizardMessages.NewAnnotationWizardPage_add_retention, Retention.class, RetentionPolicy.class, 3); + } + + @Override + protected String labelFor(Enum<?> en) { + String label= super.labelFor(en); + String mnemonic= MNEMONICS[en.ordinal()]; + return label.replaceFirst(mnemonic, "&" + mnemonic); //$NON-NLS-1$ + } + + @Override + protected String dialogSettingsSectionName() { + return SETTINGS_SECTION_NAME; + } + + @Override + protected String[] defaultSelectedEnums() { + return new String[] { RetentionPolicy.CLASS.name() }; + } + } + + /** + * Control for adding <code>@Target([ElementType])</code> + */ + private static class AddTargetControl extends AddAnnotationControl<Target, ElementType> { + private static final String SETTINGS_SECTION_NAME= "AddTarget"; //$NON-NLS-1$ + + /** + * Ordinals of enum values that were added in Java 8. As we are still on Java 7 here, they + * cannot be expressed as literals. + */ + private static final int[] fEnumValuesSinceJava8= new int[] { 8, 9 }; + + public AddTargetControl() { + super(SWT.CHECK, NewWizardMessages.NewAnnotationWizardPage_add_target, Target.class, ElementType.class, 3); + } + + @Override + protected void updateAvailableButtons() { + boolean isJava8orHigher= JavaModelUtil.is18OrHigher(fJavaProject); + for (int index : fEnumValuesSinceJava8) { + fEnumButtons.enableSelectionButton(index, isJava8orHigher); + } + } + + @Override + protected String dialogSettingsSectionName() { + return SETTINGS_SECTION_NAME; + } + } + /* * @see WizardPage#createControl */ @@ -122,6 +490,12 @@ public class NewAnnotationWizardPage extends NewTypeWizardPage { createTypeNameControls(composite, nColumns); createModifierControls(composite, nColumns); + createSeparator(composite, nColumns); + + createAddAnnotationControls(composite, nColumns); + + createSeparator(composite, nColumns); + createCommentControls(composite, nColumns); enableCommentControl(true); @@ -131,6 +505,14 @@ public class NewAnnotationWizardPage extends NewTypeWizardPage { PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_ANNOTATION_WIZARD_PAGE); } + private void createAddAnnotationControls(Composite composite, int nColumns) { + fRetentionSelection.doFillIntoGrid(composite, nColumns); + DialogField.createEmptySpace(composite, nColumns); + fTargetSelection.doFillIntoGrid(composite, nColumns); + DialogField.createEmptySpace(composite, nColumns); + fDocumentedSelection.doFillIntoGrid(composite, nColumns); + } + /* * @see WizardPage#becomesVisible */ @@ -141,4 +523,39 @@ public class NewAnnotationWizardPage extends NewTypeWizardPage { setFocus(); } } + + // ------ code generation -------- + + @Override + protected void createTypeMembers(IType newType, ImportsManager imports, IProgressMonitor monitor) throws CoreException { + String lineDelimiter= StubUtility.getLineDelimiterUsed(newType.getJavaProject()); + + fTargetSelection.addAnnotation(newType, imports, lineDelimiter); + fRetentionSelection.addAnnotation(newType, imports, lineDelimiter); + addDocumentedAnnotation(newType, imports, lineDelimiter); + + persistSettings(); + } + + private void addDocumentedAnnotation(IType newType, ImportsManager imports, String lineDelimiter) throws JavaModelException { + if (fDocumentedSelection.isSelected()) { + String typeName= imports.addImport(Documented.class.getName()); + int start= newType.getSourceRange().getOffset(); + IBuffer buffer= newType.getCompilationUnit().getBuffer(); + buffer.replace(start, 0, "@" + typeName + lineDelimiter); //$NON-NLS-1$ + } + } + + private void persistSettings() { + IDialogSettings dialogSettings= getDialogSettings(); + if (dialogSettings != null) { + IDialogSettings section= dialogSettings.getSection(PAGE_NAME); + if (section == null) { + section= dialogSettings.addNewSection(PAGE_NAME); + } + section.put(SETTINGS_ADD_DOCUMENTED, fDocumentedSelection.isSelected()); + fRetentionSelection.persistSettings(section); + fTargetSelection.persistSettings(section); + } + } } |