Search in sources :

Example 6 with LwRootContainer

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

the class SaveFormAsTemplateHandler method getTemplateText.

@Override
public String getTemplateText(final PsiFile file, final String fileText, final String nameWithoutExtension) {
    if (StdFileTypes.GUI_DESIGNER_FORM.equals(file.getFileType())) {
        LwRootContainer rootContainer = null;
        try {
            rootContainer = Utils.getRootContainer(fileText, null);
        } catch (Exception ignored) {
        }
        if (rootContainer != null && rootContainer.getClassToBind() != null) {
            try {
                Element document = JdomKt.loadElement(fileText);
                Attribute attribute = document.getAttribute(UIFormXmlConstants.ATTRIBUTE_BIND_TO_CLASS);
                attribute.detach();
                return JDOMUtil.write(document, CodeStyleSettingsManager.getSettings(file.getProject()).getLineSeparator());
            } catch (Exception ignored) {
            }
        }
    }
    return null;
}
Also used : Attribute(org.jdom.Attribute) Element(org.jdom.Element) LwRootContainer(com.intellij.uiDesigner.lw.LwRootContainer)

Example 7 with LwRootContainer

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

the class PreviewNestedFormLoader method loadForm.

public LwRootContainer loadForm(String formFileName) throws Exception {
    LwRootContainer rootContainer = super.loadForm(formFileName);
    if (!myGeneratedClasses.contains(formFileName)) {
        myGeneratedClasses.add(formFileName);
        String generatedClassName = "FormPreviewFrame" + myGeneratedClasses.size();
        PreviewFormAction.setPreviewBindings(rootContainer, generatedClassName);
        generateStubClass(rootContainer, generatedClassName);
    }
    return rootContainer;
}
Also used : LwRootContainer(com.intellij.uiDesigner.lw.LwRootContainer)

Example 8 with LwRootContainer

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

the class PsiNestedFormLoader method loadForm.

