Package de.innovationgate.eclipse.editors.actions

Source Code of de.innovationgate.eclipse.editors.actions.RunValidation

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.actions;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.all.WGADesignResourceValidator;

public class RunValidation implements IObjectActionDelegate {

  private IResource _resource;
  private IWorkbenchPart _part;

  public void setActivePart(IAction action, IWorkbenchPart targetPart) {
    _part = targetPart;
  }

  public void run(IAction action) {
    if (_resource != null) {
      call(_resource, _part.getSite().getShell());
    }
  }
 
  public static void call(final IResource fResource, Shell shell) {
   
   
    final IRunnableWithProgress runnable = new IRunnableWithProgress() {

      public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
        // count resources
        /*
        final Integer count = 0;
        try {
          fResource.accept(new IResourceVisitor() {
           
            public boolean visit(IResource resource) throws CoreException {
              if (resource instanceof IFile) {
                IFile file = (IFile) resource;
                if (file.getName().endsWith(".tml")) {
                  count++
                }
              }
              return true;
            }
          });
        } catch (CoreException e) {
        }*/
       
        try {
          final IProgressMonitor fMonitor = monitor;
          fMonitor.beginTask("Validating resources", IProgressMonitor.UNKNOWN);
          fResource.accept(new IResourceVisitor() {
           
            public boolean visit(IResource resource) throws CoreException {
              if (resource instanceof IFile) {
                IFile file = (IFile) resource;
                if (WGADesignResourceValidator.shouldBeValidated(file)) {
                  try {
                    fMonitor.setTaskName("Validating '" + file.getLocation().toString() + "'.");
                    WGADesignResourceValidator.validate(file);
                    fMonitor.worked(1);                     
                  } catch (Exception e) { 
                  }
                }
              }
              if (resource instanceof IContainer) {
                IContainer container = (IContainer) resource;
                if (WGADesignResourceValidator.shouldBeValidated(container)) {
                  try {
                    fMonitor.setTaskName("Validating '" + container.getLocation().toString() + "'.");
                    WGADesignResourceValidator.validate(container);
                    fMonitor.worked(1);                     
                  } catch (Exception e) { 
                  }
                }
              }
              if (!fMonitor.isCanceled()) {
                return true;
              } else {
                return false;
              }
            }
          });
        } catch (CoreException e) {           
        } finally {
          monitor.done();
        }
      }
    };
   
    try {
      if (shell != null) {
        ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
        dialog.run(true, true, runnable);
      } else {
        Job job = new Job("validation") {

          @Override
          protected IStatus run(IProgressMonitor monitor) {
            try {
              runnable.run(monitor);
              return Status.OK_STATUS;
            } catch (Exception e) {
              return new Status(Status.ERROR, Plugin.PLUGIN_ID, "Validation failed", e);
            }
          }
         
        };
        job.setPriority(Job.SHORT);
        job.schedule();
      }
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }     
  }


  public void selectionChanged(IAction action, ISelection selection) {
    if (selection instanceof IStructuredSelection)  {
      IStructuredSelection structSelection = (IStructuredSelection) selection;
      Object element = structSelection.getFirstElement();
      if (element instanceof IResource) {
        _resource = (IResource) element;
      } else {
        _resource = null;
      }
    } else {
      _resource = null;
    }
   
  }


}
TOP

Related Classes of de.innovationgate.eclipse.editors.actions.RunValidation

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.