]> git.uio.no Git - ifi-stolz-refaktor.git/blobdiff - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/wizards/NewElementWizard.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / wizards / NewElementWizard.java
diff --git a/case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/wizards/NewElementWizard.java b/case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/wizards/NewElementWizard.java
new file mode 100644 (file)
index 0000000..cc38398
--- /dev/null
@@ -0,0 +1,240 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2011 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jdt.internal.ui.wizards;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.core.runtime.jobs.ISchedulingRule;
+import org.eclipse.core.runtime.jobs.Job;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.ResourcesPlugin;
+
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.layout.PixelConverter;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.jface.window.Window;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.jface.wizard.WizardDialog;
+
+import org.eclipse.jface.text.templates.persistence.TemplateStore;
+
+import org.eclipse.ui.INewWizard;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
+
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
+
+import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
+import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
+
+import org.eclipse.jdt.ui.wizards.NewTypeWizardPage;
+
+import org.eclipse.jdt.internal.ui.IUIConstants;
+import org.eclipse.jdt.internal.ui.JavaPlugin;
+import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
+import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog;
+import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;
+import org.eclipse.jdt.internal.ui.text.correction.proposals.NewCUUsingWizardProposal;
+import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
+
+public abstract class NewElementWizard extends Wizard implements INewWizard {
+
+       private IWorkbench fWorkbench;
+       private IStructuredSelection fSelection;
+
+       public NewElementWizard() {
+               setNeedsProgressMonitor(true);
+       }
+
+       protected void openResource(final IFile resource) {
+               final IWorkbenchPage activePage= JavaPlugin.getActivePage();
+               if (activePage != null) {
+                       final Display display= getShell().getDisplay();
+                       if (display != null) {
+                               display.asyncExec(new Runnable() {
+                                       public void run() {
+                                               try {
+                                                       IDE.openEditor(activePage, resource, true);
+                                               } catch (PartInitException e) {
+                                                       JavaPlugin.log(e);
+                                               }
+                                       }
+                               });
+                       }
+               }
+       }
+
+       /**
+        * Subclasses should override to perform the actions of the wizard. This method is run in the
+        * wizard container's context as a workspace runnable.
+        * 
+        * @param monitor the progress monitor
+        * @throws InterruptedException when the operation is cancelled
+        * @throws CoreException if the element cannot be created
+        */
+       protected abstract void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException;
+
+       /**
+        * Returns the scheduling rule for creating the element.
+        * @return returns the scheduling rule
+        */
+       protected ISchedulingRule getSchedulingRule() {
+               return ResourcesPlugin.getWorkspace().getRoot(); // look all by default
+       }
+
+
+       protected boolean canRunForked() {
+               return true;
+       }
+
+       public abstract IJavaElement getCreatedElement();
+
+       protected void handleFinishException(Shell shell, InvocationTargetException e) {
+               String title= NewWizardMessages.NewElementWizard_op_error_title;
+               String message= NewWizardMessages.NewElementWizard_op_error_message;
+               ExceptionHandler.handle(e, shell, title, message);
+       }
+
+       /*
+        * @see Wizard#performFinish
+        */
+       @Override
+       public boolean performFinish() {
+               IWorkspaceRunnable op= new IWorkspaceRunnable() {
+                       public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
+                               try {
+                                       finishPage(monitor);
+                               } catch (InterruptedException e) {
+                                       throw new OperationCanceledException(e.getMessage());
+                               }
+                       }
+               };
+               try {
+                       ISchedulingRule rule= null;
+                       Job job= Job.getJobManager().currentJob();
+                       if (job != null)
+                               rule= job.getRule();
+                       IRunnableWithProgress runnable= null;
+                       if (rule != null)
+                               runnable= new WorkbenchRunnableAdapter(op, rule, true);
+                       else
+                               runnable= new WorkbenchRunnableAdapter(op, getSchedulingRule());
+                       getContainer().run(canRunForked(), true, runnable);
+               } catch (InvocationTargetException e) {
+                       handleFinishException(getShell(), e);
+                       return false;
+               } catch  (InterruptedException e) {
+                       return false;
+               }
+               return true;
+       }
+
+       protected void warnAboutTypeCommentDeprecation() {
+               String key= IUIConstants.DIALOGSTORE_TYPECOMMENT_DEPRECATED;
+               if (OptionalMessageDialog.isDialogEnabled(key)) {
+                       TemplateStore templates= JavaPlugin.getDefault().getTemplateStore();
+                       boolean isOldWorkspace= templates.findTemplate("filecomment") != null && templates.findTemplate("typecomment") != null; //$NON-NLS-1$ //$NON-NLS-2$
+                       if (!isOldWorkspace) {
+                               OptionalMessageDialog.setDialogEnabled(key, false);
+                       }
+                       String title= NewWizardMessages.NewElementWizard_typecomment_deprecated_title;
+                       String message= NewWizardMessages.NewElementWizard_typecomment_deprecated_message;
+                       OptionalMessageDialog.open(key, getShell(), title, null, message, MessageDialog.INFORMATION, new String[] { IDialogConstants.OK_LABEL }, 0);
+               }
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
+        */
+       public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
+               fWorkbench= workbench;
+               fSelection= currentSelection;
+       }
+
+       public IStructuredSelection getSelection() {
+               return fSelection;
+       }
+
+       public IWorkbench getWorkbench() {
+               return fWorkbench;
+       }
+
+       protected void selectAndReveal(IResource newResource) {
+               BasicNewResourceWizard.selectAndReveal(newResource, fWorkbench.getActiveWorkbenchWindow());
+       }
+
+       public void generated_904266101912896755(StructuredSelection selection, NewCUUsingWizardProposal newcuusingwizardproposal) {
+               init(JavaPlugin.getDefault().getWorkbench(), selection);
+       
+               IType createdType= null;
+       
+               if (newcuusingwizardproposal.fShowDialog) {
+                       Shell shell= JavaPlugin.getActiveWorkbenchShell();
+                       WizardDialog dialog= new WizardDialog(shell, this);
+       
+                       PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont());
+                       dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20));
+                       dialog.create();
+                       dialog.getShell().setText(CorrectionMessages.NewCUCompletionUsingWizardProposal_dialogtitle);
+       
+                       if (dialog.open() == Window.OK) {
+                               createdType= (IType) getCreatedElement();
+                       }
+               } else {
+                       addPages();
+                       try {
+                               NewTypeWizardPage page= newcuusingwizardproposal.getPage(this);
+                               page.createType(null);
+                               createdType= page.getCreatedType();
+                       } catch (CoreException e) {
+                               JavaPlugin.log(e);
+                       } catch (InterruptedException e) {
+                       }
+               }
+       
+               if (createdType != null) {
+                       IJavaElement container= createdType.getParent();
+                       if (container instanceof ICompilationUnit) {
+                               container= container.getParent();
+                       }
+                       if (!container.equals(newcuusingwizardproposal.fTypeContainer)) {
+                               // add import
+                               try {
+                                       ImportRewrite rewrite= StubUtility.createImportRewrite(newcuusingwizardproposal.fCompilationUnit, true);
+                                       rewrite.addImport(createdType.getFullyQualifiedName('.'));
+                                       JavaModelUtil.applyEdit(newcuusingwizardproposal.fCompilationUnit, rewrite.rewriteImports(null), false, null);
+                               } catch (CoreException e) {
+                               }
+                       }
+                       newcuusingwizardproposal.fCreatedType= createdType;
+               }
+       }
+
+}