1 19 20 package org.netbeans.modules.form.layoutsupport.delegates; 21 22 import java.awt.*; 23 import javax.swing.*; 24 import java.beans.*; 25 import java.util.Iterator ; 26 import java.lang.reflect.Method ; 27 28 import org.openide.nodes.*; 29 30 import org.netbeans.modules.form.layoutsupport.*; 31 import org.netbeans.modules.form.codestructure.*; 32 import org.netbeans.modules.form.FormProperty; 33 34 41 42 public class JLayeredPaneSupport extends AbsoluteLayoutSupport { 43 44 private static Method setBoundsMethod; 45 46 49 public Class getSupportedClass() { 50 return JLayeredPane.class; 51 } 52 53 64 public void convertConstraints(LayoutConstraints[] previousConstraints, 65 LayoutConstraints[] currentConstraints, 66 Component[] components) 67 { 68 return; } 70 71 78 public void addComponentsToContainer(Container container, 79 Container containerDelegate, 80 Component[] components, 81 int index) 82 { 83 if (!(container instanceof JLayeredPane)) 84 return; 85 86 for (int i=0; i < components.length; i++) { 87 LayoutConstraints constraints = getConstraints(i + index); 88 if (constraints instanceof LayeredConstraints) { 89 Component comp = components[i]; 90 container.add(comp, constraints.getConstraintsObject(), i + index); 91 92 Rectangle bounds = ((LayeredConstraints)constraints).getBounds(); 93 if (bounds.width == -1 || bounds.height == -1) { 94 Dimension pref = comp.getPreferredSize(); 95 if (bounds.width == -1) 96 bounds.width = pref.width; 97 if (bounds.height == -1) 98 bounds.height = pref.height; 99 } 100 comp.setBounds(bounds); 101 } 102 } 103 } 104 105 107 117 protected LayoutConstraints readConstraintsCode(CodeExpression constrExp, 118 CodeGroup constrCode, 119 CodeExpression compExp) 120 { 121 LayeredConstraints constr = new LayeredConstraints(0, 0, 0, -1, -1); 122 124 Iterator it = CodeStructure.getDefinedStatementsIterator(compExp); 125 CodeStatement[] statements = CodeStructure.filterStatements( 126 it, getSetBoundsMethod()); 127 if (statements.length > 0) { 128 CodeStatement boundsStatement = statements[statements.length-1]; 129 constr.readPropertyExpressions( 130 boundsStatement.getStatementParameters(), 1); 131 constrCode.addStatement(boundsStatement); 132 } 133 134 FormCodeSupport.readPropertyExpression(constrExp, 135 constr.getProperties()[0], 136 false); 137 138 return constr; 139 } 140 141 149 protected CodeExpression createConstraintsCode(CodeGroup constrCode, 150 LayoutConstraints constr, 151 CodeExpression compExp, 152 int index) 153 { 154 if (!(constr instanceof LayeredConstraints)) 155 return null; 156 157 LayeredConstraints layerConstr = (LayeredConstraints) constr; 158 layerConstr.refComponent = getLayoutContext().getPrimaryComponent(index); 159 160 CodeStructure codeStructure = getCodeStructure(); 161 162 CodeStatement boundsStatement = CodeStructure.createStatement( 163 compExp, 164 getSetBoundsMethod(), 165 layerConstr.createPropertyExpressions(codeStructure, 1)); 166 constrCode.addStatement(boundsStatement); 167 168 return codeStructure.createExpression( 169 FormCodeSupport.createOrigin(layerConstr.getProperties()[0])); 170 } 171 172 177 protected LayoutConstraints createDefaultConstraints() { 178 return new LayeredConstraints(0, 0, 0, -1, -1); 179 } 180 181 183 protected LayoutConstraints createNewConstraints( 185 LayoutConstraints currentConstr, 186 int x, int y, int w, int h) 187 { 188 int layer = currentConstr instanceof LayeredConstraints ? 189 ((LayeredConstraints)currentConstr).getLayer() : 0; 190 191 return new LayeredConstraints(layer, x, y, w, h); 192 } 193 194 private static Method getSetBoundsMethod() { 195 if (setBoundsMethod == null) { 196 try { 197 setBoundsMethod = Component.class.getMethod( 198 "setBounds", new Class [] { Integer.TYPE, Integer.TYPE, 200 Integer.TYPE, Integer.TYPE }); 201 } 202 catch (NoSuchMethodException ex) { ex.printStackTrace(); 204 } 205 } 206 return setBoundsMethod; 207 } 208 209 211 214 public static class LayeredConstraints extends AbsoluteLayoutConstraints { 215 private int layer; 216 217 public LayeredConstraints(int layer, int x, int y, int w, int h) { 218 super(x, y, w, h); 219 this.layer = layer; 220 nullMode = true; 221 } 222 223 public int getLayer() { 224 return layer; 225 } 226 227 229 public Object getConstraintsObject() { 230 return new Integer (layer); 231 } 232 233 public LayoutConstraints cloneConstraints() { 234 return new LayeredConstraints(layer, x, y, w, h); 235 } 236 237 239 protected Node.Property[] createProperties() { 240 Node.Property[] props = super.createProperties(); 241 Node.Property[] layeredProps = new Node.Property[props.length + 1]; 242 243 layeredProps[0] = 244 new FormProperty("LayeredConstraints layer", Integer.TYPE, 246 getBundle().getString("PROP_layer"), getBundle().getString("HINT_layer")) { 249 public Object getTargetValue() { 250 return new Integer (layer); 251 } 252 public void setTargetValue(Object value) { 253 layer = ((Integer )value).intValue(); 254 } 255 public boolean supportsDefaultValue () { 256 return true; 257 } 258 public Object getDefaultValue() { 259 return new Integer (0); 260 } 261 public PropertyEditor getExpliciteEditor() { 262 return new LayerEditor(); 263 } 264 public Object getValue(String key) { 265 if ("canEditAsText".equals(key)) return Boolean.TRUE; 267 return super.getValue(key); 268 } 269 public void setPropertyContext( 270 org.netbeans.modules.form.FormPropertyContext ctx) 271 { } }; 274 275 for (int i=0; i < props.length; i++) 276 layeredProps[i+1] = props[i]; 277 278 return layeredProps; 279 } 280 } 281 282 284 public static final class LayerEditor extends PropertyEditorSupport { 285 286 final String [] tags = { 287 "DEFAULT_LAYER", "PALETTE_LAYER", "MODAL_LAYER", "POPUP_LAYER", "DRAG_LAYER" }; 293 294 final Integer [] values = { 295 JLayeredPane.DEFAULT_LAYER, 296 JLayeredPane.PALETTE_LAYER, 297 JLayeredPane.MODAL_LAYER, 298 JLayeredPane.POPUP_LAYER, 299 JLayeredPane.DRAG_LAYER 300 }; 301 302 final String [] javaInitStrings = { 303 "javax.swing.JLayeredPane.DEFAULT_LAYER", "javax.swing.JLayeredPane.PALETTE_LAYER", "javax.swing.JLayeredPane.MODAL_LAYER", "javax.swing.JLayeredPane.POPUP_LAYER", "javax.swing.JLayeredPane.DRAG_LAYER" }; 309 310 public String [] getTags() { 311 return tags; 312 } 313 314 public String getAsText() { 315 Object value = getValue(); 316 for (int i=0; i < values.length; i++) 317 if (values[i].equals(value)) 318 return tags[i]; 319 320 return value.toString(); 321 } 322 323 public void setAsText(String str) { 324 for (int i=0; i < tags.length; i++) 325 if (tags[i].equals(str)) { 326 setValue(values[i]); 327 return; 328 } 329 330 try { 331 setValue(new Integer (Integer.parseInt(str))); 332 } 333 catch (NumberFormatException e) {} } 335 336 public String getJavaInitializationString() { 337 Object value = getValue(); 338 for (int i=0; i < values.length; i++) 339 if (values[i].equals(value)) 340 return javaInitStrings[i]; 341 342 return value != null ? 343 "new Integer(" + value.toString() + ")" : null; 345 } 346 } 347 } 348 | Popular Tags |