1 19 20 package org.netbeans.modules.form.layoutsupport.delegates; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.util.*; 25 import java.lang.reflect.Method ; 26 27 import org.openide.util.Utilities; 28 29 import org.netbeans.modules.form.layoutsupport.*; 30 import org.netbeans.modules.form.codestructure.*; 31 32 37 38 public class NullLayoutSupport extends AbsoluteLayoutSupport { 39 40 private static Method setBoundsMethod; 41 42 43 private static String iconURL = "org/netbeans/modules/form/layoutsupport/resources/NullLayout.gif"; 45 private static String icon32URL = "org/netbeans/modules/form/layoutsupport/resources/NullLayout32.gif"; 47 51 public Class getSupportedClass() { 52 return null; 53 } 54 55 61 public Image getIcon(int type) { 62 switch (type) { 63 case BeanInfo.ICON_COLOR_16x16: 64 case BeanInfo.ICON_MONO_16x16: 65 return Utilities.loadImage(iconURL); 66 default: 67 return Utilities.loadImage(icon32URL); 68 } 69 } 70 71 75 public CodeGroup getComponentCode(int index) { 76 LayoutConstraints constr = getConstraints(index); 79 if (constr instanceof AbsoluteLayoutConstraints) { 80 AbsoluteLayoutConstraints absConstr = 81 (AbsoluteLayoutConstraints) constr; 82 if (absConstr.refComponent == null) 83 absConstr.refComponent = 84 getLayoutContext().getPrimaryComponent(index); 85 } 86 87 return super.getComponentCode(index); 88 } 89 90 96 public void setLayoutToContainer(Container container, 97 Container containerDelegate) 98 { 99 containerDelegate.setLayout(null); 100 } 101 102 110 public void addComponentsToContainer(Container container, 111 Container containerDelegate, 112 Component[] components, 113 int index) 114 { 115 for (int i=0; i < components.length; i++) { 116 LayoutConstraints constr = getConstraints(i + index); 117 if (constr instanceof AbsoluteLayoutConstraints) { 118 AbsoluteLayoutConstraints alc = (AbsoluteLayoutConstraints)constr; 119 Component comp = components[i]; 120 Rectangle bounds = alc.getBounds(); 121 if (bounds.width == -1 || bounds.height == -1) { 122 Dimension pref = !(comp instanceof javax.swing.JComponent ) 123 && alc.refComponent != null ? 124 alc.refComponent.getPreferredSize() : 125 comp.getPreferredSize(); 126 127 if (bounds.width == -1) 128 bounds.width = pref.width; 129 if (bounds.height == -1) 130 bounds.height = pref.height; 131 } 132 containerDelegate.add(comp, i + index); 133 comp.setBounds(bounds); 134 } 135 } 136 } 137 138 140 147 protected CodeExpression createInitLayoutCode(CodeGroup layoutCode) { 148 return getCodeStructure().createNullExpression(LayoutManager.class); 149 } 150 151 164 protected CodeExpression readComponentCode(CodeStatement statement, 165 CodeGroup componentCode) 166 { 167 if (getSimpleAddMethod().equals(statement.getMetaObject())) { 168 CodeExpression compExp = statement.getStatementParameters()[0]; 169 componentCode.addStatement(statement); 170 171 AbsoluteLayoutConstraints constr = 172 new AbsoluteLayoutConstraints(0, 0, -1, -1); 173 constr.nullMode = true; 174 176 Iterator it = CodeStructure.getDefinedStatementsIterator(compExp); 178 CodeStatement[] statements = CodeStructure.filterStatements( 179 it, getSetBoundsMethod()); 180 if (statements.length > 0) { 181 CodeStatement boundsStatement = 182 statements[statements.length-1]; 183 constr.readPropertyExpressions( 184 boundsStatement.getStatementParameters(), 0); 185 componentCode.addStatement(boundsStatement); 186 } 187 getConstraintsList().add(constr); 188 189 return compExp; 190 } 191 return null; 192 } 193 194 205 protected void createComponentCode(CodeGroup componentCode, 206 CodeExpression compExp, 207 int index) 208 { 209 componentCode.addStatement( 211 CodeStructure.createStatement( 212 getActiveContainerCodeExpression(), 213 getSimpleAddMethod(), 214 new CodeExpression[] { compExp })); 215 216 LayoutConstraints constr = getConstraints(index); 218 if (constr instanceof AbsoluteLayoutConstraints) { 219 AbsoluteLayoutConstraints absConstr = 220 (AbsoluteLayoutConstraints) constr; 221 absConstr.nullMode = true; 222 absConstr.refComponent = getLayoutContext().getPrimaryComponent(index); 223 224 componentCode.addStatement( 225 CodeStructure.createStatement( 226 compExp, 227 getSetBoundsMethod(), 228 absConstr.createPropertyExpressions(getCodeStructure(), 0))); 229 } 230 } 231 232 private static Method getSetBoundsMethod() { 233 if (setBoundsMethod == null) { 234 try { 235 setBoundsMethod = Component.class.getMethod( 236 "setBounds", new Class [] { Integer.TYPE, Integer.TYPE, 238 Integer.TYPE, Integer.TYPE }); 239 } 240 catch (NoSuchMethodException ex) { ex.printStackTrace(); 242 } 243 } 244 return setBoundsMethod; 245 } 246 } 247 | Popular Tags |