org.codehaus.groovy.eclipse.dsl.RefreshDSLDFilesActionDelegate.java Source code

Java tutorial

Introduction

Here is the source code for org.codehaus.groovy.eclipse.dsl.RefreshDSLDFilesActionDelegate.java

Source

/*******************************************************************************
 * Copyright (c) 2011 Codehaus.org, SpringSource, and others.
 * 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
 *
 * Contributors:
 *      Andrew Eisenberg - Initial implemenation
 *******************************************************************************/
package org.codehaus.groovy.eclipse.dsl;

import org.codehaus.jdt.groovy.model.GroovyNature;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class RefreshDSLDFilesActionDelegate implements IWorkbenchWindowActionDelegate {

    private IProject[] groovyProjects;

    public void run(IAction action) {
        for (IProject project : groovyProjects) {
            Job refreshJob = new RefreshDSLDJob(project);
            refreshJob.setPriority(Job.LONG);
            refreshJob.schedule();
        }
    }

    public void selectionChanged(IAction action, ISelection selection) {
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection ss = (IStructuredSelection) selection;
            Object[] elts = ss.toArray();
            groovyProjects = new IProject[elts.length];
            for (int i = 0; i < elts.length; i++) {
                if (elts[i] instanceof IProject && GroovyNature.hasGroovyNature((IProject) elts[i])) {
                    groovyProjects[i] = (IProject) elts[i];
                } else {
                    // invalid selection
                    groovyProjects = null;
                    return;
                }
            }
        } else {
            groovyProjects = null;
        }
    }

    public void dispose() {
        groovyProjects = null;
    }

    public void init(IWorkbenchWindow window) {
    }

}