com.htmlhifive.tools.jslint.actions.AbstractJavaScriptAction.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.jslint.actions.AbstractJavaScriptAction.java

Source

/*
 * Copyright (C) 2012 NS Solutions Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.htmlhifive.tools.jslint.actions;

import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.FileEditorInput;

import com.htmlhifive.tools.jslint.dialog.JSLintStatusDialog;
import com.htmlhifive.tools.jslint.dialog.StatusList;

/**
 * javascript???????.<br>
 * ??????.
 * 
 * @author NS Solutions Corporation
 */
public abstract class AbstractJavaScriptAction implements IObjectActionDelegate, IEditorActionDelegate {

    /**
     * .
     */
    private IResource resource;
    /**
     * .
     */
    private Shell shell;

    @Override
    public void run(IAction action) {

        StatusList statusList = new StatusList();
        doRun(action, statusList);
        JSLintStatusDialog dialog = new JSLintStatusDialog(getShell(), statusList);
        dialog.open();
    }

    /**
     * .???.<br>
     * statusList?IStatus???????????????.
     * 
     * @param action .
     * @param statusList .
     */
    protected abstract void doRun(IAction action, StatusList statusList);

    /*
     * (? Javadoc)
     * 
     * @see
     * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
     * .IAction, org.eclipse.jface.viewers.ISelection)
     */
    @Override
    public void selectionChanged(IAction action, ISelection selection) {

        if (selection instanceof IStructuredSelection) {
            IStructuredSelection select = (IStructuredSelection) selection;
            if (select.getFirstElement() instanceof IResource) {
                resource = (IResource) select.getFirstElement();
            } else if (select.getFirstElement() instanceof IJavaProject) {
                IJavaProject project = (IJavaProject) select.getFirstElement();
                resource = project.getProject();
            }
        }

    }

    /*
     * (? Javadoc)
     * 
     * @see
     * org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface
     * .action.IAction, org.eclipse.ui.IEditorPart)
     */
    @Override
    public void setActiveEditor(IAction action, IEditorPart targetEditor) {

        if (targetEditor != null && targetEditor.getEditorInput() instanceof FileEditorInput) {
            FileEditorInput editor = (FileEditorInput) targetEditor.getEditorInput();
            resource = editor.getFile();
        }
    }

    /*
     * (? Javadoc)
     * 
     * @see
     * org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.
     * action.IAction, org.eclipse.ui.IWorkbenchPart)
     */
    @Override
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {

        shell = targetPart.getSite().getShell();
    }

    /**
     * ????.
     * 
     * @return .
     */
    IResource getResource() {

        return resource;
    }

    /**
     * ??.
     * 
     * @return .
     */
    Shell getShell() {

        return shell;
    }

}