Example usage for org.eclipse.jdt.core ITypeHierarchy getAllSubtypes

List of usage examples for org.eclipse.jdt.core ITypeHierarchy getAllSubtypes

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ITypeHierarchy getAllSubtypes.

Prototype

IType[] getAllSubtypes(IType type);

Source Link

Document

Returns all resolved subtypes (direct and indirect) of the given type, in no particular order, limited to the types in this type hierarchy's graph.

Usage

From source file:com.aliyun.odps.eclipse.launch.configuration.udf.UDFSearchEngine.java

License:Apache License

/**
 * Adds subtypes and enclosed types to the listing of 'found' types
 * /*from  w w  w  . ja  v  a2 s  . co m*/
 * @param types the list of found types thus far
 * @param monitor progress monitor
 * @param scope the scope of elements
 * @return as set of all types to consider
 */
private Set addSubtypes(List types, IProgressMonitor monitor, IJavaSearchScope scope) {
    Iterator iterator = types.iterator();
    Set result = new HashSet(types.size());
    IType type = null;
    ITypeHierarchy hierarchy = null;
    IType[] subtypes = null;
    while (iterator.hasNext()) {
        type = (IType) iterator.next();
        if (result.add(type)) {
            try {
                hierarchy = type.newTypeHierarchy(monitor);
                subtypes = hierarchy.getAllSubtypes(type);
                for (int i = 0; i < subtypes.length; i++) {
                    if (scope.encloses(subtypes[i])) {
                        result.add(subtypes[i]);
                    }
                }
            } catch (JavaModelException e) {
                JDIDebugUIPlugin.log(e);
            }
        }
        monitor.worked(1);
    }
    return result;
}

From source file:com.android.ide.eclipse.adt.internal.editors.layout.gre.ClientRulesEngine.java

License:Open Source License

