Example usage for org.eclipse.jdt.core IPackageFragmentRoot K_SOURCE

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot K_SOURCE

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot K_SOURCE.

Prototype

int K_SOURCE

To view the source code for org.eclipse.jdt.core IPackageFragmentRoot K_SOURCE.

Click Source Link

Document

Kind constant for a source path root.

Usage

From source file:org.wso2.developerstudio.eclipse.artifact.mediator.ui.wizard.CustomMediatorCreationWizard.java

License:Open Source License

public boolean performFinish() {
    try {/*w w w .j  a va2  s  .  co m*/

        if (customMediatorModel.getSelectedOption().equals("new.mediator")) {
            project = createNewProject();
            IFolder srcFolder = ProjectUtils.getWorkspaceFolder(project, "src", "main", "java");
            JavaUtils.addJavaSupportAndSourceFolder(project, srcFolder);

            /*create the new Java project*/
            String className = customMediatorModel.getMediatorClassName();
            String packageName = customMediatorModel.getMediatorClassPackageName();
            IJavaProject iJavaProject = JavaCore.create(project);
            IPackageFragmentRoot root = iJavaProject.getPackageFragmentRoot(srcFolder);
            IPackageFragment sourcePackage = root.createPackageFragment(packageName, false, null);

            /*get the Mediator class template*/
            String template = CustomMediatorClassTemplate.getClassTemplete(packageName, className);
            ICompilationUnit cu = sourcePackage.createCompilationUnit(className + ".java", template, false,
                    null);

            project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
            try {
                IEditorPart javaEditor = JavaUI.openInEditor(cu);
                JavaUI.revealInEditor(javaEditor, (IJavaElement) cu);
            } catch (Exception e) {
                log.error(e);
            }
        } else {
            project = customMediatorModel.getMediatorProject();
            project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
        }
        File pomfile = project.getFile("pom.xml").getLocation().toFile();
        getModel().getMavenInfo().setPackageName("bundle");
        if (!pomfile.exists()) {
            createPOM(pomfile);
            addDependancies(project);
        }
        MavenProject mavenProject = MavenUtils.getMavenProject(pomfile);
        boolean pluginExists = MavenUtils.checkOldPluginEntry(mavenProject, "org.apache.felix",
                "maven-bundle-plugin", "2.3.4");
        if (!pluginExists) {
            Plugin plugin = MavenUtils.createPluginEntry(mavenProject, "org.apache.felix",
                    "maven-bundle-plugin", "2.3.4", true);

            Xpp3Dom configurationNode = MavenUtils.createMainConfigurationNode(plugin);
            Xpp3Dom instructionNode = MavenUtils.createXpp3Node("instructions");
            Xpp3Dom bundleSymbolicNameNode = MavenUtils.createXpp3Node(instructionNode, "Bundle-SymbolicName");
            Xpp3Dom bundleNameNode = MavenUtils.createXpp3Node(instructionNode, "Bundle-Name");
            ;
            Xpp3Dom exportPackageNode = MavenUtils.createXpp3Node(instructionNode, "Export-Package");
            Xpp3Dom dynamicImportNode = MavenUtils.createXpp3Node(instructionNode, "DynamicImport-Package");
            bundleSymbolicNameNode.setValue(project.getName());
            bundleNameNode.setValue(project.getName());
            if (customMediatorModel.getMediatorClassPackageName() != null
                    && !customMediatorModel.getMediatorClassPackageName().trim().isEmpty()) {
                exportPackageNode.setValue(customMediatorModel.getMediatorClassPackageName());
            } else {
                IJavaProject javaProject = JavaCore.create(project);
                if (null != javaProject) {
                    StringBuffer sb = new StringBuffer();
                    for (IPackageFragment pkg : javaProject.getPackageFragments()) {
                        if (pkg.getKind() == IPackageFragmentRoot.K_SOURCE) {
                            if (pkg.hasChildren()) {
                                sb.append(pkg.getElementName()).append(",");
                            }
                        }
                    }
                    exportPackageNode.setValue(sb.toString().replaceAll(",$", ""));
                }
            }
            dynamicImportNode.setValue("*");
            configurationNode.addChild(instructionNode);
            MavenUtils.saveMavenProject(mavenProject, pomfile);
        }

        project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
        ProjectUtils.addNatureToProject(project, false, MEDIATOR_PROJECT_NATURE);
        MavenUtils.updateWithMavenEclipsePlugin(pomfile, new String[] { JDT_BUILD_COMMAND },
                new String[] { MEDIATOR_PROJECT_NATURE, JDT_PROJECT_NATURE });
        customMediatorModel.addToWorkingSet(project);
        project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
        refreshDistProjects();
    } catch (CoreException e) {
        log.error(e);
    } catch (Exception e) {
        log.error(e);
    }
    return true;
}

