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:com.siteview.mde.internal.core.converter.MonitorConverter.java

License:Open Source License

private void addPackagesFromFragRoot(IPackageFragmentRoot root, Collection result, List filter) {
    if (root == null)
        return;//from  www  .  java2s .  co m
    try {
        if (filter != null && !filter.contains("*")) { //$NON-NLS-1$
            ListIterator li = filter.listIterator();
            while (li.hasNext()) {
                String pkgName = li.next().toString();
                if (pkgName.endsWith(".*")) //$NON-NLS-1$
                    pkgName = pkgName.substring(0, pkgName.length() - 2);

                IPackageFragment frag = root.getPackageFragment(pkgName);
                if (frag != null)
                    result.add(pkgName);
            }
            return;
        }
        IJavaElement[] children = root.getChildren();
        for (int j = 0; j < children.length; j++) {
            IPackageFragment fragment = (IPackageFragment) children[j];
            String name = fragment.getElementName();
            if (fragment.hasChildren() && !result.contains(name)) {
                result.add(name);
            }
        }
    } catch (JavaModelException e) {
    }
}

From source file:com.siteview.mde.internal.core.util.PDEJavaHelper.java

License:Open Source License

private static IPackageFragment getExternalPackageFragment(String packageName, String pluginID) {
    if (pluginID == null)
        return null;
    IMonitorModelBase base = null;//from  w w w  .ja va2s.c o  m
    try {
        IMonitorModelBase plugin = MonitorRegistry.findModel(pluginID);
        if (plugin == null)
            return null;
        ImportPackageSpecification[] packages = plugin.getBundleDescription().getImportPackages();
        for (int i = 0; i < packages.length; i++)
            if (packages[i].getName().equals(packageName)) {
                ExportPackageDescription desc = (ExportPackageDescription) packages[i].getSupplier();
                if (desc != null)
                    base = MonitorRegistry.findModel(desc.getExporter().getSymbolicName());
                break;
            }
        if (base == null)
            return null;
        IResource res = base.getUnderlyingResource();
        if (res != null) {
            IJavaProject jp = JavaCore.create(res.getProject());
            if (jp != null)
                try {
                    IPackageFragmentRoot[] roots = jp.getAllPackageFragmentRoots();
                    for (int i = 0; i < roots.length; i++) {
                        IPackageFragment frag = roots[i].getPackageFragment(packageName);
                        if (frag.exists())
                            return frag;
                    }
                } catch (JavaModelException e) {
                }
        }
        IProject proj = MDECore.getWorkspace().getRoot()
                .getProject(SearchablePluginsManager.PROXY_PROJECT_NAME);
        if (proj == null)
            return searchWorkspaceForPackage(packageName, base);
        IJavaProject jp = JavaCore.create(proj);
        IPath path = new Path(base.getInstallLocation());
        // if model is in jar form
        if (!path.toFile().isDirectory()) {
            IPackageFragmentRoot root = jp.findPackageFragmentRoot(path);
            if (root != null) {
                IPackageFragment frag = root.getPackageFragment(packageName);
                if (frag.exists())
                    return frag;
            }
            // else model is in folder form, try to find model's libraries on filesystem
        } else {
            IMonitorLibrary[] libs = base.getMonitorBase().getLibraries();
            for (int i = 0; i < libs.length; i++) {
                if (IMonitorLibrary.RESOURCE.equals(libs[i].getType()))
                    continue;
                String libName = ClasspathUtilCore.expandLibraryName(libs[i].getName());
                IPackageFragmentRoot root = jp.findPackageFragmentRoot(path.append(libName));
                if (root != null) {
                    IPackageFragment frag = root.getPackageFragment(packageName);
                    if (frag.exists())
                        return frag;
                }
            }
        }
    } catch (JavaModelException e) {
    }
    return searchWorkspaceForPackage(packageName, base);
}

From source file:com.siteview.mde.internal.core.util.PDEJavaHelper.java

License:Open Source License