@Override
public String displayFragmentSourceInput() {
    try {/*from  ww w.j a  v  a  2s .  c  o  m*/
        // Compute a search scope: We need to merge all the subclasses
        // android.app.Fragment and android.support.v4.app.Fragment
        IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
        IProject project = mRulesEngine.getProject();
        final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        if (javaProject != null) {
            IType oldFragmentType = javaProject.findType(CLASS_V4_FRAGMENT);

            // First check to make sure fragments are available, and if not,
            // warn the user.
            IAndroidTarget target = Sdk.getCurrent().getTarget(project);
            // No, this should be using the min SDK instead!
            if (target.getVersion().getApiLevel() < 11 && oldFragmentType == null) {
                // Compatibility library must be present
                MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(),
                        "Fragment Warning", null,
                        "Fragments require API level 11 or higher, or a compatibility "
                                + "library for older versions.\n\n"
                                + " Do you want to install the compatibility library?",
                        MessageDialog.QUESTION, new String[] { "Install", "Cancel" },
                        1 /* default button: Cancel */);
                int answer = dialog.open();
                if (answer == 0) {
                    if (!AddSupportJarAction.install(project)) {
                        return null;
                    }
                } else {
                    return null;
                }
            }

            // Look up sub-types of each (new fragment class and compatibility fragment
            // class, if any) and merge the two arrays - then create a scope from these
            // elements.
            IType[] fragmentTypes = new IType[0];
            IType[] oldFragmentTypes = new IType[0];
            if (oldFragmentType != null) {
                ITypeHierarchy hierarchy = oldFragmentType.newTypeHierarchy(new NullProgressMonitor());
                oldFragmentTypes = hierarchy.getAllSubtypes(oldFragmentType);
            }
            IType fragmentType = javaProject.findType(CLASS_FRAGMENT);
            if (fragmentType != null) {
                ITypeHierarchy hierarchy = fragmentType.newTypeHierarchy(new NullProgressMonitor());
                fragmentTypes = hierarchy.getAllSubtypes(fragmentType);
            }
            IType[] subTypes = new IType[fragmentTypes.length + oldFragmentTypes.length];
            System.arraycopy(fragmentTypes, 0, subTypes, 0, fragmentTypes.length);
            System.arraycopy(oldFragmentTypes, 0, subTypes, fragmentTypes.length, oldFragmentTypes.length);
            scope = SearchEngine.createJavaSearchScope(subTypes, IJavaSearchScope.SOURCES);
        }

        Shell parent = AdtPlugin.getShell();
        final AtomicReference<String> returnValue = new AtomicReference<String>();
        final AtomicReference<SelectionDialog> dialogHolder = new AtomicReference<SelectionDialog>();
        final SelectionDialog dialog = JavaUI.createTypeDialog(parent, new ProgressMonitorDialog(parent), scope,
                IJavaElementSearchConstants.CONSIDER_CLASSES, false,
                // Use ? as a default filter to fill dialog with matches
                "?", //$NON-NLS-1$
                new TypeSelectionExtension() {
                    @Override
                    public Control createContentArea(Composite parentComposite) {
                        Composite composite = new Composite(parentComposite, SWT.NONE);
                        composite.setLayout(new GridLayout(1, false));
                        Button button = new Button(composite, SWT.PUSH);
                        button.setText("Create New...");
                        button.addSelectionListener(new SelectionAdapter() {
                            @Override
                            public void widgetSelected(SelectionEvent e) {
                                String fqcn = createNewFragmentClass(javaProject);
                                if (fqcn != null) {
                                    returnValue.set(fqcn);
                                    dialogHolder.get().close();
                                }
                            }
                        });
                        return composite;
                    }

                    @Override
                    public ITypeInfoFilterExtension getFilterExtension() {
                        return new ITypeInfoFilterExtension() {
                            @Override
                            public boolean select(ITypeInfoRequestor typeInfoRequestor) {
                                int modifiers = typeInfoRequestor.getModifiers();
                                if (!Flags.isPublic(modifiers) || Flags.isInterface(modifiers)
                                        || Flags.isEnum(modifiers) || Flags.isAbstract(modifiers)) {
                                    return false;
                                }
                                return true;
                            }
                        };
                    }
                });
        dialogHolder.set(dialog);

        dialog.setTitle("Choose Fragment Class");
        dialog.setMessage("Select a Fragment class (? = any character, * = any string):");
        if (dialog.open() == IDialogConstants.CANCEL_ID) {
            return null;
        }
        if (returnValue.get() != null) {
            return returnValue.get();
        }

        Object[] types = dialog.getResult();
        if (types != null && types.length > 0) {
            return ((IType) types[0]).getFullyQualifiedName();
        }
    } catch (JavaModelException e) {
        AdtPlugin.log(e, null);
    } catch (CoreException e) {
        AdtPlugin.log(e, null);
    }
    return null;
}

From source file:com.android.ide.eclipse.adt.internal.editors.layout.gre.ClientRulesEngine.java

License:Open Source License

