Search in sources :

Example 1 with IRootContainer

use of com.intellij.uiDesigner.lw.IRootContainer in project intellij-community by JetBrains.

the class ListenerNavigateButton method prepareActionGroup.

@Nullable
public static DefaultActionGroup prepareActionGroup(final RadComponent component) {
    final IRootContainer root = FormEditingUtil.getRoot(component);
    final String classToBind = root == null ? null : root.getClassToBind();
    if (classToBind != null) {
        final PsiClass aClass = FormEditingUtil.findClassToBind(component.getModule(), classToBind);
        if (aClass != null) {
            final PsiField boundField = aClass.findFieldByName(component.getBinding(), false);
            if (boundField != null) {
                return buildNavigateActionGroup(component, boundField);
            }
        }
    }
    return null;
}
Also used : IRootContainer(com.intellij.uiDesigner.lw.IRootContainer) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with IRootContainer

use of com.intellij.uiDesigner.lw.IRootContainer in project intellij-community by JetBrains.

the class NoButtonGroupInspection method checkComponentProperties.

@Override
protected void checkComponentProperties(Module module, IComponent component, FormErrorCollector collector) {
    if (FormInspectionUtil.isComponentClass(module, component, JRadioButton.class)) {
        final IRootContainer root = FormEditingUtil.getRoot(component);
        if (root == null)
            return;
        if (root.getButtonGroupName(component) == null) {
            EditorQuickFixProvider quickFixProvider = null;
            IContainer parent = component.getParentContainer();
            for (int i = 0; i < parent.getComponentCount(); i++) {
                IComponent child = parent.getComponent(i);
                if (child != component && FormInspectionUtil.isComponentClass(module, child, JRadioButton.class)) {
                    final GridConstraints c1 = component.getConstraints();
                    final GridConstraints c2 = child.getConstraints();
                    if (areCellsAdjacent(parent, c1, c2)) {
                        final String groupName = root.getButtonGroupName(child);
                        if (groupName == null) {
                            quickFixProvider = new EditorQuickFixProvider() {

                                @Override
                                public QuickFix createQuickFix(GuiEditor editor, RadComponent component) {
                                    return new CreateGroupQuickFix(editor, component, c1.getColumn() == c2.getColumn());
                                }
                            };
                            break;
                        } else {
                            quickFixProvider = new EditorQuickFixProvider() {

                                @Override
                                public QuickFix createQuickFix(GuiEditor editor, RadComponent component) {
                                    return new AddToGroupQuickFix(editor, component, groupName);
                                }
                            };
                        }
                    }
                }
            }
            collector.addError(getID(), component, null, UIDesignerBundle.message("inspection.no.button.group.error"), quickFixProvider);
        }
    }
}
Also used : QuickFix(com.intellij.uiDesigner.quickFixes.QuickFix) IComponent(com.intellij.uiDesigner.lw.IComponent) RadComponent(com.intellij.uiDesigner.radComponents.RadComponent) GuiEditor(com.intellij.uiDesigner.designSurface.GuiEditor) GridConstraints(com.intellij.uiDesigner.core.GridConstraints) IRootContainer(com.intellij.uiDesigner.lw.IRootContainer) IContainer(com.intellij.uiDesigner.lw.IContainer)

Example 3 with IRootContainer

use of com.intellij.uiDesigner.lw.IRootContainer in project intellij-community by JetBrains.

the class OneButtonGroupInspection method checkComponentProperties.

@Override
protected void checkComponentProperties(Module module, IComponent component, FormErrorCollector collector) {
    final IRootContainer root = FormEditingUtil.getRoot(component);
    if (root == null)
        return;
    String groupName = root.getButtonGroupName(component);
    if (groupName != null) {
        final String[] sameGroupComponents = root.getButtonGroupComponentIds(groupName);
        for (String id : sameGroupComponents) {
            final IComponent otherComponent = FormEditingUtil.findComponent(root, id);
            if (otherComponent != null && otherComponent != component) {
                return;
            }
        }
        collector.addError(getID(), component, null, UIDesignerBundle.message("inspection.one.button.group.error"));
    }
}
Also used : IComponent(com.intellij.uiDesigner.lw.IComponent) IRootContainer(com.intellij.uiDesigner.lw.IRootContainer)

Example 4 with IRootContainer

use of com.intellij.uiDesigner.lw.IRootContainer in project intellij-community by JetBrains.

the class BindingEditor method getFieldNames.