From source file:org.wso2.developerstudio.eclipse.artifact.registry.filter.project.export.RegistryFilterArtifactHandler.java

License:Open Source License

public List<IResource> exportArtifact(IProject project) {
    List<IResource> exportResources = new ArrayList<IResource>();
    List<String> exportedPackageList = new ArrayList<String>();
    if (!project.isOpen()) {
        return exportResources;
    }//from  www .j a  va2s .  c  om

    try {
        ArchiveManipulator archiveManipulator = new ArchiveManipulator();
        NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
        //cleaning target directory 
        clearTarget(project);
        //getting maven details
        MavenProject mavenProject = MavenUtils
                .getMavenProject(project.getFile("pom.xml").getLocation().toFile());

        // First compile the code
        project.build(IncrementalProjectBuilder.FULL_BUILD, nullProgressMonitor);

        // Get the output location
        IJavaProject javaProject = JavaCore.create(project);
        IPath outPutPath = ResourcesPlugin.getWorkspace().getRoot().getFolder(javaProject.getOutputLocation())
                .getLocation();

        // get resource location
        IPath resources = project.getFolder("src" + File.separator + "main" + File.separator + "resources")
                .getLocation();

        // getting export packages
        for (IPackageFragment pkg : javaProject.getPackageFragments()) {
            if (pkg.getKind() == IPackageFragmentRoot.K_SOURCE) {
                if (pkg.hasChildren()) {
                    exportedPackageList.add(pkg.getElementName());
                }
            }
        }

        IProject tempProject = ResourcesPlugin.getWorkspace().getRoot()
                .getProject(".temp" + System.currentTimeMillis());
        tempProject.create(nullProgressMonitor);
        tempProject.open(nullProgressMonitor);
        tempProject.setHidden(true);

        org.eclipse.osgi.storagemanager.StorageManager manager = new StorageManager(
                tempProject.getLocation().toFile(), "false");

        File filterResources = manager.createTempFile("filter_resources");
        filterResources.delete();
        filterResources.mkdir();

        FileUtils.copyDirectoryContents(outPutPath.toFile(), filterResources); // copy binaries
        if (resources.toFile().exists()) {
            FileUtils.copyDirectoryContents(resources.toFile(), filterResources); // copy resources
        }

        /* writing manifest */
        BundleManifest manifest = new BundleManifest();
        manifest.setBundleName(project.getName());
        manifest.setBundleSymbolicName(project.getName());
        if (null != mavenProject.getModel().getDescription()
                && !"".equals(mavenProject.getModel().getDescription())) {
            manifest.setBundleDescription(mavenProject.getModel().getDescription());
        } else {
            manifest.setBundleDescription(project.getName());
        }
        if (null != mavenProject.getModel().getVersion() && !"".equals(mavenProject.getDescription())) {
            manifest.setBundleVersion(mavenProject.getModel().getVersion());
        } else {
            manifest.setBundleVersion("1.0.0");
        }
        manifest.setExportPackagesList(exportedPackageList);
        File metaInfDir = new File(filterResources, "META-INF");
        if (!metaInfDir.exists())
            metaInfDir.mkdir();
        File manifestFile = new File(metaInfDir, "MANIFEST.MF");
        FileUtils.createFile(manifestFile, manifest.toString());

        File tmpArchive = new File(tempProject.getLocation().toFile(), project.getName().concat(".jar"));
        archiveManipulator.archiveDir(tmpArchive.toString(), filterResources.toString());
        IFolder binaries = project.getFolder("target");
        if (!binaries.exists()) {
            binaries.create(true, false, nullProgressMonitor);
            binaries.setHidden(true);
        }
        IFile bundleArchive = project.getFile("target/" + project.getName().concat(".jar"));
        FileUtils.copy(tmpArchive, bundleArchive.getLocation().toFile());
        exportResources.add((IResource) bundleArchive);

        // cleaning temp project
        tempProject.delete(true, nullProgressMonitor);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return exportResources;

}

From source file:org.wso2.developerstudio.eclipse.artifact.registry.handler.project.export.RegistryHandlerArtifactHandler.java

License:Open Source License

public List<IResource> exportArtifact(IProject project) throws Exception {
    List<IResource> exportResources = new ArrayList<IResource>();
    List<String> exportedPackageList = new ArrayList<String>();
    String activatorClass = new String();
    String existingActivatorClass = new String();

    ArchiveManipulator archiveManipulator = new ArchiveManipulator();

    // getting maven details
    MavenProject mavenProject = MavenUtils.getMavenProject(project.getFile("pom.xml").getLocation().toFile());

    // Get the output location
    IJavaProject javaProject = JavaCore.create(project);

    clearTarget(project);//from   ww w . j  a v  a 2s  .c  o  m
    IPath outPutPath = buildJavaProject(project);

    // get resource location
    IPath resources = getResourcePath(project);

    // getting export packages
    for (IPackageFragment pkg : javaProject.getPackageFragments()) {
        if (pkg.getKind() == IPackageFragmentRoot.K_SOURCE) {
            if (pkg.hasChildren()) {
                exportedPackageList.add(pkg.getElementName());
                for (ICompilationUnit compilationUnit : pkg.getCompilationUnits()) {
                    IType[] types = compilationUnit.getTypes();
                    for (IType type : types) {
                        if (type.getSuperInterfaceNames().length > 0
                                && Arrays.asList(type.getSuperInterfaceNames()).contains("BundleActivator")) {
                            activatorClass = type.getFullyQualifiedName();
                        }
                    }
                }
            }
        }
    }

    List<Plugin> plugins = mavenProject.getBuild().getPlugins();
    for (Plugin plugin : plugins) {
        if ("maven-bundle-plugin".equalsIgnoreCase(plugin.getArtifactId())) {
            Xpp3Dom configurationNode = (Xpp3Dom) plugin.getConfiguration();
            Xpp3Dom[] instructions = configurationNode.getChildren("instructions");
            if (instructions.length == 1) {
                Xpp3Dom[] exportPackage = instructions[0].getChildren("Export-Package");
                if (exportPackage.length == 1) {
                    exportedPackageList.clear(); //clear default configuration (All packages by default)
                    String packages = exportPackage[0].getValue();
                    if (packages != null) {
                        exportedPackageList.addAll(Arrays.asList(packages.split(",")));
                    }
                } else {
                    log.warn("Invalid configuration for <Export-Package> entry"
                            + " using default configuration for <Export-Package>");
                }
            } else {
                log.warn("Invalid instructions configuration for plugin : maven-bundle-plugin"
                        + " using default configuration for <Export-Package>");
            }
            break; //not considering multiple versions of the maven-bundle-plugin
        }
    }

    File tempProject = createTempProject();

    File bundleResources = createTempDir(tempProject, "bundle_resources");

    if (exportedPackageList.size() > 0) {
        FileUtils.copyDirectoryContents(outPutPath.toFile(), bundleResources); // copy
                                                                               // binaries
    }

    if (resources.toFile().exists()) {
        FileUtils.copyDirectoryContents(resources.toFile(), bundleResources); // copy
        // resources
    }

    @SuppressWarnings("unchecked")
    List<Dependency> dependencies = mavenProject.getDependencies();
    Iterator<Dependency> iterator = dependencies.iterator();
    while (iterator.hasNext()) {
        Dependency dependency = iterator.next();
        if ("system".equals(dependency.getScope())) {
            String systemPath = dependency.getSystemPath();
            if (systemPath != null && !systemPath.trim().isEmpty()) {
                systemPath = systemPath.replaceAll("^".concat(Pattern.quote("${basedir}/")), "");
                IFile jarFile = project.getFile(systemPath);
                if (jarFile.exists()) {
                    archiveManipulator.extract(jarFile.getLocation().toFile(), bundleResources);
                    // getting export packages
                    IPackageFragmentRoot rootPkg = JavaCore.createJarPackageFragmentRootFrom(jarFile);

                    for (IJavaElement item : rootPkg.getChildren()) {
                        if (item instanceof IPackageFragment) {
                            IPackageFragment pkg = (IPackageFragment) item;
                            if (pkg.hasChildren()) {
                                exportedPackageList.add(pkg.getElementName());
                                for (IClassFile clazz : pkg.getClassFiles()) {
                                    IType type = clazz.getType();
                                    if (type.getSuperInterfaceNames().length > 0 && Arrays
                                            .asList(type.getSuperInterfaceNames()).contains(ACTIVATOR_FQN)) {
                                        existingActivatorClass = type.getFullyQualifiedName();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }

    if (activatorClass.trim().isEmpty()) {
        activatorClass = existingActivatorClass;
    }

    /* writing manifest */
    BundleManifest manifest = new BundleManifest();
    manifest.setBundleName(project.getName());
    manifest.setBundleSymbolicName(project.getName());
    if (null != mavenProject.getModel().getDescription()
            && !"".equals(mavenProject.getModel().getDescription())) {
        manifest.setBundleDescription(mavenProject.getModel().getDescription());
    } else {
        manifest.setBundleDescription(project.getName());
    }
    if (null != mavenProject.getModel().getVersion() && !"".equals(mavenProject.getDescription())) {
        manifest.setBundleVersion(mavenProject.getModel().getVersion());
    } else {
        manifest.setBundleVersion("1.0.0");
    }
    manifest.setBundleActivatorName(activatorClass);
    manifest.setExportPackagesList(exportedPackageList);
    File metaInfDir = new File(bundleResources, "META-INF");
    if (!metaInfDir.exists())
        metaInfDir.mkdir();
    File manifestFile = new File(metaInfDir, "MANIFEST.MF");
    FileUtils.createFile(manifestFile, manifest.toString());

    File tmpArchive = new File(tempProject, project.getName().concat(".jar"));
    archiveManipulator.archiveDir(tmpArchive.toString(), bundleResources.toString());

    IFile bundleArchive = getTargetArchive(project, "jar");
    FileUtils.copy(tmpArchive, bundleArchive.getLocation().toFile());
    exportResources.add((IResource) bundleArchive);

    TempFileUtils.cleanUp();

    return exportResources;

}

From source file:org.wso2.developerstudio.eclipse.artifact.registry.handler.ui.dialog.ClassNameBrowseDlg.java

License:Open Source License

protected void fillContentProvider(AbstractContentProvider contentProvider, ItemsFilter itemsFilter,
        IProgressMonitor progressMonitor) throws CoreException {
    progressMonitor.beginTask("Searching", 1000);
    classes.clear();//ww w . ja  v a  2s . co m
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IProject[] projects = root.getProjects();

    for (IProject project : projects) {
        if (project.isOpen()) {
            if (project.hasNature("org.eclipse.jdt.core.javanature")) {
                IJavaProject javaProject = JavaCore.create(project);
                IPackageFragment[] packages = javaProject.getPackageFragments();
                for (IPackageFragment jPackage : packages) {
                    if (jPackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
                        for (final ICompilationUnit compilationUnit : jPackage.getCompilationUnits()) {
                            IType[] types = compilationUnit.getTypes();
                            for (IType type : types) {
                                switch (classFilter) {
                                case FILTER_ANNOTATION:
                                    IAnnotation annotation = type.getAnnotation(filter);
                                    if (annotation.exists()) {
                                        contentProvider.add(type, itemsFilter);
                                    }
                                    break;
                                case FILTER_SUPERINTERFACE:
                                    if (type.getSuperInterfaceNames().length > 0
                                            && Arrays.asList(type.getSuperInterfaceNames()).contains(filter)) {
                                        contentProvider.add(type, itemsFilter);
                                    }
                                    break;
                                case FILTER_SUPERCLASS:
                                default:
                                    if (filter.equals(type.getSuperclassName())) {
                                        contentProvider.add(type, itemsFilter);
                                    }
                                    break;
                                }
                            }
                        }
                        progressMonitor.worked(1);
                    }

                }
            }
        }
        progressMonitor.done();
    }
}

From source file:org.wso2.developerstudio.eclipse.utils.jdt.JavaUtils.java

License:Open Source License

public static IPath[] getJavaSourceDirectories(IProject project) throws CoreException {
    List<IPath> paths = new ArrayList<IPath>();
    try {//from  ww  w  . j av  a 2 s . co m
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject != null) {
            IPackageFragmentRoot[] packageFragmentRoots = javaProject.getPackageFragmentRoots();
            for (IPackageFragmentRoot fragmentRoot : packageFragmentRoots) {
                if (fragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    paths.add(fragmentRoot.getResource().getFullPath());
                }
            }
        } else {
            IFolder folder = project.getFolder("src").getFolder("main").getFolder("java");
            addJavaSourceFolder(folder, javaProject);
            paths.add(folder.getLocation());
        }
    } catch (JavaModelException e) {
    }
    return paths.toArray(new IPath[] {});
}

From source file:pl.wroc.pwr.jbehaveplugin.editor.JavaAnalyzer.java

License:Open Source License

public static boolean isPackageFragmentRootOfSource(IJavaElement elem) throws JavaModelException {
    if (elem instanceof IPackageFragmentRoot) {
        return ((IPackageFragmentRoot) elem).getKind() == IPackageFragmentRoot.K_SOURCE;
    }/*from  w  w w  .  ja v a  2s. c  o  m*/
    return false;
}

From source file:projectdependencies.views.ViewContentProviderJdt.java

License:Open Source License

private void buildPackageProjectMap(List<IJavaProject> referencedparam) throws JavaModelException {
    for (IJavaProject proj : referencedparam) {
        if (pkgProjects.containsValue(proj)) // already indexed
            continue;

        for (IPackageFragment pkg : proj.getPackageFragments()) {
            if (pkg.getKind() != IPackageFragmentRoot.K_SOURCE)
                continue;

            if (pkg.isDefaultPackage())
                continue;

            pkgProjects.put(pkg.getElementName(), proj);
        }//from ww  w.ja v a  2 s  . c o  m
    }
}

From source file:projectdependencies.views.ViewContentProviderJdt.java

License:Open Source License

private List<IJavaProject> getRequired(IJavaProject proj) throws JavaModelException {
    Collection<IJavaProject> required = new HashSet<IJavaProject>();

    for (IPackageFragment pkg : proj.getPackageFragments()) {
        if (pkg.getKind() != IPackageFragmentRoot.K_SOURCE)
            continue;

        for (ICompilationUnit unit : pkg.getCompilationUnits()) {
            for (IImportDeclaration importDec : unit.getImports()) {
                Collection<IJavaProject> otherProj = findProjectsForImport(importDec);

                if (otherProj != null) {
                    required.addAll(otherProj);
                }//from  w  ww . j  a  v a2s  .c o m
            }
        }
    }

    return new ArrayList<IJavaProject>(required);
}

From source file:qwickie.hyperlink.WicketHyperlink.java

License:Apache License

private void createJavaFile(final IResource resource) {
    Assert.isNotNull(resource);//from  w  w  w. j  av a 2 s.  c  o  m

    final OpenNewClassWizardAction action = new OpenNewClassWizardAction();
    final NewClassWizardPage ncwp = new NewClassWizardPage();

    ncwp.setTypeName(resource.getName().replaceAll("\\.html", ""), true);

    final IJavaProject javaProject = JavaCore.create(resource.getProject());
    IPackageFragmentRoot root = null;
    try {
        final IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                root = roots[i];
                break;
            }
        }

        ncwp.setPackageFragmentRoot(root, true);

    } catch (final JavaModelException e) {
    }
    final String os = root.getParent().getPath().toPortableString();
    final String fp = root.getResource().getFullPath().toPortableString().replaceFirst(os, "").substring(1);
    final String ps = resource.getProjectRelativePath().toPortableString().replaceFirst(fp, "");
    String pn = ps.replaceFirst(resource.getName(), "").substring(1).replaceAll("/", ".");
    pn = pn.substring(0, pn.length() - 1);

    ncwp.setPackageFragment(root.getPackageFragment(pn), true);

    ncwp.setSuperClass("org.apache.wicket.markup.html.WebPage", openJavaFile());

    action.setConfiguredWizardPage(ncwp);
    action.setOpenEditorOnFinish(true);
    action.run();
}

From source file:qwickie.util.FileSearcher.java

License:Apache License

/** return the list of IPath configured as source folders in the project */
public static List<IPath> getSourceFolders(final IProject project) {
    Assert.isNotNull(project);//from w  w  w .  ja va 2 s  .c  o  m
    final List<IPath> srcFolders = new ArrayList<IPath>();
    IJavaProject javaProject;
    try {
        javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
        final IPackageFragmentRoot[] packageFragmentRoots = javaProject.getPackageFragmentRoots();
        for (final IPackageFragmentRoot pfr : packageFragmentRoots) {
            if (pfr.getKind() == IPackageFragmentRoot.K_SOURCE) {
                srcFolders.add(pfr.getPath());
            }
        }
    } catch (final CoreException e) {
    }
    return srcFolders;
}