Search in sources :

Example 1 with WorkspaceJob

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);
}
Also used : IShellProvider(org.eclipse.jface.window.IShellProvider) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CloseResourceAction(org.eclipse.ui.actions.CloseResourceAction) IStatus(org.eclipse.core.runtime.IStatus) WorkspaceModifyOperation(org.eclipse.ui.actions.WorkspaceModifyOperation) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) OpenResourceAction(org.eclipse.ui.actions.OpenResourceAction) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) InvocationTargetException(java.lang.reflect.InvocationTargetException) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RefreshAction(org.eclipse.ui.actions.RefreshAction) CoreException(org.eclipse.core.runtime.CoreException) StructuredViewer(org.eclipse.jface.viewers.StructuredViewer)

Example 2 with WorkspaceJob

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);
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) HashSet(java.util.HashSet) Set(java.util.Set) TreeViewer(org.eclipse.jface.viewers.TreeViewer) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) TimeoutException(java.util.concurrent.TimeoutException) CoreException(org.eclipse.core.runtime.CoreException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Entry(java.util.Map.Entry) CoreException(org.eclipse.core.runtime.CoreException) TreePath(org.eclipse.jface.viewers.TreePath) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with WorkspaceJob

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();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RepositoryListenerPlugin(aQute.bnd.service.RepositoryListenerPlugin) CoreException(org.eclipse.core.runtime.CoreException) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException)

Example 4 with WorkspaceJob

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;
    }
}
Also used : IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) HashSet(java.util.HashSet) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IPath(org.eclipse.core.runtime.IPath) InputStream(java.io.InputStream) Resource(org.bndtools.templating.Resource) IResource(org.eclipse.core.resources.IResource) SubMonitor(org.eclipse.core.runtime.SubMonitor) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourceMap(org.bndtools.templating.ResourceMap) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IProjectDescription(org.eclipse.core.resources.IProjectDescription) IPerspectiveDescriptor(org.eclipse.ui.IPerspectiveDescriptor) File(java.io.File)

Example 5 with WorkspaceJob

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();
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IFormPage(org.eclipse.ui.forms.editor.IFormPage)

Aggregations

WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)13 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)13 CoreException (org.eclipse.core.runtime.CoreException)11 IStatus (org.eclipse.core.runtime.IStatus)7 Status (org.eclipse.core.runtime.Status)5 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)3 File (java.io.File)3 IOException (java.io.IOException)3 IFile (org.eclipse.core.resources.IFile)3 IProject (org.eclipse.core.resources.IProject)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 Workspace (aQute.bnd.build.Workspace)2 RemoteRepositoryPlugin (aQute.bnd.service.RemoteRepositoryPlugin)2 RepositoryBundle (bndtools.model.repo.RepositoryBundle)2 RepositoryBundleVersion (bndtools.model.repo.RepositoryBundleVersion)2 RepositoryEntry (bndtools.model.repo.RepositoryEntry)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2