FileDocCategorySizeDatePackage
NewXmlFileWizard.javaAPI DocAndroid 1.5 API8063Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.adt.wizards.newxmlfile

NewXmlFileWizard

public class NewXmlFileWizard extends org.eclipse.jface.wizard.Wizard implements org.eclipse.ui.INewWizard
The "New Android XML File Wizard" provides the ability to create skeleton XML resources files for Android projects.

The wizard has one page, {@link NewXmlFileCreationPage}, used to select the project, the resource folder, resource type and file name. It then creates the XML file.

Fields Summary
private static final String
PROJECT_LOGO_LARGE
protected static final String
MAIN_PAGE_NAME
private NewXmlFileCreationPage
mMainPage
Constructors Summary
Methods Summary
public voidaddPages()
Adds pages to this wizard.

        addPage(mMainPage);
    
protected NewXmlFileCreationPagecreateMainPage()
Creates the wizard page.

Please do NOT override this method.

This is protected so that it can be overridden by unit tests. However the contract of this class is private and NO ATTEMPT will be made to maintain compatibility between different versions of the plugin.

        return new NewXmlFileCreationPage(MAIN_PAGE_NAME);
    
private booleancreateWsParentDirectory(org.eclipse.core.resources.IContainer wsPath)

        if (wsPath.getType() == IContainer.FOLDER) {
            if (wsPath == null || wsPath.exists()) {
                return true;
            }

            IFolder folder = (IFolder) wsPath;
            try {
                if (createWsParentDirectory(wsPath.getParent())) {
                    folder.create(true /* force */, true /* local */, null /* monitor */);
                    return true;
                }
            } catch (CoreException e) {
                e.printStackTrace();
            }
        }
        
        return false;
    
private org.eclipse.core.resources.IFilecreateXmlFile()

        IFile file = mMainPage.getDestinationFile();
        String name = file.getFullPath().toString();
        boolean need_delete = false;

        if (file.exists()) {
            if (!AdtPlugin.displayPrompt("New Android XML File",
                String.format("Do you want to overwrite the file %1$s ?", name))) {
                // abort if user selects cancel.
                return null;
            }
            need_delete = true;
        } else {
            createWsParentDirectory(file.getParent());
        }
        
        TypeInfo type = mMainPage.getSelectedType();
        if (type == null) {
            // this is not expected to happen
            AdtPlugin.log(IStatus.ERROR, "Failed to create %1$s: missing type", name);  //$NON-NLS-1$
            return null;
        }
        String xmlns = type.getXmlns();
        String root = mMainPage.getRootElement();
        if (root == null) {
            // this is not expected to happen
            AdtPlugin.log(IStatus.ERROR, "Failed to create %1$s: missing root element", //$NON-NLS-1$
                    file.toString());
            return null;
        }
        
        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");   //$NON-NLS-1$

        sb.append('<").append(root);
        if (xmlns != null) {
            sb.append('\n").append("  xmlns:android=\"").append(xmlns).append("\"");  //$NON-NLS-1$ //$NON-NLS-2$
        }
        
        String attrs = type.getDefaultAttrs();
        if (attrs != null) {
            sb.append("\n  ");                       //$NON-NLS-1$
            sb.append(attrs.replace("\n", "\n  "));  //$NON-NLS-1$ //$NON-NLS-2$
        }
        
        sb.append(">\n");                            //$NON-NLS-1$
        sb.append("</").append(root).append(">\n");  //$NON-NLS-1$ //$NON-NLS-2$

        String result = sb.toString();
        String error = null;
        try {
            byte[] buf = result.getBytes("UTF8");
            InputStream stream = new ByteArrayInputStream(buf);
            if (need_delete) {
                file.delete(IFile.KEEP_HISTORY | IFile.FORCE, null /*monitor*/);
            }
            file.create(stream, true /*force*/, null /*progres*/);
            return file;
        } catch (UnsupportedEncodingException e) {
            error = e.getMessage();
        } catch (CoreException e) {
            error = e.getMessage();
        }

        error = String.format("Failed to generate %1$s: %2$s", name, error);
        AdtPlugin.displayError("New Android XML File", error);
        return null;
    
public voidinit(org.eclipse.ui.IWorkbench workbench, org.eclipse.jface.viewers.IStructuredSelection selection)


          
        setHelpAvailable(false); // TODO have help
        setWindowTitle("New Android XML File");
        setImageDescriptor();

        mMainPage = createMainPage();
        mMainPage.setTitle("New Android XML File");
        mMainPage.setDescription("Creates a new Android XML file.");
        mMainPage.setInitialSelection(selection);
    
public booleanperformFinish()
Performs any actions appropriate in response to the user having pressed the Finish button, or refuse if finishing now is not permitted: here, it actually creates the workspace project and then switch to the Java perspective.

return
True

        IFile file = createXmlFile();
        if (file == null) {
            return false;
        } else {
            // Open the file in an editor
            IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            if (win != null) {
                IWorkbenchPage page = win.getActivePage();
                if (page != null) {
                    try {
                        IDE.openEditor(page, file);
                    } catch (PartInitException e) {
                        AdtPlugin.log(e, "Failed to create %1$s: missing type",  //$NON-NLS-1$
                                file.getFullPath().toString());
                    }
                }
            }
            return true;
        }
    
private voidsetImageDescriptor()
Returns an image descriptor for the wizard logo.

        ImageDescriptor desc = IconFactory.getInstance().getImageDescriptor(PROJECT_LOGO_LARGE);
        setDefaultPageImageDescriptor(desc);