Search in sources :

Example 1 with SynchronizableDocument

use of org.eclipse.core.internal.filebuffers.SynchronizableDocument in project jbosstools-hibernate by jbosstools.

the class JPAMapToolActor method updateSelectedItems.

/**
 * @param sel - current selected workspace element for processing
 */
private synchronized void updateSelectedItems(ISelection sel) {
    // System.out.println("Blah! " + selection); //$NON-NLS-1$
    if (sel instanceof TextSelection) {
        // $NON-NLS-1$
        String fullyQualifiedName = "";
        IDocument fDocument = null;
        SynchronizableDocument sDocument = null;
        org.eclipse.jdt.core.dom.CompilationUnit resultCU = null;
        Class<?> clazz = sel.getClass();
        Field fd = null;
        try {
            // $NON-NLS-1$
            fd = clazz.getDeclaredField("fDocument");
        } catch (NoSuchFieldException e) {
        // just ignore it!
        }
        if (fd != null) {
            try {
                fd.setAccessible(true);
                fDocument = (IDocument) fd.get(sel);
                if (fDocument instanceof SynchronizableDocument) {
                    sDocument = (SynchronizableDocument) fDocument;
                }
                if (sDocument != null) {
                    ASTParser parser = ASTParser.newParser(AST.JLS9);
                    parser.setSource(sDocument.get().toCharArray());
                    parser.setResolveBindings(false);
                    resultCU = (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null);
                }
                if (resultCU != null && resultCU.types().size() > 0) {
                    if (resultCU.getPackage() != null) {
                        // $NON-NLS-1$
                        fullyQualifiedName = resultCU.getPackage().getName().getFullyQualifiedName() + ".";
                    } else {
                        // $NON-NLS-1$
                        fullyQualifiedName = "";
                    }
                    Object tmp = resultCU.types().get(0);
                    if (tmp instanceof AbstractTypeDeclaration) {
                        fullyQualifiedName += ((AbstractTypeDeclaration) tmp).getName();
                    }
                    if (!(tmp instanceof TypeDeclaration)) {
                        // ignore EnumDeclaration & AnnotationTypeDeclaration
                        // $NON-NLS-1$
                        fullyQualifiedName = "";
                    }
                }
            } catch (IllegalArgumentException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("IllegalArgumentException: ", e);
            } catch (IllegalAccessException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("IllegalAccessException: ", e);
            } catch (SecurityException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("SecurityException: ", e);
            }
        }
        clearSelectionCU();
        if (fullyQualifiedName.length() > 0) {
            ICompilationUnit cu = Utils.findCompilationUnit(fullyQualifiedName);
            addCompilationUnit(cu);
        }
    } else if (sel instanceof TreeSelection) {
        clearSelectionCU();
        TreeSelection treeSelection = (TreeSelection) sel;
        Iterator<?> it = treeSelection.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            processJavaElements(obj);
        }
    } else {
        // System.out.println("2 Blah! " + selection); //$NON-NLS-1$
        sel = null;
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextSelection(org.eclipse.jface.text.TextSelection) Field(java.lang.reflect.Field) SynchronizableDocument(org.eclipse.core.internal.filebuffers.SynchronizableDocument) TreeSelection(org.eclipse.jface.viewers.TreeSelection) Iterator(java.util.Iterator) ASTParser(org.eclipse.jdt.core.dom.ASTParser) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) IDocument(org.eclipse.jface.text.IDocument) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

Field (java.lang.reflect.Field)1 Iterator (java.util.Iterator)1 SynchronizableDocument (org.eclipse.core.internal.filebuffers.SynchronizableDocument)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 ASTParser (org.eclipse.jdt.core.dom.ASTParser)1 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)1 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)1 IDocument (org.eclipse.jface.text.IDocument)1 TextSelection (org.eclipse.jface.text.TextSelection)1 TreeSelection (org.eclipse.jface.viewers.TreeSelection)1