@Override
public String displayCustomViewClassInput() {
    try {//from  w ww .j  a  va2s. c  o  m
        IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
        IProject project = mRulesEngine.getProject();
        final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        if (javaProject != null) {
            // Look up sub-types of each (new fragment class and compatibility fragment
            // class, if any) and merge the two arrays - then create a scope from these
            // elements.
            IType[] viewTypes = new IType[0];
            IType fragmentType = javaProject.findType(CLASS_VIEW);
            if (fragmentType != null) {
                ITypeHierarchy hierarchy = fragmentType.newTypeHierarchy(new NullProgressMonitor());
                viewTypes = hierarchy.getAllSubtypes(fragmentType);
            }
            scope = SearchEngine.createJavaSearchScope(viewTypes, IJavaSearchScope.SOURCES);
        }

        Shell parent = AdtPlugin.getShell();
        final AtomicReference<String> returnValue = new AtomicReference<String>();
        final AtomicReference<SelectionDialog> dialogHolder = new AtomicReference<SelectionDialog>();
        final SelectionDialog dialog = JavaUI.createTypeDialog(parent, new ProgressMonitorDialog(parent), scope,
                IJavaElementSearchConstants.CONSIDER_CLASSES, false,
                // Use ? as a default filter to fill dialog with matches
                "?", //$NON-NLS-1$
                new TypeSelectionExtension() {
                    @Override
                    public Control createContentArea(Composite parentComposite) {
                        Composite composite = new Composite(parentComposite, SWT.NONE);
                        composite.setLayout(new GridLayout(1, false));
                        Button button = new Button(composite, SWT.PUSH);
                        button.setText("Create New...");
                        button.addSelectionListener(new SelectionAdapter() {
                            @Override
                            public void widgetSelected(SelectionEvent e) {
                                String fqcn = createNewCustomViewClass(javaProject);
                                if (fqcn != null) {
                                    returnValue.set(fqcn);
                                    dialogHolder.get().close();
                                }
                            }
                        });
                        return composite;
                    }

                    @Override
                    public ITypeInfoFilterExtension getFilterExtension() {
                        return new ITypeInfoFilterExtension() {
                            @Override
                            public boolean select(ITypeInfoRequestor typeInfoRequestor) {
                                int modifiers = typeInfoRequestor.getModifiers();
                                if (!Flags.isPublic(modifiers) || Flags.isInterface(modifiers)
                                        || Flags.isEnum(modifiers) || Flags.isAbstract(modifiers)) {
                                    return false;
                                }
                                return true;
                            }
                        };
                    }
                });
        dialogHolder.set(dialog);

        dialog.setTitle("Choose Custom View Class");
        dialog.setMessage("Select a Custom View class (? = any character, * = any string):");
        if (dialog.open() == IDialogConstants.CANCEL_ID) {
            return null;
        }
        if (returnValue.get() != null) {
            return returnValue.get();
        }

        Object[] types = dialog.getResult();
        if (types != null && types.length > 0) {
            return ((IType) types[0]).getFullyQualifiedName();
        }
    } catch (JavaModelException e) {
        AdtPlugin.log(e, null);
    } catch (CoreException e) {
        AdtPlugin.log(e, null);
    }
    return null;
}

From source file:com.android.ide.eclipse.adt.internal.editors.layout.LayoutContentAssist.java

License:Open Source License

@Override
protected boolean computeAttributeValues(List<ICompletionProposal> proposals, int offset, String parentTagName,
        String attributeName, Node node, String wordPrefix, boolean skipEndTag, int replaceLength) {
    super.computeAttributeValues(proposals, offset, parentTagName, attributeName, node, wordPrefix, skipEndTag,
            replaceLength);// w  w w.j a  va2 s  . co  m

    boolean projectOnly = false;
    List<String> superClasses = null;
    if (VIEW_FRAGMENT.equals(parentTagName)
            && (attributeName.endsWith(ATTR_NAME) || attributeName.equals(ATTR_CLASS))) {
        // Insert fragment class matches
        superClasses = Arrays.asList(CLASS_V4_FRAGMENT, CLASS_FRAGMENT);
    } else if (VIEW_TAG.equals(parentTagName) && attributeName.endsWith(ATTR_CLASS)) {
        // Insert custom view matches
        superClasses = Collections.singletonList(CLASS_VIEW);
        projectOnly = true;
    } else if (attributeName.endsWith(ATTR_CONTEXT)) {
        // Insert activity matches
        superClasses = Collections.singletonList(CLASS_ACTIVITY);
    }

    if (superClasses != null) {
        IProject project = mEditor.getProject();
        if (project == null) {
            return false;
        }
        try {
            IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
            IType type = javaProject.findType(superClasses.get(0));
            Set<IType> elements = new HashSet<IType>();
            if (type != null) {
                ITypeHierarchy hierarchy = type.newTypeHierarchy(new NullProgressMonitor());
                IType[] allSubtypes = hierarchy.getAllSubtypes(type);
                for (IType subType : allSubtypes) {
                    if (!projectOnly || subType.getResource() != null) {
                        elements.add(subType);
                    }
                }
            }
            assert superClasses.size() <= 2; // If more, need to do additional work below
            if (superClasses.size() == 2) {
                type = javaProject.findType(superClasses.get(1));
                if (type != null) {
                    ITypeHierarchy hierarchy = type.newTypeHierarchy(new NullProgressMonitor());
                    IType[] allSubtypes = hierarchy.getAllSubtypes(type);
                    for (IType subType : allSubtypes) {
                        if (!projectOnly || subType.getResource() != null) {
                            elements.add(subType);
                        }
                    }
                }
            }

            List<IType> sorted = new ArrayList<IType>(elements);
            Collections.sort(sorted, new Comparator<IType>() {
                @Override
                public int compare(IType type1, IType type2) {
                    String fqcn1 = type1.getFullyQualifiedName();
                    String fqcn2 = type2.getFullyQualifiedName();
                    int category1 = fqcn1.startsWith(ANDROID_PKG_PREFIX) ? 1 : -1;
                    int category2 = fqcn2.startsWith(ANDROID_PKG_PREFIX) ? 1 : -1;
                    if (category1 != category2) {
                        return category1 - category2;
                    }
                    return fqcn1.compareTo(fqcn2);
                }
            });
            addMatchingProposals(proposals, sorted.toArray(), offset, node, wordPrefix, (char) 0,
                    false /* isAttribute */, false /* isNew */, false /* skipEndTag */, replaceLength);
            return true;
        } catch (CoreException e) {
            AdtPlugin.log(e, null);
        }
    }

    return false;
}

