Example usage for org.eclipse.jdt.core.search SearchEngine createWorkspaceScope

List of usage examples for org.eclipse.jdt.core.search SearchEngine createWorkspaceScope

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.search SearchEngine createWorkspaceScope.

Prototype

public static IJavaSearchScope createWorkspaceScope() 

Source Link

Document

Returns a Java search scope with the workspace as the only limit.

Usage

From source file:org.eclipselabs.collage.model.resourceid.JavaClassFileIdentifier.java

License:Open Source License

@Override
public IEditorPart openInEditor() throws CoreException {
    synchronized (foundElementLock) {
        if (foundElement == null) {
            SearchEngine engine = new SearchEngine();
            SearchPattern pattern = SearchPattern.createPattern(className, IJavaSearchConstants.TYPE,
                    IJavaSearchConstants.DECLARATIONS,
                    SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
            ClassFileSearchRequestor requestor = new ClassFileSearchRequestor();
            engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
                    SearchEngine.createWorkspaceScope(), requestor, new NullProgressMonitor());
            foundElement = requestor.getElement();
        }/*from  ww w . j a va  2 s.  co m*/
        if (foundElement != null) {
            return JavaUI.openInEditor(foundElement);
        }
    }
    CollageUtilities.showError(CollageActivator.PLUGIN_NAME, "Unable to open an editor for " + shortName + ".");
    return null;
}

From source file:org.fastcode.popup.actions.easycreate.NewMemberCreateActionSupport.java

License:Open Source License

/**
 *
 * @return// w  ww  .  j  a  va 2  s.  c o  m
 * @throws Exception
 */
@Override
protected IType[] getTypesFromUser(final String title, final String description) throws Exception {

    if (!isSimpleType()) {
        final String parameterizedTypeChoice = Activator.getDefault().getPreferenceStore()
                .getString(P_GLOBAL_PARAMETERIZED_TYPE_CHOICE);

        boolean createParameterDialog = true;
        if (parameterizedTypeChoice.equals(GLOBAL_ASK_TO_CREATE)) {
            final MessageDialogWithToggle dialogWithToggle = MessageDialogWithToggle.openYesNoQuestion(
                    this.editorPart.getSite().getShell(), "Parameterized Type",
                    "Would you like to create a parameterized type?", "Remember Decision", false,
                    Activator.getDefault().getPreferenceStore(), P_ASK_FOR_PARAMETERIZED_TYPE);
            if (dialogWithToggle.getReturnCode() != MESSAGE_DIALOG_RETURN_YES) {
                createParameterDialog = false;
            }
        } else if (parameterizedTypeChoice.equals(GLOBAL_NEVER_CREATE)) {
            createParameterDialog = false;
        }

        if (!createParameterDialog) {
            return null;
        }
    }

    final SelectionDialog selectionDialog = createTypeDialog(this.editorPart.getSite().getShell(), null,
            SearchEngine.createWorkspaceScope(), IJavaElementSearchConstants.CONSIDER_ALL_TYPES, true,
            EMPTY_STR);
    selectionDialog.setTitle(title);
    selectionDialog.setMessage(title + " For " + description);

    if (selectionDialog.open() == Window.CANCEL) {
        return null;
    }

    if (selectionDialog.getResult() == null || selectionDialog.getResult().length == 0) {
        return null;
    }
    final IType[] paramArr = new IType[1];
    paramArr[0] = (IType) selectionDialog.getResult()[0];
    return paramArr;
}

From source file:org.fastcode.popup.actions.easycreate.NewMemberCreateActionSupport.java

License:Open Source License

/**
 *
 * @param description//w  ww .j a  v a  2s . co m
 * @param title
 * @return
 * @throws Exception
 */
