Search in sources :

Example 1 with XYLayoutManager

use of com.intellij.uiDesigner.shared.XYLayoutManager in project intellij-community by JetBrains.

the class XmlReader method createComponent.

@NotNull
public static RadComponent createComponent(@NotNull final ModuleProvider module, @NotNull final LwComponent lwComponent, @NotNull final ClassLoader loader, final Locale stringDescriptorLocale) throws Exception {
    // Id
    final String id = lwComponent.getId();
    final RadComponent component;
    Class componentClass = null;
    if (lwComponent instanceof LwNestedForm) {
        LwNestedForm nestedForm = (LwNestedForm) lwComponent;
        boolean recursiveNesting = false;
        try {
            Utils.validateNestedFormLoop(nestedForm.getFormFileName(), new PsiNestedFormLoader(module.getModule()));
        } catch (RecursiveFormNestingException ex) {
            recursiveNesting = true;
        }
        if (recursiveNesting) {
            component = RadErrorComponent.create(module, id, lwComponent.getComponentClassName(), lwComponent.getErrorComponentProperties(), UIDesignerBundle.message("error.recursive.form.nesting"));
        } else {
            component = new RadNestedForm(module, nestedForm.getFormFileName(), id);
        }
    } else {
        if (lwComponent.getErrorComponentProperties() == null) {
            componentClass = Class.forName(lwComponent.getComponentClassName(), true, loader);
        }
        if (lwComponent instanceof LwHSpacer) {
            component = new RadHSpacer(module, id);
        } else if (lwComponent instanceof LwVSpacer) {
            component = new RadVSpacer(module, id);
        } else if (lwComponent instanceof LwAtomicComponent) {
            if (componentClass == null) {
                component = createErrorComponent(module, id, lwComponent, loader);
            } else {
                RadComponent component1;
                try {
                    if (JTable.class.isAssignableFrom(componentClass)) {
                        component1 = new RadTable(module, componentClass, id);
                    } else {
                        component1 = new RadAtomicComponent(module, componentClass, id);
                    }
                } catch (final Exception exc) {
                    String errorDescription = MessageFormat.format(UIDesignerBundle.message("error.class.cannot.be.instantiated"), lwComponent.getComponentClassName());
                    final String message = FormEditingUtil.getExceptionMessage(exc);
                    if (message != null) {
                        errorDescription += ": " + message;
                    }
                    component1 = RadErrorComponent.create(module, id, lwComponent.getComponentClassName(), lwComponent.getErrorComponentProperties(), errorDescription);
                }
                component = component1;
            }
        } else if (lwComponent instanceof LwScrollPane) {
            component = new RadScrollPane(module, componentClass, id);
        } else if (lwComponent instanceof LwTabbedPane) {
            component = new RadTabbedPane(module, componentClass, id);
        } else if (lwComponent instanceof LwSplitPane) {
            component = new RadSplitPane(module, componentClass, id);
        } else if (lwComponent instanceof LwToolBar) {
            component = new RadToolBar(module, componentClass, id);
        } else if (lwComponent instanceof LwContainer) {
            final LwContainer lwContainer = (LwContainer) lwComponent;
            LayoutManager layout = lwContainer.getLayout();
            if (layout instanceof XYLayoutManager) {
                // replace stub layout with the real one
                final XYLayoutManagerImpl xyLayoutManager = new XYLayoutManagerImpl();
                layout = xyLayoutManager;
                xyLayoutManager.setPreferredSize(lwComponent.getBounds().getSize());
            }
            if (componentClass == null) {
                component = createErrorComponent(module, id, lwComponent, loader);
            } else {
                if (lwContainer instanceof LwRootContainer) {
                    component = new RadRootContainer(module, id);
                    if (stringDescriptorLocale != null) {
                        ((RadRootContainer) component).setStringDescriptorLocale(stringDescriptorLocale);
                    }
                } else {
                    component = new RadContainer(module, componentClass, id);
                    String layoutManagerName = lwContainer.getLayoutManager();
                    if (layoutManagerName == null || layoutManagerName.length() == 0) {
                        if (layout instanceof XYLayoutManager) {
                            layoutManagerName = UIFormXmlConstants.LAYOUT_XY;
                        } else {
                            layoutManagerName = UIFormXmlConstants.LAYOUT_INTELLIJ;
                        }
                    }
                    RadLayoutManager layoutManager = LayoutManagerRegistry.createLayoutManager(layoutManagerName);
                    RadContainer container = (RadContainer) component;
                    layoutManager.readLayout(lwContainer, container);
                    container.setLayoutManager(layoutManager);
                }
                ((RadContainer) component).setLayout(layout);
            }
        } else {
            throw new IllegalArgumentException("unexpected component: " + lwComponent);
        }
    }
    // binding
    component.setBinding(lwComponent.getBinding());
    component.setCustomCreate(lwComponent.isCustomCreate());
    component.setDefaultBinding(lwComponent.isDefaultBinding());
    // bounds
    component.setBounds(lwComponent.getBounds());
    // properties
    if (stringDescriptorLocale != null) {
        component.putClientProperty(RadComponent.CLIENT_PROP_LOAD_TIME_LOCALE, stringDescriptorLocale);
    }
    final LwIntrospectedProperty[] properties = lwComponent.getAssignedIntrospectedProperties();
    if (componentClass != null) {
        final Palette palette = Palette.getInstance(module.getProject());
        for (final LwIntrospectedProperty lwProperty : properties) {
            final IntrospectedProperty property = palette.getIntrospectedProperty(component, lwProperty.getName());
            if (property == null) {
                continue;
            }
            component.loadLwProperty(lwComponent, lwProperty, property);
        }
    }
    // GridConstraints
    component.getConstraints().restore(lwComponent.getConstraints());
    component.setCustomLayoutConstraints(lwComponent.getCustomLayoutConstraints());
    HashMap clientProps = lwComponent.getDelegeeClientProperties();
    for (Object o : clientProps.entrySet()) {
        Map.Entry entry = (Map.Entry) o;
        Object value = entry.getValue();
        if (value instanceof StringDescriptor) {
            value = ((StringDescriptor) value).getValue();
        }
        component.getDelegee().putClientProperty(entry.getKey(), value);
    }
    if (component instanceof RadContainer) {
        final RadContainer container = (RadContainer) component;
        //noinspection ConstantConditions
        final LwContainer lwContainer = (LwContainer) lwComponent;
        copyBorder(container, lwContainer);
        // add children
        for (int i = 0; i < lwContainer.getComponentCount(); i++) {
            container.addComponent(createComponent(module, (LwComponent) lwContainer.getComponent(i), loader, stringDescriptorLocale));
        }
    }
    if (component instanceof RadRootContainer) {
        final RadRootContainer radRootContainer = (RadRootContainer) component;
        //noinspection ConstantConditions
        final LwRootContainer lwRootContainer = (LwRootContainer) lwComponent;
        radRootContainer.setClassToBind(lwRootContainer.getClassToBind());
        radRootContainer.setMainComponentBinding(lwRootContainer.getMainComponentBinding());
        radRootContainer.setButtonGroups(lwRootContainer.getButtonGroups());
        radRootContainer.setInspectionSuppressions(lwRootContainer.getInspectionSuppressions());
        radRootContainer.getDelegee().setBackground(new JBColor(Color.WHITE, UIUtil.getListBackground()));
    }
    component.doneLoadingFromLw();
    component.putClientProperty(RadComponent.CLIENT_PROP_LOAD_TIME_LOCALE, null);
    return component;
}
Also used : Palette(com.intellij.uiDesigner.palette.Palette) HashMap(java.util.HashMap) JBColor(com.intellij.ui.JBColor) XYLayoutManager(com.intellij.uiDesigner.shared.XYLayoutManager) RecursiveFormNestingException(com.intellij.uiDesigner.compiler.RecursiveFormNestingException) RecursiveFormNestingException(com.intellij.uiDesigner.compiler.RecursiveFormNestingException) XYLayoutManager(com.intellij.uiDesigner.shared.XYLayoutManager) IntrospectedProperty(com.intellij.uiDesigner.propertyInspector.IntrospectedProperty) HashMap(java.util.HashMap) Map(java.util.Map) PsiNestedFormLoader(com.intellij.uiDesigner.make.PsiNestedFormLoader) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with XYLayoutManager

