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

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

Introduction

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

Prototype

IPackageFragment getPackageFragment(String packageName);

Source Link

Document

Returns the package fragment with the given package name.

Usage

From source file:de.hentschel.visualdbc.datasource.ui.test.testCase.swtbot.SWTBotJavaPackageSettingControlTest.java

License:Open Source License

/**
 * Tests the select button for resources.
 *//*from  w  w w . j  av a  2s .co  m*/
@Test
public void testSelectingResource() throws CoreException, InterruptedException {
    // Create project
    IJavaProject javaProject = createProject();
    IProject project = javaProject.getProject();
    assertTrue(project.exists());
    IFolder srcFolder = project.getFolder("src");
    assertTrue(srcFolder.exists());
    IFolder packageAFolder = srcFolder.getFolder("sWTBotJavaPackageSettingControlTestA");
    assertTrue(packageAFolder.exists());
    IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
    assertTrue(roots.length >= 1);
    IPackageFragmentRoot defaultPackage = roots[0];
    assertNotNull(defaultPackage);
    IPackageFragment packageA = defaultPackage.getPackageFragment("sWTBotJavaPackageSettingControlTestA");
    assertNotNull(packageA);
    IPackageFragment packageB = defaultPackage.getPackageFragment("sWTBotJavaPackageSettingControlTestB");
    assertNotNull(packageB);
    // Create control
    final ISettingControl settingControl = SettingControlUtil.createSettingControl(getControlId());
    assertNotNull(settingControl);
    assertEquals(0, settingControl.getSettingControlListeners().length);
    // Create shell and UI control instance and set initial value
    IRunnableWithResult<Control> createRun = new AbstractRunnableWithResult<Control>() {
        @Override
        public void run() {
            Shell shell = new Shell(Display.getDefault());
            shell.setText("SWTBotJavaPackageSettingControlTest");
            shell.setLayout(new FillLayout());
            shell.setSize(300, 300);
            Control control = settingControl.createControl(shell);
            setResult(control);
            shell.open();
        }
    };
    Display.getDefault().syncExec(createRun);
    final Control control = createRun.getResult();
    try {
        assertNotNull(control);
        // Create bot and get Shell
        SWTWorkbenchBot bot = new SWTWorkbenchBot();
        SWTBotShell botShell = bot.shell("SWTBotJavaPackageSettingControlTest");
        SWTBotRadio resourceRadio = botShell.bot().radio("Resource");
        SWTBotText pathText = botShell.bot().text();
        SWTBotButton clickButton = botShell.bot().button();
        // Select type
        resourceRadio.click();
        // Select package a
        clickButton.click();
        SWTBotShell selectShell = botShell.bot().shell("Select container");
        String[] segments = packageA.getResource().getFullPath().segments();
        TestUtilsUtil.selectInTree(selectShell.bot().tree(), segments);
        selectShell.bot().button("OK").click();
        assertEquals(packageA.getResource().getFullPath(),
                TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        assertEquals(packageA.getPath().toString(), pathText.getText());
        // Select package b, package a must be preselected
        clickButton.click();
        selectShell = botShell.bot().shell("Select container");
        TableCollection selection = selectShell.bot().tree().selection();
        assertEquals(1, selection.rowCount());
        assertEquals(packageA.getElementName(), selection.get(0).get(0));
        segments = packageB.getResource().getFullPath().segments();
        TestUtilsUtil.selectInTree(selectShell.bot().tree(), segments);
        selectShell.bot().button("OK").click();
        assertEquals(packageB.getResource().getFullPath(),
                TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        assertEquals(packageB.getPath().toString(), pathText.getText());
    } finally {
        // Close shell
        if (control != null) {
            control.getDisplay().syncExec(new Runnable() {
                @Override
                public void run() {
                    control.getShell().close();
                }
            });
        }
    }
}

From source file:de.hentschel.visualdbc.datasource.ui.test.testCase.swtbot.SWTBotJavaPackageSettingControlTest.java

License:Open Source License

/**
 * Tests getting and setting values by API and user.
 *///from  ww  w  . j av a2  s .c  om
@Test
public void testGettingAndSettingValues() throws Exception {
    IJavaProject javaProject = createProject();
    IProject project = javaProject.getProject();
    assertTrue(project.exists());
    IFolder srcFolder = project.getFolder("src");
    assertTrue(srcFolder.exists());
    IFolder packageAFolder = srcFolder.getFolder("sWTBotJavaPackageSettingControlTestA");
    assertTrue(packageAFolder.exists());
    IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
    assertTrue(roots.length >= 1);
    IPackageFragmentRoot defaultPackage = roots[0];
    assertNotNull(defaultPackage);
    IPackageFragment packageA = defaultPackage.getPackageFragment("sWTBotJavaPackageSettingControlTestA");
    assertNotNull(packageA);
    IPackageFragment packageB = defaultPackage.getPackageFragment("sWTBotJavaPackageSettingControlTestB");
    assertNotNull(packageB);
    doTest(new Object[] { packageA, project, ResourceUtil.getLocation(packageAFolder), javaProject,
            defaultPackage },
            new Object[] { project, packageAFolder, ResourceUtil.getLocation(project),
                    ResourceUtil.getLocation(packageAFolder), packageA, packageB });
}

From source file:de.ovgu.featureide.core.mpl.job.MPLRenameExternalJob.java

License:Open Source License

private boolean renameDefaultPackage(IPackageFragmentRoot packageFragmentRoot,
        ICompilationUnit[] compilationUnits) {
    if (compilationUnits != null && compilationUnits.length > 0) {
        RefactoringContribution contribution = RefactoringCore
                .getRefactoringContribution(IJavaRefactorings.MOVE);
        MoveDescriptor descriptor = (MoveDescriptor) contribution.createDescriptor();

        descriptor.setProject(arguments.externalProject.getName());
        descriptor.setDestination(packageFragmentRoot.getPackageFragment(arguments.prefix));
        descriptor.setMoveResources(new IFile[0], new IFolder[0], compilationUnits);
        descriptor.setUpdateReferences(true);

        RefactoringStatus status = new RefactoringStatus();
        try {//from w w  w  . java  2  s .  com
            final NullProgressMonitor monitor = new NullProgressMonitor();
            Refactoring refactoring = descriptor.createRefactoring(status);
            new PerformRefactoringOperation(refactoring, CheckConditionsOperation.ALL_CONDITIONS).run(monitor);
        } catch (CoreException e) {
            MPLPlugin.getDefault().logError(e);
            return false;
        }
    }
    return true;
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockEditor.java

License:Open Source License

/********************************************************************************/

void handleDelete(String proj, String what, String path) throws BedrockException {
    IResource rs = null;/*from ww w.j  a v a 2s  . c  o m*/
    FileData fd = null;
    IProject ip = our_plugin.getProjectManager().findProject(proj);
    IJavaProject ijp = JavaCore.create(ip);

    if (what.equals("PROJECT")) {
        if (ip == null)
            throw new BedrockException("Can't find project to delete");
        rs = ip;
    } else if (what.equals("FILE")) {
        fd = file_map.get(path);
        if (fd != null) {
            rs = fd.getSearchUnit().getResource();
        }
        if (rs == null)
            throw new BedrockException("Can't find file to delete");
    } else if (what.equals("CLASS")) {
        IType ityp = null;
        String bcls = baseClassName(path);
        String file = getFileFromClass(proj, bcls);
        fd = file_map.get(file);
        try {
            if (ijp != null)
                ityp = ijp.findType(bcls);
        } catch (JavaModelException e) {
        }
        if (ityp == null)
            throw new BedrockException("Can't find class to delete");
        rs = ityp.getResource();
    } else if (what.equals("PACKAGE")) {
        IPackageFragmentRoot ipfr = null;
        try {
            for (IPackageFragmentRoot pfr : ijp.getAllPackageFragmentRoots()) {
                try {
                    if (!pfr.isExternal() && !pfr.isArchive()
                            && pfr.getKind() == IPackageFragmentRoot.K_SOURCE) {
                        ipfr = pfr;
                        break;
                    }
                } catch (JavaModelException e) {
                }
            }
        } catch (JavaModelException e) {
            throw new BedrockException("Problem finding package root", e);
        }
        if (ipfr == null)
            throw new BedrockException("Can't find source fragment root");
        IPackageFragment ifr = ipfr.getPackageFragment(path);
        if (ifr == null)
            throw new BedrockException("Can't find package to delete");
        rs = ifr.getResource();
    }

    if (rs != null) {
        BedrockPlugin.logD("Delete resource " + rs);
        try {
            rs.delete(IResource.FORCE | IResource.KEEP_HISTORY | IResource.ALWAYS_DELETE_PROJECT_CONTENT,
                    new BedrockProgressMonitor(our_plugin, "Deleting " + path));
        } catch (CoreException e) {
            throw new BedrockException("Problem with delete", e);
        }
    }

    if (fd != null) {
        String file = fd.getFileName();
        file_map.remove(file);
        IvyXmlWriter xw = our_plugin.beginMessage("RESOURCE");
        xw.begin("DELTA");
        xw.field("KIND", "REMOVED");
        xw.field("PATH", file);
        xw.begin("RESOURCE");
        xw.field("LOCATION", file);
        xw.field("TYPE", "FILE");
        xw.field("PROJECT", proj);
        xw.end("RESOURCE");
        xw.end("DELTA");
        our_plugin.finishMessage(xw);
    }
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockProject.java

License:Open Source License

IPackageFragment findPackageFragment(String proj, String pkg) throws BedrockException {
    IProject ip = findProject(proj);/*from   ww  w . jav a 2  s  .c  om*/
    IJavaProject ijp = JavaCore.create(ip);
    if (ijp == null)
        return null;

    try {
        for (IPackageFragmentRoot pfr : ijp.getAllPackageFragmentRoots()) {
            try {
                if (!pfr.isExternal() && !pfr.isArchive() && pfr.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    IPackageFragment ipf = pfr.getPackageFragment(pkg);
                    if (ipf != null && ipf.isOpen()) {
                        File f = BedrockUtil.getFileForPath(ipf.getPath(), ip);
                        if (f.exists())
                            return ipf;
                        BedrockPlugin.logE("Fragment path doesn't exist: " + f);
                    }
                }
            } catch (JavaModelException e) {
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
        throw new BedrockException("Problem finding package roots: " + e, e);
    }

    return null;
}

From source file:es.bsc.servicess.ide.editors.CommonFormPage.java

License:Apache License

/** Get the eclipse compilation unit for the core element interface 
 * form a selected orchestration class//from   w  w w.ja va 2s .  c o m
 * @param serviceClass Orchestration class
 * @param project Service implementation project
 * @param prMetadata Project Metadata
 * @return Core element interface
 * @throws PartInitException
 * @throws JavaModelException
 */
public static ICompilationUnit getCEInterface(String serviceClass, IJavaProject project,
        ProjectMetadata prMetadata) throws PartInitException, JavaModelException {
    IPackageFragmentRoot root = prMetadata.getPackageFragmentRoot(project);
    log.debug("Getting Package fragment " + Signature.getQualifier(serviceClass));
    IPackageFragment frag = root.getPackageFragment(Signature.getQualifier(serviceClass));
    log.debug("Comparing " + frag.getElementName() + " with " + serviceClass);
    if (frag.exists()) {
        log.debug("Looking for " + Signature.getSimpleName(serviceClass) + "Itf.java in " + frag.getPath());
        return frag.getCompilationUnit(Signature.getSimpleName(serviceClass) + "Itf.java");
    } else
        throw new PartInitException("Package fragment for " + serviceClass + " not found");
}

From source file:es.bsc.servicess.ide.editors.CommonFormPage.java

License:Apache License

/** Get the eclipse compilation unit for the orchestration class
 * @param serviceClass Name of the orchestration class
 * @param project Service implementation project
 * @param prMetadata Project Metadata/*from www  . j  a  v  a2  s  . c  om*/
 * @return Compilation unit of the orchetration class
 * @throws Exception 
 */
public static IType getExternalOrchestrationClass(String serviceClass, IJavaProject project,
        ProjectMetadata prMetadata) throws Exception {
    String libraryLocation = (prMetadata.getOrchestrationClass(serviceClass).getLibraryLocation());
    IType type = null;
    for (IPackageFragmentRoot r : project.getAllPackageFragmentRoots()) {
        /*log.debug("PFR: " + r.getElementName()+ " entry: "
           + r.getResolvedClasspathEntry().getPath() + "(Looking for: "+ libraryLocation+")");*/
        if (r.getResolvedClasspathEntry().getPath().toOSString().trim().equals(libraryLocation.trim())) {
            IPackageFragment frag = r.getPackageFragment(Signature.getQualifier(serviceClass));
            return frag.getClassFile(Signature.getSimpleName(serviceClass) + ".class").getType();
        }
    }
    throw new PartInitException("Type not found");
}

From source file:es.bsc.servicess.ide.editors.CommonFormPage.java

License:Apache License

/** Search a class in a service java package
 * @param project Service implementation project
 * @param packageName package name//ww w .  jav a2s  . c o  m
 * @param classname class name to search
 * @return Fully qualified domain name of the class if find, or null if not find
 * @throws JavaModelException
 */
private static String searchClassInPackages(IJavaProject project, String packageName, String classname)
        throws JavaModelException {
    for (IPackageFragmentRoot r : project.getAllPackageFragmentRoots()) {
        IPackageFragment pack = r.getPackageFragment(packageName);
        if (pack != null && pack.exists()) {
            ICompilationUnit cu = pack.getCompilationUnit(classname + ".java");
            if (cu != null && cu.exists()) {
                return packageName + "." + classname;
            } else {
                IClassFile cf = pack.getClassFile(classname + ".class");
                if (cf != null && cf.exists()) {
                    return packageName + "." + classname;
                }
            }
        }
    }
    return null;
}

From source file:es.bsc.servicess.ide.PackagingUtils.java

License:Apache License

/** Get the java packages of the Core Elements classes
 * @param elementsInPackage Elements in the package
 * @param elements Service Core Element description
 * @param sourceDir Folder of the java project sources
 * @return/*  w w  w.j a v  a 2 s .c o  m*/
 */
private static IPackageFragment[] getCorePackageFragments(String[] elementsInPackage,
        HashMap<String, ServiceElement> elements, IPackageFragmentRoot sourceDir) {
    ArrayList<IPackageFragment> pfs = new ArrayList<IPackageFragment>();
    for (String e : elementsInPackage) {
        ServiceElement se = elements.get(e);
        if (se instanceof MethodCoreElement) {
            String dc = ((MethodCoreElement) se).getDeclaringClass();
            if (dc != null) {
                log.debug("Declaring class for " + e + " is " + dc);
                String p = Signature.getQualifier(dc);
                if (p != null) {
                    IPackageFragment pf = sourceDir.getPackageFragment(p);
                    log.debug("Package fragment for " + e + " is " + pf.getElementName());
                    if (pf != null && pf.exists()) {
                        pfs.add(pf);
                    }
                }
            }
        }
    }
    return pfs.toArray(new IPackageFragment[pfs.size()]);

}

From source file:es.bsc.servicess.ide.PackagingUtils.java

License:Apache License

/**Get Java packages for the orchestration Elements
 * @param elements Orchestration Elements array
 * @param sourceDir Service implementation source Folder
 * @return Array of Java packages//from w  ww  .  ja  va  2  s .c o m
 */
private static IPackageFragment[] getOrchestrationPackageFragments(String[] elements,
        IPackageFragmentRoot sourceDir) {
    ArrayList<IPackageFragment> pfs = new ArrayList<IPackageFragment>();
    for (String dc : elements) {
        if (dc != null) {
            String p = Signature.getQualifier(dc);
            if (p != null) {
                IPackageFragment pf = sourceDir.getPackageFragment(p);
                if (pf != null && pf.exists()) {
                    pfs.add(pf);
                }
            }
        }
    }
    return pfs.toArray(new IPackageFragment[pfs.size()]);

}