Search in sources :

Example 1 with LwComponent

use of com.intellij.uiDesigner.lw.LwComponent 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)

Example 2 with LwComponent

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

the class CutCopyPasteSupport method loadComponentsToPaste.

private static boolean loadComponentsToPaste(final GuiEditor editor, final String serializedComponents, final TIntArrayList xs, final TIntArrayList ys, final ArrayList<RadComponent> componentsToPaste) {
    final PsiPropertiesProvider provider = new PsiPropertiesProvider(editor.getModule());
    try {
        //noinspection HardCodedStringLiteral
        final Document document = SAX_BUILDER.build(new StringReader(serializedComponents), "UTF-8");
        final Element rootElement = document.getRootElement();
        if (!rootElement.getName().equals(ELEMENT_SERIALIZED)) {
            return false;
        }
        final List children = rootElement.getChildren();
        for (final Object aChildren : children) {
            final Element e = (Element) aChildren;
            // we need to add component to a container in order to read them
            final LwContainer container = new LwContainer(JPanel.class.getName());
            final String parentLayout = e.getAttributeValue(ATTRIBUTE_PARENT_LAYOUT);
            if (parentLayout != null) {
                container.setLayoutManager(parentLayout);
            }
            final int x = Integer.parseInt(e.getAttributeValue(ATTRIBUTE_X));
            final int y = Integer.parseInt(e.getAttributeValue(ATTRIBUTE_Y));
            xs.add(x);
            ys.add(y);
            final Element componentElement = (Element) e.getChildren().get(0);
            final LwComponent lwComponent = LwContainer.createComponentFromTag(componentElement);
            container.addComponent(lwComponent);
            lwComponent.read(componentElement, provider);
            // pasted components should have no bindings
            FormEditingUtil.iterate(lwComponent, new FormEditingUtil.ComponentVisitor<LwComponent>() {

                public boolean visit(final LwComponent c) {
                    if (c.getBinding() != null && FormEditingUtil.findComponentWithBinding(editor.getRootContainer(), c.getBinding()) != null) {
                        c.setBinding(null);
                    }
                    c.setId(FormEditingUtil.generateId(editor.getRootContainer()));
                    return true;
                }
            });
            final ClassLoader loader = LoaderFactory.getInstance(editor.getProject()).getLoader(editor.getFile());
            final RadComponent radComponent = XmlReader.createComponent(editor, lwComponent, loader, editor.getStringDescriptorLocale());
            componentsToPaste.add(radComponent);
        }
    } catch (Exception e) {
        return false;
    }
    return true;
}
Also used : Element(org.jdom.Element) RadComponent(com.intellij.uiDesigner.radComponents.RadComponent) LwContainer(com.intellij.uiDesigner.lw.LwContainer) Document(org.jdom.Document) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) TIntArrayList(gnu.trove.TIntArrayList) List(java.util.List) LwComponent(com.intellij.uiDesigner.lw.LwComponent)

Example 3 with LwComponent

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

the class FormPropertyTableCellRenderer method customizeCellRenderer.

protected void customizeCellRenderer(final JTable table, final Object value, final boolean selected, final boolean hasFocus, final int row, final int column) {
    if (value == null) {
        return;
    }
    final FormProperty property = (FormProperty) value;
    final LwComponent component = property.getLwComponent();
    // Icon
    final Icon icon;
    final String fqClassName = component.getComponentClassName();
    final ComponentItem item = myPalette.getItem(fqClassName);
    if (item != null) {
        icon = item.getSmallIcon();
    } else {
        icon = UIDesignerIcons.Unknown_small;
    }
    setIcon(icon);
    // Binding
    append(component.getBinding(), myAttrs1);
    // Component class name and package
    final String shortClassName;
    final String packageName;
    final int lastDotIndex = fqClassName.lastIndexOf('.');
    if (lastDotIndex != -1) {
        shortClassName = fqClassName.substring(lastDotIndex + 1);
        packageName = fqClassName.substring(0, lastDotIndex);
    } else {
        // default package
        shortClassName = fqClassName;
        packageName = null;
    }
    LOG.assertTrue(shortClassName.length() > 0);
    append(" ", myAttrs2);
    /*small gap between icon and class name*/
    append(shortClassName, myAttrs2);
    if (packageName != null) {
        append(" (", myAttrs3);
        append(packageName, myAttrs3);
        append(")", myAttrs3);
    }
}
Also used : ComponentItem(com.intellij.uiDesigner.palette.ComponentItem) LwComponent(com.intellij.uiDesigner.lw.LwComponent)

Aggregations

LwComponent (com.intellij.uiDesigner.lw.LwComponent)3 ArrayList (java.util.ArrayList)2 Document (com.intellij.openapi.editor.Document)1 Module (com.intellij.openapi.module.Module)1 FormEditingUtil (com.intellij.uiDesigner.FormEditingUtil)1 PsiPropertiesProvider (com.intellij.uiDesigner.PsiPropertiesProvider)1 AlienFormFileException (com.intellij.uiDesigner.compiler.AlienFormFileException)1 LwContainer (com.intellij.uiDesigner.lw.LwContainer)1 LwRootContainer (com.intellij.uiDesigner.lw.LwRootContainer)1 ComponentItem (com.intellij.uiDesigner.palette.ComponentItem)1 RadComponent (com.intellij.uiDesigner.radComponents.RadComponent)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 TIntArrayList (gnu.trove.TIntArrayList)1 StringReader (java.io.StringReader)1 List (java.util.List)1 Document (org.jdom.Document)1 Element (org.jdom.Element)1