protected IType openTypeDialog(final String title, final String description) throws Exception {
    final SelectionDialog selectionDialog = createTypeDialog(this.editorPart.getSite().getShell(), null,
            SearchEngine.createWorkspaceScope(), IJavaElementSearchConstants.CONSIDER_ALL_TYPES, true,
            EMPTY_STR);
    selectionDialog.setTitle(title);
    selectionDialog.setMessage(title + " For " + description);

    if (selectionDialog.open() == Window.CANCEL || selectionDialog.getResult() == null
            || selectionDialog.getResult().length == 0) {
        return null;
    }

    return (IType) selectionDialog.getResult()[0];
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param javaProject/*from  www . jav a 2 s .  c o m*/
 * @param imports
 * @param classType
 *
 * @param typesToImport
 * @return
 * @throws Exception
 */
public static List<IType> gatherImports(final IJavaProject javaProject, final String[] imports,
        final int classType, final String action, final List<IType> typesToImport) throws Exception {
    final List<IType> impTypes = new ArrayList<IType>();
    boolean showChoice = true;
    final Shell parentShell = MessageUtil.getParentShell();
    final Shell shell = parentShell == null ? new Shell() : parentShell;

    if (imports == null) {
        return impTypes;
    }
    for (final String imp1 : imports) {
        final String imp = imp1.trim();

        if (imp.endsWith(ASTERISK)) {

            final int indx = imp.lastIndexOf(DOT_CHAR);
            String clssName = null;

            if (indx > 0 && imp.length() > indx) {
                clssName = imp.substring(indx + 1);

                final String pkg = imp.substring(0, indx);
                // javaProject.
                final IPackageFragment packageFragment = getPackageFragmentFromWorkspace(pkg);
                if (packageFragment != null) {
                    final Pattern pattern = Pattern.compile(clssName.replace(ASTERISK, ".*"));
                    for (final ICompilationUnit unit : packageFragment.getCompilationUnits()) {
                        final Matcher matcher = pattern.matcher(unit.getElementName());
                        if (matcher.matches()) {
                            // Make sure it is in project classpath.
                            final IType tmpType = javaProject
                                    .findType(unit.findPrimaryType().getFullyQualifiedName());
                            if (tmpType != null && tmpType.exists()) {
                                impTypes.add(unit.findPrimaryType());
                            }
                        }
                    }
                }
            } else {
                final SelectionDialog typeDialog = JavaUI.createTypeDialog(shell, null,
                        SearchEngine.createWorkspaceScope(),
                        IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES, true, imp);
                final int ret = typeDialog.open();
                final Object[] result = typeDialog.getResult();
                if (ret != CANCEL) {
                    for (final Object type1 : result) {
                        impTypes.add((IType) type1);
                    }
                }
                showChoice = false;
            }
        } else {
            // final FastCodeType fastCodeType = parseType(imp,
            // javaProject.findType(imp).getCompilationUnit());
            final IType type = javaProject.findType(imp);
            if (type != null && type.exists()) {
                impTypes.add(type);
                // showChoice = false;
            }
            /*for (final FastCodeType ftype : fastCodeType.getParameters()) {
               final IType type2 = javaProject.findType(ftype.getName());
               if (type2 != null && type2.exists()) {
                  impTypes.add(type2);
               }
            }*/
        }
    }

    if (!impTypes.isEmpty() && showChoice) {
        final IType[] impArray = impTypes.toArray(new IType[impTypes.size()]);
        // IType[] impArray = new IType[impTypes.size()];
        // int count = 0;
        // for (IType iType : impTypes) {
        // impArray[count++] = iType;
        // }
        final ClassSelectionDialog classSelectionDialog = new ClassSelectionDialog(shell, action,
                "Choose the classes to " + action, impArray, true);
        final int ret = classSelectionDialog.open();
        if (ret == CANCEL) {
            impTypes.clear();
            return impTypes;
        } else {
            final Object[] results = classSelectionDialog.getResult();
            impTypes.clear();
            if (results != null) {
                for (final Object result : results) {
                    impTypes.add((IType) result);
                    typesToImport.add((IType) result);
                }
            }
        }
    } else {
        typesToImport.addAll(impTypes);
    }

    return impTypes;
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 * @param fastCodeContext//from www. j  a  v a  2 s. c  o m
 * @param createSimilarDescriptor
 * @param length
 * @throws Exception
 */
public static void populatecreateDescClassWithUserInput(final FastCodeContext fastCodeContext,
        final CreateSimilarDescriptor createSimilarDescriptor, final String[] inputs,
        final boolean differentName, final IType typeToWorkOn) throws Exception {

    final Shell parentShell = MessageUtil.getParentShell();
    final Shell shell = parentShell == null ? new Shell() : parentShell;
    IPackageFragmentRoot packageFragmentRoot = null;
    IPackageFragment tmpPackageFragment = null;
    final GlobalSettings globalSettings = getInstance();
    String superClass = EMPTY_STR;
    IType supType = null;
    int k = 0;
    boolean isMultiple = false;
    if (inputs.length > 1) {
        isMultiple = true;
    }
    createSimilarDescriptor.setNoOfInputs(inputs.length);
    for (final String input : inputs) {
        if (differentName) {
            final Pattern p = Pattern.compile(createSimilarDescriptor.getFromPattern());
            final Matcher m = p.matcher(typeToWorkOn.getFullyQualifiedName());

            if (!m.matches()) {
                continue;
            }

            final String replatePart = m.group(m.groupCount());
            createSimilarDescriptor.createReplacePartAndValue(replatePart, input);
        }
    }

    final CreateSimilarDescriptorClass[] createSimilarDescUserChoice = new CreateSimilarDescriptorClass[createSimilarDescriptor
            .getCreateSimilarDescriptorClasses().length];
    for (final CreateSimilarDescriptorClass createSimilarDescriptorClass : createSimilarDescriptor
            .getCreateSimilarDescriptorClasses()) {
        IPackageFragment packageFragment = null;
        if (createSimilarDescriptorClass == null) {
            continue;
        }
        String toName = createSimilarDescriptorClass.getToPattern();
        IType[] fldTypeArr = null;
        String targetProject = null;
        if (packageFragmentRoot == null) {
            targetProject = createSimilarDescriptorClass.getProject();
            if (fastCodeContext.getFromType() == null) {
                final IJavaProject project = getJavaProject(targetProject);
                if (project != null && project.exists() && !isEmpty(createSimilarDescriptorClass.getPackge())) {
                    for (final IPackageFragmentRoot pkgFragmntRoot : project.getPackageFragmentRoots()) {
                        packageFragment = pkgFragmntRoot
                                .getPackageFragment(createSimilarDescriptorClass.getPackge());
                        if (packageFragment != null && packageFragment.exists()) {
                            packageFragmentRoot = pkgFragmntRoot;
                            break;
                        }
                    }
                }
                if (packageFragment == null || !packageFragment.exists()) {
                    final SelectionDialog packageDialog = JavaUI.createPackageDialog(shell, project, 0, null);
                    if (packageDialog.open() == CANCEL) {
                        return;
                    }
                    packageFragment = (IPackageFragment) packageDialog.getResult()[0];
                    packageFragmentRoot = (IPackageFragmentRoot) packageFragment.getParent();
                } else if (isEmpty(createSimilarDescriptorClass.getSubPackage())
                        && packageFragment.hasSubpackages()) {
                    final List<IPackageFragment> subPackages = new ArrayList<IPackageFragment>();
                    for (final IJavaElement chldPkgFragment : packageFragmentRoot.getChildren()) {
                        if (chldPkgFragment instanceof IPackageFragment && chldPkgFragment.getElementName()
                                .startsWith(packageFragment.getElementName())) {
                            subPackages.add((IPackageFragment) chldPkgFragment);
                        }
                    }
                    if (!subPackages.isEmpty()) {
                        final PackageSelectionDialog selectionDialog = new PackageSelectionDialog(shell,
                                "Sub Package", "Choose the sub pacage from below",
                                subPackages.toArray(new IPackageFragment[0]));
                        if (selectionDialog.open() != CANCEL) {
                            packageFragment = (IPackageFragment) selectionDialog.getFirstResult();
                        }
                    }
                }
            } else {
                if (fastCodeContext.isUnitTest()) {
                    String sourcePath = globalSettings.isUseDefaultForPath()
                            ? globalSettings.getSourcePathTest()
                            : createSimilarDescriptorClass.getSourcePath();
                    sourcePath = getDefaultPathFromProject(fastCodeContext.getFromType().getJavaProject(),
                            "test", sourcePath);
                    packageFragmentRoot = getPackageRootFromProject(
                            fastCodeContext.getFromType().getJavaProject(), sourcePath);
                } else if (!isEmpty(targetProject)) {
                    final String sourcePath = globalSettings.isUseDefaultForPath()
                            ? getPathFromGlobalSettings(
                                    fastCodeContext.getFromType().getJavaProject().getElementName())
                            : createSimilarDescriptorClass.getSourcePath();
                    ;
                    packageFragmentRoot = getPackageRootFromProject(createSimilarDescriptorClass.getProject(),
                            sourcePath);
                } else {
                    packageFragmentRoot = (IPackageFragmentRoot) fastCodeContext.getFromType()
                            .getPackageFragment().getParent();
                    targetProject = packageFragmentRoot.getParent().getElementName();
                }
                final String fullname = fastCodeContext.getFromType().getFullyQualifiedName();
                final String fromPattern = createSimilarDescriptor.getFromPattern();
                if (fromPattern != null) {
                    parseTokens(fromPattern, fullname, fastCodeContext.getPlaceHolders());
                }
                if (packageFragmentRoot == null || !packageFragmentRoot.exists()) {
                    throw new Exception("Unable to find source path for, please check configuration.");
                }
                toName = replacePlaceHolders(toName, fastCodeContext.getPlaceHolders());
                if (createSimilarDescriptor.isDifferentName()) {
                    toName = fullname.replaceAll(createSimilarDescriptor.getReplacePart(),
                            createSimilarDescriptor.getReplaceValue());
                }
                final int lastDotPos = toName.lastIndexOf(DOT_CHAR);
                final String newpkg = lastDotPos != -1 ? toName.substring(0, lastDotPos)
                        : fastCodeContext.getFromType().getPackageFragment().getElementName();
                packageFragment = packageFragmentRoot.getPackageFragment(newpkg);

            }
            tmpPackageFragment = packageFragment;

        }

        if (tmpPackageFragment != null) {
            final List<IType> importTypes = new ArrayList<IType>();

            final IJavaProject javaProject = tmpPackageFragment.getJavaProject();
            final String[] impTypes = replacePlaceHolders(createSimilarDescriptorClass.getImportTypes(),
                    fastCodeContext.getPlaceHolders());
            final String[] superTypes = replacePlaceHolders(createSimilarDescriptorClass.getSuperTypes(),
                    fastCodeContext.getPlaceHolders());
            final String[] implementTypes = replacePlaceHolders(
                    createSimilarDescriptorClass.getImplementTypes(), fastCodeContext.getPlaceHolders());
            gatherImports(javaProject, impTypes, IJavaElementSearchConstants.CONSIDER_ALL_TYPES, "import",
                    importTypes);
            final List<IType> implTypes = gatherImports(javaProject, implementTypes,
                    IJavaElementSearchConstants.CONSIDER_INTERFACES, "implement", importTypes);
            // createSimilarDescriptorClass.setUserInputInterface(implTypes);
            // createSimilarDescriptorClass.setUserInputImports(importTypes);
            if (superTypes != null && superTypes.length > 0) {

                final FastCodeType[] fastCodeTypes = new FastCodeType[superTypes.length];
                int i = 0;
                for (final String superType : superTypes) {
                    fastCodeTypes[i++] = parseType(superType, null);
                }
                final ClassSelectionDialog classSelectionDialog = new ClassSelectionDialog(shell, "Super Class",
                        "Choose the classes to extend", fastCodeTypes, false);
                final int ret = classSelectionDialog.open();
                if (ret != CANCEL) {
                    // final FastCodeType fastCodeType = (FastCodeType)
                    // classSelectionDialog.getResult()[0];
                    supType = tmpPackageFragment.getJavaProject()
                            .findType(classSelectionDialog.getResult()[0].toString());

                    if (supType != null && supType.exists()) {
                        // superClass = flattenType(fastCodeType, false);
                        superClass = superClass.replace(supType.getFullyQualifiedName(),
                                supType.getElementName());
                        // placeHolders.put("super_class", superClass);
                        if (!supType.isBinary() && supType.getCompilationUnit() != null
                                && supType.getCompilationUnit().exists()) {
                            fastCodeContext.addResource(new FastCodeResource(supType.getResource()));
                        }
                    }
                }

            }

            if (createSimilarDescriptorClass.isCreateFields()) {
                final SelectionDialog selectionDialog = JavaUI.createTypeDialog(shell, null,
                        SearchEngine.createWorkspaceScope(),
                        IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES, true,
                        createSimilarDescriptorClass.getCreateFieldsName());
                selectionDialog.setMessage("Please select one or more classes to create field.");
                selectionDialog.setTitle("Select Class");

                if (selectionDialog.open() != CANCEL) {
                    int i = 0;
                    final Object[] tmpArray = selectionDialog.getResult();
                    fldTypeArr = new IType[tmpArray.length];
                    for (final Object type : tmpArray) {
                        final IType fldType = (IType) type;
                        if (isAbstract(fldType.getFlags())) {
                            openWarning(shell, "Warning",
                                    "Cannot make an instance of an abstract class " + fldType.getElementName());
                            continue;
                        }
                        fldTypeArr[i] = fldType;
                        i++;
                    }

                }
            }
            createSimilarDescUserChoice[k] = new CreateSimilarDescriptorClass.Builder()
                    .withUserInputPackage(packageFragment).withUserInputImports(importTypes)
                    .withUserInputInterface(implTypes).withSuperClass(superClass)
                    .withUserInputFieldTypes(fldTypeArr)
                    .withCreateFields(createSimilarDescriptorClass.isCreateFields())
                    .withClassAnnotations(createSimilarDescriptorClass.getClassAnnotations())
                    .withClassBody(createSimilarDescriptorClass.getClassBody())
                    .withClassHeader(createSimilarDescriptorClass.getClassHeader())
                    .withClassInsideBody(createSimilarDescriptorClass.getClassInsideBody())
                    .withClassType(createSimilarDescriptorClass.getClassType())
                    .withConvertMethodParam(createSimilarDescriptorClass.isConvertMethodParam())
                    .withConvertMethodParamFrom(createSimilarDescriptorClass.getConvertMethodParamFrom())
                    .withConvertMethodParamTo(createSimilarDescriptorClass.getConvertMethodParamTo())
                    .withCreateDefaultConstructor(createSimilarDescriptorClass.isCreateDefaultConstructor())
                    .withCreateEqualsHashcode(
                            isMultiple ? false : createSimilarDescriptorClass.isCreateEqualsHashcode())
                    .withCreateFieldsName(createSimilarDescriptorClass.getCreateFieldsName())
                    .withCreateInstanceConstructor(
                            isMultiple ? false : createSimilarDescriptorClass.isCreateInstanceConstructor())
                    .withCreateMethodBody(createSimilarDescriptorClass.isCreateMethodBody())
                    .withCreateToString(isMultiple ? false : createSimilarDescriptorClass.isCreateToString())
                    .withCreateUnitTest(createSimilarDescriptorClass.isCreateUnitTest())
                    .withFieldAnnotations(createSimilarDescriptorClass.getFieldAnnotations())
                    .withFinalClass(createSimilarDescriptorClass.isFinalClass())
                    .withImplementTypes(createSimilarDescriptorClass.getImplementTypes())
                    .withImportTypes(createSimilarDescriptorClass.getImportTypes())
                    .withInclGetterSetterForInstance(
                            createSimilarDescriptorClass.isInclGetterSetterForInstance())
                    .withMethodAnnotations(createSimilarDescriptorClass.getMethodAnnotations())
                    .withPackge(createSimilarDescriptorClass.getPackge())
                    .withProject(createSimilarDescriptorClass.getProject())
                    .withRelationTypeToParent(createSimilarDescriptorClass.getRelationTypeToParent())
                    .withSourcePath(createSimilarDescriptorClass.getSourcePath())
                    .withSubPackage(createSimilarDescriptorClass.getSubPackage())
                    .withSuperTypes(createSimilarDescriptorClass.getSuperTypes())
                    .withUserInputSuperClass(supType).withToPattern(createSimilarDescriptorClass.getToPattern())
                    .withInclInstance(createSimilarDescriptorClass.isInclInstance())
                    .withRelatedDescriptors(new ArrayList<CreateSimilarDescriptorClass>()).build();

            k++;

        }
    }
    if (tmpPackageFragment != null) {
        createSimilarDescriptor.setCreateSimilarDescUserChoice(createSimilarDescUserChoice);
        createSimilarDescriptor.numbersOfCreateSimilarDescUserChoiceClasses(createSimilarDescUserChoice);
    }
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param file//  www.  ja v a 2  s .  c om
 * @param type
 * @return
 * @throws JavaModelException
 */

public static boolean isFileReferenced(final IFile file, final int type) throws JavaModelException {
    final IProject prj = file.getProject();
    final IJavaProject javaProject = JavaCore.create(prj);
    /*
    final IPath path = file.getFullPath();
    */
    final IJavaElement element = JavaCore.create(file);
    if (element instanceof ICompilationUnit) {
        final IType iType = javaProject
                .findType(((ICompilationUnit) element).findPrimaryType().getFullyQualifiedName());

        final IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
        final List<IJavaElement> javaElements = new SearchUtil().search(iType, type, scope);
        if (javaElements.size() > 0) {
            return true;
            /*for (final IJavaElement element : javaElements) {
               if (type == IJavaSearchConstants.TYPE || type == IJavaSearchConstants.METHOD) {
                  return true;
               }
            }*/
        }
    }
    return false;

}

From source file:org.fusesource.ide.camel.editor.globalconfiguration.beans.BeanConfigUtil.java

License:Open Source License

public String handleClassBrowse(IProject project, Shell shell) {
    IJavaSearchScope scope = null;/*from ww w  .j  av a 2  s . co m*/
    if (project != null) {
        IJavaProject jproject = JavaCore.create(project);
        if (jproject == null) {
            scope = SearchEngine.createWorkspaceScope();
        } else {
            scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { jproject });
        }
    }

    try {
        SelectionDialog dialog = JavaUI.createTypeDialog(shell, null, scope,
                IJavaElementSearchConstants.CONSIDER_CLASSES, false, "*Bean"); //$NON-NLS-1$
        if (dialog.open() == SelectionDialog.OK) {
            Object[] result = dialog.getResult();
            if (result.length > 0 && result[0] instanceof IType) {
                return ((IType) result[0]).getFullyQualifiedName();
            }
        }
    } catch (JavaModelException e) {
        CamelEditorUIActivator.pluginLog().logError(e);
    }
    return null;
}

From source file:org.fusesource.ide.jvmmonitor.internal.ui.actions.OpenDeclarationAction.java

License:Open Source License

/**
 * Searches the source for the given class name.
 * //from w  ww .j a  v a2s  . com
 * @param name
 *            The class name
 * @return The source
 * @throws CoreException
 */
IType doSearchSource(String name) throws CoreException {
    final List<IType> results = new ArrayList<IType>();

    // create requester
    SearchRequestor requestor = new SearchRequestor() {
        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (element instanceof IType) {
                results.add((IType) element);
            }
        }
    };

    String baseName = name.replace('$', '.');

    // create search engine and pattern
    SearchEngine engine = new SearchEngine();
    SearchPattern pattern = SearchPattern.createPattern(baseName, IJavaSearchConstants.TYPE,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);

    // search the source for the given name
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            SearchEngine.createWorkspaceScope(), requestor, null);

    if (results.size() > 0) {
        // at most one source should be found
        return results.get(0);
    }

    return null;
}

