use of org.eclipse.core.resources.WorkspaceJob in project translationstudio8 by heartsome.
the class ResourceMgmtActionProvider method makeActions.
protected void makeActions() {
IShellProvider sp = new IShellProvider() {
public Shell getShell() {
return shell;
}
};
openProjectAction = new OpenResourceAction(sp);
openProjectAction.setText(WorkbenchNavigatorMessages.actions_ResourceMgmtActionProvider_openProjectAction);
closeProjectAction = new CloseResourceAction(sp);
closeProjectAction.setText(WorkbenchNavigatorMessages.actions_ResourceMgmtActionProvider_closeProjectAction);
//
// closeUnrelatedProjectsAction = new CloseUnrelatedProjectsAction(sp);
// closeUnrelatedProjectsAction.setText("关闭无关的项目");
refreshAction = new RefreshAction(sp) {
public void run() {
final IStatus[] errorStatus = new IStatus[1];
errorStatus[0] = Status.OK_STATUS;
final WorkspaceModifyOperation op = (WorkspaceModifyOperation) createOperation(errorStatus);
WorkspaceJob job = new //$NON-NLS-1$
WorkspaceJob(//$NON-NLS-1$
"refresh") {
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
try {
op.run(monitor);
if (shell != null && !shell.isDisposed()) {
shell.getDisplay().asyncExec(new Runnable() {
public void run() {
StructuredViewer viewer = getActionSite().getStructuredViewer();
if (viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed()) {
viewer.refresh();
}
}
});
}
} catch (InvocationTargetException e) {
String msg = NLS.bind(WorkbenchNavigatorMessages.actions_ResourceMgmtActionProvider_logTitle, getClass().getName(), e.getTargetException());
throw new CoreException(new Status(IStatus.ERROR, NavigatorPlugin.PLUGIN_ID, IStatus.ERROR, msg, e.getTargetException()));
} catch (InterruptedException e) {
return Status.CANCEL_STATUS;
}
return errorStatus[0];
}
};
ISchedulingRule rule = op.getRule();
if (rule != null) {
job.setRule(rule);
}
job.setUser(true);
job.schedule();
}
};
refreshAction.setText(WorkbenchNavigatorMessages.actions_ResourceMgmtActionProvider_refreshAction);
//$NON-NLS-1$
refreshAction.setDisabledImageDescriptor(getImageDescriptor("dlcl16/refresh_nav.gif"));
//$NON-NLS-1$
refreshAction.setImageDescriptor(getImageDescriptor("elcl16/refresh_nav.gif"));
refreshAction.setActionDefinitionId(IWorkbenchCommandConstants.FILE_REFRESH);
if (getContext() == null) {
refreshAction.setEnabled(false);
} else {
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
refreshAction.selectionChanged(selection);
refreshAction.setEnabled(!selection.isEmpty());
}
// buildAction = new BuildAction(sp, IncrementalProjectBuilder.INCREMENTAL_BUILD);
// buildAction.setActionDefinitionId(IWorkbenchCommandConstants.PROJECT_BUILD_PROJECT);
}
use of org.eclipse.core.resources.WorkspaceJob in project bndtools by bndtools.
the class RepositoriesViewRefresher method refreshRepositories.
public void refreshRepositories(final RepositoryPlugin target) {
synchronized (this) {
if (busy) {
redo = true;
return;
}
busy = true;
redo = false;
}
//
// Since this can delay, we move this to the background
//
new WorkspaceJob("Updating repositories content") {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
try {
if (monitor == null)
monitor = new NullProgressMonitor();
Set<RepositoryPlugin> repos = new HashSet<>();
if (target != null)
repos.add(target);
else {
for (RefreshModel m : viewers.values()) {
repos.addAll(m.getRepositories());
}
}
ensureLoaded(monitor, repos);
// get repositories first, then do UI thread work
final Map<Entry<TreeViewer, RefreshModel>, List<RepositoryPlugin>> entryRepos = new HashMap<>();
for (Map.Entry<TreeViewer, RefreshModel> entry : viewers.entrySet()) {
entryRepos.put(entry, entry.getValue().getRepositories());
}
//
// And now back to the UI thread
//
getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
synchronized (RepositoriesViewRefresher.this) {
redo = false;
}
for (Map.Entry<TreeViewer, RefreshModel> entry : viewers.entrySet()) {
TreePath[] expandedTreePaths = entry.getKey().getExpandedTreePaths();
entry.getKey().setInput(entryRepos.get(entry));
if (expandedTreePaths != null && expandedTreePaths.length > 0)
entry.getKey().setExpandedTreePaths(expandedTreePaths);
}
synchronized (RepositoriesViewRefresher.this) {
busy = false;
if (redo) {
refreshRepositories(null);
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
return Status.OK_STATUS;
}
}.schedule(1000);
}
use of org.eclipse.core.resources.WorkspaceJob in project bndtools by bndtools.
the class WorkspaceRepositoryChangeDetector method resourceChanged.
@Override
public void resourceChanged(IResourceChangeEvent event) {
try {
if (refresh.get())
return;
if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
RootFolderVisitor rootFolderVisitor = new RootFolderVisitor();
event.getDelta().accept(rootFolderVisitor);
if (refresh.getAndSet(false)) {
WorkspaceJob job = new WorkspaceJob("Refresh Workspace Repository") {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) {
if (monitor == null)
monitor = new NullProgressMonitor();
List<RepositoryListenerPlugin> plugins = workspace.getPlugins(RepositoryListenerPlugin.class);
monitor.beginTask("Refresh ", plugins.size());
int n = 0;
for (RepositoryListenerPlugin rlp : plugins) try {
monitor.worked(n++);
rlp.repositoryRefreshed(repository);
} catch (Exception e) {
e.printStackTrace();
}
monitor.done();
return Status.OK_STATUS;
}
};
job.schedule(500);
}
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.eclipse.core.resources.WorkspaceJob in project bndtools by bndtools.
the class WorkspaceSetupWizard method performFinish.
@Override
public boolean performFinish() {
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
final File targetDir = previewPage.getTargetDir();
final Set<String> checkedPaths = previewPage.getCheckedPaths();
final boolean cleanBuild = setupPage.isCleanBuild();
try {
// Expand the template
ResourceMap outputs = previewPage.getTemplateOutputs();
final Set<File> topLevelFolders = new HashSet<>();
for (Entry<String, Resource> entry : outputs.entries()) {
String path = entry.getKey();
if (checkedPaths.contains(path)) {
Resource resource = entry.getValue();
// Create the folder or file resource
File file = new File(targetDir, path);
switch(resource.getType()) {
case Folder:
Files.createDirectories(file.toPath());
break;
case File:
File parentDir = file.getParentFile();
Files.createDirectories(parentDir.toPath());
try (InputStream in = resource.getContent()) {
IO.copy(in, file);
}
break;
default:
throw new IllegalArgumentException("Unknown resource type " + resource.getType());
}
// Remember the top-level folders we create, for importing below
if (file.getParentFile().equals(targetDir))
topLevelFolders.add(file);
}
}
// Import anything that looks like an Eclipse project & do a full rebuild
final IWorkspaceRunnable importProjectsRunnable = new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
File[] children = targetDir.listFiles();
if (children != null) {
int work = children.length;
if (cleanBuild)
work += 2;
SubMonitor progress = SubMonitor.convert(monitor, work);
for (File folder : children) {
if (folder.isDirectory() && topLevelFolders.contains(folder)) {
String projectName = folder.getName();
File projectFile = new File(folder, IProjectDescription.DESCRIPTION_FILE_NAME);
if (projectFile.exists()) {
IProject project = workspace.getRoot().getProject(projectName);
if (!project.exists()) {
// No existing project in the workspace, so import the generated project.
SubMonitor subProgress = progress.newChild(1);
project.create(subProgress.newChild(1));
project.open(subProgress.newChild(1));
// Now make sure it is associated with the right location
IProjectDescription description = project.getDescription();
IPath path = Path.fromOSString(projectFile.getParentFile().getAbsolutePath());
description.setLocation(path);
project.move(description, IResource.REPLACE, progress);
} else {
// If a project with the same name exists, does it live in the same location? If not, we can't import the generated project.
File existingLocation = project.getLocation().toFile();
if (!existingLocation.equals(folder)) {
String message = String.format("Cannot import generated project from %s. A project named %s already exists in the workspace and is mapped to location %s", folder.getAbsolutePath(), projectName, existingLocation);
throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, message, null));
}
SubMonitor subProgress = progress.newChild(1);
// Open it if closed
project.open(subProgress.newChild(1));
// Refresh, as the template may have generated new content
project.refreshLocal(IResource.DEPTH_INFINITE, subProgress.newChild(1));
}
}
}
}
if (cleanBuild)
workspace.build(IncrementalProjectBuilder.CLEAN_BUILD, progress.newChild(2));
}
}
};
getContainer().run(true, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
workspace.run(importProjectsRunnable, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
new WorkspaceJob("Load Repositories") {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
try {
Central.refreshPlugins();
} catch (Exception e) {
// There may be no workspace yet
}
return Status.OK_STATUS;
}
}.schedule();
}
});
// Prompt to switch to the bndtools perspective
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
IPerspectiveDescriptor currentPerspective = window.getActivePage().getPerspective();
if (!"bndtools.perspective".equals(currentPerspective.getId())) {
if (MessageDialog.openQuestion(getShell(), "Bndtools Perspective", "Switch to the Bndtools perspective?")) {
workbench.showPerspective("bndtools.perspective", window);
}
}
return true;
} catch (InvocationTargetException e) {
ErrorDialog.openError(getShell(), "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generating template output", e.getTargetException()));
return false;
} catch (Exception e) {
ErrorDialog.openError(getShell(), "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generating template output", e));
return false;
}
}
use of org.eclipse.core.resources.WorkspaceJob in project bndtools by bndtools.
the class PluginsPart method doReload.
void doReload() {
IFormPage page = (IFormPage) getManagedForm().getContainer();
final IFile file = ResourceUtil.getFile(page.getEditorInput());
if (file != null && file.exists()) {
WorkspaceJob job = new WorkspaceJob("Reload Plugins") {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
file.touch(monitor);
return Status.OK_STATUS;
}
};
job.setUser(true);
job.schedule();
}
}
Aggregations