update copyright
[fedora-idea.git] / plugins / ui-designer / src / com / intellij / uiDesigner / make / FormLayoutSourceGenerator.java
blobec43aad5aff4b9a86cca258cc9914f84f59c9b70
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.uiDesigner.make;
19 import com.intellij.uiDesigner.compiler.FormLayoutCodeGenerator;
20 import com.intellij.uiDesigner.compiler.Utils;
21 import com.intellij.uiDesigner.compiler.FormLayoutUtils;
22 import com.intellij.uiDesigner.core.GridConstraints;
23 import com.intellij.uiDesigner.lw.LwComponent;
24 import com.intellij.uiDesigner.lw.LwContainer;
25 import com.jgoodies.forms.layout.CellConstraints;
26 import com.jgoodies.forms.layout.FormLayout;
27 import org.jetbrains.annotations.NonNls;
29 import java.awt.Insets;
31 /**
32 * @author yole
34 public class FormLayoutSourceGenerator extends LayoutSourceGenerator {
35 private boolean myHaveCc = false;
37 @Override
38 public void generateContainerLayout(final LwContainer component, final FormSourceCodeGenerator generator, final String variable) {
39 FormLayout layout = (FormLayout) component.getLayout();
40 generator.startMethodCall(variable, "setLayout");
42 generator.startConstructor(FormLayout.class.getName());
43 generator.push(FormLayoutUtils.getEncodedColumnSpecs(layout));
44 generator.push(FormLayoutUtils.getEncodedRowSpecs(layout));
45 generator.endConstructor();
47 generator.endMethod();
49 generateGroups(generator, variable, "setRowGroups", layout.getRowGroups());
50 generateGroups(generator, variable, "setColumnGroups", layout.getColumnGroups());
53 private static void generateGroups(final FormSourceCodeGenerator generator, final String variable,
54 @NonNls final String methodName, final int[][] groups) {
55 if (groups.length == 0) return;
56 generator.startMethodCall("((com.jgoodies.forms.layout.FormLayout) " + variable + ".getLayout())",
57 methodName);
59 @NonNls StringBuilder groupBuilder = new StringBuilder("new int[][] {");
60 for(int i=0; i<groups.length; i++) {
61 if (i > 0) groupBuilder.append(", ");
62 groupBuilder.append("new int[] { ");
63 for(int j=0; j<groups [i].length; j++) {
64 if (j > 0) groupBuilder.append(", ");
65 groupBuilder.append(groups [i] [j]);
67 groupBuilder.append(" }");
69 groupBuilder.append(" }");
71 generator.pushVar(groupBuilder.toString());
72 generator.endMethod();
75 public void generateComponentLayout(final LwComponent component, final FormSourceCodeGenerator generator, final String variable, final String parentVariable) {
76 if (!myHaveCc) {
77 generator.append("com.jgoodies.forms.layout.CellConstraints cc = new com.jgoodies.forms.layout.CellConstraints();\n");
78 myHaveCc = true;
80 generator.startMethodCall(parentVariable, "add");
81 generator.pushVar(variable);
83 CellConstraints cc = (CellConstraints) component.getCustomLayoutConstraints();
84 GridConstraints constraints = component.getConstraints();
85 final boolean haveInsets = !cc.insets.equals(new Insets(0, 0, 0, 0));
86 if (haveInsets) {
87 generator.startConstructor(CellConstraints.class.getName());
89 else {
90 if (constraints.getColSpan() == 1 && constraints.getRowSpan() == 1) {
91 generator.startMethodCall("cc", "xy");
93 else if (constraints.getRowSpan() == 1) {
94 generator.startMethodCall("cc", "xyw");
96 else {
97 generator.startMethodCall("cc", "xywh");
100 generator.push(constraints.getColumn()+1);
101 generator.push(constraints.getRow()+1);
102 if (constraints.getColSpan() > 1 || constraints.getRowSpan() > 1 || haveInsets) {
103 generator.push(constraints.getColSpan());
105 if (constraints.getRowSpan() > 1 || haveInsets) {
106 generator.push(constraints.getRowSpan());
109 if (cc.hAlign != CellConstraints.DEFAULT || cc.vAlign != CellConstraints.DEFAULT || haveInsets) {
110 @NonNls String hAlign = (cc.hAlign == CellConstraints.DEFAULT)
111 ? "DEFAULT"
112 : FormLayoutCodeGenerator.HORZ_ALIGN_FIELDS [Utils.alignFromConstraints(constraints, true)];
113 @NonNls String vAlign = (cc.vAlign == CellConstraints.DEFAULT)
114 ? "DEFAULT"
115 : FormLayoutCodeGenerator.VERT_ALIGN_FIELDS [Utils.alignFromConstraints(constraints, false)];
116 generator.pushVar("com.jgoodies.forms.layout.CellConstraints." + hAlign);
117 generator.pushVar("com.jgoodies.forms.layout.CellConstraints." + vAlign);
120 if (haveInsets) {
121 generator.newInsets(cc.insets);
123 generator.endMethod();
124 generator.endMethod();