From source file:org.gemoc.execution.sequential.javaengine.PlainK3ExecutionEngine.java

License:Open Source License

/**
 * search the bundle that contains the Main class. The search is done in the workspace scope (ie. if it is defined
 * in the current workspace it will find it
 * /*from  w w  w  .  j av  a 2  s.  co  m*/
 * @return the name of the bundle containing the Main class or null if not found
 */
private IType getITypeMainByWorkspaceScope(String className) {
    SearchPattern pattern = SearchPattern.createPattern(className, IJavaSearchConstants.CLASS,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
    IJavaSearchScope scope = SearchEngine.createWorkspaceScope();

    final List<IType> binaryType = new ArrayList<IType>();

    SearchRequestor requestor = new SearchRequestor() {
        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            binaryType.add((IType) match.getElement());
        }
    };
    SearchEngine engine = new SearchEngine();

    try {
        engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                requestor, null);
    } catch (CoreException e1) {
        throw new RuntimeException("Error while searching the bundle: " + e1.getMessage());
        // return new Status(IStatus.ERROR, Activator.PLUGIN_ID, );
    }

    return binaryType.isEmpty() ? null : binaryType.get(0);
}

From source file:org.gemoc.execution.sequential.javaxdsml.presentation.GemocXDSMLFormComposite.java