From source file:com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo.java

License:Open Source License

/**
 * Returns all activities found in the given project (including those in libraries,
 * except for android.jar itself)/*from   ww  w.java  2 s . c  om*/
 *
 * @param project the project
 * @return a list of activity classes as fully qualified class names
 */
@SuppressWarnings("restriction") // BinaryType
@NonNull
public static List<String> getProjectActivities(IProject project) {
    final List<String> activities = new ArrayList<String>();
    try {
        final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        if (javaProject != null) {
            IType[] activityTypes = new IType[0];
            IType activityType = javaProject.findType(CLASS_ACTIVITY);
            if (activityType != null) {
                ITypeHierarchy hierarchy = activityType.newTypeHierarchy(javaProject,
                        new NullProgressMonitor());
                activityTypes = hierarchy.getAllSubtypes(activityType);
                for (IType type : activityTypes) {
                    if (type instanceof BinaryType
                            && (type.getClassFile() == null || type.getClassFile().getResource() == null)) {
                        continue;
                    }
                    activities.add(type.getFullyQualifiedName());
                }
            }
        }
    } catch (CoreException e) {
        AdtPlugin.log(e, null);
    }

    return activities;
}

From source file:com.android.ide.eclipse.adt.internal.editors.manifest.model.UiClassAttributeNode.java

License:Open Source License

@Override
public String[] getPossibleValues(String prefix) {
    // Compute a list of existing classes for content assist completion
    IProject project = getProject();//ww  w. j  ava  2 s. c  om
    if (project == null || mReferenceClass == null) {
        return null;
    }

    try {
        IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        IType type = javaProject.findType(mReferenceClass);
        // Use sets because query sometimes repeats the same class
        Set<String> libraryTypes = new HashSet<String>(80);
        Set<String> localTypes = new HashSet<String>(30);
        if (type != null) {
            ITypeHierarchy hierarchy = type.newTypeHierarchy(new NullProgressMonitor());
            IType[] allSubtypes = hierarchy.getAllSubtypes(type);
            for (IType subType : allSubtypes) {
                int flags = subType.getFlags();
                if (Flags.isPublic(flags) && !Flags.isAbstract(flags)) {
                    String fqcn = subType.getFullyQualifiedName();
                    if (subType.getResource() != null) {
                        localTypes.add(fqcn);
                    } else {
                        libraryTypes.add(fqcn);
                    }
                }
            }
        }

        List<String> local = new ArrayList<String>(localTypes);
        List<String> library = new ArrayList<String>(libraryTypes);
        Collections.sort(local);
        Collections.sort(library);
        List<String> combined = new ArrayList<String>(local.size() + library.size());
        combined.addAll(local);
        combined.addAll(library);
        return combined.toArray(new String[combined.size()]);
    } catch (Exception e) {
        AdtPlugin.log(e, null);
    }

    return null;
}