private static IPackageFragment searchWorkspaceForPackage(String packageName, IMonitorModelBase base) {
    IMonitorLibrary[] libs = base.getMonitorBase().getLibraries();
    ArrayList libPaths = new ArrayList();
    IPath path = new Path(base.getInstallLocation());
    if (libs.length == 0) {
        libPaths.add(path);//from  w ww  .ja  va2 s  .  c om
    }
    for (int i = 0; i < libs.length; i++) {
        if (IMonitorLibrary.RESOURCE.equals(libs[i].getType()))
            continue;
        String libName = ClasspathUtilCore.expandLibraryName(libs[i].getName());
        libPaths.add(path.append(libName));
    }
    IProject[] projects = MDECore.getWorkspace().getRoot().getProjects();
    for (int i = 0; i < projects.length; i++) {
        try {
            if (!projects[i].hasNature(JavaCore.NATURE_ID) || !projects[i].isOpen())
                continue;
            IJavaProject jp = JavaCore.create(projects[i]);
            ListIterator li = libPaths.listIterator();
            while (li.hasNext()) {
                IPackageFragmentRoot root = jp.findPackageFragmentRoot((IPath) li.next());
                if (root != null) {
                    IPackageFragment frag = root.getPackageFragment(packageName);
                    if (frag.exists())
                        return frag;
                }
            }
        } catch (CoreException e) {
        }
    }
    return null;
}

From source file:com.sun.codemodel.ClassGenerator.java

License:Apache License

private void modifyObjectFactory(ElementClass ec, IProgressMonitor pm) throws JavaModelException {
    JDefinedClass c = ec.getjDefClass();
    if (ec.getElementType() != null) {
        IPackageFragmentRoot pfr = project.getPackageFragmentRoot(generated);
        if (pfr != null && pfr.exists()) {
            IPackageFragment pf = pfr.getPackageFragment(c.getPackage().name());
            if (pf != null && pf.exists()) {
                ICompilationUnit of = pf.getCompilationUnit("ObjectFactory.java");
                if (of != null && !of.exists()) {
                    of = pf.createCompilationUnit("ObjectFactory.java", "", true, pm);
                    of.createPackageDeclaration(c.getPackage().name(), pm);
                    of.createImport("javax.xml.bind.annotation.XmlRegistry", null, Flags.AccDefault, pm);
                    String lineDelimiter = StubUtility.getLineDelimiterUsed(project);
                    IType t = of.createType(generateObjectFactory(lineDelimiter), null, false, pm);
                    t.createMethod("public ObjectFactory(){}" + lineDelimiter, null, false, pm);
                }/*from w  ww .ja  v a  2 s  .  c  o m*/
                of = addClass(ec, of, pm);

            } else
                throw new JavaModelException(
                        new Exception("Error package " + c.getPackage().name() + " does not exists"),
                        JavaModelStatus.ERROR);

        } else
            throw new JavaModelException(
                    new Exception("Error package root " + generated.getName() + " does not exists"),
                    JavaModelStatus.ERROR);
    }
}

From source file:com.testify.ecfeed.ui.common.EclipseModelImplementer.java

License:Open Source License

private IPackageFragment getPackageFragment(String name) throws CoreException {
    IPackageFragmentRoot packageFragmentRoot = getPackageFragmentRoot();
    IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(name);
    if (packageFragment.exists() == false) {
        packageFragment = packageFragmentRoot.createPackageFragment(name, false, null);
    }/* ww w  . ja  va  2  s .c  o m*/
    return packageFragment;
}

From source file:com.worldline.awltech.i18ntools.wizard.core.modules.ResourceBundleWrapper.java

License:Open Source License

/**
 * Loads the currently selected compilation unit.
 *//* ww w  .j  a va  2 s.  c o m*/
