Package sharpen.builder

Source Code of sharpen.builder.SharpenNature

/* Copyright (C) 2004 - 2008  Versant Inc.  http://www.db4o.com

This file is part of the sharpen open source java to c# translator.

sharpen is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation and as clarified by db4objects' GPL
interpretation policy, available at
http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
Suite 350, San Mateo, CA 94403, USA.

sharpen is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package sharpen.builder;

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;

public class SharpenNature implements IProjectNature {

  /**
   * ID of this project nature
   */
  public static final String NATURE_ID = Activator.PLUGIN_ID + ".sharpenNature";

  private IProject _project;

  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.core.resources.IProjectNature#configure()
   */
  public void configure() throws CoreException {
    IProjectDescription desc = _project.getDescription();
    ICommand[] commands = desc.getBuildSpec();

    if (containsBuilderCommand(commands)) return;

    desc.setBuildSpec(append(commands, newBuilderCommand(desc)));
    _project.setDescription(desc, null);
  }

  private boolean containsBuilderCommand(ICommand[] commands) {
    for (int i = 0; i < commands.length; ++i) {
      if (commands[i].getBuilderName().equals(SharpenBuilder.BUILDER_ID)) {
        return true;
      }
    }
    return false;
  }

  private ICommand newBuilderCommand(IProjectDescription desc) {
    ICommand command = desc.newCommand();
    command.setBuilderName(SharpenBuilder.BUILDER_ID);
    return command;
  }

  private ICommand[] append(ICommand[] commands, ICommand command) {
    ICommand[] newCommands = new ICommand[commands.length + 1];
    System.arraycopy(commands, 0, newCommands, 0, commands.length);   
    newCommands[newCommands.length - 1] = command;
    return newCommands;
  }

  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.core.resources.IProjectNature#deconfigure()
   */
  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(SharpenBuilder.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;
      }
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.core.resources.IProjectNature#getProject()
   */
  public IProject getProject() {
    return _project;
  }

  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.core.resources.IProjectNature#setProject(org.eclipse.core.resources.IProject)
   */
  public void setProject(IProject project) {
    this._project = project;
  }

}
TOP

Related Classes of sharpen.builder.SharpenNature

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.