Package org.eclipse.maven.handler

Source Code of org.eclipse.maven.handler.MavenCommandHandler

package org.eclipse.maven.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.maven.Activator;
import org.eclipse.maven.core.MavenCommand;
import org.eclipse.maven.core.MavenExecutor;
import org.eclipse.ui.handlers.HandlerUtil;

public class MavenCommandHandler extends AbstractHandler {
 
  @Override
  public Object execute(final ExecutionEvent event) throws ExecutionException {
   
    final IStructuredSelection selection = (IStructuredSelection) HandlerUtil
    .getActiveMenuSelection(event);

    if ( selection.size() > ) {
      Activator.outPut.write("Please select a single project.");
    } else {
      final String workspace_location = ResourcesPlugin.getWorkspace().getRoot()
          .getRawLocation().toString();
     
      final String pomLocation = findPomLocation(selection, workspace_location);
      MavenCommand command = new MavenCommand();
     
      if ( pomLocation != null ) {
        String cmd = event.getParameter("commandLine");
        command.setCommandLine(cmd);
       
       
        Job job = new MavenExecutor(command, pomLocation);
       
        job.schedule();
       
        return job;
      }
    }
    return null;
  }
 
  protected String findPomLocation(IStructuredSelection selection, String workspace_location) {
    String pomLocation = null;
    IProject project = getProject(selection);
    IResource pom = project.findMember("pom.xml");
   
    if ( pom != null )
      pomLocation = pom.getRawLocation().toPortableString();
       
    return pomLocation;
  }
 
  public IProject getProject(IStructuredSelection selection) {
    IProject project = null;

    if (selection.getFirstElement() instanceof IResource) {
      project = ((IResource) selection.getFirstElement()).getProject();
    } else if ( selection.getFirstElement() instanceof IJavaProject ) {
      project = ((IJavaProject)selection.getFirstElement()).getProject();
    } else if ( selection.getFirstElement() instanceof IJavaElement ) {
      project = (IProject) ((IJavaElement)selection.getFirstElement()).getResource().getProject();
    }
   
    return project;
  }

}
TOP

Related Classes of org.eclipse.maven.handler.MavenCommandHandler

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.