private void loadCompilationUnit() {
    final IProject project = this.javaProject.getProject();
    final IResource sourceFolderResource = project
            .getFolder(new Path(this.configuration.getJavaSourceFolder()));
    final IPackageFragmentRoot ipfr = this.javaProject.getPackageFragmentRoot(sourceFolderResource);

    IPackageFragment ipf = ipfr.getPackageFragment(this.packageName);
    if (!ipf.exists()) {
        try {
            ipf = ipfr.createPackageFragment(this.packageName, false, new NullProgressMonitor());
        } catch (final JavaModelException e) {
            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    RefactoringWizardMessages.ERROR_CREATE_PACKAGE.value(), e));
        }
    }

    final String javaUnitName = this.resourceBundleName.concat(".java");
    this.enumJavaCompilationUnit = ipf.getCompilationUnit(javaUnitName);
    if (!this.enumJavaCompilationUnit.exists()) {
        final String contents = this.createJavaUnitContents();

        // Format the source code before trying to set it to the compilation unit.
        CodeFormatter formatter = ToolFactory.createCodeFormatter(this.javaProject.getOptions(true),
                ToolFactory.M_FORMAT_EXISTING);
        IDocument document = new Document(contents);

        TextEdit textEdit = formatter.format(
                CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, contents, 0,
                contents.length(), 0, null);
        try {
            textEdit.apply(document);
        } catch (MalformedTreeException e1) {
            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    RefactoringWizardMessages.ERROR_REFACTOR_TEMPLATE.value(), e1));
        } catch (BadLocationException e1) {
            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    RefactoringWizardMessages.ERROR_REFACTOR_TEMPLATE.value(), e1));
        }
        try {
            // Set the source into the compilation unit.
            this.enumJavaCompilationUnit = ipf.createCompilationUnit(javaUnitName, document.get(), false,
                    new NullProgressMonitor());
        } catch (final JavaModelException e) {
            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    RefactoringWizardMessages.ERROR_CREATE_CU.value(), e));
        }
    }

    final ASTParser enumSourceParser = ASTParser.newParser(AST.JLS4);
    enumSourceParser.setProject(this.javaProject);
    enumSourceParser.setBindingsRecovery(true);
    enumSourceParser.setResolveBindings(true);
    enumSourceParser.setKind(ASTParser.K_COMPILATION_UNIT);
    enumSourceParser.setSource(this.enumJavaCompilationUnit);

    this.enumDomCompilationUnit = (CompilationUnit) enumSourceParser.createAST(new NullProgressMonitor());
    this.enumDomCompilationUnit.recordModifications();
}

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

License:Open Source License

/**
 * Tests error messages on the file tab.
 *//*  www  .  j  av a2  s.co  m*/
