com.intellij.uiDesigner.make.FormLayoutSourceGenerator.java Source code

Java tutorial

Introduction

Here is the source code for com.intellij.uiDesigner.make.FormLayoutSourceGenerator.java

Source

/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.uiDesigner.make;

import com.intellij.uiDesigner.compiler.FormLayoutCodeGenerator;
import com.intellij.uiDesigner.compiler.Utils;
import com.intellij.uiDesigner.compiler.FormLayoutUtils;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.lw.LwComponent;
import com.intellij.uiDesigner.lw.LwContainer;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import org.jetbrains.annotations.NonNls;

import java.awt.Insets;

/**
 * @author yole
 */
public class FormLayoutSourceGenerator extends LayoutSourceGenerator {
    private boolean myHaveCc = false;

    @Override
    public void generateContainerLayout(final LwContainer component, final FormSourceCodeGenerator generator,
            final String variable) {
        FormLayout layout = (FormLayout) component.getLayout();
        generator.startMethodCall(variable, "setLayout");

        generator.startConstructor(FormLayout.class.getName());
        generator.push(FormLayoutUtils.getEncodedColumnSpecs(layout));
        generator.push(FormLayoutUtils.getEncodedRowSpecs(layout));
        generator.endConstructor();

        generator.endMethod();

        generateGroups(generator, variable, "setRowGroups", layout.getRowGroups());
        generateGroups(generator, variable, "setColumnGroups", layout.getColumnGroups());
    }

    private static void generateGroups(final FormSourceCodeGenerator generator, final String variable,
            @NonNls final String methodName, final int[][] groups) {
        if (groups.length == 0)
            return;
        generator.startMethodCall("((com.jgoodies.forms.layout.FormLayout) " + variable + ".getLayout())",
                methodName);

        @NonNls
        StringBuilder groupBuilder = new StringBuilder("new int[][] {");
        for (int i = 0; i < groups.length; i++) {
            if (i > 0)
                groupBuilder.append(", ");
            groupBuilder.append("new int[] { ");
            for (int j = 0; j < groups[i].length; j++) {
                if (j > 0)
                    groupBuilder.append(", ");
                groupBuilder.append(groups[i][j]);
            }
            groupBuilder.append(" }");
        }
        groupBuilder.append(" }");

        generator.pushVar(groupBuilder.toString());
        generator.endMethod();
    }

    public void generateComponentLayout(final LwComponent component, final FormSourceCodeGenerator generator,
            final String variable, final String parentVariable) {
        if (!myHaveCc) {
            generator.append(
                    "com.jgoodies.forms.layout.CellConstraints cc = new com.jgoodies.forms.layout.CellConstraints();\n");
            myHaveCc = true;
        }
        generator.startMethodCall(parentVariable, "add");
        generator.pushVar(variable);

        CellConstraints cc = (CellConstraints) component.getCustomLayoutConstraints();
        GridConstraints constraints = component.getConstraints();
        final boolean haveInsets = !cc.insets.equals(new Insets(0, 0, 0, 0));
        if (haveInsets) {
            generator.startConstructor(CellConstraints.class.getName());
        } else {
            if (constraints.getColSpan() == 1 && constraints.getRowSpan() == 1) {
                generator.startMethodCall("cc", "xy");
            } else if (constraints.getRowSpan() == 1) {
                generator.startMethodCall("cc", "xyw");
            } else {
                generator.startMethodCall("cc", "xywh");
            }
        }
        generator.push(constraints.getColumn() + 1);
        generator.push(constraints.getRow() + 1);
        if (constraints.getColSpan() > 1 || constraints.getRowSpan() > 1 || haveInsets) {
            generator.push(constraints.getColSpan());
        }
        if (constraints.getRowSpan() > 1 || haveInsets) {
            generator.push(constraints.getRowSpan());
        }

        if (cc.hAlign != CellConstraints.DEFAULT || cc.vAlign != CellConstraints.DEFAULT || haveInsets) {
            @NonNls
            String hAlign = (cc.hAlign == CellConstraints.DEFAULT) ? "DEFAULT"
                    : FormLayoutCodeGenerator.HORZ_ALIGN_FIELDS[Utils.alignFromConstraints(constraints, true)];
            @NonNls
            String vAlign = (cc.vAlign == CellConstraints.DEFAULT) ? "DEFAULT"
                    : FormLayoutCodeGenerator.VERT_ALIGN_FIELDS[Utils.alignFromConstraints(constraints, false)];
            generator.pushVar("com.jgoodies.forms.layout.CellConstraints." + hAlign);
            generator.pushVar("com.jgoodies.forms.layout.CellConstraints." + vAlign);
        }

        if (haveInsets) {
            generator.newInsets(cc.insets);
        }
        generator.endMethod();
        generator.endMethod();
    }
}