Package tool.action

Source Code of tool.action.RefreshFromRepositoryAction

package tool.action;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;

import tool.ToolPlugin;
import tool.repository.Refresh;
import tool.repository.RepositoryExport;

public class RefreshFromRepositoryAction  extends Action implements IObjectActionDelegate{
  private IWorkbenchPage page;
  private ISelectionProvider provider;
  private IResource resource;
  private IAction action;
  private IWorkbenchPart workbenchPart;
 
  public RefreshFromRepositoryAction(){
    setText("Refresh from repository");
  }
  public RefreshFromRepositoryAction(IWorkbenchPage page, ISelectionProvider selectionProvider) {
    this();
    this.page = page;
    provider = selectionProvider;
   
  }
  public RefreshFromRepositoryAction(IResource resource){
    this.resource = resource;
  }
  @Override
  public boolean isEnabled() {
    return true;
  }
  @Override
  public void run() {
   
    /*
     * find the project
     */
    if (this.resource == null) {
     
      ISelection selection = provider.getSelection();
      if(selection.isEmpty())
        return;

      IStructuredSelection sSelection = (IStructuredSelection) selection;
      if(sSelection.size() == 1 &&
          sSelection.getFirstElement() instanceof IResource)
      {
        this.resource = (IResource)sSelection.getFirstElement();
       
      } else {
        return;
      }
    }
    try {
      Refresh runner = new Refresh(this.resource, this.resource.getProject());
     
      IWorkbench wb = PlatformUI.getWorkbench();
      IProgressService ps = wb.getProgressService();
      ps.run(false, true, runner);
     
      //IWorkspace workspace = ResourcesPlugin.getWorkspace();
      //workspace.run(runner, this.resource.getProject(), IWorkspace.AVOID_UPDATE, null);//TODO add a progress monitor
     
    //} catch (CoreException e) {
    //  ToolPlugin.showError("Error refreshing plan: " + this.resource.getName(), e);
    } catch (InvocationTargetException e) {
      ToolPlugin.showError("Error refreshing plan: " + this.resource.getName(), e);
    } catch (InterruptedException e) {
      ToolPlugin.showError("Error refreshing plan: " + this.resource.getName(), e);
    }
   
  }
  @Override
  public void run(IAction action) {
    this.action = action;
    if (this.resource == null) {
      return;
    }
    run();
  }
  @Override
  public void selectionChanged(IAction action, ISelection selection) {
    IStructuredSelection sSelection = (IStructuredSelection) selection;
    if(sSelection.size() == 1 &&
        sSelection.getFirstElement() instanceof IResource)
    {
      resource = (IResource)sSelection.getFirstElement();
    } else {
      return;
    }
   
  }
  @Override
  public void setActivePart(IAction action, IWorkbenchPart part) {
    this.action = action;
    this.workbenchPart = part;
  }
}
TOP

Related Classes of tool.action.RefreshFromRepositoryAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.