private static String[] getFieldNames(final RadComponent component, final String currentName) {
    final ArrayList<String> result = new ArrayList<>();
    if (currentName != null) {
        result.add(currentName);
    }
    final IRootContainer root = FormEditingUtil.getRoot(component);
    final String className = root.getClassToBind();
    if (className == null) {
        return ArrayUtil.toStringArray(result);
    }
    final PsiClass aClass = FormEditingUtil.findClassToBind(component.getModule(), className);
    if (aClass == null) {
        return ArrayUtil.toStringArray(result);
    }
    final PsiField[] fields = aClass.getFields();
    for (final PsiField field : fields) {
        if (field.hasModifierProperty(PsiModifier.STATIC)) {
            continue;
        }
        final String fieldName = field.getName();
        if (Comparing.equal(currentName, fieldName)) {
            continue;
        }
        if (!FormEditingUtil.isBindingUnique(component, fieldName, root)) {
            continue;
        }
        final String componentClassName;
        if (component instanceof RadErrorComponent) {
            componentClassName = component.getComponentClassName();
        } else if (component instanceof RadHSpacer || component instanceof RadVSpacer) {
            componentClassName = Spacer.class.getName();
        } else {
            componentClassName = component.getComponentClass().getName();
        }
        final PsiType componentType;
        try {
            componentType = JavaPsiFacade.getInstance(component.getProject()).getElementFactory().createTypeFromText(componentClassName, null);
        } catch (IncorrectOperationException e) {
            continue;
        }
        final PsiType fieldType = field.getType();
        if (!fieldType.isAssignableFrom(componentType)) {
            continue;
        }
        result.add(fieldName);
    }
    String text = FormInspectionUtil.getText(component.getModule(), component);
    if (text != null) {
        String binding = BindingProperty.suggestBindingFromText(component, text);
        if (binding != null && !result.contains(binding)) {
            result.add(binding);
        }
    }
    final String[] names = ArrayUtil.toStringArray(result);
    Arrays.sort(names);
    return names;
}
Also used : ArrayList(java.util.ArrayList) RadErrorComponent(com.intellij.uiDesigner.radComponents.RadErrorComponent) RadVSpacer(com.intellij.uiDesigner.radComponents.RadVSpacer) IRootContainer(com.intellij.uiDesigner.lw.IRootContainer) IncorrectOperationException(com.intellij.util.IncorrectOperationException) RadHSpacer(com.intellij.uiDesigner.radComponents.RadHSpacer)

Example 5 with IRootContainer

use of com.intellij.uiDesigner.lw.IRootContainer in project intellij-community by JetBrains.

the class ErrorAnalyzer method analyzeErrors.

/**
   * @param editor if null, no quick fixes are created. This is used in form to source compiler.
   */
