ICompilationUnit cu = (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT);   IResource resource; try { resource = cu.getCorrespondingResource(); } catch (JavaModelException e) { MessageDialog.openError(shell, CCMessages.getString("Generator.errortitle"), e.getMessage()); return; } if (resource != null && resource.isReadOnly()) { MessageDialog.openError(shell, CCMessages.getString("Generator.errortitle"), CCMessages .getString("Generator.readonly")); return; }  resource = null;
    public void generateMethod(IType type, Shell shell, IProgressMonitor monitor) throws JavaModelException    {        String className = type.getElementName();        ICompilationUnit cu = (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT);        // first check if file is writable        IResource resource;        try        {            resource = cu.getCorrespondingResource();        }        catch (JavaModelException e)        {            MessageDialog.openError(shell, CCMessages.getString("Generator.errortitle"), e.getMessage());            return;        }        if (resource != null && resource.isReadOnly())        {            MessageDialog.openError(shell, CCMessages.getString("Generator.errortitle"), CCMessages                .getString("Generator.readonly"));            return;        }        resource = null;        String title = MessageFormat.format(CCMessages.getString("Generator.generating"), new Object[]{className}); //$NON-NLS-1$        monitor.beginTask(title, 100);        monitor.worked(10);        monitor.setTaskName(title + CCMessages.getString("Generator.parsing")); //$NON-NLS-1$        String src = createMethod(type);        monitor.worked(30);        monitor.setTaskName(title + CCMessages.getString("Generator.formatting")); //$NON-NLS-1$        Document document = new Document(src);        TextEdit text = ToolFactory.createCodeFormatter(null).format(            CodeFormatter.K_UNKNOWN,            src,            0,            src.length(),            getIndentUsed(type, cu) + 1,            null);        try        {            text.apply(document);        }        catch (MalformedTreeException ex)        {            MessageDialog.openError(shell, CCMessages.getString("Generator.errortitle"), ex.getMessage()); //$NON-NLS-1$        }        catch (BadLocationException ex)        {            MessageDialog.openError(shell, CCMessages.getString("Generator.errortitle"), ex.getMessage()); //$NON-NLS-1$        }        monitor.worked(20);        monitor.setTaskName(title + CCMessages.getString("Generator.adding")); //$NON-NLS-1$        type.createMethod(document.get() + LINE_SEPARATOR, null, false, null);        monitor.worked(20);        monitor.setTaskName(title + CCMessages.getString("Generator.imports")); //$NON-NLS-1$        addImports(type);        monitor.worked(20);        monitor.done();    }