Example usage for org.eclipse.jdt.core IJavaElement getJavaProject

List of usage examples for org.eclipse.jdt.core IJavaElement getJavaProject

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getJavaProject.

Prototype

IJavaProject getJavaProject();

Source Link

Document

Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project).

Usage

From source file:org.jboss.tools.ws.ui.utils.JBossWSUIUtils.java

License:Open Source License

public static IStatus validatePackageName(String name, IJavaElement context) {
    IStatus status = null;//from  w  w w  . j a  va  2s  .c  o  m
    if (context == null || !context.exists()) {
        status = JavaConventions.validatePackageName(name, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
        if (status != null && !status.isOK()) {
            return status;
        }
    }
    String[] sourceComplianceLevels = getSourceComplianceLevels(context);
    status = JavaConventions.validatePackageName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
    if (status != null && status.getSeverity() == IStatus.ERROR) {
        return status;
    }

    IPackageFragmentRoot[] roots = null;
    try {
        IResource[] srcFolders = JBossWSCreationUtils.getJavaSourceRoots(context.getJavaProject());
        roots = new IPackageFragmentRoot[srcFolders.length];
        int i = 0;
        for (IResource src : srcFolders) {
            roots[i] = context.getJavaProject().getPackageFragmentRoot(src);
            i++;
        }
    } catch (JavaModelException e) {
        JBossWSUIPlugin.log(e);
    }
    for (IPackageFragmentRoot root : roots) {
        if (root != null) {
            IPackageFragment pack = root.getPackageFragment(name);
            try {
                IPath rootPath = root.getPath();
                IPath outputPath = root.getJavaProject().getOutputLocation();
                if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
                    // if the bin folder is inside of our root, don't allow
                    // to name a package
                    // like the bin folder
                    IPath packagePath = pack.getPath();
                    if (outputPath.isPrefixOf(packagePath)) {
                        status = StatusUtils
                                .warningStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_IsOutputFolder);
                        return status;
                    }
                }
                if (pack.exists()) {
                    if (pack.containsJavaResources() || !pack.hasSubpackages()) {
                        status = StatusUtils
                                .warningStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_PackageExists);
                    } else {
                        status = StatusUtils
                                .warningStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_PackageNotShown);
                    }
                    return status;
                } else {
                    if (pack.getResource() == null) {
                        continue;
                    }
                    URI location = pack.getResource().getLocationURI();
                    if (location != null) {
                        IFileStore store = EFS.getStore(location);
                        if (store.fetchInfo().exists()) {
                            status = StatusUtils.warningStatus(
                                    JBossWSUIMessages.Error_JBossWS_GenerateWizard_PackageExistsDifferentCase);
                            return status;
                        }
                    }
                }
            } catch (CoreException e) {
                JBossWSUIPlugin.log(e);
            }
        }
    }
    return status;
}

From source file:org.jetbrains.kotlin.wizards.NewUnitWizardPage.java

License:Apache License

private String getPackageFromSelection() {
    String defaultPackage = DEFAULT_PACKAGE;

    if (selection.isEmpty()) {
        return defaultPackage;
    }// w  ww  .  j  av  a  2 s . c  om

    Object selectedObject = selection.getFirstElement();

    if (selectedObject instanceof IJavaElement) {
        IJavaElement selectedJavaElement = (IJavaElement) selectedObject;
        switch (selectedJavaElement.getElementType()) {
        case IJavaElement.PACKAGE_FRAGMENT:
            return selectedJavaElement.getElementName();

        case IJavaElement.COMPILATION_UNIT:
            try {
                return selectedJavaElement.getJavaProject()
                        .findPackageFragment(selectedJavaElement.getPath().makeAbsolute().removeLastSegments(1))
                        .getElementName();
            } catch (Exception e) {
                KotlinLogger.logAndThrow(e);
            }
            break;
        }
    } else if (selectedObject instanceof IResource) {
        IResource selectedResource = (IResource) selectedObject;
        switch (selectedResource.getType()) {
        case IResource.FILE:
            try {
                return JavaCore.create(selectedResource.getProject())
                        .findPackageFragment(
                                selectedResource.getFullPath().makeAbsolute().removeLastSegments(1))
                        .getElementName();
            } catch (Exception e) {
                KotlinLogger.logAndThrow(e);
            }
            break;
        }
    }

    return defaultPackage;
}

From source file:org.key_project.key4eclipse.common.ui.wizard.AbstractNewJavaProjectWizard.java