From source file:com.android.ide.eclipse.adt.internal.wizards.templates.NewTemplatePage.java

License:Open Source License

private String chooseActivity() {
    try {/*from   w  ww . j av  a 2s . c om*/
        // Compute a search scope: We need to merge all the subclasses
        // android.app.Fragment and android.support.v4.app.Fragment
        IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
        IProject project = mValues.project;
        IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        IType activityType = null;

        if (javaProject != null) {
            activityType = javaProject.findType(CLASS_ACTIVITY);
        }
        if (activityType == null) {
            IJavaProject[] projects = BaseProjectHelper.getAndroidProjects(null);
            for (IJavaProject p : projects) {
                activityType = p.findType(CLASS_ACTIVITY);
                if (activityType != null) {
                    break;
                }
            }
        }
        if (activityType != null) {
            NullProgressMonitor monitor = new NullProgressMonitor();
            ITypeHierarchy hierarchy = activityType.newTypeHierarchy(monitor);
            IType[] classes = hierarchy.getAllSubtypes(activityType);
            scope = SearchEngine.createJavaSearchScope(classes, IJavaSearchScope.SOURCES);
        }

        Shell parent = AdtPlugin.getShell();
        final SelectionDialog dialog = JavaUI.createTypeDialog(parent, new ProgressMonitorDialog(parent), scope,
                IJavaElementSearchConstants.CONSIDER_CLASSES, false,
                // Use ? as a default filter to fill dialog with matches
                "?", //$NON-NLS-1$
                new TypeSelectionExtension() {
                    @Override
                    public ITypeInfoFilterExtension getFilterExtension() {
                        return new ITypeInfoFilterExtension() {
                            @Override
                            public boolean select(ITypeInfoRequestor typeInfoRequestor) {
                                int modifiers = typeInfoRequestor.getModifiers();
                                if (!Flags.isPublic(modifiers) || Flags.isInterface(modifiers)
                                        || Flags.isEnum(modifiers)) {
                                    return false;
                                }
                                return true;
                            }
                        };
                    }
                });

        dialog.setTitle("Choose Activity Class");
        dialog.setMessage("Select an Activity class (? = any character, * = any string):");
        if (dialog.open() == IDialogConstants.CANCEL_ID) {
            return null;
        }

        Object[] types = dialog.getResult();
        if (types != null && types.length > 0) {
            return ((IType) types[0]).getFullyQualifiedName();
        }
    } catch (JavaModelException e) {
        AdtPlugin.log(e, null);
    } catch (CoreException e) {
        AdtPlugin.log(e, null);
    }
    return null;
}

From source file:com.android.ide.eclipse.auidt.internal.editors.layout.gre.ClientRulesEngine.java

License:Open Source License

