下面列出了怎么用org.eclipse.ui.actions.WorkspaceModifyOperation的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void run(IIntroSite site, Properties params) {
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
IProject project = createProject(monitor);
createExample(project);
}
};
try {
PlatformUI.getWorkbench().getProgressService().run(true, true, op);
final IIntroManager introManager = PlatformUI.getWorkbench().getIntroManager();
IIntroPart part = introManager.getIntro();
introManager.closeIntro(part);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IDE.openEditor(page, ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("hello-world/greeter.sol")));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testOnRemoveTwoProjects() {
try {
Assert.assertEquals(0, this.getCachedPackageFragmentRootData().size());
final IJavaProject project = JavaProjectSetupUtil.createJavaProject("testProject");
final IJavaProject project2 = JavaProjectSetupUtil.createJavaProject("testProject2");
final int sizeBefore = this.getCachedPackageFragmentRootData().size();
final IFile file = this.createJar(project);
JavaProjectSetupUtil.addJarToClasspath(project, file);
JavaProjectSetupUtil.addJarToClasspath(project2, file);
this.assertBothProjects(sizeBefore);
final WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
@Override
protected void execute(final IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
project.getProject().delete(true, monitor);
project2.getProject().delete(true, monitor);
}
};
op.run(IResourcesSetupUtil.monitor());
this.assertNonProjects();
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
public static void setReference(final IProject from, final IProject to)
throws CoreException, InvocationTargetException,
InterruptedException {
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
IProjectDescription projectDescription = from.getDescription();
IProject[] projects = projectDescription
.getReferencedProjects();
IProject[] newProjects = new IProject[projects.length + 1];
System.arraycopy(projects, 0, newProjects, 0, projects.length);
newProjects[projects.length] = to;
projectDescription.setReferencedProjects(newProjects);
from.setDescription(projectDescription, monitor());
}
}.run(monitor());
}
public static IFolder createFolder(IPath wsRelativePath) throws InvocationTargetException, InterruptedException {
final IFolder folder = root().getFolder(wsRelativePath);
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
create(folder.getParent());
folder.delete(true, monitor());
folder.create(true, true, monitor());
}
}.run(monitor());
return folder;
}
public static IFile createFile(IPath wsRelativePath, final String s)
throws CoreException, InvocationTargetException,
InterruptedException {
final IFile file = root().getFile(wsRelativePath);
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
create(file.getParent());
file.delete(true, monitor());
try {
file.create(new StringInputStream(s, file.getCharset(true)), true, monitor());
} catch (UnsupportedEncodingException exc) {
throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.xtext.junit4", exc.getMessage(), exc));
}
}
}.run(monitor());
return file;
}
private static void create(final IContainer container)
throws CoreException, InvocationTargetException,
InterruptedException {
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
if (!container.exists()) {
create(container.getParent());
if (container instanceof IFolder) {
((IFolder) container).create(true, true, monitor());
} else {
IProject iProject = (IProject) container;
createProject(iProject);
}
}
}
}.run(monitor());
}
public static void setReference(final IProject from, final IProject to)
throws CoreException, InvocationTargetException,
InterruptedException {
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
IProjectDescription projectDescription = from.getDescription();
IProject[] projects = projectDescription
.getReferencedProjects();
IProject[] newProjects = new IProject[projects.length + 1];
System.arraycopy(projects, 0, newProjects, 0, projects.length);
newProjects[projects.length] = to;
projectDescription.setReferencedProjects(newProjects);
from.setDescription(projectDescription, monitor());
}
}.run(monitor());
}
public static IFolder createFolder(IPath wsRelativePath) throws InvocationTargetException, InterruptedException {
final IFolder folder = root().getFolder(wsRelativePath);
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
create(folder.getParent());
folder.delete(true, monitor());
folder.create(true, true, monitor());
}
}.run(monitor());
return folder;
}
public static IFile createFile(IPath wsRelativePath, final String s)
throws CoreException, InvocationTargetException,
InterruptedException {
final IFile file = root().getFile(wsRelativePath);
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
create(file.getParent());
file.delete(true, monitor());
try (InputStream stream = new StringInputStream(s, file.getCharset(true))) {
file.create(stream, true, monitor());
} catch (IOException exc) {
throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.xtext.ui.testing", exc.getMessage(), exc));
}
}
}.run(monitor());
return file;
}
private static void create(final IContainer container)
throws CoreException, InvocationTargetException,
InterruptedException {
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
if (!container.exists()) {
create(container.getParent());
if (container instanceof IFolder) {
((IFolder) container).create(true, true, monitor());
} else {
IProject iProject = (IProject) container;
createProject(iProject);
}
}
}
}.run(monitor());
}
public <Result> Result run(IWorkspaceModifyOperationWithResult<? extends Result> op) {
try {
return new WorkspaceModifyOperation(op.getRule()) {
Result result;
@Override
protected void execute(IProgressMonitor monitor) throws InvocationTargetException, CoreException, InterruptedException {
this.result = op.compute(monitor);
}
protected Result getResult() throws InvocationTargetException, InterruptedException {
run(monitor());
return result;
}
}.getResult();
} catch (InvocationTargetException | InterruptedException e) {
return Exceptions.throwUncheckedException(e);
}
}
@Test
public void testFolderExists() {
try {
final String folderPath = "/foo/bar";
this.resourceChangeRegistry.registerExists(folderPath, this.uri);
Assert.assertTrue(this.resourceChangeRegistry.getExistsListeners().containsKey(folderPath));
final IProject project = WorkbenchTestHelper.createPluginProject("foo");
Assert.assertTrue(this.resourceChangeRegistry.getExistsListeners().containsKey(folderPath));
final IFolder folder = project.getFolder("bar");
Assert.assertTrue(this.resourceChangeRegistry.getExistsListeners().containsKey(folderPath));
final WorkspaceModifyOperation _function = new WorkspaceModifyOperation() {
@Override
protected void execute(final IProgressMonitor it) throws CoreException, InvocationTargetException, InterruptedException {
folder.create(true, true, null);
}
};
this.modifyWorkspace(_function);
Assert.assertFalse(this.resourceChangeRegistry.getExistsListeners().containsKey(folderPath));
Assert.assertEquals(1, this.resourceChangeRegistry.queuedURIs.size());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
@Test
public void testFolderChildren() {
try {
final IProject project = WorkbenchTestHelper.createPluginProject("foo");
final String folderPath = "/foo/bar";
this.resourceChangeRegistry.registerGetChildren(folderPath, this.uri);
Assert.assertTrue(this.resourceChangeRegistry.getChildrenListeners().containsKey(folderPath));
final IFolder folder = project.getFolder("bar");
final WorkspaceModifyOperation _function = new WorkspaceModifyOperation() {
@Override
protected void execute(final IProgressMonitor it) throws CoreException, InvocationTargetException, InterruptedException {
folder.create(true, true, null);
}
};
this.modifyWorkspace(_function);
Assert.assertFalse(this.resourceChangeRegistry.getChildrenListeners().containsKey(folderPath));
Assert.assertEquals(1, this.resourceChangeRegistry.queuedURIs.size());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
@Override
public boolean performFinish() {
IRunnableWithProgress op = new WorkspaceModifyOperation(null) {
@Override
protected void execute(IProgressMonitor monitor) throws CoreException, InterruptedException {
ensureSCTNature(getProject(modelFilePage.getContainerFullPath()));
Resource resource = createDefaultModel(modelFilePage.getURI());
openModel(resource);
}
};
try {
getContainer().run(false, true, op);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
protected boolean createNewStatechart() {
final DiagramCreationDesccription create = getDiagramDescription();
IRunnableWithProgress op = new WorkspaceModifyOperation(null) {
@Override
protected void execute(IProgressMonitor monitor) throws CoreException, InterruptedException {
diagram = new DiagramCreator().createDiagram(create, monitor);
}
};
try {
getContainer().run(false, true, op);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return diagram != null;
}
@Override
final protected IRunnableWithProgress createOperation(final IStatus[] errorStatus) {
return new WorkspaceModifyOperation() {
@Override
public void execute(final IProgressMonitor monitor) {
final Iterator<? extends IResource> resourcesEnum = resources.iterator();
try {
while (resourcesEnum.hasNext()) {
try {
final IResource resource = resourcesEnum.next();
refreshResource(resource, null);
} catch (final CoreException e) {}
if (monitor.isCanceled()) { throw new OperationCanceledException(); }
}
} finally {
monitor.done();
}
}
};
}
/** Create the container tree.
*
* @param container the container
* @throws InvocationTargetException in case of error.
* @throws InterruptedException in case of errir.
*/
public static void create(org.eclipse.core.resources.IContainer container)
throws InvocationTargetException, InterruptedException {
new WorkspaceModifyOperation() {
@SuppressWarnings("synthetic-access")
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
if (!container.exists()) {
create(container.getParent());
if (container instanceof IFolder) {
((IFolder) container).create(true, true, new NullProgressMonitor());
} else {
IProject iProject = (IProject) container;
createProject(iProject);
}
}
}
}.run(new NullProgressMonitor());
}
/** Create a file.
*
* @param fullFileName the name of the file to create.
* @param content the content of the file.
* @return the file.
* @throws Exception
*/
public IFile createFileImpl(String fullFileName, String content) throws Exception {
final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(fullFileName));
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
create(file.getParent());
file.delete(true, new NullProgressMonitor());
try (final InputStream is = new StringInputStream(content)) {
file.create(is, true, new NullProgressMonitor());
} catch (Exception exception) {
//
}
}
}.run(new NullProgressMonitor());
getFiles().add(file);
return file;
}
/** Create a file.
*
* @param file the name of the file to create.
* @param content the content of the file.
* @return the file.
* @throws Exception
*/
public IFile createFileImpl(IFile file, InputStream content) throws Exception {
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
create(file.getParent());
file.delete(true, new NullProgressMonitor());
file.create(content, true, new NullProgressMonitor());
}
}.run(new NullProgressMonitor());
getFiles().add(file);
return file;
}
private void refreshReportProject( final IProject project )
{
WorkspaceModifyOperation op = new WorkspaceModifyOperation( ) {
protected void execute( IProgressMonitor monitor )
throws CoreException
{
project.refreshLocal( IResource.DEPTH_INFINITE, monitor );
}
};
try
{
new ProgressMonitorDialog( composite.getShell( ) ).run( false,
true,
op );
}
catch ( Exception e )
{
ExceptionUtil.handle( e );
}
}
@Override
public void rename(String newName) {
try {
new ProgressMonitorDialog(Display.getDefault().getActiveShell()).run(true, false,
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException, InterruptedException {
closeAllEditors();
projectListeners.stream().forEach(l -> l.projectClosed(Repository.this, monitor));
disableBuild();
getProject().move(org.eclipse.core.runtime.Path.fromOSString(newName), true, monitor);
RepositoryManager.getInstance().setRepository(newName, monitor);
}
});
} catch (InvocationTargetException | InterruptedException e) {
new BonitaErrorDialog(Display.getDefault().getActiveShell(), "Rename failed", e.getMessage(), e).open();
}
}
/***/
@Before
public void setUp2() throws Exception {
waitForIdleState();
projectExplorer = (ProjectExplorer) EclipseUIUtils.showView(ProjectExplorer.VIEW_ID);
UIUtils.waitForUiThread();
assertNotNull("Cannot show Project Explorer.", projectExplorer);
commonViewer = projectExplorer.getCommonViewer();
assertFalse("Expected projects as top level elements in navigator.", broker.isWorkingSetTopLevel());
assertNull(
"Select working set drop down contribution was visible when projects are configured as top level elements.",
getWorkingSetDropDownContribution());
final Multimap<ProjectType, String> typeNamesMapping = HashMultimap.create();
typeNamesMapping.putAll(LIBRARY, LIBRARY_PROJECTS);
typeNamesMapping.putAll(TEST, TEST_PROJECTS);
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException, InterruptedException {
for (final Entry<ProjectType, Collection<String>> entry : typeNamesMapping.asMap().entrySet()) {
for (final String projectName : entry.getValue()) {
createN4JSProject(projectName, entry.getKey());
}
}
// Actually close "Closed*" projects
closeProject("ClosedL2");
closeProject("ClosedT2");
}
}.run(new NullProgressMonitor());
// Wait for workbench to reflect project changes
waitForIdleState();
commonViewer.refresh();
// Disable auto-building, as there is no real code to build involved
wasAutobuilding = IResourcesSetupUtil.setAutobuild(false);
}
protected void doRename() throws Exception {
IResourcesSetupUtil.waitForBuild();
final Change change = createChange(uriB, "C");
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException,
InterruptedException {
undoChange = change.perform(monitor);
}
}.run(null);
}
protected void undoRename() throws Exception {
IResourcesSetupUtil.waitForBuild();
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException,
InterruptedException {
undoChange.perform(monitor);
}
}.run(null);
}
public static void removeReference(final IProject from, final IProject to)
throws CoreException, InvocationTargetException,
InterruptedException {
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
IProjectDescription projectDescription = from.getDescription();
IProject[] projects = projectDescription
.getReferencedProjects();
for (int i = 0; i < projects.length; ++i) {
if (to.equals(projects[i])) {
// Remove the nature
IProject[] newProjects = new IProject[projects.length - 1];
System.arraycopy(projects, 0, newProjects, 0, i);
System.arraycopy(projects, i + 1, newProjects, i, projects.length
- i - 1);
projectDescription.setReferencedProjects(newProjects);
from.setDescription(projectDescription, null);
return;
}
}
}
}.run(monitor());
}
public static void removeReference(final IProject from, final IProject to)
throws CoreException, InvocationTargetException,
InterruptedException {
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor)
throws CoreException, InvocationTargetException,
InterruptedException {
IProjectDescription projectDescription = from.getDescription();
IProject[] projects = projectDescription
.getReferencedProjects();
for (int i = 0; i < projects.length; ++i) {
if (to.equals(projects[i])) {
// Remove the nature
IProject[] newProjects = new IProject[projects.length - 1];
System.arraycopy(projects, 0, newProjects, 0, i);
System.arraycopy(projects, i + 1, newProjects, i, projects.length
- i - 1);
projectDescription.setReferencedProjects(newProjects);
from.setDescription(projectDescription, null);
return;
}
}
}
}.run(monitor());
}
public void run(IWorkspaceModifyOperation op) {
try {
new WorkspaceModifyOperation(op.getRule()) {
@Override
protected void execute(IProgressMonitor monitor) throws InvocationTargetException, CoreException, InterruptedException {
op.accept(monitor);
}
}.run(monitor());
} catch (InvocationTargetException | InterruptedException e) {
Exceptions.throwUncheckedException(e);
}
}
@Test public void testOverrideIndicatorAnnotationAfterFileRename() throws Exception {
testHelper.createFile("test/SuperClass","package test\nclass SuperClass { def foo() {}}");
final IFile subClassFile = testHelper.createFile("test/SubClass","package test\nclass SubClass extends SuperClass { override foo () {}}");
IResourcesSetupUtil.waitForBuild();
XtextEditor openEditor = testHelper.openEditor(subClassFile);
final OverrideIndicatorAnnotation[] annotationBeforeFileRename = new OverrideIndicatorAnnotation[]{null};
final OverrideIndicatorAnnotation[] annotationAfterFileRename = new OverrideIndicatorAnnotation[]{null};
final ISourceViewer sourceViewer = openEditor.getInternalSourceViewer();
sleepWhile(Predicates.isNull(), new Provider<Object>() {
@Override
public OverrideIndicatorAnnotation get() {
annotationBeforeFileRename[0] = Iterators.getOnlyElement(Iterators.filter(sourceViewer.getAnnotationModel().getAnnotationIterator(), OverrideIndicatorAnnotation.class), null);
return annotationBeforeFileRename[0];
}
},TimeUnit.SECONDS.toMillis(10));
assertNotNull(annotationBeforeFileRename[0]);
new WorkspaceModifyOperation() {
@Override
protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException,
InterruptedException {
subClassFile.move(subClassFile.getFullPath().removeLastSegments(1).append("Test.xtend"), true, monitor);
}
}.run(new NullProgressMonitor());
IResourcesSetupUtil.waitForBuild();
sleepWhile(Predicates.isNull(), new Provider<Object>() {
@Override
public OverrideIndicatorAnnotation get() {
OverrideIndicatorAnnotation ann = Iterators.getOnlyElement(Iterators.filter(sourceViewer.getAnnotationModel().getAnnotationIterator(), OverrideIndicatorAnnotation.class), null);
if (ann != annotationBeforeFileRename[0])
annotationAfterFileRename[0] = ann;
return annotationAfterFileRename[0];
}
},TimeUnit.SECONDS.toMillis(10));
assertNotNull(annotationAfterFileRename[0]);
assertNotSame(annotationBeforeFileRename[0], annotationAfterFileRename[0]);
}
public void modifyWorkspace(final WorkspaceModifyOperation op) {
try {
this.resourceChangeRegistry.waitForEvent = true;
op.run(null);
while (this.resourceChangeRegistry.waitForEvent) {
Thread.sleep(100);
}
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
/**
* Adds a new experiment to the project
*
* @param projectElement
* The project to add to
* @param experimentName
* Name of the experiment
* @return The newly created experiment
*/
public static TmfExperimentElement addExperiment(TmfProjectElement projectElement, String experimentName) {
TmfExperimentFolder experimentsFolder = projectElement.getExperimentsFolder();
if (experimentsFolder != null) {
IFolder experimentFolder = experimentsFolder.getResource();
final IFolder folder = experimentFolder.getFolder(experimentName);
WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
@Override
public void execute(IProgressMonitor monitor) throws CoreException {
monitor.beginTask("", 1000);
folder.create(false, true, monitor);
monitor.done();
}
};
try {
PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
} catch (InterruptedException | InvocationTargetException | RuntimeException exception) {
exception.printStackTrace();
}
experimentsFolder.refresh();
for (ITmfProjectModelElement el : experimentsFolder.getChildren()) {
if (el.getName().equals(experimentName) && (el instanceof TmfExperimentElement)) {
return (TmfExperimentElement) el;
}
}
}
return null;
}