Example usage for org.eclipse.jdt.core IBuffer save

List of usage examples for org.eclipse.jdt.core IBuffer save

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IBuffer save.

Prototype

public void save(IProgressMonitor progress, boolean force) throws JavaModelException;

Source Link

Document

Saves the contents of this buffer to its underlying resource.

Usage

From source file:org.eclipse.gmf.tests.gen.CompilationTest.java

License:Open Source License

public void testPreexistingImportConflicts() throws Exception {
    DiaGenSource gmfGenSource = createLibraryGen(false);
    gmfGenSource.getGenDiagram().getEditorGen().setSameFileForDiagramAndModel(false);
    String pluginId = gmfGenSource.getGenDiagram().getEditorGen().getPlugin().getID();
    IProject diagramProject = ResourcesPlugin.getWorkspace().getRoot().getProject(pluginId);
    if (!diagramProject.isAccessible()) {
        //Initialize the plugin the same way it would be initialized if present.
        Generator.createEMFProject(diagramProject.getFolder("src").getFullPath(), null, //$NON-NLS-1$
                Collections.<IProject>emptyList(), new NullProgressMonitor(),
                Generator.EMF_PLUGIN_PROJECT_STYLE);
    }/*from  w w w  . j  a v a  2  s .  co m*/
    IJavaProject javaProject = JavaCore.create(diagramProject);
    assertTrue(javaProject.exists());
    IPackageFragment pf = javaProject.getPackageFragmentRoot(diagramProject.getFolder("src")) //$NON-NLS-1$
            .createPackageFragment(gmfGenSource.getGenDiagram().getNotationViewFactoriesPackageName(), false,
                    new NullProgressMonitor());
    ICompilationUnit cu = pf
            .getCompilationUnit(gmfGenSource.getGenDiagram().getNotationViewFactoryClassName() + ".java"); //$NON-NLS-1$
    String contents = MessageFormat.format(
            "package {0};\nimport {2};\n /**\n * @generated\n */\npublic class {1} '{ }'", //$NON-NLS-1$
            gmfGenSource.getGenDiagram().getNotationViewFactoriesPackageName(),
            gmfGenSource.getGenDiagram().getNotationViewFactoryClassName(), "javax.swing.text.View");
    if (cu.exists()) {
        IBuffer buffer = cu.getBuffer();
        buffer.setContents(contents);
        buffer.save(new NullProgressMonitor(), true);
    } else {
        pf.createCompilationUnit(cu.getElementName(), contents, false, new NullProgressMonitor());
    }
    generateAndCompile(gmfGenSource);
}

From source file:org.eclipse.objectteams.otdt.internal.ui.bindingeditor.BindingEditorDialog.java

License:Open Source License

protected void okPressed() {
    setReturnCode(OK);/*  w ww  . j ava 2s  . c  om*/

    IProgressMonitor monitor = new NullProgressMonitor();
    ICompilationUnit cu = _team.getCompilationUnit();
    Map<String, String> options = cu.getJavaProject().getOptions(true);
    // recognize OT/J syntax even in fragments not starting with "team":
    options.put(org.eclipse.jdt.internal.compiler.impl.CompilerOptions.OPTION_AllowScopedKeywords,
            org.eclipse.jdt.internal.compiler.impl.CompilerOptions.DISABLED);
    TextEdit edits = _root.rewrite(_originalDocument, options);

    try {
        edits.apply(_originalDocument);
        String newSource = _originalDocument.get();
        IBuffer buf = cu.getBuffer();
        buf.setContents(newSource);
        // TODO(jsv) use "organize imports" also for closed files, this version works only on open file  
        try {
            IWorkbenchPage activePage = org.eclipse.jdt.internal.ui.JavaPlugin.getActivePage();
            IWorkbenchSite site = activePage.getActiveEditor().getEditorSite();
            OrganizeImportsAction organizeImportsAction = new OrganizeImportsAction(site);
            organizeImportsAction.run(cu);
        } catch (NullPointerException ex) {
            org.eclipse.jdt.internal.ui.JavaPlugin.log(ex);
        }

        buf.save(monitor, false);
    } catch (Exception ex) {
        org.eclipse.jdt.internal.ui.JavaPlugin.log(ex);
    }
    close();
}

From source file:org.seasar.doma.extension.domax.ResourceFileChangeListener.java

License:Apache License

private void submitJob(final IResource resource, DaoMethod daoMethod) {
    IJavaProject javaProject = daoMethod.getJavaProject();
    try {/*from www .j  a v  a 2 s. com*/
        IType type = javaProject.findType(daoMethod.getClassName());
        if (type == null) {
            return;
        }
        final ICompilationUnit compilationUnit = type.getCompilationUnit();
        if (compilationUnit == null) {
            return;
        }

        WorkspaceJob job = new WorkspaceJob("building " + daoMethod.getClassName()) {

            @Override
            public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
                compilationUnit.becomeWorkingCopy(monitor);
                IBuffer buffer = compilationUnit.getBuffer();
                buffer.append("");
                buffer.save(monitor, true);
                buffer.close();
                compilationUnit.commitWorkingCopy(true, monitor);
                resource.getProject().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
                return Status.OK_STATUS;
            }
        };
        job.setPriority(Job.BUILD);
        job.schedule();
    } catch (JavaModelException e) {
        Logger.error(e);
    }
}