Search in sources :

Example 1 with JavaTypeConstraintService

use of org.eclipse.sapphire.java.JavaTypeConstraintService in project liferay-ide by liferay.

the class HierarchyJavaTypeBrowseActionHandler method browse.

@Override
public String browse(Presentation context) {
    Element element = getModelElement();
    Property property = property();
    IProject project = element.adapt(IProject.class);
    try {
        JavaTypeConstraintService typeService = property.service(JavaTypeConstraintService.class);
        EnumSet<JavaTypeKind> kinds = EnumSet.noneOf(JavaTypeKind.class);
        kinds.addAll(typeService.kinds());
        int browseDialogStyle = IJavaElementSearchConstants.CONSIDER_ALL_TYPES;
        int count = kinds.size();
        if (count == 1) {
            JavaTypeKind kind = kinds.iterator().next();
            switch(kind) {
                case CLASS:
                    browseDialogStyle = IJavaElementSearchConstants.CONSIDER_CLASSES;
                    break;
                case ABSTRACT_CLASS:
                    browseDialogStyle = IJavaElementSearchConstants.CONSIDER_CLASSES;
                    break;
                case INTERFACE:
                    browseDialogStyle = IJavaElementSearchConstants.CONSIDER_INTERFACES;
                    break;
                case ANNOTATION:
                    browseDialogStyle = IJavaElementSearchConstants.CONSIDER_ANNOTATION_TYPES;
                    break;
                case ENUM:
                    browseDialogStyle = IJavaElementSearchConstants.CONSIDER_ENUMS;
                    break;
                default:
                    throw new IllegalStateException();
            }
        } else if (count == 2) {
            if (kinds.contains(JavaTypeKind.CLASS) || kinds.contains(JavaTypeKind.ABSTRACT_CLASS)) {
                if (kinds.contains(JavaTypeKind.INTERFACE)) {
                    browseDialogStyle = IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES;
                } else if (kinds.contains(JavaTypeKind.ENUM)) {
                    browseDialogStyle = IJavaElementSearchConstants.CONSIDER_CLASSES_AND_ENUMS;
                }
            }
        }
        IJavaSearchScope scope = null;
        IType type = JavaCore.create(project).findType(_typeName);
        if (type != null) {
            scope = SearchEngine.createHierarchyScope(type);
        }
        SwtPresentation swt = (SwtPresentation) context;
        SelectionDialog dlg = JavaUI.createTypeDialog(swt.shell(), null, scope, browseDialogStyle, false, _filter, null);
        String title = property.definition().getLabel(true, CapitalizationType.TITLE_STYLE, false);
        dlg.setTitle(Msgs.select + title);
        if (dlg.open() == SelectionDialog.OK) {
            Object[] results = dlg.getResult();
            assert (results != null) && (results.length == 1);
            if (results[0] instanceof IType) {
                return ((IType) results[0]).getFullyQualifiedName();
            }
        }
    } catch (JavaModelException jme) {
        HookUI.logError(jme);
    }
    return null;
}
Also used : JavaTypeKind(org.eclipse.sapphire.java.JavaTypeKind) JavaModelException(org.eclipse.jdt.core.JavaModelException) Element(org.eclipse.sapphire.Element) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) IProject(org.eclipse.core.resources.IProject) IType(org.eclipse.jdt.core.IType) JavaTypeConstraintService(org.eclipse.sapphire.java.JavaTypeConstraintService) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SwtPresentation(org.eclipse.sapphire.ui.forms.swt.SwtPresentation) Property(org.eclipse.sapphire.Property)

Aggregations

IProject (org.eclipse.core.resources.IProject)1 IType (org.eclipse.jdt.core.IType)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)1 Element (org.eclipse.sapphire.Element)1 Property (org.eclipse.sapphire.Property)1 JavaTypeConstraintService (org.eclipse.sapphire.java.JavaTypeConstraintService)1 JavaTypeKind (org.eclipse.sapphire.java.JavaTypeKind)1 SwtPresentation (org.eclipse.sapphire.ui.forms.swt.SwtPresentation)1 SelectionDialog (org.eclipse.ui.dialogs.SelectionDialog)1