1 19 20 package org.netbeans.modules.form; 21 22 import java.awt.*; 23 import java.beans.*; 24 25 import org.openide.nodes.*; 26 27 33 34 public class RADVisualFormContainer extends RADVisualContainer implements FormContainer 35 { 36 public static final String PROP_FORM_SIZE_POLICY = "formSizePolicy"; public static final String PROP_FORM_SIZE = "formSize"; public static final String PROP_FORM_POSITION = "formPosition"; public static final String PROP_GENERATE_POSITION = "generatePosition"; public static final String PROP_GENERATE_SIZE = "generateSize"; public static final String PROP_GENERATE_CENTER = "generateCenter"; 43 public static final int GEN_BOUNDS = 0; 44 public static final int GEN_PACK = 1; 45 public static final int GEN_NOTHING = 2; 46 47 private Dimension designerSize; 49 private Dimension formSize; private Point formPosition; 51 private boolean generatePosition = true; 52 private boolean generateSize = true; 53 private boolean generateCenter = true; 54 private int formSizePolicy = GEN_NOTHING; 55 56 59 64 public String getName() { 65 return FormUtils.getBundleString("CTL_FormTopContainerName"); } 67 68 73 public void setName(String value) { 74 } 76 77 public Point getFormPosition() { 78 if (formPosition == null) { 79 formPosition = new Point(0,0); } 81 return formPosition; 82 } 83 84 public void setFormPosition(Point value) { 85 Object old = formPosition; 86 formPosition = value; 87 getFormModel().fireSyntheticPropertyChanged(this, PROP_FORM_POSITION, 88 old, value); 89 } 90 91 public Dimension getFormSize() { 92 return formSize; 93 } 94 95 public void setFormSize(Dimension value) { 96 Dimension old = setFormSizeImpl(value); 97 98 Dimension designerSize; 102 if (getBeanInstance() instanceof Dialog 103 || getBeanInstance() instanceof Frame) 104 { 105 Dimension diffDim = getWindowContentDimensionDiff(); 106 designerSize = new Dimension(value.width - diffDim.width, 107 value.height - diffDim.height); 108 } 109 else designerSize = value; 110 setDesignerSizeImpl(designerSize, false); 111 112 getFormModel().fireSyntheticPropertyChanged(this, PROP_FORM_SIZE, old, value); 113 getFormModel().fireSyntheticPropertyChanged(this, FormDesigner.PROP_DESIGNER_SIZE, null, null); 114 } 115 116 private Dimension setFormSizeImpl(Dimension value) { 117 Dimension old = formSize; 118 formSize = value; 119 if (getNodeReference() != null) { getNodeReference().firePropertyChangeHelper(PROP_FORM_SIZE, old, value); 121 } 122 return old; 123 } 124 125 public Dimension getDesignerSize() { 126 return designerSize; 127 } 128 129 public void setDesignerSize(Dimension value) { 130 Dimension old = setDesignerSizeImpl(value); 131 132 if (getFormSizePolicy() == GEN_BOUNDS) { Dimension formSize; 134 if (getBeanInstance() instanceof Dialog 135 || getBeanInstance() instanceof Frame) 136 { 137 Dimension diffDim = getWindowContentDimensionDiff(); 138 formSize = new Dimension(value.width + diffDim.width, 139 value.height + diffDim.height); 140 } 141 else formSize = value; 142 setFormSizeImpl(formSize); 143 } 144 145 getFormModel().fireSyntheticPropertyChanged(this, FormDesigner.PROP_DESIGNER_SIZE, old, value); 146 } 147 148 private boolean shouldPersistDesignerSize() { 149 return !hasExplicitSize() && (getLayoutSupport() != null); 152 } 153 154 Dimension setDesignerSizeImpl(Dimension value) { 155 return setDesignerSizeImpl(value, shouldPersistDesignerSize()); 156 } 157 158 private Dimension setDesignerSizeImpl(Dimension value, boolean persistent) { 159 Dimension old = designerSize; 160 designerSize = value; 161 setAuxValue(FormDesigner.PROP_DESIGNER_SIZE, persistent ? value : null); 162 if (getNodeReference() != null) { getNodeReference().firePropertyChangeHelper(FormDesigner.PROP_DESIGNER_SIZE, old, value); 164 } 165 return old; 166 } 167 168 public boolean getGeneratePosition() { 169 return generatePosition; 170 } 171 172 public void setGeneratePosition(boolean value) { 173 boolean old = generatePosition; 174 generatePosition = value; 175 getFormModel().fireSyntheticPropertyChanged(this, PROP_GENERATE_POSITION, 176 old ? Boolean.TRUE : Boolean.FALSE, value ? Boolean.TRUE : Boolean.FALSE); 177 } 178 179 public boolean getGenerateSize() { 180 return generateSize; 181 } 182 183 public void setGenerateSize(boolean value) { 184 boolean old = generateSize; 185 generateSize = value; 186 getFormModel().fireSyntheticPropertyChanged(this, PROP_GENERATE_SIZE, 187 old ? Boolean.TRUE : Boolean.FALSE, value ? Boolean.TRUE : Boolean.FALSE); 188 } 189 190 public boolean getGenerateCenter() { 191 return generateCenter; 192 } 193 194 public void setGenerateCenter(boolean value) { 195 boolean old = generateCenter; 196 generateCenter = value; 197 getFormModel().fireSyntheticPropertyChanged(this, PROP_GENERATE_CENTER, 198 old ? Boolean.TRUE : Boolean.FALSE, value ? Boolean.TRUE : Boolean.FALSE); 199 } 200 201 public boolean hasExplicitSize() { 202 return getFormSizePolicy() == GEN_BOUNDS && getGenerateSize(); 203 } 204 205 public int getFormSizePolicy() { 206 return java.awt.Window .class.isAssignableFrom(getBeanClass()) 207 || javax.swing.JInternalFrame .class.isAssignableFrom(getBeanClass()) 208 ? formSizePolicy : GEN_NOTHING; 209 } 210 211 public void setFormSizePolicy(int value) { 212 int old = formSizePolicy; 213 formSizePolicy = value; 214 if (value == GEN_BOUNDS) { 215 if (designerSize != null) { 216 setDesignerSize(getDesignerSize()); if (getGenerateSize()) 219 setAuxValue(FormDesigner.PROP_DESIGNER_SIZE, null); 220 } 221 } 222 else if (!getFormModel().isFreeDesignDefaultLayout()) { 223 setAuxValue(FormDesigner.PROP_DESIGNER_SIZE, getDesignerSize()); 225 } 226 getFormModel().fireSyntheticPropertyChanged(this, PROP_FORM_SIZE_POLICY, 227 new Integer (old), new Integer (value)); 228 } 229 230 233 protected Node.Property[] createSyntheticProperties() { 234 java.util.ResourceBundle bundle = FormUtils.getBundle(); 235 236 Node.Property policyProperty = new PropertySupport.ReadWrite( 237 PROP_FORM_SIZE_POLICY, 238 Integer.TYPE, 239 bundle.getString("MSG_FormSizePolicy"), bundle.getString("HINT_FormSizePolicy")) { 242 public Object getValue() throws 243 IllegalAccessException , IllegalArgumentException , java.lang.reflect.InvocationTargetException { 244 return new Integer (getFormSizePolicy()); 245 } 246 247 public void setValue(Object val) throws IllegalAccessException , 248 IllegalArgumentException , java.lang.reflect.InvocationTargetException { 249 if (!(val instanceof Integer )) throw new IllegalArgumentException (); 250 setFormSizePolicy(((Integer )val).intValue()); 251 if (getNodeReference() != null) 252 getNodeReference().fireComponentPropertySetsChange(); 253 } 254 255 public boolean canWrite() { 256 return !isReadOnly(); 257 } 258 259 260 public java.beans.PropertyEditor getPropertyEditor() { 261 return new SizePolicyEditor(); 262 } 263 264 }; 265 266 Node.Property sizeProperty = new PropertySupport.ReadWrite( 267 PROP_FORM_SIZE, 268 Dimension.class, 269 bundle.getString("MSG_FormSize"), bundle.getString("HINT_FormSize")) { 272 public Object getValue() throws 273 IllegalAccessException , IllegalArgumentException , java.lang.reflect.InvocationTargetException { 274 return getFormSize(); 275 } 276 277 public void setValue(Object val) throws IllegalAccessException , 278 IllegalArgumentException , java.lang.reflect.InvocationTargetException { 279 setFormSize((Dimension)val); 281 } 282 283 public boolean canWrite() { 284 return !isReadOnly() 285 && getFormSizePolicy() == GEN_BOUNDS 286 && getGenerateSize(); 287 } 288 }; 289 290 Node.Property positionProperty = new PropertySupport.ReadWrite( 291 PROP_FORM_POSITION, 292 Point.class, 293 bundle.getString("MSG_FormPosition"), bundle.getString("HINT_FormPosition")) { 296 public Object getValue() throws 297 IllegalAccessException , IllegalArgumentException , java.lang.reflect.InvocationTargetException { 298 return getFormPosition(); 299 } 300 301 public void setValue(Object val) throws IllegalAccessException , 302 IllegalArgumentException , java.lang.reflect.InvocationTargetException { 303 if (!(val instanceof Point)) throw new IllegalArgumentException (); 304 setFormPosition((Point)val); 305 } 306 307 public boolean canWrite() { 308 return !isReadOnly() 309 && getFormSizePolicy() == GEN_BOUNDS 310 && getGeneratePosition() 311 && !getGenerateCenter(); 312 } 313 }; 314 315 Node.Property genPositionProperty = new PropertySupport.ReadWrite( 316 PROP_GENERATE_POSITION, 317 Boolean.TYPE, 318 bundle.getString("MSG_GeneratePosition"), bundle.getString("HINT_GeneratePosition")) { 321 public Object getValue() throws 322 IllegalAccessException , IllegalArgumentException , java.lang.reflect.InvocationTargetException { 323 return getGeneratePosition() ? Boolean.TRUE : Boolean.FALSE; 324 } 325 326 public void setValue(Object val) throws IllegalAccessException , 327 IllegalArgumentException , java.lang.reflect.InvocationTargetException { 328 if (!(val instanceof Boolean )) throw new IllegalArgumentException (); 329 setGeneratePosition(((Boolean )val).booleanValue()); 330 if (getNodeReference() != null) 331 getNodeReference().fireComponentPropertySetsChange(); 332 } 333 334 public boolean canWrite() { 335 return !isReadOnly() 336 && getFormSizePolicy() == GEN_BOUNDS 337 && !getGenerateCenter(); 338 } 339 }; 340 341 Node.Property genSizeProperty = new PropertySupport.ReadWrite( 342 PROP_GENERATE_SIZE, 343 Boolean.TYPE, 344 bundle.getString("MSG_GenerateSize"), bundle.getString("HINT_GenerateSize")) { 347 public Object getValue() throws 348 IllegalAccessException , IllegalArgumentException , java.lang.reflect.InvocationTargetException { 349 return getGenerateSize() ? Boolean.TRUE : Boolean.FALSE; 350 } 351 352 public void setValue(Object val) throws IllegalAccessException , 353 IllegalArgumentException , java.lang.reflect.InvocationTargetException { 354 if (!(val instanceof Boolean )) throw new IllegalArgumentException (); 355 setGenerateSize(((Boolean )val).booleanValue()); 356 if (getNodeReference() != null) 357 getNodeReference().fireComponentPropertySetsChange(); 358 } 359 360 public boolean canWrite() { 361 return !isReadOnly() && getFormSizePolicy() == GEN_BOUNDS; 362 } 363 }; 364 365 Node.Property genCenterProperty = new PropertySupport.ReadWrite( 366 PROP_GENERATE_CENTER, 367 Boolean.TYPE, 368 bundle.getString("MSG_GenerateCenter"), bundle.getString("HINT_GenerateCenter")) { 371 public Object getValue() throws 372 IllegalAccessException , IllegalArgumentException , java.lang.reflect.InvocationTargetException { 373 return getGenerateCenter() ? Boolean.TRUE : Boolean.FALSE; 374 } 375 376 public void setValue(Object val) throws IllegalAccessException , 377 IllegalArgumentException , java.lang.reflect.InvocationTargetException { 378 if (!(val instanceof Boolean )) throw new IllegalArgumentException (); 379 setGenerateCenter(((Boolean )val).booleanValue()); 380 if (getNodeReference() != null) 381 getNodeReference().fireComponentPropertySetsChange(); 382 } 383 384 public boolean canWrite() { 385 return !isReadOnly() && getFormSizePolicy() == GEN_BOUNDS; 386 } 387 }; 388 389 Node.Property designerSizeProperty = new PropertySupport.ReadOnly( 390 FormDesigner.PROP_DESIGNER_SIZE, 391 Dimension.class, 392 bundle.getString("MSG_DesignerSize"), bundle.getString("HINT_DesignerSize")) { 395 public Object getValue() 396 throws IllegalAccessException , IllegalArgumentException , 397 java.lang.reflect.InvocationTargetException 398 { 399 return getDesignerSize(); 400 } 401 402 public void setValue(Object val) 403 throws IllegalAccessException , IllegalArgumentException , 404 java.lang.reflect.InvocationTargetException 405 { 406 setDesignerSize((Dimension)val); 409 } 410 }; 411 412 java.util.List propList = new java.util.ArrayList (); 413 414 propList.add(JavaCodeGenerator.createBeanClassNameProperty(this)); 415 416 if (java.awt.Window .class.isAssignableFrom(getBeanClass()) 417 || javax.swing.JInternalFrame .class.isAssignableFrom(getBeanClass())) 418 { 419 propList.add(sizeProperty); 420 propList.add(positionProperty); 421 propList.add(policyProperty); 422 propList.add(genPositionProperty); 423 propList.add(genSizeProperty); 424 propList.add(genCenterProperty); 425 } 426 427 propList.add(designerSizeProperty); 428 429 Node.Property[] props = new Node.Property[propList.size()]; 430 propList.toArray(props); 431 return props; 432 } 433 434 438 private static Dimension windowContentDimensionDiff; 439 440 public Dimension getWindowContentDimensionDiff() { 441 boolean undecorated = true; 442 Object beanInstance = getBeanInstance(); 443 if (beanInstance instanceof java.awt.Frame ) { 444 undecorated = ((java.awt.Frame )beanInstance).isUndecorated(); 445 } else if (beanInstance instanceof java.awt.Dialog ) { 446 undecorated = ((java.awt.Dialog )beanInstance).isUndecorated(); 447 } 448 return undecorated ? new Dimension(0, 0) : getDecoratedWindowContentDimensionDiff(); 449 } 450 451 public static Dimension getDecoratedWindowContentDimensionDiff() { 452 if (windowContentDimensionDiff == null) { 453 javax.swing.JFrame frame = new javax.swing.JFrame (); 454 frame.pack(); 455 Dimension d1 = frame.getSize(); 456 Dimension d2 = frame.getRootPane().getSize(); 457 windowContentDimensionDiff = 458 new Dimension(d1.width - d2.width, d1.height - d2.height); 459 } 460 return windowContentDimensionDiff; 461 } 462 463 void setNodeReference(RADComponentNode node) { 464 super.setNodeReference(node); 465 if (node != null) { 466 Object beanInstance = getBeanInstance(); 467 if ((beanInstance instanceof java.awt.Frame ) 468 || (beanInstance instanceof java.awt.Dialog )) { 469 node.addPropertyChangeListener(new PropertyChangeListener() { 472 public void propertyChange(PropertyChangeEvent evt) { 473 if ("undecorated".equals(evt.getPropertyName())) { setDesignerSize(getDesignerSize()); 476 } 477 } 478 }); 479 } 480 } 481 } 482 483 486 final public static class SizePolicyEditor extends java.beans.PropertyEditorSupport { 487 488 private static final String [] names = { 489 FormUtils.getBundleString("VALUE_sizepolicy_full"), FormUtils.getBundleString("VALUE_sizepolicy_pack"), FormUtils.getBundleString("VALUE_sizepolicy_none"), }; 493 494 495 public String [] getTags() { 496 return names; 497 } 498 499 500 public String getAsText() { 501 int value =((Integer )getValue()).intValue(); 502 return names[value]; 503 } 504 505 508 public void setAsText(String str) { 509 if (names[0].equals(str)) 510 setValue(new Integer (0)); 511 else if (names[1].equals(str)) 512 setValue(new Integer (1)); 513 else if (names[2].equals(str)) 514 setValue(new Integer (2)); 515 } 516 } 517 } 518 | Popular Tags |