Search in sources :

Example 1 with DClassElement

use of org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement in project intellij-community by JetBrains.

the class DynamicDialog method doOKAction.

@Override
protected void doOKAction() {
    super.doOKAction();
    mySettings.setContainingClassName((String) myClassComboBox.getSelectedItem());
    mySettings.setStatic(myStaticCheckBox.isSelected());
    GrTypeElement typeElement = getEnteredTypeName();
    if (typeElement == null) {
        mySettings.setType(CommonClassNames.JAVA_LANG_OBJECT);
    } else {
        PsiType type = typeElement.getType();
        if (type instanceof PsiPrimitiveType) {
            type = TypesUtil.boxPrimitiveType(type, typeElement.getManager(), ProjectScope.getAllScope(myProject));
        }
        final String typeQualifiedName = type.getCanonicalText();
        if (typeQualifiedName != null) {
            mySettings.setType(typeQualifiedName);
        } else {
            mySettings.setType(type.getPresentableText());
        }
    }
    final Document document = PsiDocumentManager.getInstance(myProject).getDocument(myContext.getContainingFile());
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        UndoManager.getInstance(myProject).undoableActionPerformed(new GlobalUndoableAction(document) {

            @Override
            public void undo() throws UnexpectedUndoException {
                final DItemElement itemElement;
                if (mySettings.isMethod()) {
                    final List<ParamInfo> myPairList = mySettings.getParams();
                    final String[] argumentsTypes = QuickfixUtil.getArgumentsTypes(myPairList);
                    itemElement = myDynamicManager.findConcreteDynamicMethod(mySettings.getContainingClassName(), mySettings.getName(), argumentsTypes);
                } else {
                    itemElement = myDynamicManager.findConcreteDynamicProperty(mySettings.getContainingClassName(), mySettings.getName());
                }
                if (itemElement == null) {
                    Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
                    return;
                }
                final DClassElement classElement = myDynamicManager.getClassElementByItem(itemElement);
                if (classElement == null) {
                    Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
                    return;
                }
                removeElement(itemElement);
                if (classElement.getMethods().isEmpty() && classElement.getProperties().isEmpty()) {
                    myDynamicManager.removeClassElement(classElement);
                }
            }

            @Override
            public void redo() throws UnexpectedUndoException {
                addElement(mySettings);
            }
        });
        addElement(mySettings);
    }, "Add dynamic element", null);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) DClassElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) List(java.util.List) Document(com.intellij.openapi.editor.Document) DItemElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DItemElement)

Example 2 with DClassElement

use of org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement in project intellij-community by JetBrains.

the class GrDynamicImplicitProperty method navigate.

@Override
public void navigate(boolean requestFocus) {
    if (canNavigateToSource()) {
        super.navigate(requestFocus);
        return;
    }
    DynamicToolWindowWrapper.getInstance(myProject).getToolWindow().activate(() -> {
        DynamicToolWindowWrapper toolWindowWrapper = DynamicToolWindowWrapper.getInstance(myProject);
        final TreeTable treeTable = toolWindowWrapper.getTreeTable();
        final ListTreeTableModelOnColumns model = toolWindowWrapper.getTreeTableModel();
        Object root = model.getRoot();
        if (root == null || !(root instanceof DefaultMutableTreeNode))
            return;
        DefaultMutableTreeNode treeRoot = ((DefaultMutableTreeNode) root);
        final PsiClass psiClass = getContainingClassElement();
        if (psiClass == null)
            return;
        final DefaultMutableTreeNode desiredNode;
        DPropertyElement dynamicProperty = null;
        PsiClass trueSuper = null;
        for (PsiClass aSuper : PsiUtil.iterateSupers(psiClass, true)) {
            dynamicProperty = DynamicManager.getInstance(myProject).findConcreteDynamicProperty(aSuper.getQualifiedName(), getName());
            if (dynamicProperty != null) {
                trueSuper = aSuper;
                break;
            }
        }
        if (trueSuper == null)
            return;
        final DefaultMutableTreeNode classNode = TreeUtil.findNodeWithObject(treeRoot, new DClassElement(myProject, trueSuper.getQualifiedName()));
        if (classNode == null)
            return;
        desiredNode = TreeUtil.findNodeWithObject(classNode, dynamicProperty);
        if (desiredNode == null)
            return;
        final TreePath path = TreeUtil.getPathFromRoot(desiredNode);
        treeTable.getTree().expandPath(path);
        treeTable.getTree().setSelectionPath(path);
        treeTable.getTree().fireTreeExpanded(path);
        ToolWindowManager.getInstance(myProject).getFocusManager().requestFocus(treeTable, true);
        treeTable.revalidate();
        treeTable.repaint();
    }, true);
}
Also used : ListTreeTableModelOnColumns(com.intellij.ui.treeStructure.treetable.ListTreeTableModelOnColumns) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DPropertyElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DPropertyElement) DClassElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement) TreePath(javax.swing.tree.TreePath) TreeTable(com.intellij.ui.treeStructure.treetable.TreeTable)

