Package org.apache.click.eclipse.core.builder

Source Code of org.apache.click.eclipse.core.builder.ClickProjectNature

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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 org.apache.click.eclipse.core.builder;


import org.apache.click.eclipse.ClickPlugin;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;

/**
* The project nature for the Click project.
* <p>
* This nature enables <code>ClickProjectBuilder</code> for the project.
*
* @author Naoki Takezoe
* @see ClickProjectBuilder
*/
public class ClickProjectNature implements IProjectNature {
 
  public static final String NATURE_ID = "org.apache.click.eclipse.ClickProjectNature";
 
  private IProject project;
 
  /**
   * Adds the <code>ClickProjectBuilder</code> to the project.
   */
  public void configure() throws CoreException {
    IProjectDescription desc = project.getDescription();
    ICommand[] commands = desc.getBuildSpec();

    for (int i = 0; i < commands.length; ++i) {
      if (commands[i].getBuilderName().equals(ClickProjectBuilder.BUILDER_ID)) {
        return;
      }
    }

    ICommand[] newCommands = new ICommand[commands.length + 1];
    System.arraycopy(commands, 0, newCommands, 0, commands.length);
    ICommand command = desc.newCommand();
    command.setBuilderName(ClickProjectBuilder.BUILDER_ID);
    newCommands[newCommands.length - 1] = command;
    desc.setBuildSpec(newCommands);
    project.setDescription(desc, null);
  }

  /**
   * Removes the <code>ClickProjectBuilder</code> from the project.
   */
  public void deconfigure() throws CoreException {
    IProjectDescription description = getProject().getDescription();
    ICommand[] commands = description.getBuildSpec();
    for (int i = 0; i < commands.length; ++i) {
      if (commands[i].getBuilderName().equals(ClickProjectBuilder.BUILDER_ID)) {
        ICommand[] newCommands = new ICommand[commands.length - 1];
        System.arraycopy(commands, 0, newCommands, 0, i);
        System.arraycopy(commands, i + 1, newCommands, i,
            commands.length - i - 1);
        description.setBuildSpec(newCommands);
        return;
      }
    }
  }

  public IProject getProject() {
    return project;
  }

  public void setProject(IProject project) {
    this.project = project;
  }
 
  /**
   * Adds the <code>ClickProjectNature</code> to the given project.
   *
   * @param project the project
   */
  public static void addNatute(IProject project) {
    try {
      IProjectDescription description = project.getDescription();
      String[] natures = description.getNatureIds();
     
      // Add the nature
      String[] newNatures = new String[natures.length + 1];
      System.arraycopy(natures, 0, newNatures, 0, natures.length);
      newNatures[natures.length] = ClickProjectNature.NATURE_ID;
      description.setNatureIds(newNatures);
      project.setDescription(description, null);
     
    } catch(CoreException e){
      ClickPlugin.log(e);
    }
  }
 
  /**
   * Removes the <code>ClickProjectNature</code> from the given project.
   *
   * @param project the project
   */
  public static void removeNature(IProject project){
    try {
      IProjectDescription description = project.getDescription();
      String[] natures = description.getNatureIds();

      for (int i = 0; i < natures.length; ++i) {
        if (ClickProjectNature.NATURE_ID.equals(natures[i])) {
          // Remove the nature
          String[] newNatures = new String[natures.length - 1];
          System.arraycopy(natures, 0, newNatures, 0, i);
          System.arraycopy(natures, i + 1, newNatures, i,
              natures.length - i - 1);
          description.setNatureIds(newNatures);
          project.setDescription(description, null);
          return;
        }
      }
    } catch (CoreException e) {
      ClickPlugin.log(e);
    }
  }

}
TOP

Related Classes of org.apache.click.eclipse.core.builder.ClickProjectNature

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.