public static void analyzeErrors(@NotNull final Module module, @NotNull final VirtualFile formFile, @Nullable final GuiEditor editor, @NotNull final IRootContainer rootContainer, @Nullable final ProgressIndicator progress) {
    if (module.isDisposed()) {
        return;
    }
    // 1. Validate class to bind
    final String classToBind = rootContainer.getClassToBind();
    final PsiClass psiClass;
    if (classToBind != null) {
        psiClass = FormEditingUtil.findClassToBind(module, classToBind);
        if (psiClass == null) {
            final QuickFix[] fixes = editor != null ? new QuickFix[] { new CreateClassToBindFix(editor, classToBind) } : QuickFix.EMPTY_ARRAY;
            final ErrorInfo errorInfo = new ErrorInfo(null, null, UIDesignerBundle.message("error.class.does.not.exist", classToBind), HighlightDisplayLevel.ERROR, fixes);
            rootContainer.putClientProperty(CLIENT_PROP_CLASS_TO_BIND_ERROR, errorInfo);
        } else {
            rootContainer.putClientProperty(CLIENT_PROP_CLASS_TO_BIND_ERROR, null);
        }
    } else {
        rootContainer.putClientProperty(CLIENT_PROP_CLASS_TO_BIND_ERROR, null);
        psiClass = null;
    }
    // 2. Validate bindings to fields
    // field name -> error message
    // for performance reasons
    final ArrayList<String> usedBindings = new ArrayList<>();
    final Set<IButtonGroup> processedGroups = new HashSet<>();
    FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor<IComponent>() {

        public boolean visit(final IComponent component) {
            if (progress != null && progress.isCanceled())
                return false;
            // Reset previous error (if any)
            component.putClientProperty(CLIENT_PROP_BINDING_ERROR, null);
            final String binding = component.getBinding();
            // a. Check that field exists and field is not static
            if (psiClass != null && binding != null) {
                if (validateFieldInClass(component, binding, component.getComponentClassName(), psiClass, editor, module))
                    return true;
            }
            // b. Check that binding is unique
            if (binding != null) {
                if (usedBindings.contains(binding)) {
                    // TODO[vova] implement
                    component.putClientProperty(CLIENT_PROP_BINDING_ERROR, new ErrorInfo(component, null, UIDesignerBundle.message("error.binding.already.exists", binding), HighlightDisplayLevel.ERROR, QuickFix.EMPTY_ARRAY));
                    return true;
                }
                usedBindings.add(binding);
            }
            IButtonGroup group = FormEditingUtil.findGroupForComponent(rootContainer, component);
            if (group != null && !processedGroups.contains(group)) {
                processedGroups.add(group);
                if (group.isBound()) {
                    validateFieldInClass(component, group.getName(), ButtonGroup.class.getName(), psiClass, editor, module);
                }
            }
            return true;
        }
    });
    if (progress != null)
        progress.checkCanceled();
    // Check that there are no panels in XY with children
    FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor<IComponent>() {

        public boolean visit(final IComponent component) {
            if (progress != null && progress.isCanceled())
                return false;
            // Clear previous error (if any)
            component.putClientProperty(CLIENT_PROP_ERROR_ARRAY, null);
            if (!(component instanceof IContainer)) {
                return true;
            }
            final IContainer container = (IContainer) component;
            if (container instanceof IRootContainer) {
                final IRootContainer rootContainer = (IRootContainer) container;
                if (rootContainer.getComponentCount() > 1) {
                    // TODO[vova] implement
                    putError(component, new ErrorInfo(component, null, UIDesignerBundle.message("error.multiple.toplevel.components"), HighlightDisplayLevel.ERROR, QuickFix.EMPTY_ARRAY));
                }
            } else if (container.isXY() && container.getComponentCount() > 0) {
                // TODO[vova] implement
                putError(component, new ErrorInfo(component, null, UIDesignerBundle.message("error.panel.not.laid.out"), HighlightDisplayLevel.ERROR, QuickFix.EMPTY_ARRAY));
            }
            return true;
        }
    });
    if (progress != null)
        progress.checkCanceled();
    try {
        // Run inspections for form elements
        final PsiFile formPsiFile = PsiManager.getInstance(module.getProject()).findFile(formFile);
        if (formPsiFile != null && rootContainer instanceof RadRootContainer) {
            final List<FormInspectionTool> formInspectionTools = new ArrayList<>();
            final FormInspectionTool[] registeredFormInspections = Extensions.getExtensions(FormInspectionTool.EP_NAME);
            for (FormInspectionTool formInspectionTool : registeredFormInspections) {
                if (formInspectionTool.isActive(formPsiFile) && !rootContainer.isInspectionSuppressed(formInspectionTool.getShortName(), null)) {
                    formInspectionTools.add(formInspectionTool);
                }
            }
            if (formInspectionTools.size() > 0 && editor != null) {
                for (FormInspectionTool tool : formInspectionTools) {
                    tool.startCheckForm(rootContainer);
                }
                FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor<RadComponent>() {

                    public boolean visit(final RadComponent component) {
                        if (progress != null && progress.isCanceled())
                            return false;
                        for (FormInspectionTool tool : formInspectionTools) {
                            if (rootContainer.isInspectionSuppressed(tool.getShortName(), component.getId()))
                                continue;
                            ErrorInfo[] errorInfos = tool.checkComponent(editor, component);
                            if (errorInfos != null) {
                                ArrayList<ErrorInfo> errorList = getErrorInfos(component);
                                if (errorList == null) {
                                    errorList = new ArrayList<>();
                                    component.putClientProperty(CLIENT_PROP_ERROR_ARRAY, errorList);
                                }
                                Collections.addAll(errorList, errorInfos);
                            }
                        }
                        return true;
                    }
                });
                for (FormInspectionTool tool : formInspectionTools) {
                    tool.doneCheckForm(rootContainer);
                }
            }
        }
    } catch (Exception e) {
        LOG.error(e);
    }
}
Also used : IButtonGroup(com.intellij.uiDesigner.lw.IButtonGroup) IComponent(com.intellij.uiDesigner.lw.IComponent) ArrayList(java.util.ArrayList) RadRootContainer(com.intellij.uiDesigner.radComponents.RadRootContainer) RadComponent(com.intellij.uiDesigner.radComponents.RadComponent) FormInspectionTool(com.intellij.uiDesigner.inspections.FormInspectionTool) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IRootContainer(com.intellij.uiDesigner.lw.IRootContainer) IContainer(com.intellij.uiDesigner.lw.IContainer) HashSet(com.intellij.util.containers.HashSet)

Aggregations

IRootContainer (com.intellij.uiDesigner.lw.IRootContainer)6 IComponent (com.intellij.uiDesigner.lw.IComponent)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 IContainer (com.intellij.uiDesigner.lw.IContainer)2 RadComponent (com.intellij.uiDesigner.radComponents.RadComponent)2 ArrayList (java.util.ArrayList)2 GridConstraints (com.intellij.uiDesigner.core.GridConstraints)1 GuiEditor (com.intellij.uiDesigner.designSurface.GuiEditor)1 FormInspectionTool (com.intellij.uiDesigner.inspections.FormInspectionTool)1 IButtonGroup (com.intellij.uiDesigner.lw.IButtonGroup)1 QuickFix (com.intellij.uiDesigner.quickFixes.QuickFix)1 RadErrorComponent (com.intellij.uiDesigner.radComponents.RadErrorComponent)1 RadHSpacer (com.intellij.uiDesigner.radComponents.RadHSpacer)1 RadRootContainer (com.intellij.uiDesigner.radComponents.RadRootContainer)1 RadVSpacer (com.intellij.uiDesigner.radComponents.RadVSpacer)1 HashSet (com.intellij.util.containers.HashSet)1 Nullable (org.jetbrains.annotations.Nullable)1