use of com.intellij.uiDesigner.shared.XYLayoutManager in project intellij-community by JetBrains.

the class SurroundAction method actionPerformed.

public void actionPerformed(final GuiEditor editor, final List<RadComponent> selection, final AnActionEvent e) {
    // the action is also reused as quickfix for NoScrollPaneInspection, so this code should be kept here
    FormEditingUtil.remapToActionTargets(selection);
    if (!editor.ensureEditable()) {
        return;
    }
    final RadContainer selectionParent = FormEditingUtil.getSelectionParent(selection);
    assert selectionParent != null;
    final Palette palette = Palette.getInstance(editor.getProject());
    final ComponentItem cItem = palette.getItem(myComponentClass);
    assert cItem != null : myComponentClass;
    CommandProcessor.getInstance().executeCommand(editor.getProject(), () -> {
        RadContainer newContainer = (RadContainer) InsertComponentProcessor.createInsertedComponent(editor, cItem);
        if (newContainer == null) {
            return;
        }
        if (cItem == palette.getPanelItem()) {
            if (selectionParent.getLayoutManager().isGrid()) {
                try {
                    newContainer.setLayoutManager(LayoutManagerRegistry.createLayoutManager(selectionParent.getLayoutManager().getName()));
                } catch (Exception e1) {
                    LOG.error(e1);
                    return;
                }
            } else {
                newContainer.setLayoutManager(LayoutManagerRegistry.createDefaultGridLayoutManager(editor.getProject()));
            }
        }
        Rectangle rc = new Rectangle(0, 0, 1, 1);
        int minIndex = Integer.MAX_VALUE;
        if (selectionParent.getLayoutManager().isGrid()) {
            rc = FormEditingUtil.getSelectionBounds(selection);
        } else if (selectionParent.getLayoutManager().isIndexed()) {
            for (RadComponent c : selection) {
                minIndex = Math.min(minIndex, selectionParent.indexOfComponent(c));
            }
        }
        for (RadComponent c : selection) {
            selectionParent.removeComponent(c);
        }
        if (selectionParent.getLayoutManager().isGrid()) {
            final GridConstraints newConstraints = newContainer.getConstraints();
            newConstraints.setRow(rc.y);
            newConstraints.setColumn(rc.x);
            newConstraints.setRowSpan(rc.height);
            newConstraints.setColSpan(rc.width);
        } else if (selectionParent.getLayout() instanceof XYLayoutManager && selection.size() == 1) {
            newContainer.setBounds(selection.get(0).getBounds());
        }
        if (selection.size() == 1) {
            newContainer.setCustomLayoutConstraints(selection.get(0).getCustomLayoutConstraints());
        }
        if (minIndex != Integer.MAX_VALUE) {
            selectionParent.addComponent(newContainer, minIndex);
        } else {
            selectionParent.addComponent(newContainer);
        }
        if (newContainer instanceof RadTabbedPane) {
            // the first tab is created by RadTabbedPane itself
            assert newContainer.getComponentCount() == 1;
            newContainer = (RadContainer) newContainer.getComponent(0);
        } else if (newContainer instanceof RadSplitPane) {
            if (selection.size() > 2) {
                RadContainer panel = InsertComponentProcessor.createPanelComponent(editor);
                panel.setCustomLayoutConstraints(LwSplitPane.POSITION_LEFT);
                newContainer.addComponent(panel);
                newContainer = panel;
            } else {
                if (selection.size() > 0) {
                    selection.get(0).setCustomLayoutConstraints(LwSplitPane.POSITION_LEFT);
                }
                if (selection.size() > 1) {
                    selection.get(1).setCustomLayoutConstraints(LwSplitPane.POSITION_RIGHT);
                }
            }
        }
        // otherwise, copy column properties and row/col spans
        if (newContainer.getComponentClass().equals(JPanel.class) && selection.size() > 1) {
            if (selectionParent.getLayoutManager().isGrid()) {
                newContainer.getGridLayoutManager().copyGridSection(selectionParent, newContainer, rc);
            } else {
                // TODO[yole]: correctly handle surround from indexed
                newContainer.setLayout(new GridLayoutManager(rc.height, rc.width));
            }
        }
        for (RadComponent c : selection) {
            if (selectionParent.getLayoutManager().isGrid()) {
                if (selection.size() > 1) {
                    c.getConstraints().setRow(c.getConstraints().getRow() - rc.y);
                    c.getConstraints().setColumn(c.getConstraints().getColumn() - rc.x);
                } else {
                    c.getConstraints().setRow(0);
                    c.getConstraints().setColumn(0);
                    c.getConstraints().setRowSpan(1);
                    c.getConstraints().setColSpan(1);
                }
            }
            newContainer.addComponent(c);
        }
        editor.refreshAndSave(true);
    }, null, null);
}
Also used : ComponentItem(com.intellij.uiDesigner.palette.ComponentItem) Palette(com.intellij.uiDesigner.palette.Palette) GridLayoutManager(com.intellij.uiDesigner.core.GridLayoutManager) GridConstraints(com.intellij.uiDesigner.core.GridConstraints) XYLayoutManager(com.intellij.uiDesigner.shared.XYLayoutManager)

