Package tool.commands

Source Code of tool.commands.CommonHandler

package tool.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;

import tool.model.ToolComponent;

public abstract class CommonHandler  extends AbstractHandler{

  public IProject getProjectFromSelection(ISelection selection) {
    IProject project = null;
    if (selection instanceof TreeSelection){
      Object object = ((TreeSelection)selection).getFirstElement();
      if (object instanceof IResource){
        project = ((IResource)object).getProject();
      } else if (object instanceof ToolComponent){
        /*
         * we are going to find the file of the component or its parent, then locate the project
         */
        IFile file = null;
        ToolComponent subject = (ToolComponent)object;
        while (file == null){
          file = subject.getFile();
          if (file == null)
            subject = (ToolComponent) subject.getParent();
        }
        project = file.getProject();
      }
    }
    return project;
  }

  public IWorkspace getWorkspace(){
    return ResourcesPlugin.getWorkspace();
  }
}
TOP

Related Classes of tool.commands.CommonHandler

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.