@Override
public String displayFragmentSourceInput() {
    try {//from  ww w .  j a va  2 s  .  c om
        // Compute a search scope: We need to merge all the subclasses
        // android.app.Fragment and android.support.v4.app.Fragment
        IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
        IProject project = mRulesEngine.getProject();
        final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        if (javaProject != null) {
            IType oldFragmentType = javaProject.findType(CLASS_V4_FRAGMENT);

            // First check to make sure fragments are available, and if not,
            // warn the user.
            IAndroidTarget target = Sdk.getCurrent().getTarget(project);
            // No, this should be using the min SDK instead!
            if (target.getVersion().getApiLevel() < 11 && oldFragmentType == null) {
                // Compatibility library must be present
                MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(),
                        "Fragment Warning", null,
                        "Fragments require API level 11 or higher, or a compatibility "
                                + "library for older versions.\n\n"
                                + " Do you want to install the compatibility library?",
                        MessageDialog.QUESTION, new String[] { "Install", "Cancel" },
                        1 /* default button: Cancel */);
                int answer = dialog.open();
                if (answer == 0) {
                    if (!AddSupportJarAction.install(project)) {
                        return null;
                    }
                } else {
                    return null;
                }
            }

            // Look up sub-types of each (new fragment class and compatibility fragment
            // class, if any) and merge the two arrays - then create a scope from these
            // elements.
            IType[] fragmentTypes = new IType[0];
            IType[] oldFragmentTypes = new IType[0];
            if (oldFragmentType != null) {
                ITypeHierarchy hierarchy = oldFragmentType.newTypeHierarchy(new NullProgressMonitor());
                oldFragmentTypes = hierarchy.getAllSubtypes(oldFragmentType);
            }
            IType fragmentType = javaProject.findType(CLASS_FRAGMENT);
            if (fragmentType != null) {
                ITypeHierarchy hierarchy = fragmentType.newTypeHierarchy(new NullProgressMonitor());
                fragmentTypes = hierarchy.getAllSubtypes(fragmentType);
            }
            IType[] subTypes = new IType[fragmentTypes.length + oldFragmentTypes.length];
            System.arraycopy(fragmentTypes, 0, subTypes, 0, fragmentTypes.length);
            System.arraycopy(oldFragmentTypes, 0, subTypes, fragmentTypes.length, oldFragmentTypes.length);
            scope = SearchEngine.createJavaSearchScope(subTypes, IJavaSearchScope.SOURCES);
        }

        Shell parent = AdtPlugin.getDisplay().getActiveShell();
        final AtomicReference<String> returnValue = new AtomicReference<String>();
        final AtomicReference<SelectionDialog> dialogHolder = new AtomicReference<SelectionDialog>();
        final SelectionDialog dialog = JavaUI.createTypeDialog(parent, new ProgressMonitorDialog(parent), scope,
                IJavaElementSearchConstants.CONSIDER_CLASSES, false,
                // Use ? as a default filter to fill dialog with matches
                "?", //$NON-NLS-1$
                new TypeSelectionExtension() {
                    @Override
                    public Control createContentArea(Composite parentComposite) {
                        Composite composite = new Composite(parentComposite, SWT.NONE);
                        composite.setLayout(new GridLayout(1, false));
                        Button button = new Button(composite, SWT.PUSH);
                        button.setText("Create New...");
                        button.addSelectionListener(new SelectionAdapter() {
                            @Override
                            public void widgetSelected(SelectionEvent e) {
                                String fqcn = createNewFragmentClass(javaProject);
                                if (fqcn != null) {
                                    returnValue.set(fqcn);
                                    dialogHolder.get().close();
                                }
                            }
                        });
                        return composite;
                    }

                    @Override
                    public ITypeInfoFilterExtension getFilterExtension() {
                        return new ITypeInfoFilterExtension() {
                            @Override
                            public boolean select(ITypeInfoRequestor typeInfoRequestor) {
                                int modifiers = typeInfoRequestor.getModifiers();
                                if (!Flags.isPublic(modifiers) || Flags.isInterface(modifiers)
                                        || Flags.isEnum(modifiers)) {
                                    return false;
                                }
                                return true;
                            }
                        };
                    }
                });
        dialogHolder.set(dialog);

        dialog.setTitle("Choose Fragment Class");
        dialog.setMessage("Select a Fragment class (? = any character, * = any string):");
        if (dialog.open() == IDialogConstants.CANCEL_ID) {
            return null;
        }
        if (returnValue.get() != null) {
            return returnValue.get();
        }

        Object[] types = dialog.getResult();
        if (types != null && types.length > 0) {
            return ((IType) types[0]).getFullyQualifiedName();
        }
    } catch (JavaModelException e) {
        AdtPlugin.log(e, null);
    } catch (CoreException e) {
        AdtPlugin.log(e, null);
    }
    return null;
}

