Home | History | Annotate | Download | only in templates
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.ide.eclipse.adt.ndk.internal.templates;
     18 
     19 import com.android.ide.eclipse.adt.ndk.internal.Messages;
     20 
     21 import org.eclipse.cdt.core.CCorePlugin;
     22 import org.eclipse.cdt.core.model.CoreModel;
     23 import org.eclipse.cdt.core.model.ICProject;
     24 import org.eclipse.cdt.core.model.IPathEntry;
     25 import org.eclipse.cdt.core.templateengine.TemplateCore;
     26 import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
     27 import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;
     28 import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
     29 import org.eclipse.core.resources.IFolder;
     30 import org.eclipse.core.resources.IProject;
     31 import org.eclipse.core.resources.ResourcesPlugin;
     32 import org.eclipse.core.runtime.CoreException;
     33 import org.eclipse.core.runtime.IProgressMonitor;
     34 import org.eclipse.core.runtime.Path;
     35 
     36 import java.util.ArrayList;
     37 import java.util.List;
     38 
     39 public class SetFolders extends ProcessRunner {
     40 
     41     @Override
     42     public void process(TemplateCore template, ProcessArgument[] args, String processId,
     43             IProgressMonitor monitor)
     44             throws ProcessFailureException {
     45         String projectName = null;
     46         String[] sourceFolders = null;
     47         String[] outputFolders = null;
     48 
     49         for (ProcessArgument arg : args) {
     50             String argName = arg.getName();
     51             if (argName.equals("projectName")) { //$NON-NLS-1$
     52                 projectName = arg.getSimpleValue();
     53             } else if (argName.equals("sourceFolders")) { //$NON-NLS-1$
     54                 sourceFolders = arg.getSimpleArrayValue();
     55             } else if (argName.equals("outputFolders")) { //$NON-NLS-1$
     56                 outputFolders = arg.getSimpleArrayValue();
     57             }
     58         }
     59 
     60         // Get the project
     61         if (projectName == null)
     62             throw new ProcessFailureException(Messages.SetFolders_Missing_project_name);
     63 
     64         IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
     65         if (!project.exists())
     66             throw new ProcessFailureException(Messages.SetFolders_Project_does_not_exist);
     67 
     68         // Create the folders
     69         if (sourceFolders == null && outputFolders == null)
     70             throw new ProcessFailureException(Messages.SetFolders_No_folders);
     71 
     72         try {
     73             // Add them in
     74             ICProject cproject = CCorePlugin.getDefault().getCoreModel().create(project);
     75             IPathEntry[] pathEntries = cproject.getRawPathEntries();
     76             List<IPathEntry> newEntries = new ArrayList<IPathEntry>(pathEntries.length);
     77             for (IPathEntry pathEntry : pathEntries) {
     78                 // remove the old source and output entries
     79                 if (pathEntry.getEntryKind() != IPathEntry.CDT_SOURCE
     80                         && pathEntry.getEntryKind() != IPathEntry.CDT_OUTPUT) {
     81                     newEntries.add(pathEntry);
     82                 }
     83             }
     84             if (sourceFolders != null)
     85                 for (String sourceFolder : sourceFolders) {
     86                     IFolder folder = project.getFolder(new Path(sourceFolder));
     87                     if (!folder.exists())
     88                         folder.create(true, true, monitor);
     89                     newEntries.add(CoreModel.newSourceEntry(folder.getFullPath()));
     90                 }
     91             if (outputFolders != null)
     92                 for (String outputFolder : outputFolders) {
     93                     IFolder folder = project.getFolder(new Path(outputFolder));
     94                     if (!folder.exists())
     95                         folder.create(true, true, monitor);
     96                     newEntries.add(CoreModel.newOutputEntry(folder.getFullPath()));
     97                 }
     98             cproject.setRawPathEntries(newEntries.toArray(new IPathEntry[newEntries.size()]),
     99                     monitor);
    100         } catch (CoreException e) {
    101             throw new ProcessFailureException(e);
    102         }
    103     }
    104 
    105 }
    106