public LwRootContainer loadForm(String formFileName) throws Exception {
    if (myFormCache.containsKey(formFileName)) {
        return myFormCache.get(formFileName);
    }
    VirtualFile formFile = ResourceFileUtil.findResourceFileInDependents(myModule, formFileName);
    if (formFile == null) {
        throw new Exception("Could not find nested form file " + formFileName);
    }
    final LwRootContainer container = Utils.getRootContainer(formFile.getInputStream(), new PsiPropertiesProvider(myModule));
    myFormCache.put(formFileName, container);
    return container;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LwRootContainer(com.intellij.uiDesigner.lw.LwRootContainer) PsiPropertiesProvider(com.intellij.uiDesigner.PsiPropertiesProvider)

Example 9 with LwRootContainer

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

the class ComponentItemDialog method saveNestedForm.

private boolean saveNestedForm() {
    VirtualFile formFile = ResourceFileUtil.findResourceFileInProject(myProject, myTfNestedForm.getText());
    if (formFile == null) {
        Messages.showErrorDialog(getWindow(), UIDesignerBundle.message("add.component.cannot.load.form", myTfNestedForm.getText()), CommonBundle.getErrorTitle());
        return false;
    }
    LwRootContainer lwRootContainer;
    try {
        lwRootContainer = Utils.getRootContainer(formFile.getInputStream(), null);
    } catch (Exception e) {
        Messages.showErrorDialog(getWindow(), e.getMessage(), CommonBundle.getErrorTitle());
        return false;
    }
    if (lwRootContainer.getClassToBind() == null) {
        Messages.showErrorDialog(getWindow(), UIDesignerBundle.message("add.component.form.not.bound"), CommonBundle.getErrorTitle());
        return false;
    }
    if (lwRootContainer.getComponent(0).getBinding() == null) {
        Messages.showErrorDialog(getWindow(), UIDesignerBundle.message("add.component.root.not.bound"), CommonBundle.getErrorTitle());
        return false;
    }
    PsiClass psiClass = JavaPsiFacade.getInstance(myProject).findClass(lwRootContainer.getClassToBind(), GlobalSearchScope.projectScope(myProject));
    if (psiClass != null) {
        myItemToBeEdited.setClassName(getClassOrInnerName(psiClass));
    } else {
        myItemToBeEdited.setClassName(lwRootContainer.getClassToBind());
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LwRootContainer(com.intellij.uiDesigner.lw.LwRootContainer)

Example 10 with LwRootContainer

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

the class Generator method exposeForm.

/**
   * @param rootContainer output parameter; should be LwRootContainer[1]
   */
public static FormProperty[] exposeForm(final Project project, final VirtualFile formFile, final LwRootContainer[] rootContainer) throws MyException {
    final Module module = ModuleUtil.findModuleForFile(formFile, project);
    LOG.assertTrue(module != null);
    final PsiPropertiesProvider propertiesProvider = new PsiPropertiesProvider(module);
    final Document doc = FileDocumentManager.getInstance().getDocument(formFile);
    final LwRootContainer _rootContainer;
    try {
        _rootContainer = Utils.getRootContainer(doc.getText(), propertiesProvider);
    } catch (AlienFormFileException e) {
        throw new MyException(e.getMessage());
    } catch (Exception e) {
        throw new MyException(UIDesignerBundle.message("error.cannot.process.form.file", e));
    }
    rootContainer[0] = _rootContainer;
    final String classToBind = _rootContainer.getClassToBind();
    if (classToBind == null) {
        throw new MyException(UIDesignerBundle.message("error.form.is.not.bound.to.a.class"));
    }
    final PsiClass boundClass = FormEditingUtil.findClassToBind(module, classToBind);
    if (boundClass == null) {
        throw new MyException(UIDesignerBundle.message("error.bound.class.does.not.exist", classToBind));
    }
    final ArrayList<FormProperty> result = new ArrayList<>();
    final MyException[] exception = new MyException[1];
    FormEditingUtil.iterate(_rootContainer, new FormEditingUtil.ComponentVisitor<LwComponent>() {

        public boolean visit(final LwComponent component) {
            final String binding = component.getBinding();
            if (binding == null) {
                return true;
            }
            final PsiField[] fields = boundClass.getFields();
            PsiField field = null;
            for (int i = fields.length - 1; i >= 0; i--) {
                if (binding.equals(fields[i].getName())) {
                    field = fields[i];
                    break;
                }
            }
            if (field == null) {
                exception[0] = new MyException(UIDesignerBundle.message("error.field.not.found.in.class", binding, classToBind));
                return false;
            }
            final PsiClass fieldClass = getClassByType(field.getType());
            if (fieldClass == null) {
                exception[0] = new MyException(UIDesignerBundle.message("error.invalid.binding.field.type", binding, classToBind));
                return false;
            }
            if (instanceOf(fieldClass, JTextComponent.class.getName())) {
                result.add(new FormProperty(component, "getText", "setText", String.class.getName()));
            } else if (instanceOf(fieldClass, JCheckBox.class.getName())) {
                result.add(new FormProperty(component, "isSelected", "setSelected", boolean.class.getName()));
            }
            return true;
        }
    });
    if (exception[0] != null) {
        throw exception[0];
    }
    return result.toArray(new FormProperty[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) AlienFormFileException(com.intellij.uiDesigner.compiler.AlienFormFileException) LwRootContainer(com.intellij.uiDesigner.lw.LwRootContainer) Document(com.intellij.openapi.editor.Document) IncorrectOperationException(com.intellij.util.IncorrectOperationException) AlienFormFileException(com.intellij.uiDesigner.compiler.AlienFormFileException) LwComponent(com.intellij.uiDesigner.lw.LwComponent) Module(com.intellij.openapi.module.Module) PsiPropertiesProvider(com.intellij.uiDesigner.PsiPropertiesProvider) FormEditingUtil(com.intellij.uiDesigner.FormEditingUtil)

Aggregations

LwRootContainer (com.intellij.uiDesigner.lw.LwRootContainer)15 Module (com.intellij.openapi.module.Module)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Project (com.intellij.openapi.project.Project)3 FormEditingUtil (com.intellij.uiDesigner.FormEditingUtil)3 PsiPropertiesProvider (com.intellij.uiDesigner.PsiPropertiesProvider)3 AlienFormFileException (com.intellij.uiDesigner.compiler.AlienFormFileException)3 CompiledClassPropertiesProvider (com.intellij.uiDesigner.lw.CompiledClassPropertiesProvider)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 FailSafeClassReader (com.intellij.compiler.instrumentation.FailSafeClassReader)2 InstrumenterClassWriter (com.intellij.compiler.instrumentation.InstrumenterClassWriter)2 Document (com.intellij.openapi.editor.Document)2 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)2 IComponent (com.intellij.uiDesigner.lw.IComponent)2 PsiGenerationInfo (com.intellij.codeInsight.generation.PsiGenerationInfo)1 WordOccurrence (com.intellij.lang.cacheBuilder.WordOccurrence)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 PsiClass (com.intellij.psi.PsiClass)1 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)1