Example 3 with XYLayoutManager

use of com.intellij.uiDesigner.shared.XYLayoutManager in project intellij-community by JetBrains.

the class GridDropLocation method dropIntoGrid.

protected static void dropIntoGrid(final RadContainer container, final RadComponent[] components, int row, int column, final ComponentDragObject dragObject) {
    assert components.length > 0;
    for (int i = 0; i < components.length; i++) {
        RadComponent c = components[i];
        if (c instanceof RadContainer) {
            final LayoutManager layout = ((RadContainer) c).getLayout();
            if (layout instanceof XYLayoutManager) {
                ((XYLayoutManager) layout).setPreferredSize(c.getSize());
            }
        }
        int relativeCol = dragObject.getRelativeCol(i);
        int relativeRow = dragObject.getRelativeRow(i);
        LOG.debug("dropIntoGrid: relativeRow=" + relativeRow + ", relativeCol=" + relativeCol);
        int colSpan = dragObject.getColSpan(i);
        int rowSpan = dragObject.getRowSpan(i);
        assert row + relativeRow >= 0;
        assert column + relativeCol >= 0;
        if (rowSpan > 1 || colSpan > 1) {
            if ((row + relativeRow + rowSpan > container.getGridRowCount() && rowSpan > 1) || (column + relativeCol + colSpan > container.getGridColumnCount() && colSpan > 1) || container.findComponentInRect(row + relativeRow, column + relativeCol, rowSpan, colSpan) != null) {
                rowSpan = 1;
                colSpan = 1;
            }
        }
        if (!container.getGridLayoutManager().isGridDefinedByComponents()) {
            assert relativeRow + rowSpan <= container.getGridRowCount();
            assert relativeCol + colSpan <= container.getGridColumnCount();
        }
        RadComponent old = container.findComponentInRect(row + relativeRow, column + relativeCol, rowSpan, colSpan);
        if (old != null) {
            LOG.error("Drop rectangle not empty: (" + (row + relativeRow) + ", " + (column + relativeCol) + ", " + rowSpan + ", " + colSpan + "), component ID=" + old.getId());
        }
        final GridConstraints constraints = c.getConstraints();
        constraints.setRow(row + relativeRow);
        constraints.setColumn(column + relativeCol);
        constraints.setRowSpan(rowSpan);
        constraints.setColSpan(colSpan);
        LOG.info("GridDropLocation.dropIntoGrid() constraints=" + constraints);
        container.addComponent(c);
        // Fill DropInfo
        c.revalidate();
    }
    container.revalidate();
    LOG.info("GridDropLocation.dropIntoGrid() done");
}
Also used : LayoutManager(java.awt.LayoutManager) XYLayoutManager(com.intellij.uiDesigner.shared.XYLayoutManager) GridConstraints(com.intellij.uiDesigner.core.GridConstraints) RadComponent(com.intellij.uiDesigner.radComponents.RadComponent) XYLayoutManager(com.intellij.uiDesigner.shared.XYLayoutManager) RadContainer(com.intellij.uiDesigner.radComponents.RadContainer)

