Package de.innovationgate.eclipse.editors.all

Source Code of de.innovationgate.eclipse.editors.all.ProjectBuilderManager$ContainsWGADesignResourcesVisitor

/*******************************************************************************
* 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.all;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
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 de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.ResourceIDs;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;

/**
* this manager is responsible for updating project builders
* for e.g. ensure WGADesignResourceValidator is present on all projects
*
*
*/
public class ProjectBuilderManager implements IResourceChangeListener {
 
  private Set<IProject> _projectsAddedOrChanged = new HashSet<IProject>();
 
  private class ContainsWGADesignResourcesVisitor implements IResourceVisitor {
   
      private boolean _result = false;

      public boolean visit(IResource resource) throws CoreException {
        if (resource instanceof IContainer) {
          if (WGADesignStructureHelper.isDesignFolder((IContainer)resource)) {
            _result = true;
            return false;
          }
        }
        return true;
      }

      public boolean isTrue() {
        return _result;
      }
   
  }

  public void resourceChanged(IResourceChangeEvent event) {
    if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
      IResourceDelta delta = event.getDelta();
      if (delta != null) {
        try {
          delta.accept(new IResourceDeltaVisitor() {
            public boolean visit(IResourceDelta delta) throws CoreException {
              if (delta.getResource() instanceof IProject) {
                IProject project = (IProject) delta.getResource();
                if (delta.getKind() == IResourceDelta.ADDED || delta.getKind() == IResourceDelta.CHANGED) {
                  _projectsAddedOrChanged.add(project);
                }
              }
              return true;
            }           
          });
        } catch (CoreException e) {
          Plugin.getDefault().logError("Error performing project builder updates." , e);
        }
      }
    } else if (event.getType() == IResourceChangeEvent.PRE_BUILD) {
      for (IProject project : _projectsAddedOrChanged) {
        try {
          createOrUpdateBuilders(project);
        } catch (CoreException e) {
          Plugin.getDefault().logError("Error performing project builder updates." , e);
        }
      }
      _projectsAddedOrChanged.clear();
    }
  }
   

  private void createOrUpdateBuilders(IProject project) throws CoreException {
    if (project != null && project.isAccessible()) {       
      WorkbenchUtils.removeBuilder(project, ResourceIDs.BUILDER_TMLFILE_VALIDATOR);
     
      // check if the project contains wga design resources
      ContainsWGADesignResourcesVisitor visitor = new ContainsWGADesignResourcesVisitor();
      project.accept(visitor);
      if (visitor.isTrue()) {
        WorkbenchUtils.addBuilder(project, ResourceIDs.BUILDER_WGA_DESIGNRESOURCE_VALIDATOR)
      } else {
        // in older versions we added the validator to all projects - remove these none necessary builders now
        WorkbenchUtils.removeBuilder(project, ResourceIDs.BUILDER_WGA_DESIGNRESOURCE_VALIDATOR);
      }
     
     
    }
  }
 
  public void performPluginStartup() {
    Job builderUpdate = new Job("Updating project builders") {
      @Override
      protected IStatus run(IProgressMonitor monitor) {       
        IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
        try {
          monitor.beginTask("Updating project builders", projects.length);       
          for (IProject project : projects) {
            monitor.worked(1);
            try {   
              createOrUpdateBuilders(project);
            } catch (Exception e) {
              Plugin.getDefault().logError("Unable to update builders on project '" + project.getName(), e);             
            }
          }
        } finally {
          monitor.done();
        }
        return Status.OK_STATUS;
      }
    };
    builderUpdate.setPriority(Job.SHORT);
    builderUpdate.schedule();
  }
}
TOP

Related Classes of de.innovationgate.eclipse.editors.all.ProjectBuilderManager$ContainsWGADesignResourcesVisitor

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.