License:Open Source License

/**
 * Returns the {@link IResource} that will contain java source code.
 * @return The found {@link IResource} with java source code or {@code null} if no one was found. 
 * @throws JavaModelException Occurred Exception.
 *///from w ww .  j  a  va  2 s .com
protected IResource getSourceDirectory() throws JavaModelException {
    IResource sourceDirectory = null;
    IJavaElement newElement = getCreatedElement();
    if (newElement != null) {
        IPackageFragmentRoot[] roots = newElement.getJavaProject().getPackageFragmentRoots();
        int i = 0;
        while (sourceDirectory == null && i < roots.length) {
            if (roots[i].getResource() != null) {
                sourceDirectory = roots[i].getResource();
            }
        }
    }
    return sourceDirectory;
}

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Returns the tab width used in the given {@link IJavaElement}.
 * @param element The {@link IJavaElement} to get its tab width.
 * @return The tab width./* w ww  .  j  a  v  a  2 s.c  o m*/
 */
public static int getTabWidth(IJavaElement element) {
    return element != null ? CodeFormatterUtil.getTabWidth(element.getJavaProject()) : 0;
}

From source file:org.limy.eclipse.code.header.AddHeaderCore.java

License:Open Source License

/**
 * u}bsO??B/*www.  j  ava2 s.  co m*/
 * @param javaElement Javav?WFNg
 */
private void initOptions(IJavaElement javaElement) {

    ScopedPreferenceStore store = new ScopedPreferenceStore(
            new ProjectScope(javaElement.getJavaProject().getProject()),
            LimyCodePlugin.getDefault().getBundle().getSymbolicName());
    String projectName = store.getString(LimyCodeConstants.PREF_PROJECT_NAME);
    if (projectName.length() == 0) {
        projectName = javaElement.getJavaProject().getElementName();
    }

    options.put("project", projectName);
    options.put("project_name", projectName);
    options.put("file_name", javaElement.getResource().getName());
    if (javaElement instanceof IPackageDeclaration) {
        ICompilationUnit cunit = (ICompilationUnit) ((IPackageDeclaration) javaElement).getParent();
        createTypeOptions(cunit.findPrimaryType());
    }
    if (javaElement instanceof ICompilationUnit) {
        ICompilationUnit cunit = (ICompilationUnit) javaElement;
        createTypeOptions(cunit.findPrimaryType());
    }
    if (javaElement instanceof IType) {
        createTypeOptions((IType) javaElement);
    }

    options.put("user", System.getProperty("user.name"));
}

From source file:org.limy.eclipse.code.javadoc.LimyAddJavadocOperation.java

License:Open Source License

/**
 * R?gCfg?B/*from www.j a v  a2s .c  om*/
 * @param javaElement JavaNX
 * @param member ?o
 * @param document hL?g
 * @param lineDelim f~^
 * @param comment R?g
 * @return Cfg
 * @throws BadLocationException hL?gO
 * @throws CoreException RAO
 */
private String createIndentedComment(IJavaElement javaElement, IMember member, IDocument document,
        String lineDelim) throws BadLocationException, CoreException {

    // JavadocR?g??
    String comment = createComment(member, lineDelim);

    // v?WFNgL^u?
    int tabWidth = LimyJavaUtils.getTabWidth(javaElement.getJavaProject());

    // ?o`u?se
    IRegion region = document.getLineInformationOfOffset(LimyJavaUtils.getMemberStartOffset(member, document));
    String line = document.get(region.getOffset(), region.getLength());

    String indentString = IndentManipulation.extractIndentString(line, tabWidth, tabWidth);

    // R?gCfg
    return IndentManipulation.changeIndent(comment, 0, tabWidth, tabWidth, indentString, lineDelim);
}

From source file:org.limy.eclipse.qalab.action.CreateBuildXmlAction.java

License:Open Source License

@Override
protected void doAction(IJavaElement javaElement, IProgressMonitor monitor) throws CoreException {

    CreateBuildXml builder = new CreateBuildXml();

    final LimyQalabEnvironment env = LimyQalabPluginUtils.createEnv(javaElement.getJavaProject().getProject());

    final BuildWarning warningInfo = builder.prepareBuildFiles(env);
    builder.createFiles(env);// ww  w  .  j  a  v a 2s  . c  o  m

    new WorkbenchJob("info") {
        @Override
        public IStatus runInUIThread(IProgressMonitor monitor) {
            QalabActionUtils.showConfirmDialog(warningInfo,
                    "build.xml???B\n" + "Ant?sR}hv?vg?s?B\n"
                            + "EclipseAnt?s?A?????B");
            checkAntEnvironment(env);
            return Status.OK_STATUS;
        }
    }.schedule();
}

