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.
16 package com
.intellij
.uiDesigner
.make
;
18 import com
.intellij
.uiDesigner
.compiler
.GridBagConverter
;
19 import com
.intellij
.uiDesigner
.core
.Spacer
;
20 import com
.intellij
.uiDesigner
.lw
.LwComponent
;
21 import com
.intellij
.uiDesigner
.lw
.LwContainer
;
22 import gnu
.trove
.TIntObjectHashMap
;
23 import org
.jetbrains
.annotations
.NonNls
;
31 public class GridBagLayoutSourceGenerator
extends LayoutSourceGenerator
{
32 private boolean myHaveGbc
= false;
33 @NonNls private static final TIntObjectHashMap
<String
> myFillMap
= new TIntObjectHashMap
<String
>();
34 @NonNls private static final TIntObjectHashMap
<String
> myAnchorMap
= new TIntObjectHashMap
<String
>();
37 myFillMap
.put(GridBagConstraints
.HORIZONTAL
, "java.awt.GridBagConstraints.HORIZONTAL");
38 myFillMap
.put(GridBagConstraints
.VERTICAL
, "java.awt.GridBagConstraints.VERTICAL");
39 myFillMap
.put(GridBagConstraints
.BOTH
, "java.awt.GridBagConstraints.BOTH");
41 myAnchorMap
.put(GridBagConstraints
.NORTHWEST
, "java.awt.GridBagConstraints.NORTHWEST");
42 myAnchorMap
.put(GridBagConstraints
.NORTH
, "java.awt.GridBagConstraints.NORTH");
43 myAnchorMap
.put(GridBagConstraints
.NORTHEAST
, "java.awt.GridBagConstraints.NORTHEAST");
44 myAnchorMap
.put(GridBagConstraints
.EAST
, "java.awt.GridBagConstraints.EAST");
45 myAnchorMap
.put(GridBagConstraints
.SOUTHEAST
, "java.awt.GridBagConstraints.SOUTHEAST");
46 myAnchorMap
.put(GridBagConstraints
.SOUTH
, "java.awt.GridBagConstraints.SOUTH");
47 myAnchorMap
.put(GridBagConstraints
.SOUTHWEST
, "java.awt.GridBagConstraints.SOUTHWEST");
48 myAnchorMap
.put(GridBagConstraints
.WEST
, "java.awt.GridBagConstraints.WEST");
52 public void generateContainerLayout(final LwContainer container
,
53 final FormSourceCodeGenerator generator
,
54 final String variable
) {
55 generator
.startMethodCall(variable
, "setLayout");
57 generator
.startConstructor(GridBagLayout
.class.getName());
58 generator
.endConstructor();
60 generator
.endMethod();
64 public void generateComponentLayout(final LwComponent component
,
65 final FormSourceCodeGenerator generator
,
66 final String variable
,
67 final String parentVariable
) {
68 GridBagConstraints gbc
;
69 if (component
.getCustomLayoutConstraints() instanceof GridBagConstraints
) {
70 gbc
= (GridBagConstraints
) component
.getCustomLayoutConstraints();
73 gbc
= new GridBagConstraints();
76 GridBagConverter
.constraintsToGridBag(component
.getConstraints(), gbc
);
78 generateGridBagConstraints(generator
, gbc
, variable
, parentVariable
);
81 private void generateConversionResult(final FormSourceCodeGenerator generator
,
82 final GridBagConverter
.Result result
,
83 final String variable
, final String parentVariable
) {
84 checkSetSize(generator
, variable
, "setMinimumSize", result
.minimumSize
);
85 checkSetSize(generator
, variable
, "setPreferredSize", result
.preferredSize
);
86 checkSetSize(generator
, variable
, "setMaximumSize", result
.maximumSize
);
88 generateGridBagConstraints(generator
, result
.constraints
, variable
, parentVariable
);
91 private void generateGridBagConstraints(final FormSourceCodeGenerator generator
,
92 final GridBagConstraints constraints
,
93 final String variable
,
94 final String parentVariable
) {
96 generator
.append("java.awt.GridBagConstraints gbc;\n");
99 generator
.append("gbc = new java.awt.GridBagConstraints();\n");
101 GridBagConstraints defaults
= new GridBagConstraints();
102 if (defaults
.gridx
!= constraints
.gridx
) {
103 setIntField(generator
, "gridx", constraints
.gridx
);
105 if (defaults
.gridy
!= constraints
.gridy
) {
106 setIntField(generator
, "gridy", constraints
.gridy
);
108 if (defaults
.gridwidth
!= constraints
.gridwidth
) {
109 setIntField(generator
, "gridwidth", constraints
.gridwidth
);
111 if (defaults
.gridheight
!= constraints
.gridheight
) {
112 setIntField(generator
, "gridheight", constraints
.gridheight
);
114 if (defaults
.weightx
!= constraints
.weightx
) {
115 setDoubleField(generator
, "weightx", constraints
.weightx
);
117 if (defaults
.weighty
!= constraints
.weighty
) {
118 setDoubleField(generator
, "weighty", constraints
.weighty
);
120 if (defaults
.anchor
!= constraints
.anchor
) {
121 setIntField(generator
, "anchor", constraints
.anchor
, myAnchorMap
);
123 if (defaults
.fill
!= constraints
.fill
) {
124 setIntField(generator
, "fill", constraints
.fill
, myFillMap
);
126 if (defaults
.ipadx
!= constraints
.ipadx
) {
127 setIntField(generator
, "ipadx", constraints
.ipadx
);
129 if (defaults
.ipady
!= constraints
.ipady
) {
130 setIntField(generator
, "ipady", constraints
.ipady
);
132 if (!defaults
.insets
.equals(constraints
.insets
)) {
133 generator
.append("gbc.insets=");
134 generator
.newInsets(constraints
.insets
);
137 generator
.startMethodCall(parentVariable
, "add");
138 generator
.pushVar(variable
);
139 generator
.pushVar("gbc");
140 generator
.endMethod();
143 private static void setIntField(final FormSourceCodeGenerator generator
, @NonNls final String fieldName
, final int value
) {
144 generator
.append("gbc.");
145 generator
.append(fieldName
);
146 generator
.append("=");
147 generator
.append(value
);
148 generator
.append(";\n");
151 private static void setIntField(final FormSourceCodeGenerator generator
, @NonNls final String fieldName
, final int value
,
152 final TIntObjectHashMap
<String
> map
) {
153 generator
.append("gbc.");
154 generator
.append(fieldName
);
155 generator
.append("=");
156 if (map
.containsKey(value
)) {
157 generator
.append(map
.get(value
));
160 generator
.append(value
);
162 generator
.append(";\n");
165 private static void setDoubleField(final FormSourceCodeGenerator generator
, @NonNls final String fieldName
, final double value
) {
166 generator
.append("gbc.");
167 generator
.append(fieldName
);
168 generator
.append("=");
169 generator
.append(value
);
170 generator
.append(";\n");
173 private static void checkSetSize(final FormSourceCodeGenerator generator
,
174 final String variable
,
175 @NonNls final String methodName
,
176 final Dimension dimension
) {
177 if (dimension
!= null) {
178 generator
.startMethodCall(variable
, methodName
);
179 generator
.newDimension(dimension
);
180 generator
.endMethod();
185 public String
mapComponentClass(final String componentClassName
) {
186 if (componentClassName
.equals(Spacer
.class.getName())) {
187 return JPanel
.class.getName();
189 return super.mapComponentClass(componentClassName
);