@NotNull public static ErrorInfo[] getAllErrorsForComponent(@NotNull IComponent component) { List<ErrorInfo> result = new ArrayList<ErrorInfo>(); ErrorInfo errorInfo = (ErrorInfo)component.getClientProperty(CLIENT_PROP_CLASS_TO_BIND_ERROR); if (errorInfo != null) { result.add(errorInfo); } errorInfo = (ErrorInfo)component.getClientProperty(CLIENT_PROP_BINDING_ERROR); if (errorInfo != null) { result.add(errorInfo); } final ArrayList<ErrorInfo> errorInfos = getErrorInfos(component); if (errorInfos != null) { result.addAll(errorInfos); } return result.toArray(new ErrorInfo[result.size()]); }
private static void retargetComponentProperties(final GuiEditor editor, final RadComponent c, final RadComponent newComponent) { FormEditingUtil.iterate(editor.getRootContainer(), new FormEditingUtil.ComponentVisitor() { public boolean visit(final IComponent component) { RadComponent rc = (RadComponent) component; for(IProperty p: component.getModifiedProperties()) { if (p instanceof IntroComponentProperty) { IntroComponentProperty icp = (IntroComponentProperty) p; final String value = icp.getValue(rc); if (value.equals(c.getId())) { try { icp.setValue((RadComponent)component, newComponent.getId()); } catch (Exception e) { // ignore } } } } return true; } }); }
private static boolean haveCustomComponents(final GuiEditor editor) { // quick & dirty check if (editor.isFormInvalid()) { return true; } final Ref<Boolean> result = new Ref<Boolean>(); FormEditingUtil.iterate(editor.getRootContainer(), new FormEditingUtil.ComponentVisitor() { public boolean visit(final IComponent component) { if (component instanceof RadErrorComponent || !component.getComponentClassName().startsWith("javax.swing")) { result.set(Boolean.TRUE); return false; } return true; } }); return !result.isNull(); }
private void doPaste(final ComponentDropLocation location) { if (location.canDrop(myPastedComponentList) && myEditor.ensureEditable()) { final RadComponent[] componentsToPaste = myComponentsToPaste.toArray(new RadComponent[myComponentsToPaste.size()]); CommandProcessor.getInstance().executeCommand( myEditor.getProject(), new Runnable() { public void run() { location.processDrop(myEditor, componentsToPaste, null, myPastedComponentList); for(RadComponent c: componentsToPaste) { FormEditingUtil.iterate(c, new FormEditingUtil.ComponentVisitor() { public boolean visit(final IComponent component) { if (component.getBinding() != null) { InsertComponentProcessor.createBindingField(myEditor, (RadComponent) component); } return true; } }); } FormEditingUtil.selectComponents(myEditor, myComponentsToPaste); myEditor.refreshAndSave(true); } }, UIDesignerBundle.message("command.paste"), null); endPaste(); } }
private Map<String, String> saveTabbedPaneSelectedTabs() { final Map<String, String> result = new HashMap<String, String>(); FormEditingUtil.iterate(getRootContainer(), new FormEditingUtil.ComponentVisitor() { public boolean visit(final IComponent component) { if (component instanceof RadTabbedPane) { RadTabbedPane tabbedPane = (RadTabbedPane)component; RadComponent c = tabbedPane.getSelectedTab(); if (c != null) { result.put(tabbedPane.getId(), c.getId()); } } return true; } }); return result; }
private void restoreTabbedPaneSelectedTabs(final Map<String, String> tabbedPaneSelectedTabs) { FormEditingUtil.iterate(getRootContainer(), new FormEditingUtil.ComponentVisitor() { public boolean visit(final IComponent component) { if (component instanceof RadTabbedPane) { RadTabbedPane tabbedPane = (RadTabbedPane)component; String selectedTabId = tabbedPaneSelectedTabs.get(tabbedPane.getId()); if (selectedTabId != null) { for (RadComponent c : tabbedPane.getComponents()) { if (c.getId().equals(selectedTabId)) { tabbedPane.selectTab(c); break; } } } } return true; } }); }
public void addError(@NotNull final String inspectionId, final IComponent component, @Nullable IProperty prop, @NotNull String errorMessage, EditorQuickFixProvider... editorQuickFixProviders) { if (myResults == null) { myResults = new ArrayList<ErrorInfo>(); } List<QuickFix> quickFixes = new ArrayList<QuickFix>(); for (EditorQuickFixProvider provider : editorQuickFixProviders) { if (provider != null) { quickFixes.add(provider.createQuickFix(myEditor, myComponent)); } } final ErrorInfo errorInfo = new ErrorInfo(myComponent, prop == null ? null : prop.getName(), errorMessage, myProfile.getErrorLevel(HighlightDisplayKey.find(inspectionId), myFormPsiFile), quickFixes.toArray(new QuickFix[quickFixes.size()])); errorInfo.setInspectionId(inspectionId); myResults.add(errorInfo); }
protected void checkComponentProperties(Module module, final IComponent component, final FormErrorCollector collector) { final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module); final PsiManager psiManager = PsiManager.getInstance(module.getProject()); final PsiClass aClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(component.getComponentClassName(), scope); if (aClass == null) { return; } for(final IProperty prop: component.getModifiedProperties()) { final PsiMethod getter = PropertyUtil.findPropertyGetter(aClass, prop.getName(), false, true); if (getter == null) continue; final LanguageLevel languageLevel = LanguageLevelUtil.getEffectiveLanguageLevel(module); if (Java15APIUsageInspection.isForbiddenApiUsage(getter, languageLevel)) { registerError(component, collector, prop, "@since " + Java15APIUsageInspection.getShortName(languageLevel)); } } }
@Nullable public static String getText(@NotNull final Module module, final IComponent component) { IProperty textProperty = findProperty(component, SwingProperties.TEXT); if (textProperty != null) { Object propValue = textProperty.getPropertyValue(component); String value = null; if (propValue instanceof StringDescriptor) { StringDescriptor descriptor = (StringDescriptor) propValue; if (component instanceof RadComponent) { value = StringDescriptorManager.getInstance(module).resolve((RadComponent) component, descriptor); } else { value = StringDescriptorManager.getInstance(module).resolve(descriptor, null); } } else if (propValue instanceof String) { value = (String) propValue; } if (value != null) { return value; } } return null; }
protected void checkComponentProperties(Module module, IComponent component, FormErrorCollector collector) { String value = FormInspectionUtil.getText(module, component); if (value == null) { return; } IProperty textProperty = FormInspectionUtil.findProperty(component, SwingProperties.TEXT); SupportCode.TextWithMnemonic twm = SupportCode.parseText(value); if (twm.myMnemonicIndex < 0 && twm.myText.length() > 0) { if (FormInspectionUtil.isComponentClass(module, component, AbstractButton.class)) { collector.addError(getID(), component, textProperty, UIDesignerBundle.message("inspection.missing.mnemonics.message", value), new MyEditorQuickFixProvider()); } else if (FormInspectionUtil.isComponentClass(module, component, JLabel.class)) { IProperty labelForProperty = FormInspectionUtil.findProperty(component, SwingProperties.LABEL_FOR); if (labelForProperty != null && !StringUtil.isEmpty((String) labelForProperty.getPropertyValue(component))) { collector.addError(getID(), component, textProperty, UIDesignerBundle.message("inspection.missing.mnemonics.message", value), new MyEditorQuickFixProvider()); } } } }
public static Set<String> collectUsedBundleNames(final IRootContainer rootContainer) { final Set<String> bundleNames = new HashSet<String>(); iterateStringDescriptors(rootContainer, new StringDescriptorVisitor<IComponent>() { public boolean visit(final IComponent component, final StringDescriptor descriptor) { if(descriptor.getBundleName() != null && !bundleNames.contains(descriptor.getBundleName())) { bundleNames.add(descriptor.getBundleName()); } return true; } }); return bundleNames; }
/** * Finds component with the specified <code>id</code> starting from the * <code>container</code>. The method goes recursively through the hierarchy * of components. Note, that if <code>container</code> itself has <code>id</code> * then the method immediately retuns it. * * @return the found component. */ @Nullable public static IComponent findComponent(@NotNull final IComponent component, @NotNull final String id) { if(id.equals(component.getId())) { return component; } if(!(component instanceof IContainer)) { return null; } final IContainer uiContainer = (IContainer) component; for(int i = 0; i < uiContainer.getComponentCount(); i++) { final IComponent found = findComponent(uiContainer.getComponent(i), id); if(found != null) { return found; } } return null; }
@Override protected void actionPerformed(final GuiEditor editor, final List<RadComponent> selection, final AnActionEvent e) { final ComponentTreeBuilder builder = DesignerToolWindowManager.getInstance(editor).getComponentTreeBuilder(); builder.beginUpdateSelection(); try { FormEditingUtil.iterate(editor.getRootContainer(), new FormEditingUtil.ComponentVisitor() { @Override public boolean visit(final IComponent component) { ((RadComponent) component).setSelected(true); return true; } }); } finally { builder.endUpdateSelection(); } }
private Map<String, String> saveTabbedPaneSelectedTabs() { final Map<String, String> result = new HashMap<String, String>(); FormEditingUtil.iterate(getRootContainer(), new FormEditingUtil.ComponentVisitor() { @Override public boolean visit(final IComponent component) { if(component instanceof RadTabbedPane) { RadTabbedPane tabbedPane = (RadTabbedPane) component; RadComponent c = tabbedPane.getSelectedTab(); if(c != null) { result.put(tabbedPane.getId(), c.getId()); } } return true; } }); return result; }
@Override protected void checkComponentProperties(Module module, final IComponent component, final FormErrorCollector collector) { final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module); final PsiManager psiManager = PsiManager.getInstance(module.getProject()); final PsiClass aClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(component.getComponentClassName(), scope); if(aClass == null) { return; } for(final IProperty prop : component.getModifiedProperties()) { final PsiMethod getter = PropertyUtil.findPropertyGetter(aClass, prop.getName(), false, true); if(getter == null) { continue; } final LanguageLevel languageLevel = EffectiveLanguageLevelUtil.getEffectiveLanguageLevel(module); if(Java15APIUsageInspection.isForbiddenApiUsage(getter, languageLevel)) { registerError(component, collector, prop, "@since " + Java15APIUsageInspection.getShortName(languageLevel)); } } }
private static void putError(final IComponent component, final ErrorInfo errorInfo) { ArrayList<ErrorInfo> errorList = getErrorInfos(component); if (errorList == null) { errorList = new ArrayList<ErrorInfo>(); component.putClientProperty(CLIENT_PROP_ERROR_ARRAY, errorList); } errorList.add(errorInfo); }
private void createBoundFields(final PsiClass formClass) throws IncorrectOperationException { final Module module = myEditor.getRootContainer().getModule(); final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module); final PsiManager psiManager = PsiManager.getInstance(myEditor.getProject()); final Ref<IncorrectOperationException> exception = new Ref<IncorrectOperationException>(); FormEditingUtil.iterate(myEditor.getRootContainer(), new FormEditingUtil.ComponentVisitor() { public boolean visit(final IComponent component) { if (component.getBinding() != null) { final PsiClass fieldClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(component.getComponentClassName(), scope); if (fieldClass != null) { PsiType fieldType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(fieldClass); try { PsiField field = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createField(component.getBinding(), fieldType); formClass.add(field); } catch (IncorrectOperationException e) { exception.set(e); return false; } } } return true; } }); if (!exception.isNull()) { throw exception.get(); } }
public ErrorInfo(IComponent component, @NonNls final String propertyName, @NotNull final String description, @NotNull HighlightDisplayLevel highlightDisplayLevel, @NotNull final QuickFix[] fixes) { myComponent = component instanceof RadComponent ? (RadComponent) component : null; myHighlightDisplayLevel = highlightDisplayLevel; myPropertyName = propertyName; myDescription = description; myFixes = fixes; }
protected void actionPerformed(final GuiEditor editor, final List<RadComponent> selection, final AnActionEvent e) { final ComponentTreeBuilder builder = DesignerToolWindowManager.getInstance(editor).getComponentTreeBuilder(); builder.beginUpdateSelection(); try { FormEditingUtil.iterate(editor.getRootContainer(), new FormEditingUtil.ComponentVisitor() { public boolean visit(final IComponent component) { ((RadComponent) component).setSelected(true); return true; } }); } finally { builder.endUpdateSelection(); } }
private void refreshProperties() { final Ref<Boolean> anythingModified = new Ref<Boolean>(); FormEditingUtil.iterate(myRootContainer, new FormEditingUtil.ComponentVisitor() { public boolean visit(final IComponent component) { final RadComponent radComponent = (RadComponent)component; boolean componentModified = false; for (IProperty prop : component.getModifiedProperties()) { if (prop instanceof IntroStringProperty) { IntroStringProperty strProp = (IntroStringProperty)prop; componentModified = strProp.refreshValue(radComponent) || componentModified; } } if (component instanceof RadContainer) { componentModified = ((RadContainer)component).updateBorder() || componentModified; } if (component.getParentContainer() instanceof RadTabbedPane) { componentModified = ((RadTabbedPane)component.getParentContainer()).refreshChildTitle(radComponent) || componentModified; } if (componentModified) { anythingModified.set(Boolean.TRUE); } return true; } }); if (!anythingModified.isNull()) { refresh(); DesignerToolWindow designerToolWindow = DesignerToolWindowManager.getInstance(this); ComponentTree tree = designerToolWindow.getComponentTree(); if (tree != null) tree.repaint(); PropertyInspector inspector = designerToolWindow.getPropertyInspector(); if (inspector != null) inspector.synchWithTree(true); } }
@Nullable public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { if (file.getFileType().equals(StdFileTypes.GUI_DESIGNER_FORM)) { final VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile == null) { return null; } final Module module = ModuleUtil.findModuleForFile(virtualFile, file.getProject()); if (module == null) { return null; } final LwRootContainer rootContainer; try { rootContainer = Utils.getRootContainer(file.getText(), new PsiPropertiesProvider(module)); } catch (Exception e) { return null; } if (rootContainer.isInspectionSuppressed(getShortName(), null)) { return null; } final FormFileErrorCollector collector = new FormFileErrorCollector(file, manager, isOnTheFly); startCheckForm(rootContainer); FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor() { public boolean visit(final IComponent component) { if (!rootContainer.isInspectionSuppressed(getShortName(), component.getId())) { checkComponentProperties(module, component, collector); } return true; } }); doneCheckForm(rootContainer); return collector.result(); } return null; }
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() { public QuickFix createQuickFix(GuiEditor editor, RadComponent component) { return new CreateGroupQuickFix(editor, component, c1.getColumn() == c2.getColumn()); } }; break; } else { quickFixProvider = new EditorQuickFixProvider() { 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); } } }
public void addError(final String inspectionId, final IComponent component, @Nullable IProperty prop, @NotNull String errorMessage, EditorQuickFixProvider... editorQuickFixProviders) { final ProblemDescriptor problemDescriptor = myManager.createProblemDescriptor(myFile, JDOMUtil.escapeText(errorMessage), (LocalQuickFix)null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myOnTheFly); if (problemDescriptor instanceof ProblemDescriptorBase && component != null) { FormElementNavigatable navigatable = new FormElementNavigatable(myFile.getProject(), myFile.getVirtualFile(), component.getId()); ((ProblemDescriptorBase) problemDescriptor).setNavigatable(navigatable); } myProblems.add(problemDescriptor); }