jp.littleforest.pathtools.handlers.AbstractOpenHandler.java Source code

Java tutorial

Introduction

Here is the source code for jp.littleforest.pathtools.handlers.AbstractOpenHandler.java

Source

/******************************************************************************
 * Copyright (c) 2011-2012 Yusuke Komori. 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
 *****************************************************************************/
package jp.littleforest.pathtools.handlers;

import java.io.File;

import jp.littleforest.pathtools.Activator;
import jp.littleforest.pathtools.util.IResourceUtil;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;

/**
 * ???????????<br />
 * 
 * @author y-komori
 */
public abstract class AbstractOpenHandler extends SingleDynamicHandler {

    private static final String PATH_HOLDER = "{path}";

    /**
     * ?????<br />
     *
     * @param command
     *            
     * @param path
     *            {path} ???
     */
    protected void execRuntime(String command, String path) {
        try {
            if (path != null) {
                command = command.replace(PATH_HOLDER, path);
            }
            Runtime.getRuntime().exec(command);
            Activator.logInfo("Command executed. : " + command);
        } catch (Throwable ex) {
            Activator.logError("Unable to run command. : " + command, ex);
        }
    }

    /* (non-Javadoc)
     * @see jp.littleforest.pathtools.handlers.DynamicHandler#isEnabled(org.eclipse.core.runtime.IAdaptable)
     */
    @Override
    protected boolean isEnabled(IAdaptable adaptable) {
        boolean enabled = false;
        if (adaptable instanceof IResource) {
            this.selected = adaptable;
            enabled = true;
        } else if (adaptable instanceof IPackageFragment) {
            IPackageFragment ipf = (IPackageFragment) adaptable;
            if (ipf.getJavaProject() instanceof IJavaProject) {
                // ?Jar??
                this.selected = getJarFile(ipf);
                enabled = true;
            }
        } else if (adaptable instanceof IJavaElement) {
            this.selected = getJarFile(adaptable);
            enabled = true;
        } else {
            this.selected = adaptable.getAdapter(IResource.class);
            enabled = true;
        }
        return enabled;
    }

    /**
     * JarPackageFragmentRoot ? Jar ????<br />
     *
     * @param adaptable
     *            JarPackageFragmentRoot ? {@link IAdaptable}
     * @return Jar ?? {@link File}
     */
    protected File getJarFile(IAdaptable adaptable) {//JarPackageFragmentRoot
        IJavaElement jpfr = (IJavaElement) adaptable;
        File selected = jpfr.getPath().makeAbsolute().toFile();
        if (!(selected).exists()) {
            File projectFile = new File(jpfr.getJavaProject().getProject().getLocation().toOSString());
            selected = new File(projectFile.getParent() + selected.toString());
        }
        return selected;
    }

    /* (non-Javadoc)
     * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {
        if (selected == null || selectedClass == null) {
            return null;
        }

        File directory = null;

        if (selected instanceof IContainer) {
            directory = new File(IResourceUtil.toOSPath((IContainer) selected));
        } else if (selected instanceof IFile) {
            IContainer parent = ((IFile) selected).getParent();
            directory = new File(IResourceUtil.toOSPath(parent));
        } else if (selected instanceof File) {
            File file = (File) selected;
            if (!file.isDirectory()) {
                directory = file.getParentFile();
            } else {
                directory = file;
            }
        }

        if (directory != null) {
            String command = getCommandLine();
            execRuntime(command, directory.getAbsolutePath());
        } else {
            Activator.logError("Directory could'nt find. : " + selected.toString());
        }

        return null;
    }

    /**
     * ???<br />
     * ????????<br />
     * 
     * @return 
     */
    abstract protected String getCommandLine();

}