|
From: <caw...@us...> - 2007-05-01 16:06:09
|
Revision: 2409
http://svn.sourceforge.net/rubyeclipse/?rev=2409&view=rev
Author: cawilliams
Date: 2007-05-01 09:06:07 -0700 (Tue, 01 May 2007)
Log Message:
-----------
add hacky hook into class creation so we can inject in imports,
fix test case wizard to import test/unit,
fix test case wizard to actually open the generated file after completing.
Modified Paths:
--------------
trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/internal/testunit/wizards/NewTestCaseCreationWizard.java
trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/testunit/wizards/RubyNewTestCaseWizardPage.java
trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/wizards/NewTypeWizardPage.java
Modified: trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/internal/testunit/wizards/NewTestCaseCreationWizard.java
===================================================================
--- trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/internal/testunit/wizards/NewTestCaseCreationWizard.java 2007-05-01 15:30:23 UTC (rev 2408)
+++ trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/internal/testunit/wizards/NewTestCaseCreationWizard.java 2007-05-01 16:06:07 UTC (rev 2409)
@@ -1,252 +1,69 @@
package org.rubypeople.rdt.internal.testunit.wizards;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.reflect.InvocationTargetException;
-
-import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.IWorkspaceRoot;
-import org.eclipse.core.resources.IWorkspaceRunnable;
-import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.OperationCanceledException;
-import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.jobs.ISchedulingRule;
-import org.eclipse.core.runtime.jobs.Job;
-import org.eclipse.jface.operation.IRunnableWithProgress;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.wizard.Wizard;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.INewWizard;
-import org.eclipse.ui.IWorkbench;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchWizard;
-import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.ide.IDE;
-import org.rubypeople.rdt.core.RubyCore;
-import org.rubypeople.rdt.core.formatter.Indents;
-import org.rubypeople.rdt.internal.testunit.ui.TestunitPlugin;
+import org.rubypeople.rdt.core.IRubyElement;
+import org.rubypeople.rdt.internal.ui.RubyPlugin;
import org.rubypeople.rdt.internal.ui.RubyPluginImages;
-import org.rubypeople.rdt.internal.ui.actions.WorkbenchRunnableAdapter;
-import org.rubypeople.rdt.internal.ui.util.ExceptionHandler;
-import org.rubypeople.rdt.internal.ui.wizards.NewWizardMessages;
+import org.rubypeople.rdt.internal.ui.wizards.NewElementWizard;
import org.rubypeople.rdt.testunit.wizards.RubyNewTestCaseWizardPage;
-public class NewTestCaseCreationWizard extends Wizard implements INewWizard {
-
- private static final String RUBY_FILE_EXTENSION = ".rb";
- private RubyNewTestCaseWizardPage page;
- private IStructuredSelection selection;
-
- /**
- * Constructor for SampleNewWizard.
- */
- public NewTestCaseCreationWizard() {
- super();
- setWindowTitle(WizardMessages.Wizard_title_new_testcase);
- setDefaultPageImageDescriptor(RubyPluginImages.DESC_WIZBAN_NEWCLASS);
- setNeedsProgressMonitor(true);
- }
-
- /**
- * Adding the page to the wizard.
- */
-
- public void addPages() {
- page = new RubyNewTestCaseWizardPage(selection);
- page.init(selection);
- addPage(page);
- }
-
- /**
- * This method is called when 'Finish' button is pressed in the wizard. We
- * will create an operation and run it using wizard as execution context.
- */
- 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= Platform.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 boolean canRunForked() {
- return true;
+public class NewTestCaseCreationWizard extends NewElementWizard {
+
+ private RubyNewTestCaseWizardPage fPage;
+
+ public NewTestCaseCreationWizard(RubyNewTestCaseWizardPage page) {
+ setDefaultPageImageDescriptor(RubyPluginImages.DESC_WIZBAN_NEWCLASS);
+ setDialogSettings(RubyPlugin.getDefault().getDialogSettings());
+ setWindowTitle(WizardMessages.Wizard_title_new_testcase);
+
+ fPage= page;
}
- /**
- * Returns the scheduling rule for creating the element.
- */
- protected ISchedulingRule getSchedulingRule() {
- return ResourcesPlugin.getWorkspace().getRoot(); // look all by default
+ public NewTestCaseCreationWizard() {
+ this(null);
}
-
- 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);
- }
-
+
/*
- * (non-Javadoc)
- *
+ * @see Wizard#createPages
+ */
+ public void addPages() {
+ super.addPages();
+ if (fPage == null) {
+ fPage= new RubyNewTestCaseWizardPage();
+ fPage.init(getSelection());
+ }
+ addPage(fPage);
+ }
+
+ /* (non-Rubydoc)
* @see org.eclipse.jdt.internal.ui.wizards.NewElementWizard#finishPage(org.eclipse.core.runtime.IProgressMonitor)
*/
protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException {
- page.createType(monitor); // use the full progress monitor
+ fPage.createType(monitor); // use the full progress monitor
}
-
- /**
- * The worker method. It will find the container, create the file if missing
- * or just replace its contents, and open the editor on the newly created
- * file.
- * @param superclassName
- */
-
- private void doFinish(String containerName, String className, String superclassName, IProgressMonitor monitor)
- throws CoreException {
- // create a sample file
- monitor.beginTask("Creating " + className, 2);
- IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
- IResource resource = root.findMember(new Path(containerName));
- if (!resource.exists() || !(resource instanceof IContainer)) {
- throwCoreException("Container \"" + containerName + "\" does not exist.");
- }
- IContainer container = (IContainer) resource;
- String fileName = classNameToFileName(className) + RUBY_FILE_EXTENSION;
-
- final IFile file = container.getFile(new Path(fileName));
- try {
- InputStream stream = openContentStream(className, superclassName);
- if (file.exists()) {
- file.setContents(stream, true, true, monitor);
- } else {
- file.create(stream, true, monitor);
- }
- stream.close();
- } catch (IOException e) {
- }
- monitor.worked(1);
- monitor.setTaskName("Opening file for editing...");
- getShell().getDisplay().asyncExec(new Runnable() {
-
- public void run() {
- IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
- .getActivePage();
- try {
- IDE.openEditor(page, file, true);
- } catch (PartInitException e) {
- }
- }
- });
- monitor.worked(1);
- }
-
- /**
- * Convert a Constant Class Name (in camels) to a file name (all lowercase,
- * uppercase characters get downcased and have underscores put in front,
- * except first character.)
- *
- * @param className
- * @return
- */
- private String classNameToFileName(String className) {
- className = stripNamespace(className);
- StringBuffer buffer = new StringBuffer();
- for (int i = 0; i < className.length(); i++) {
- char c = className.charAt(i);
- if (Character.isUpperCase(c)) {
- if (i != 0) buffer.append('_');
- buffer.append(Character.toLowerCase(c));
- } else {
- buffer.append(c);
- }
- }
- return buffer.toString();
- }
-
- private String stripNamespace(String className) {
- if (className == null || className.length() == 0) return className;
- if (className.lastIndexOf("::") != -1) {
- return className.substring(className.lastIndexOf("::") + 2);
+
+ /* (non-Rubydoc)
+ * @see org.eclipse.jface.wizard.IWizard#performFinish()
+ */
+ public boolean performFinish() {
+ boolean res= super.performFinish();
+ if (res) {
+ IResource resource= fPage.getModifiedResource();
+ if (resource != null) {
+ selectAndReveal(resource);
+ openResource((IFile) resource);
+ }
}
- return className;
+ return res;
}
- /**
- * We will initialize file contents with a sample text.
- *
- * @param className
- * The Filename chosen by the user
- * @param superclassName
- */
-
- private InputStream openContentStream(String className, String superclassName) {
- StringBuffer contents = new StringBuffer();
- String endLine = System.getProperty("line.separator");
- if (endLine == null) endLine = "\n";
- contents.append("require 'test/unit'");
- contents.append(endLine);
-
- contents.append("class ");
- contents.append(className);
- contents.append(" < ");
- contents.append(superclassName);
- contents.append(endLine);
-
- contents.append(Indents.createIndentString(1, RubyCore.getOptions()));
- contents.append(endLine);
-
- contents.append(Indents.createIndentString(1, RubyCore.getOptions()));
- contents.append(endLine);
-
- contents.append("end");
- contents.append(endLine);
- return new ByteArrayInputStream(contents.toString().getBytes());
- }
-
- private void throwCoreException(String message) throws CoreException {
- IStatus status = new Status(IStatus.ERROR, TestunitPlugin.PLUGIN_ID, IStatus.OK, message, null);
- throw new CoreException(status);
- }
-
- /**
- * We will accept the selection in the workbench to see if we can initialize
- * from it.
- *
- * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
- */
- public void init(IWorkbench workbench, IStructuredSelection selection) {
- this.selection = selection;
- }
+ /* (non-Rubydoc)
+ * @see org.eclipse.jdt.internal.ui.wizards.NewElementWizard#getCreatedElement()
+ */
+ public IRubyElement getCreatedElement() {
+ return fPage.getCreatedType();
+ }
}
\ No newline at end of file
Modified: trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/testunit/wizards/RubyNewTestCaseWizardPage.java
===================================================================
--- trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/testunit/wizards/RubyNewTestCaseWizardPage.java 2007-05-01 15:30:23 UTC (rev 2408)
+++ trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/testunit/wizards/RubyNewTestCaseWizardPage.java 2007-05-01 16:06:07 UTC (rev 2409)
@@ -1,12 +1,14 @@
package org.rubypeople.rdt.testunit.wizards;
+import java.util.ArrayList;
+import java.util.List;
+
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.IDialogPage;
import org.eclipse.jface.dialogs.IDialogSettings;
-import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
@@ -66,11 +68,11 @@
private IType fClassUnderTest;
/**
- * Constructor for SampleNewWizardPage.
+ * Constructor for RubyNewTestCaseWizardPage.
*
* @param pageName
*/
- public RubyNewTestCaseWizardPage(ISelection selection) {
+ public RubyNewTestCaseWizardPage() {
super(true, PAGE_NAME);
setTitle(WizardMessages.NewTestCaseWizardPage_title);
@@ -133,7 +135,7 @@
*/
protected void createTypeMembers(IType type, IProgressMonitor monitor) throws CoreException {
String lineDelimiter= StubUtility.getLineDelimiterUsed(type.getRubyProject());
-
+
if (fMethodStubsButtons.isSelected(IDX_CONSTRUCTOR)) {
createConstructor(type, lineDelimiter);
}
@@ -151,6 +153,13 @@
}
}
+ @Override
+ protected List<String> addImports() {
+ List<String> imports = new ArrayList<String>();
+ imports.add("test/unit");
+ // TODO Add import for class under test?
+ return imports;
+ }
private void createConstructor(IType type, String lineDelimiter) throws CoreException {
StringBuffer content = new StringBuffer("def initialize");
@@ -363,45 +372,30 @@
return fClassUnderTestText;
}
- /**
- * Initialized the page with the current selection
- * @param selection The selection
+ /**
+ * The wizard owning this page is responsible for calling this method with the
+ * current selection. The selection is used to initialize the fields of the wizard
+ * page.
+ *
+ * @param selection used to initialize the fields
*/
public void init(IStructuredSelection selection) {
- IRubyElement element= getInitialRubyElement(selection);
-
- initContainerPage(element);
- // TODO Uncomment to set up type page
-// initTypePage(element);
- // put default class to test
-// if (element != null) {
-// IType classToTest= null;
- // evaluate the enclosing type
-// IType typeInCompUnit= (IType) element.getAncestor(IRubyElement.TYPE);
-// if (typeInCompUnit != null) {
-// if (typeInCompUnit.getRubyScript() != null) {
-// classToTest= typeInCompUnit;
-// }
-// } else {
-// IRubyScript cu= (IRubyScript) element.getAncestor(IRubyElement.SCRIPT);
-// if (cu != null)
-// classToTest= cu.findPrimaryType();
+ IRubyElement jelem= getInitialRubyElement(selection);
+ initContainerPage(jelem);
+ initTypePage(jelem);
+ doStatusUpdate();
+
+// boolean createConstructors= false;
+// boolean createUnimplemented= true;
+// IDialogSettings dialogSettings= getDialogSettings();
+// if (dialogSettings != null) {
+// IDialogSettings section= dialogSettings.getSection(PAGE_NAME);
+// if (section != null) {
+// createConstructors= section.getBoolean(SETTINGS_CREATECONSTR);
// }
- // TODO uncomment to set class under test
-// if (classToTest != null) {
-// try {
-// if (!TestSearchEngine.isTestImplementor(classToTest)) {
-// setClassUnderTest(classToTest.getFullyQualifiedName('.'));
-// }
-// } catch (RubyModelException e) {
-// TestunitPlugin.log(e);
-// }
-// }
// }
-
- restoreWidgetValues();
-
- updateStatus(getStatusList());
+//
+// setMethodStubSelection(createConstructors, true);
}
/**
Modified: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/wizards/NewTypeWizardPage.java
===================================================================
--- trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/wizards/NewTypeWizardPage.java 2007-05-01 15:30:23 UTC (rev 2408)
+++ trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/wizards/NewTypeWizardPage.java 2007-05-01 16:06:07 UTC (rev 2409)
@@ -685,7 +685,17 @@
}
private String constructSimpleTypeStub(String lineDelimiter) {
- StringBuffer buf= new StringBuffer("class "); //$NON-NLS-1$
+ StringBuffer buf= new StringBuffer(); //$NON-NLS-1$
+ List<String> imports = addImports();
+ if (imports != null) {
+ for (String string : imports) {
+ buf.append("require \"");
+ buf.append(string);
+ buf.append('"');
+ buf.append(lineDelimiter);
+ }
+ }
+ buf.append("class "); //$NON-NLS-1$
buf.append(getTypeName());
String superclass = getSuperClass();
if (superclass != null && superclass.trim().length() > 0 && !superclass.trim().equals("Object") ) {
@@ -697,6 +707,11 @@
return buf.toString();
}
+ protected List<String> addImports() {
+ // This is an ugly hack since we don't have an Iportsmanager or ImportRewrite yet.
+ return null;
+ }
+
/**
* Opens a selection dialog that allows to select the super interfaces. The selected interfaces are
* directly added to the wizard page using {@link #addSuperModule(String)}.
@@ -772,10 +787,10 @@
}
/**
- * Returns the resource handle that corresponds to the compilation unit to was or
+ * Returns the resource handle that corresponds to the ruby script that was or
* will be created or modified.
* @return A resource or null if the page contains illegal values.
- * @since 3.0
+ * @since 1.0
*/
public IResource getModifiedResource() {
ISourceFolder pack= getSourceFolder();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|