Example 4 with XYLayoutManager

use of com.intellij.uiDesigner.shared.XYLayoutManager in project intellij-community by JetBrains.

the class GridBuildUtil method breakGrid.

public static void breakGrid(final GuiEditor editor) {
    final ArrayList<RadComponent> selection = FormEditingUtil.getSelectedComponents(editor);
    if (selection.size() != 1) {
        return;
    }
    if (!(selection.get(0) instanceof RadContainer)) {
        return;
    }
    final RadContainer container = (RadContainer) selection.get(0);
    if (container instanceof RadScrollPane || container instanceof RadSplitPane || container instanceof RadTabbedPane) {
        return;
    }
    final RadContainer parent = container.getParent();
    if (parent instanceof RadRootContainer) {
        editor.getRootContainer().setMainComponentBinding(container.getBinding());
    }
    // In other words, breaking of XY is a deletion of unnecessary intermediate panel
    if (container.isXY() && !parent.isXY()) {
        return;
    }
    if (parent != null && parent.isXY()) {
        // parent is XY
        // put the contents of the container into 'parent' and remove 'container'
        final int dx = container.getX();
        final int dy = container.getY();
        while (container.getComponentCount() > 0) {
            final RadComponent component = container.getComponent(0);
            component.shift(dx, dy);
            parent.addComponent(component);
        }
        parent.removeComponent(container);
    } else {
        // container becomes XY
        final XYLayoutManager xyLayout = new XYLayoutManagerImpl();
        container.setLayout(xyLayout);
        xyLayout.setPreferredSize(container.getSize());
    }
    editor.refreshAndSave(true);
}
Also used : XYLayoutManager(com.intellij.uiDesigner.shared.XYLayoutManager)