License:Open Source License

/**
 * Creates the listeners in charge of the behavior for the buttons
 *///from   www  .j a  v a 2 s.c  om
protected void initButtonListeners(Button btnBrowseEMFProject, Button btnBrowseGenmodel,
        Button btSelectRootModelElement, Button btnBrowseXtextEditor, Button btnBrowseSiriusEditor,
        Button btnBrowseSiriusAnimator, Button btnBrowseDSAProject, Button btnBrowseEntryPoint) {
    btnBrowseEMFProject.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.Selection:
                SelectEMFIProjectDialog dialog = new SelectEMFIProjectDialog(
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                int res = dialog.open();
                if (res == WizardDialog.OK) {
                    // update the project model
                    txtEMFProject.setText(((IProject) dialog.getResult()[0]).getName());
                }
                break;
            }
        }
    });

    btnBrowseGenmodel.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.Selection:
                SelectAnyIFileDialog dialog = new SelectAnyIFileDialog();
                dialog.setPattern("*.genmodel");
                if (dialog.open() == Dialog.OK) {
                    txtGenmodel.setText("platform:/resource"
                            + ((IResource) dialog.getResult()[0]).getFullPath().toString());
                }
                break;
            }
        }
    });
    btSelectRootModelElement.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.Selection:
                String xdsmlURIAsString = xdsmlWrappedObject.languageDefinition.getDomainModelProject()
                        .getGenmodeluri();
                if (xdsmlURIAsString != null) {
                    LabelProvider labelProvider = new ENamedElementQualifiedNameLabelProvider();
                    ResourceSet resSet = new ResourceSetImpl();
                    resSet.getResource(URI.createURI(xdsmlURIAsString), true);
                    SelectAnyEObjectDialog dialog = new SelectAnyConcreteEClassDialog(resSet, labelProvider);
                    int res = dialog.open();
                    if (res == WizardDialog.OK) {
                        // update the project model
                        // xdsmlWrappedObject.setRootContainerModelElement(labelProvider.getText(dialog.getFirstResult()));
                        txtRootContainerModelElement.setText(labelProvider.getText(dialog.getFirstResult()));
                    }
                }
                break;
            }
        }
    });

    btnBrowseXtextEditor.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.Selection:
                SelectXtextIProjectDialog dialog = new SelectXtextIProjectDialog(
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                int res = dialog.open();
                if (res == WizardDialog.OK) {
                    // update the project model
                    txtXTextEditorProject.setText(((IProject) dialog.getResult()[0]).getName());
                }
                break;
            }
        }
    });
    btnBrowseSiriusEditor.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.Selection:
                SelectODesignIProjectDialog dialog = new SelectODesignIProjectDialog(
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                int res = dialog.open();
                if (res == WizardDialog.OK) {
                    // update the project model
                    txtSiriusEditorProject.setText(((IProject) dialog.getResult()[0]).getName());
                }
                break;
            }
        }
    });
    btnBrowseSiriusAnimator.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.Selection:
                SelectODesignIProjectDialog dialog = new SelectODesignIProjectDialog(
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                int res = dialog.open();
                if (res == WizardDialog.OK) {
                    // update the project model
                    txtSiriusAnimationProject.setText(((IProject) dialog.getResult()[0]).getName());
                }
                break;
            }
        }
    });
    btnBrowseDSAProject.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            switch (e.type) {
            case SWT.Selection:
                SelectDSAIProjectDialog dialog = new SelectDSAIProjectDialog(
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                int res = dialog.open();
                if (res == WizardDialog.OK) {
                    // update the project model
                    txtDSAProject.setText(((IProject) dialog.getResult()[0]).getName());
                }
                break;
            }
        }
    });

    btnBrowseEntryPoint.addSelectionListener(new SelectionAdapter() {
        @SuppressWarnings("restriction")
        @Override
        public void widgetSelected(SelectionEvent e) {
            IJavaSearchScope searchScope = SearchEngine.createWorkspaceScope();
            IRunnableContext c = new BusyIndicatorRunnableContext();
            SelectionDialog dialog;
            try {
                dialog = JavaUI.createTypeDialog(getShell(), c, searchScope,
                        IJavaElementSearchConstants.CONSIDER_CLASSES, false);

                dialog.open();
                if (dialog.getReturnCode() == Dialog.OK) {
                    IType type = (IType) dialog.getResult()[0];
                    txtEntryPoint.setText(type.getFullyQualifiedName());

                }
            } catch (JavaModelException e1) {
                Activator.error(e1.getMessage(), e1);
            }
        }
    });

}