From source file:com.android.ide.eclipse.auidt.internal.editors.manifest.ManifestInfo.java

License:Open Source License

/**
 * Returns all activities found in the given project (including those in libraries,
 * except for android.jar itself)/*from www .j a v  a 2s  .c om*/
 *
 * @param project the project
 * @return a list of activity classes as fully qualified class names
 */
@SuppressWarnings("restriction") // BinaryType
@NonNull
public static List<String> getProjectActivities(IProject project) {
    final List<String> activities = new ArrayList<String>();
    try {
        final IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        if (javaProject != null) {
            IType[] activityTypes = new IType[0];
            IType activityType = javaProject.findType(SdkConstants.CLASS_ACTIVITY);
            if (activityType != null) {
                ITypeHierarchy hierarchy = activityType.newTypeHierarchy(javaProject,
                        new NullProgressMonitor());
                activityTypes = hierarchy.getAllSubtypes(activityType);
                for (IType type : activityTypes) {
                    if (type instanceof BinaryType
                            && (type.getClassFile() == null || type.getClassFile().getResource() == null)) {
                        continue;
                    }
                    activities.add(type.getFullyQualifiedName());
                }
            }
        }
    } catch (CoreException e) {
        AdtPlugin.log(e, null);
    }

    return activities;
}

From source file:com.android.ide.eclipse.auidt.internal.wizards.templates.NewTemplatePage.java

License:Open Source License

private String chooseActivity() {
    try {/*from w  w  w  . j av a2s.co  m*/
        // Compute a search scope: We need to merge all the subclasses
        // android.app.Fragment and android.support.v4.app.Fragment
        IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
        IProject project = mValues.project;
        IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        IType activityType = null;

        if (javaProject != null) {
            activityType = javaProject.findType(CLASS_ACTIVITY);
        }
        if (activityType == null) {
            IJavaProject[] projects = BaseProjectHelper.getAndroidProjects(null);
            for (IJavaProject p : projects) {
                activityType = p.findType(CLASS_ACTIVITY);
                if (activityType != null) {
                    break;
                }
            }
        }
        if (activityType != null) {
            NullProgressMonitor monitor = new NullProgressMonitor();
            ITypeHierarchy hierarchy = activityType.newTypeHierarchy(monitor);
            IType[] classes = hierarchy.getAllSubtypes(activityType);
            scope = SearchEngine.createJavaSearchScope(classes, IJavaSearchScope.SOURCES);
        }

        Shell parent = AdtPlugin.getDisplay().getActiveShell();
        final SelectionDialog dialog = JavaUI.createTypeDialog(parent, new ProgressMonitorDialog(parent), scope,
                IJavaElementSearchConstants.CONSIDER_CLASSES, false,
                // Use ? as a default filter to fill dialog with matches
                "?", //$NON-NLS-1$
                new TypeSelectionExtension() {
                    @Override
                    public ITypeInfoFilterExtension getFilterExtension() {
                        return new ITypeInfoFilterExtension() {
                            @Override
                            public boolean select(ITypeInfoRequestor typeInfoRequestor) {
                                int modifiers = typeInfoRequestor.getModifiers();
                                if (!Flags.isPublic(modifiers) || Flags.isInterface(modifiers)
                                        || Flags.isEnum(modifiers)) {
                                    return false;
                                }
                                return true;
                            }
                        };
                    }
                });

        dialog.setTitle("Choose Activity Class");
        dialog.setMessage("Select an Activity class (? = any character, * = any string):");
        if (dialog.open() == IDialogConstants.CANCEL_ID) {
            return null;
        }

        Object[] types = dialog.getResult();
        if (types != null && types.length > 0) {
            return ((IType) types[0]).getFullyQualifiedName();
        }
    } catch (JavaModelException e) {
        AdtPlugin.log(e, null);
    } catch (CoreException e) {
        AdtPlugin.log(e, null);
    }
    return null;
}