Example 3 with DClassElement

use of org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement in project intellij-community by JetBrains.

the class DynamicTest method getDClassElement.

@NotNull
private DClassElement getDClassElement() {
    final DRootElement rootElement = DynamicManager.getInstance(getProject()).getRootElement();
    final DClassElement classElement = rootElement.getClassElement(getTestName(false));
    assertNotNull(classElement);
    return classElement;
}
Also used : DClassElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement) DRootElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DRootElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with DClassElement

use of org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement in project intellij-community by JetBrains.

the class GrDynamicImplicitMethod method navigate.

@Override
public void navigate(boolean requestFocus) {
    DynamicToolWindowWrapper.getInstance(getProject()).getToolWindow().activate(() -> {
        DynamicToolWindowWrapper toolWindowWrapper = DynamicToolWindowWrapper.getInstance(getProject());
        final TreeTable treeTable = toolWindowWrapper.getTreeTable();
        final ListTreeTableModelOnColumns model = toolWindowWrapper.getTreeTableModel();
        Object root = model.getRoot();
        if (root == null || !(root instanceof DefaultMutableTreeNode))
            return;
        DefaultMutableTreeNode treeRoot = ((DefaultMutableTreeNode) root);
        DefaultMutableTreeNode desiredNode;
        JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
        final PsiClassType fqClassName = facade.getElementFactory().createTypeByFQClassName(myContainingClassName, ProjectScope.getAllScope(getProject()));
        final PsiClass psiClass = fqClassName.resolve();
        if (psiClass == null)
            return;
        PsiClass trueClass = null;
        DMethodElement methodElement = null;
        final GrParameter[] parameters = getParameters();
        List<String> parameterTypes = new ArrayList<>();
        for (GrParameter parameter : parameters) {
            final String type = parameter.getType().getCanonicalText();
            parameterTypes.add(type);
        }
        for (PsiClass aSuper : PsiUtil.iterateSupers(psiClass, true)) {
            methodElement = DynamicManager.getInstance(getProject()).findConcreteDynamicMethod(aSuper.getQualifiedName(), getName(), ArrayUtil.toStringArray(parameterTypes));
            if (methodElement != null) {
                trueClass = aSuper;
                break;
            }
        }
        if (trueClass == null)
            return;
        final DefaultMutableTreeNode classNode = TreeUtil.findNodeWithObject(treeRoot, new DClassElement(getProject(), trueClass.getQualifiedName()));
        if (classNode == null)
            return;
        desiredNode = TreeUtil.findNodeWithObject(classNode, methodElement);
        if (desiredNode == null)
            return;
        final TreePath path = TreeUtil.getPathFromRoot(desiredNode);
        treeTable.getTree().expandPath(path);
        treeTable.getTree().setSelectionPath(path);
        treeTable.getTree().fireTreeExpanded(path);
        //        ToolWindowManager.getInstance(myProject).getFocusManager().requestFocus(treeTable, true);
        treeTable.revalidate();
        treeTable.repaint();
    }, true);
}
Also used : ListTreeTableModelOnColumns(com.intellij.ui.treeStructure.treetable.ListTreeTableModelOnColumns) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeTable(com.intellij.ui.treeStructure.treetable.TreeTable) ArrayList(java.util.ArrayList) DMethodElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DMethodElement) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) DClassElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement) TreePath(javax.swing.tree.TreePath)

Aggregations

DClassElement (org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement)4 ListTreeTableModelOnColumns (com.intellij.ui.treeStructure.treetable.ListTreeTableModelOnColumns)2 TreeTable (com.intellij.ui.treeStructure.treetable.TreeTable)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 TreePath (javax.swing.tree.TreePath)2 GlobalUndoableAction (com.intellij.openapi.command.undo.GlobalUndoableAction)1 UnexpectedUndoException (com.intellij.openapi.command.undo.UnexpectedUndoException)1 Document (com.intellij.openapi.editor.Document)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NotNull (org.jetbrains.annotations.NotNull)1 DItemElement (org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DItemElement)1 DMethodElement (org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DMethodElement)1 DPropertyElement (org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DPropertyElement)1 DRootElement (org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DRootElement)1 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)1 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)1