From source file:org.limy.eclipse.qalab.action.CreateReportAction.java

License:Open Source License

@Override
protected void doAction(IJavaElement javaElement, IProgressMonitor monitor) throws CoreException {

    IProject project = javaElement.getJavaProject().getProject();
    LimyQalabEnvironment env = LimyQalabPluginUtils.createEnv(project);
    CreateBuildXml builder = new CreateBuildXml();

    final BuildWarning warningInfo = builder.prepareBuildFiles(env);
    builder.createFiles(env);/*  www  .  j a  v  a 2 s  .co  m*/

    doReport(env);

    new WorkbenchJob("info") {
        @Override
        public IStatus runInUIThread(IProgressMonitor monitor) {
            QalabActionUtils.showConfirmDialog(warningInfo, "Antgp|?[g???B\n"
                    + "Ant?ov?WFNg?[g limy.log t@C?o?B\n"
                    + "v?ZXobNOEh?s?B?soProgressr?[mF?B\n"
                    + "\n"
                    + "v?WFNgK???AEclipse?B\n"
                    + "???AR}hgp?j?[ 'build.xml??' p\n"
                    + "R}hv?vgAnt?s|?[g???B");
            return Status.OK_STATUS;
        }
    }.schedule();

}

From source file:org.limy.eclipse.qalab.action.CreateReportProcessAction.java

License:Open Source License

@Override
protected void doAction(IJavaElement javaElement, IProgressMonitor monitor) throws CoreException {

    IProject project = javaElement.getJavaProject().getProject();
    LimyQalabEnvironment env = LimyQalabPluginUtils.createEnv(project);
    CreateBuildXml builder = new CreateBuildXml();

    final BuildWarning warningInfo = builder.prepareBuildFiles(env);
    builder.createFiles(env);//  ww  w .  j  a  v a2 s.  c o  m

    IPath projectPath = project.getProject().getLocation();
    IPreferenceStore store = env.getStore();
    IPath path = projectPath.append(store.getString(LimyQalabConstants.KEY_BUILD_XML));
    try {
        ProcessUtils.execProgramNoWait(projectPath.toFile(), "ant.bat", "-f", path.toFile().getAbsolutePath(),
                "-l", LimyQalabUtils.createTempFile(project, "ant.log").getAbsolutePath());
    } catch (IOException e) {
        LimyEclipsePluginUtils.log(e);
    }

    new WorkbenchJob("info") {
        @Override
        public IStatus runInUIThread(IProgressMonitor monitor) {
            QalabActionUtils.showConfirmDialog(warningInfo,
                    "Antgp|?[g???B\n"
                            + "Ant?oRPOJECT_ROOT/.limy/ant.log t@C?o?B\n"
                            + "??Ov?ZX?s?A?soEclipse?mF?B\n"
                            + "?????I???A?Ot@Crr??B");
            return Status.OK_STATUS;
        }
    }.schedule();
}

From source file:org.limy.eclipse.qalab.action.OpenTestClassAction.java

License:Open Source License

@Override
public void doRun(ISelection selection, IProgressMonitor monitor) throws CoreException {

    Collection<IJavaElement> javaElements = getSelectedJavaElements();
    if (!javaElements.isEmpty()) {
        IJavaElement javaElement = javaElements.iterator().next();
        IJavaProject javaProject = javaElement.getJavaProject();

        LimyQalabEnvironment env = LimyQalabPluginUtils.createEnv(javaProject.getProject());

        IType type = null;/* w  w w  .  j a v a2  s  .  c  o  m*/
        if (QalabResourceUtils.isTestResource(javaElement.getResource())) {
            // eXg\?[X
            String mainClassName = QalabResourceUtils.getQualifiedMainClassName(env, javaElement.getResource());
            type = javaProject.findType(mainClassName);
        } else {
            // ?C\?[X
            String testClassName = QalabResourceUtils.getQualifiedTestClassName(env, javaElement.getResource());
            type = javaProject.findType(testClassName);
        }
        if (type != null) {
            LimyUIUtils.openInEditor(type);
            //                LimyUIUtils.openFile((IFile)type.getResource());
        }

    }

}