Example 5 with XYLayoutManager

use of com.intellij.uiDesigner.shared.XYLayoutManager in project intellij-community by JetBrains.

the class GridBuildUtil method convertToGridImpl.

private static void convertToGridImpl(final GuiEditor editor, final int gridType) {
    final boolean createNewContainer;
    final RadContainer parent;
    final RadComponent[] componentsToConvert;
    {
        final ArrayList<RadComponent> selection = FormEditingUtil.getSelectedComponents(editor);
        if (selection.size() == 0) {
            // root container selected
            final RadRootContainer rootContainer = editor.getRootContainer();
            if (rootContainer.getComponentCount() < 2) {
                // nothing to convert
                return;
            }
            componentsToConvert = new RadComponent[rootContainer.getComponentCount()];
            for (int i = 0; i < componentsToConvert.length; i++) {
                componentsToConvert[i] = rootContainer.getComponent(i);
            }
            parent = rootContainer;
            createNewContainer = true;
        } else if (selection.size() == 1 && selection.get(0) instanceof RadContainer) {
            parent = (RadContainer) selection.get(0);
            componentsToConvert = new RadComponent[parent.getComponentCount()];
            for (int i = 0; i < componentsToConvert.length; i++) {
                componentsToConvert[i] = parent.getComponent(i);
            }
            createNewContainer = false;
        } else {
            componentsToConvert = selection.toArray(new RadComponent[selection.size()]);
            parent = selection.get(0).getParent();
            createNewContainer = true;
        }
    }
    if (!parent.isXY()) {
        // only components in XY can be layed out in grid
        return;
    }
    for (int i = 1; i < componentsToConvert.length; i++) {
        final RadComponent component = componentsToConvert[i];
        if (component.getParent() != parent) {
            return;
        }
    }
    final GridLayoutManager gridLayoutManager;
    if (componentsToConvert.length == 0) {
        // we convert empty XY panel to grid
        gridLayoutManager = new GridLayoutManager(1, 1);
    } else {
        if (gridType == VERTICAL_GRID) {
            gridLayoutManager = createOneDimensionGrid(componentsToConvert, true);
        } else if (gridType == HORIZONTAL_GRID) {
            gridLayoutManager = createOneDimensionGrid(componentsToConvert, false);
        } else if (gridType == GRID) {
            gridLayoutManager = createTwoDimensionGrid(componentsToConvert);
        } else {
            throw new IllegalArgumentException("invalid grid type: " + gridType);
        }
    }
    for (final RadComponent component : componentsToConvert) {
        if (component instanceof RadContainer) {
            final LayoutManager layout = ((RadContainer) component).getLayout();
            if (layout instanceof XYLayoutManager) {
                ((XYLayoutManager) layout).setPreferredSize(component.getSize());
            }
        }
    }
    if (createNewContainer) {
        // we should create a new panel
        final Module module = editor.getModule();
        final ComponentItem panelItem = Palette.getInstance(editor.getProject()).getPanelItem();
        final RadContainer newContainer = new RadContainer(editor, FormEditingUtil.generateId(editor.getRootContainer()));
        newContainer.setLayout(gridLayoutManager);
        newContainer.init(editor, panelItem);
        for (RadComponent componentToConvert : componentsToConvert) {
            newContainer.addComponent(componentToConvert);
        }
        final Point topLeftPoint = getTopLeftPoint(componentsToConvert);
        newContainer.setLocation(topLeftPoint);
        final Point bottomRightPoint = getBottomRightPoint(componentsToConvert);
        final Dimension size = new Dimension(bottomRightPoint.x - topLeftPoint.x, bottomRightPoint.y - topLeftPoint.y);
        Util.adjustSize(newContainer.getDelegee(), newContainer.getConstraints(), size);
        newContainer.getDelegee().setSize(size);
        parent.addComponent(newContainer);
        FormEditingUtil.clearSelection(editor.getRootContainer());
        newContainer.setSelected(true);
        // restore binding of main component
        {
            final String mainComponentBinding = editor.getRootContainer().getMainComponentBinding();
            if (mainComponentBinding != null && parent instanceof RadRootContainer) {
                newContainer.setBinding(mainComponentBinding);
                editor.getRootContainer().setMainComponentBinding(null);
            }
        }
    } else {
        // convert entire 'parent' to grid
        parent.setLayout(gridLayoutManager);
        FormEditingUtil.clearSelection(editor.getRootContainer());
        parent.setSelected(true);
    }
    editor.refreshAndSave(true);
}
Also used : ComponentItem(com.intellij.uiDesigner.palette.ComponentItem) ArrayList(java.util.ArrayList) GridLayoutManager(com.intellij.uiDesigner.core.GridLayoutManager) XYLayoutManager(com.intellij.uiDesigner.shared.XYLayoutManager) GridLayoutManager(com.intellij.uiDesigner.core.GridLayoutManager) XYLayoutManager(com.intellij.uiDesigner.shared.XYLayoutManager) Module(com.intellij.openapi.module.Module)

Aggregations

XYLayoutManager (com.intellij.uiDesigner.shared.XYLayoutManager)5 GridConstraints (com.intellij.uiDesigner.core.GridConstraints)2 GridLayoutManager (com.intellij.uiDesigner.core.GridLayoutManager)2 ComponentItem (com.intellij.uiDesigner.palette.ComponentItem)2 Palette (com.intellij.uiDesigner.palette.Palette)2 Module (com.intellij.openapi.module.Module)1 JBColor (com.intellij.ui.JBColor)1 RecursiveFormNestingException (com.intellij.uiDesigner.compiler.RecursiveFormNestingException)1 PsiNestedFormLoader (com.intellij.uiDesigner.make.PsiNestedFormLoader)1 IntrospectedProperty (com.intellij.uiDesigner.propertyInspector.IntrospectedProperty)1 RadComponent (com.intellij.uiDesigner.radComponents.RadComponent)1 RadContainer (com.intellij.uiDesigner.radComponents.RadContainer)1 LayoutManager (java.awt.LayoutManager)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1