| /******************************************************************************* |
| * Copyright (c) 2010, 2013 xored software, Inc and others. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * xored software, Inc. - initial API and Implementation (Alex Panchenko) |
| * NumberFour AG - createFolder() added (Alex Panchenko) |
| *******************************************************************************/ |
| package org.eclipse.dltk.utils; |
| |
| import org.eclipse.core.resources.IContainer; |
| import org.eclipse.core.resources.IFolder; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.IProjectDescription; |
| import org.eclipse.core.resources.IResource; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.OperationCanceledException; |
| |
| public class ResourceUtil { |
| |
| public static void addNature(IProject project, IProgressMonitor monitor, |
| String nature) throws CoreException { |
| if (monitor != null && monitor.isCanceled()) { |
| throw new OperationCanceledException(); |
| } |
| if (!project.hasNature(nature)) { |
| IProjectDescription description = project.getDescription(); |
| String[] prevNatures = description.getNatureIds(); |
| String[] newNatures = new String[prevNatures.length + 1]; |
| System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length); |
| newNatures[prevNatures.length] = nature; |
| description.setNatureIds(newNatures); |
| project.setDescription(description, monitor); |
| } else { |
| if (monitor != null) { |
| monitor.worked(1); |
| } |
| } |
| } |
| |
| /** |
| * Creates a folder and all parent folders if not existing. |
| */ |
| public static void createFolder(IFolder folder, IProgressMonitor monitor) |
| throws CoreException { |
| if (!folder.exists()) { |
| final IContainer parent = folder.getParent(); |
| if (parent instanceof IFolder) { |
| createFolder((IFolder) parent, null); |
| } |
| folder.create(IResource.FORCE, true, monitor); |
| } |
| } |
| } |