Bug 534706 - Application model editor - Create dialog persist package

The Abstract new class Page now persist the last selected package.
(The solution works also for NewAddonCW, NewDynamicMenuContributionCW,
NewImperativeExplressionCW, NewPartCW, and NewToolControlCW)

Change-Id: I4e3ac01c189ec3fa2441f47c7794bf9d9f451f09
Signed-off-by: Patrik Suzzi <psuzzi@itemis.com>
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/classes/AbstractNewClassPage.java b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/classes/AbstractNewClassPage.java
index 7242c82..683f52a 100644
--- a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/classes/AbstractNewClassPage.java
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/classes/AbstractNewClassPage.java
@@ -53,6 +53,7 @@
 import org.eclipse.jface.databinding.swt.IWidgetValueProperty;
 import org.eclipse.jface.databinding.swt.WidgetProperties;
 import org.eclipse.jface.databinding.wizard.WizardPageSupport;
+import org.eclipse.jface.dialogs.IDialogSettings;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.viewers.ILabelProvider;
 import org.eclipse.jface.viewers.IStructuredSelection;
@@ -80,6 +81,11 @@
 	public static final String PACKAGE_FRAGMENT = "packageFragment"; //$NON-NLS-1$
 	public static final String PROPERTY_NAME = "name"; //$NON-NLS-1$
 
+	/** Name of the setting section for the new class wizard */
+	private static final String SETTING_SECTION_NEW_CLASS = "org.eclipse.e4.tools.wizards.newclass"; //$NON-NLS-1$
+	/** The package dialog setting */
+	private static final String SETTING_PACKAGE = "package"; //$NON-NLS-1$
+
 	public static class JavaClass {
 
 		protected PropertyChangeSupport support = new PropertyChangeSupport(this);
@@ -176,15 +182,41 @@
 	 *            used to initialize the fields
 	 */
 	public void init(IStructuredSelection selection) {
-		if (selection == null || selection.isEmpty() || selection.getFirstElement() == null) {
-			return;
-		}
-		if (selection.getFirstElement() instanceof IPackageFragment) {
+		if (selection != null && !selection.isEmpty() && selection.getFirstElement() != null
+				&& selection.getFirstElement() instanceof IPackageFragment) {
 			final IPackageFragment pkg = (IPackageFragment) selection.getFirstElement();
 			initialPackage = pkg.getElementName();
+		} else {
+			String settingPackage = getDialogSettings().get(SETTING_PACKAGE);
+			if (settingPackage != null) {
+				initialPackage = settingPackage;
+			}
 		}
 	}
 
+	/**
+	 * Gets called if the wizard is finished
+	 */
+	public void performFinish() {
+		String packageName = clazz.getPackageFragment().getElementName();
+		getDialogSettings().put(SETTING_PACKAGE, packageName);
+	}
+
+	/**
+	 * {@inheritDoc}
+	 *
+	 * @see org.eclipse.jface.wizard.WizardPage#getDialogSettings()
+	 */
+	@Override
+	protected IDialogSettings getDialogSettings() {
+		final IDialogSettings mainSettings = ToolsPlugin.getDefault().getDialogSettings();
+		IDialogSettings settingsSection = mainSettings.getSection(SETTING_SECTION_NEW_CLASS);
+		if (settingsSection == null) {
+			settingsSection = mainSettings.addNewSection(SETTING_SECTION_NEW_CLASS);
+		}
+		return settingsSection;
+	}
+
 	@SuppressWarnings("unchecked")
 	@Override
 	public void createControl(Composite parent) {
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/classes/AbstractNewClassWizard.java b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/classes/AbstractNewClassWizard.java
index 6581766..303d9d8 100644
--- a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/classes/AbstractNewClassWizard.java
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/classes/AbstractNewClassWizard.java
@@ -56,6 +56,7 @@
 	protected IPackageFragmentRoot root;
 	protected IFile file;
 	private IStructuredSelection selection;
+	private AbstractNewClassPage newClassPage;
 
 	public AbstractNewClassWizard() {
 		setWindowTitle(Messages.AbstractNewClassWizard_NewClass);
@@ -82,7 +83,8 @@
 	public void addPage(IWizardPage page) {
 		super.addPage(page);
 		if (page instanceof AbstractNewClassPage) {
-			((AbstractNewClassPage) page).init(getSelection());
+			newClassPage = ((AbstractNewClassPage) page);
+			newClassPage.init(getSelection());
 		}
 	}
 
@@ -271,6 +273,9 @@
 				// TODO Auto-generated catch block
 				e1.printStackTrace();
 			}
+			if (newClassPage != null) {
+				newClassPage.performFinish();
+			}
 			final String cuName = clazz.getName() + JAVA;
 			final ICompilationUnit unit = fragment.getCompilationUnit(cuName);
 			final IResource resource = unit.getResource();