Java tutorial
/* * TURNUS, the co-exploration framework * * Copyright (C) 2014 EPFL SCI STI MM * * This file is part of TURNUS. * * TURNUS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * TURNUS 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 TURNUS. If not, see <http://www.gnu.org/licenses/>. * * Additional permission under GNU GPL version 3 section 7 * * If you modify this Program, or any covered work, by linking or combining it * with Eclipse (or a modified version of Eclipse or an Eclipse plugin or * an Eclipse library), containing parts covered by the terms of the * Eclipse Public License (EPL), the licensors of this Program grant you * additional permission to convey the resulting work. Corresponding Source * for a non-source form of such a combination shall include the source code * for the parts of Eclipse libraries used as well as that of the covered work. * */ package co.turnus.widgets.util; import java.io.File; import java.util.ArrayList; import java.util.List; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; /** * * @author Simone Casale Brunet * */ public class WidgetsUtils { /** * Get the file extension (e.g. "java", "jpg") * * @param file * the input file * @return the file extension */ public static String getExtension(File file) { if (!file.getName().isEmpty()) { String fileName = file.getName(); int mid = fileName.lastIndexOf("."); return fileName.substring(mid + 1, fileName.length()); } return ""; } /** * Returns the qualified name of the given file, i.e. qualified.name.of.File * for <code>/project/sourceFolder/qualified/name/of/File.fileExt</code> or * <code>/project/outputFolder/qualified/name/of/File.fileExt</code>. * * @param file * a file * @return a qualified name, or <code>null</code> if the file is not in a * source folder */ public static String getQualifiedName(IFile file) { IProject project = file.getProject(); IJavaProject javaProject = JavaCore.create(project); if (!javaProject.exists()) { return null; } try { IPath path = file.getParent().getFullPath(); IPackageFragment fragment = null; if (javaProject.getOutputLocation().isPrefixOf(path)) { // create relative path int count = path.matchingFirstSegments(javaProject.getOutputLocation()); IPath relPath = path.removeFirstSegments(count); // creates full path to source for (IFolder folder : getSourceFolders(project)) { path = folder.getFullPath().append(relPath); fragment = javaProject.findPackageFragment(path); if (fragment != null) { break; } } } else { fragment = javaProject.findPackageFragment(path); } if (fragment == null) { return null; } String name = file.getFullPath().removeFileExtension().lastSegment(); if (fragment.isDefaultPackage()) { // handles the default package case return name; } return fragment.getElementName() + "." + name; } catch (JavaModelException e) { e.printStackTrace(); return null; } } /** * Return the path of the file without the project name<br/> * Example: <i>[projectName]/dir1/dir2/dir3</i>, [projectName] is removed * * @param file * @return */ public static String getRelativePath(IFile file) { String fullPath = file.getFullPath().toString(); return fullPath.replaceFirst("/".concat(file.getProject().getName()).concat("/"), ""); } /** * Returns the list of source folders of the given project as a list of * absolute workspace paths. * * @param project * a project * @return a list of absolute workspace paths */ public static List<IFolder> getSourceFolders(IProject project) { List<IFolder> srcFolders = new ArrayList<IFolder>(); IJavaProject javaProject = JavaCore.create(project); if (!javaProject.exists()) { return srcFolders; } // iterate over package roots try { for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) { IResource resource = root.getCorrespondingResource(); if (resource != null && resource.getType() == IResource.FOLDER) { srcFolders.add((IFolder) resource); } } } catch (CoreException e) { e.printStackTrace(); } return srcFolders; } }