/******************************************************************************* * Copyright (c) 2010, 2011 Obeo. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Obeo - initial API and implementation *******************************************************************************/ package org.eclipse.mylyn.docs.intent.client.ui.ide.navigator; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.emf.ecore.EObject; import org.eclipse.mylyn.docs.intent.client.ui.ide.Activator; import org.eclipse.mylyn.docs.intent.client.ui.ide.builder.IntentNature; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IViewReference; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.navigator.CommonNavigator; import org.eclipse.ui.navigator.CommonViewer; /** * A Job that refreshes Intent projects in the project explorer. * * @author <a href="mailto:alex.lagarde@obeo.fr">Alex Lagarde</a> */ public class ProjectExplorerRefreshJob extends Job { /** * Name to associate to this job. */ public static final String PROJECT_EXPLORER_REFRESH_JOB_NAME = "Refreshing Intent Index"; /** * The project to refresh. */ private IProject project; /** * Default constructor. * * @param project * the project to refresh * @param elementToRefresh * the element to refresh inside this project (can be null) */ public ProjectExplorerRefreshJob(IProject project, EObject elementToRefresh) { super(PROJECT_EXPLORER_REFRESH_JOB_NAME); this.setSystem(true); this.project = project; } /** * {@inheritDoc} * * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) */ @Override protected IStatus run(IProgressMonitor monitor) { try { // If project is accessible if (project.isAccessible() && project.getNature(IntentNature.NATURE_ID) != null) { // if the project explorer view is opened for (IWorkbenchWindow activeWorkbenchWindow : PlatformUI.getWorkbench().getWorkbenchWindows()) { IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage(); for (IViewReference viewRef : activePage.getViewReferences()) { if (viewRef.getView(false) instanceof CommonNavigator) { // We refresh the project explorer on this project final CommonNavigator projectExplorer = (CommonNavigator)viewRef.getView(false); Display.getDefault().asyncExec(new Runnable() { public void run() { refreshProjectExplorer(projectExplorer); } }); } } } } } catch (CoreException e) { return new Status(Status.ERROR, Activator.PLUGIN_ID, "Error while refreshing the project explorer view"); } return Status.OK_STATUS; } /** * Refreshes the project explorer. * * @param projectExplorer * the project explorer to refresh */ private void refreshProjectExplorer(final CommonNavigator projectExplorer) { CommonViewer commonViewer = projectExplorer.getCommonViewer(); if (!commonViewer.getControl().isDisposed()) { commonViewer.refresh(project, true); } } }