Search in sources :

Example 1 with LwContainer

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

Aggregations

LwComponent (com.intellij.uiDesigner.lw.LwComponent)1 LwContainer (com.intellij.uiDesigner.lw.LwContainer)1 RadComponent (com.intellij.uiDesigner.radComponents.RadComponent)1 TIntArrayList (gnu.trove.TIntArrayList)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Document (org.jdom.Document)1 Element (org.jdom.Element)1