|
From: Christopher W. <caw...@us...> - 2006-01-31 20:30:04
|
Update of /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/wizards In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv673/src/org/rubypeople/rdt/ui/wizards Added Files: RubyTypeSelectionDialog.java RubyClassSelectionDialog.java RubyNewClassWizard.java Log Message: add a new Ruby Class Wizard (it's not especially useful yet, but it is more obvious than new -> file -> name it with a standard ruby file name/extension) --- NEW FILE: RubyNewClassWizard.java --- package org.rubypeople.rdt.ui.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.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Status; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; 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.internal.ui.RubyPlugin; import org.rubypeople.rdt.internal.ui.RubyPluginImages; import org.rubypeople.rdt.internal.ui.wizards.RubyNewClassWizardPage; /** * This is a sample new wizard. Its role is to create a new file resource in the * provided container. If the container resource (a folder or a project) is * selected in the workspace when the wizard is opened, it will accept it as the * target container. The wizard creates one file with the extension "mpe". If a * sample multi-page editor (also available as a template) is registered for the * same extension, it will be able to open it. */ public class RubyNewClassWizard extends Wizard implements INewWizard { private static final String RUBY_FILE_EXTENSION = ".rb"; private RubyNewClassWizardPage page; private ISelection selection; /** * Constructor for SampleNewWizard. */ public RubyNewClassWizard() { super(); setDefaultPageImageDescriptor(RubyPluginImages.DESC_WIZBAN_NEWCLASS); setNeedsProgressMonitor(true); } /** * Adding the page to the wizard. */ public void addPages() { page = new RubyNewClassWizardPage(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() { final String containerName = page.getContainerName(); final String className = page.getClassName(); final String superclassName = page.getSuperclassName(); IRunnableWithProgress op = new IRunnableWithProgress() { public void run(IProgressMonitor monitor) throws InvocationTargetException { try { doFinish(containerName, className, superclassName, monitor); } catch (CoreException e) { throw new InvocationTargetException(e); } finally { monitor.done(); } } }; try { getContainer().run(true, false, op); } catch (InterruptedException e) { return false; } catch (InvocationTargetException e) { Throwable realException = e.getTargetException(); MessageDialog.openError(getShell(), "Error", realException.getMessage()); return false; } return true; } /** * 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) { 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(); } /** * 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(); contents.append("class "); contents.append(className); if (superclassName != null && superclassName != "Object") { contents.append(" < "); contents.append(superclassName); } contents.append("\n \nend\n"); return new ByteArrayInputStream(contents.toString().getBytes()); } private void throwCoreException(String message) throws CoreException { IStatus status = new Status(IStatus.ERROR, RubyPlugin.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; } } --- NEW FILE: RubyTypeSelectionDialog.java --- package org.rubypeople.rdt.ui.wizards; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.dialogs.ElementListSelectionDialog; import org.rubypeople.rdt.core.IRubyElement; import org.rubypeople.rdt.core.IRubyScript; import org.rubypeople.rdt.core.RubyCore; import org.rubypeople.rdt.core.RubyModelException; import org.rubypeople.rdt.internal.core.RubyElement; import org.rubypeople.rdt.internal.ui.RubyPlugin; import org.rubypeople.rdt.internal.ui.util.RubyElementVisitor; import org.rubypeople.rdt.ui.RubyElementLabelProvider; public class RubyTypeSelectionDialog extends ElementListSelectionDialog { public RubyTypeSelectionDialog(Shell parent) { super(parent, new RubyElementLabelProvider()); setElements(getElements()); setMultipleSelection(false); } /** * Meant for subclasses to override what elements to show * @return */ protected Object[] getElements() { return getAllTypes(); } /** * @return */ private IRubyElement[] getAllTypes() { List typeList = new ArrayList(); IProject[] projects = RubyCore.getRubyProjects(); for (int i = 0; i < projects.length; i++) { IRubyElement[] types = findElements(projects[i], IRubyElement.TYPE); typeList.addAll(Arrays.asList(types)); } IRubyElement[] allTypes = new IRubyElement[typeList.size()]; System.arraycopy(typeList.toArray(), 0, allTypes, 0, allTypes.length); return allTypes; } public static IRubyElement[] findElements(final IFile file, int elementType) { IRubyScript script = RubyCore.create(file); try { script.reconcile(); IRubyElement[] children = script.getChildren(); List types = new ArrayList(); for (int i = 0; i < children.length; i++) { if (children[i].isType(elementType)) types.add(children[i]); } IRubyElement[] array = new IRubyElement[types.size()]; System.arraycopy(types.toArray(), 0, array, 0, types.size()); return array; } catch (RubyModelException e) { RubyPlugin.log(e); } return new IRubyElement[0]; } /** * @param rubyProject * @param elementType */ public static IRubyElement[] findElements(IProject rubyProject, int elementType) { if (rubyProject == null) { return new IRubyElement[0]; } try { List allElements = new ArrayList(); RubyElementVisitor visitor = new RubyElementVisitor(); rubyProject.accept(visitor); Object[] rubyFiles = visitor.getCollectedRubyFiles(); for (int i = 0; i < rubyFiles.length; i++) { IFile rubyFile = (IFile) rubyFiles[i]; IRubyElement[] fileElements = findElements(rubyFile, elementType); for (int j = 0; j < fileElements.length; j++) { allElements.add(fileElements[j]); } } Object[] listArray = allElements.toArray(); IRubyElement[] array = new IRubyElement[allElements.size()]; System.arraycopy(listArray, 0, array, 0, listArray.length); return array; } catch (CoreException e) { e.printStackTrace(); } return new RubyElement[0]; } } --- NEW FILE: RubyClassSelectionDialog.java --- package org.rubypeople.rdt.ui.wizards; import java.util.ArrayList; import java.util.List; import org.eclipse.swt.widgets.Shell; import org.rubypeople.rdt.core.IRubyElement; import org.rubypeople.rdt.core.IType; public class RubyClassSelectionDialog extends RubyTypeSelectionDialog { public RubyClassSelectionDialog(Shell parent) { super(parent); } protected Object[] getElements() { Object[] types = super.getElements(); List classes = new ArrayList(); for (int i = 0; i < types.length; i++) { IType type = (IType) types[i]; if (type.isClass()) classes.add(types[i]); } IRubyElement[] elements = new IRubyElement[classes.size()]; System.arraycopy(classes.toArray(), 0, elements, 0, elements.length); return elements; } } |