@Test
public void testPackageErrorMessages() 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();
    assertNotNull(control);
    try {
        // Create bot and get Shell
        SWTWorkbenchBot bot = new SWTWorkbenchBot();
        SWTBotShell botShell = bot.shell("SWTBotJavaPackageSettingControlTest");
        SWTBotRadio resourceRadio = botShell.bot().radio("Resource");
        SWTBotRadio packageRadio = botShell.bot().radio("Package");
        SWTBotText pathText = botShell.bot().text();
        // Select type
        packageRadio.click();
        // Test initial value
        assertEquals("No directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertNull(TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch to other invalid type
        resourceRadio.click();
        assertEquals("No existing project or folder defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new Path(""), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch back from invalid type
        packageRadio.click();
        assertEquals("No directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertNull(TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch to other invalid type and make it valid
        resourceRadio.click();
        assertEquals("No existing project or folder defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new Path(""), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        pathText.setText(packageB.getPath().toString());
        assertNull(TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(packageB.getPath(), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch back from valid type
        packageRadio.click();
        assertEquals("No directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertNull(TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
    } 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 error messages on the file tab.
 *///from w  w w .  ja v a  2s.  c  om
@Test
public void testFileErrorMessages() 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 fileRadio = botShell.bot().radio("Directory");
        SWTBotRadio packageRadio = botShell.bot().radio("Package");
        SWTBotText pathText = botShell.bot().text();
        // Select type
        fileRadio.click();
        // Test initial value
        assertEquals("No existing directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new File(""), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Set invalid path
        pathText.setText("INVALID");
        assertEquals("No existing directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new File("INVALID"), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Set valid path
        pathText.setText(ResourceUtil.getLocation(packageA.getResource()).toString());
        assertNull(TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(ResourceUtil.getLocation(packageA.getResource()),
                TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Set invalid path
        pathText.setText("INVALID");
        assertEquals("No existing directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new File("INVALID"), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch to other invalid type
        packageRadio.click();
        assertEquals("No directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertNull(TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch back from invalid type
        fileRadio.click();
        assertEquals("No existing directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new File("INVALID"), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch to other invalid type and make it valid
        packageRadio.click();
        assertEquals("No directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertNull(TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        botShell.bot().button().click();
        SWTBotShell selectShell = botShell.bot().shell("Select package");
        selectShell.bot().text().setText(packageB.getElementName()); // Filter the table entries, required in tests if scroll bars are used.
        selectShell.bot().table().select(packageB.getElementName());
        selectShell.bot().button("OK").click();
        assertNull(TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(getExpectedPackage(packageB),
                TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch back from valid type
        fileRadio.click();
        assertEquals("No existing directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new File("INVALID"), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
    } 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 error messages on the resource tab.
 *//*from  ww w.j  a  v a2 s .  co m*/
@Test
public void testResourceErrorMessages() 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");
        SWTBotRadio fileRadio = botShell.bot().radio("Directory");
        SWTBotText pathText = botShell.bot().text();
        // Select type
        resourceRadio.click();
        // Test initial value
        assertEquals("No existing project or folder defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new Path(""), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Set invalid path
        pathText.setText("INVALID");
        assertEquals("No existing project or folder defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new Path("INVALID"), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Set valid path
        pathText.setText(packageA.getPath().toString());
        assertNull(TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(packageA.getPath(), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Set invalid path
        pathText.setText("INVALID");
        assertEquals("No existing project or folder defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new Path("INVALID"), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch to other invalid type
        fileRadio.click();
        assertEquals("No existing directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new File(""), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch back from invalid type
        resourceRadio.click();
        assertEquals("No existing project or folder defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new Path("INVALID"), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch to other invalid type and make it valid
        fileRadio.click();
        assertEquals("No existing directory defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new File(""), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        pathText.setText(ResourceUtil.getLocation(packageB.getResource()).toString());
        assertNull(TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(ResourceUtil.getLocation(packageB.getResource()),
                TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        // Switch back from valid type
        resourceRadio.click();
        assertEquals("No existing project or folder defined.",
                TestDataSourceUIUtil.getValidationMessageThreadSave(settingControl, control));
        assertEquals(new Path("INVALID"), TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
    } 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 the select button for packages.
 */// w  w  w  .  j  a v  a 2  s  .com
@Test
public void testSelectingPackage() 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 packageRadio = botShell.bot().radio("Package");
        SWTBotText pathText = botShell.bot().text();
        SWTBotButton clickButton = botShell.bot().button();
        // Select type
        packageRadio.click();
        // Select package a
        clickButton.click();
        SWTBotShell selectShell = botShell.bot().shell("Select package");
        selectShell.bot().text().setText(packageA.getElementName()); // Filter the table entries, required in tests if scroll bars are used.
        selectShell.bot().table().select(packageA.getElementName());
        selectShell.bot().button("OK").click();
        assertEquals(getExpectedPackage(packageA),
                TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        assertEquals(packageA.getElementName(), pathText.getText());
        // Select package b, package a must be preselected (not testable)
        clickButton.click();
        selectShell = botShell.bot().shell("Select package");
        selectShell.bot().text().setText(packageB.getElementName()); // Filter the table entries, required in tests if scroll bars are used.
        selectShell.bot().table().select(packageB.getElementName());
        selectShell.bot().button("OK").click();
        assertEquals(getExpectedPackage(packageB),
                TestDataSourceUIUtil.getValueThreadSave(settingControl, control));
        assertEquals(packageB.getElementName(), pathText.getText());
    } finally {
        // Close shell
        if (control != null) {
            control.getDisplay().syncExec(new Runnable() {
                @Override
                public void run() {
                    control.getShell().close();
                }
            });
        }
    }
}