182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll/*
282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * Copyright (C) 2011 The Android Open Source Project
382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll *
482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * Licensed under the Eclipse Public License, Version 1.0 (the "License");
582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * you may not use this file except in compliance with the License.
682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * You may obtain a copy of the License at
782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll *
882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll *      http://www.eclipse.org/org/documents/epl-v10.php
982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll *
1082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * Unless required by applicable law or agreed to in writing, software
1182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * distributed under the License is distributed on an "AS IS" BASIS,
1282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * See the License for the specific language governing permissions and
1482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * limitations under the License.
1582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll */
1682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
1782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollpackage com.android.ide.eclipse.ndk.internal.templates;
1882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
1982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport com.android.ide.eclipse.ndk.internal.Messages;
2082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
2182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.CCorePlugin;
2282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.model.CoreModel;
2382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.model.ICProject;
2482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.model.IPathEntry;
2582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.templateengine.TemplateCore;
2682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.templateengine.process.ProcessArgument;
2782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.templateengine.process.ProcessFailureException;
2882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.templateengine.process.ProcessRunner;
2982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.resources.IFolder;
3082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.resources.IProject;
3182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.resources.ResourcesPlugin;
3282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.runtime.CoreException;
3382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.runtime.IProgressMonitor;
3482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.runtime.Path;
3582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
3682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport java.util.ArrayList;
3782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport java.util.List;
3882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
3982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollpublic class SetFolders extends ProcessRunner {
4082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
4182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll    @Override
4282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll    public void process(TemplateCore template, ProcessArgument[] args, String processId,
4382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            IProgressMonitor monitor)
4482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            throws ProcessFailureException {
4582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        String projectName = null;
4682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        String[] sourceFolders = null;
4782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        String[] outputFolders = null;
4882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
4982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        for (ProcessArgument arg : args) {
5082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            String argName = arg.getName();
5182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            if (argName.equals("projectName")) { //$NON-NLS-1$
5282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                projectName = arg.getSimpleValue();
5382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            } else if (argName.equals("sourceFolders")) { //$NON-NLS-1$
5482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                sourceFolders = arg.getSimpleArrayValue();
5582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            } else if (argName.equals("outputFolders")) { //$NON-NLS-1$
5682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                outputFolders = arg.getSimpleArrayValue();
5782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            }
5882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        }
5982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
6082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        // Get the project
6182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        if (projectName == null)
6282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            throw new ProcessFailureException(Messages.SetFolders_Missing_project_name);
6382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
6482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
6582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        if (!project.exists())
6682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            throw new ProcessFailureException(Messages.SetFolders_Project_does_not_exist);
6782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
6882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        // Create the folders
6982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        if (sourceFolders == null && outputFolders == null)
7082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            throw new ProcessFailureException(Messages.SetFolders_No_folders);
7182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
7282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        try {
7382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            // Add them in
7482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            ICProject cproject = CCorePlugin.getDefault().getCoreModel().create(project);
7582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            IPathEntry[] pathEntries = cproject.getRawPathEntries();
7682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            List<IPathEntry> newEntries = new ArrayList<IPathEntry>(pathEntries.length);
7782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            for (IPathEntry pathEntry : pathEntries) {
7882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                // remove the old source and output entries
7982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                if (pathEntry.getEntryKind() != IPathEntry.CDT_SOURCE
8082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                        && pathEntry.getEntryKind() != IPathEntry.CDT_OUTPUT) {
8182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                    newEntries.add(pathEntry);
8282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                }
8382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            }
8482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            if (sourceFolders != null)
8582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                for (String sourceFolder : sourceFolders) {
8682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                    IFolder folder = project.getFolder(new Path(sourceFolder));
8782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                    if (!folder.exists())
8882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                        folder.create(true, true, monitor);
8982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                    newEntries.add(CoreModel.newSourceEntry(folder.getFullPath()));
9082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                }
9182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            if (outputFolders != null)
9282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                for (String outputFolder : outputFolders) {
9382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                    IFolder folder = project.getFolder(new Path(outputFolder));
9482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                    if (!folder.exists())
9582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                        folder.create(true, true, monitor);
9682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                    newEntries.add(CoreModel.newOutputEntry(folder.getFullPath()));
9782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                }
9882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            cproject.setRawPathEntries(newEntries.toArray(new IPathEntry[newEntries.size()]),
9982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll                    monitor);
10082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        } catch (CoreException e) {
10182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll            throw new ProcessFailureException(e);
10282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll        }
10382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll    }
10482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll
10582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll}
106