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.Constructor ; 26 27 import org.openide.nodes.Node; 28 import org.openide.util.Utilities; 29 import org.openide.util.SharedClassObject; 30 31 import org.netbeans.lib.awtextra.AbsoluteLayout; 32 import org.netbeans.lib.awtextra.AbsoluteConstraints; 33 34 import org.netbeans.modules.form.layoutsupport.*; 35 import org.netbeans.modules.form.codestructure.*; 36 import org.netbeans.modules.form.FormProperty; 37 import org.netbeans.modules.form.FormLoaderSettings; 38 39 49 50 public class AbsoluteLayoutSupport extends AbstractLayoutSupport { 51 52 53 private static String iconURL = 54 "org/netbeans/modules/form/layoutsupport/resources/AbsoluteLayout.gif"; 56 private static String icon32URL = 57 "org/netbeans/modules/form/layoutsupport/resources/AbsoluteLayout32.gif"; 59 private static Constructor constrConstructor; 60 61 private static FormLoaderSettings formSettings = (FormLoaderSettings) 62 FormLoaderSettings.getInstance(); 63 64 67 public Class getSupportedClass() { 68 return AbsoluteLayout.class; 69 } 70 71 77 public Image getIcon(int type) { 78 switch (type) { 79 case BeanInfo.ICON_COLOR_16x16: 80 case BeanInfo.ICON_MONO_16x16: 81 return Utilities.loadImage(iconURL); 82 default: 83 return Utilities.loadImage(icon32URL); 84 } 85 } 86 87 98 public void convertConstraints(LayoutConstraints[] previousConstraints, 99 LayoutConstraints[] currentConstraints, 100 Component[] components) 101 { 102 if (currentConstraints == null || components == null) 103 return; 104 105 for (int i=0; i < currentConstraints.length; i++) 106 if (currentConstraints[i] == null) { 107 Rectangle bounds = components[i].getBounds(); 108 Dimension prefSize = components[i].getPreferredSize(); 109 int x = bounds.x; 110 int y = bounds.y; 111 int w = computeConstraintSize(bounds.width, -1, prefSize.width); 112 int h = computeConstraintSize(bounds.height, -1, prefSize.height); 113 114 currentConstraints[i] = new AbsoluteLayoutConstraints(x, y, w, h); 115 } 116 } 117 118 135 public LayoutConstraints getNewConstraints(Container container, 136 Container containerDelegate, 137 Component component, 138 int index, 139 Point posInCont, 140 Point posInComp) 141 { 142 int x = posInCont.x; 143 int y = posInCont.y; 144 int w = -1; 145 int h = -1; 146 147 LayoutConstraints constr = getConstraints(index); 148 149 if (component != null) { 150 int currentW; 151 int currentH; 152 153 if (constr instanceof AbsoluteLayoutConstraints) { 154 currentW = ((AbsoluteLayoutConstraints)constr).w; 155 currentH = ((AbsoluteLayoutConstraints)constr).h; 156 } 157 else { 158 currentW = -1; 159 currentH = -1; 160 } 161 162 Dimension size = component.getSize(); 163 Dimension prefSize = component.getPreferredSize(); 164 165 w = computeConstraintSize(size.width, currentW, prefSize.width); 166 h = computeConstraintSize(size.height, currentH, prefSize.height); 167 } 168 169 if (posInComp != null) { 170 x -= posInComp.x; 171 y -= posInComp.y; 172 } 173 174 if (formSettings.getApplyGridToPosition()) { 175 x = computeGridSize(x, formSettings.getGridX()); 176 y = computeGridSize(y, formSettings.getGridY()); 177 } 178 179 assistantParams = new Object [] {Integer.valueOf(x), Integer.valueOf(y)}; 180 return createNewConstraints(constr, x, y, w, h); 181 } 182 183 private Object [] assistantParams; 184 public String getAssistantContext() { 185 return "absoluteLayout"; } 187 188 public Object [] getAssistantParams() { 189 return assistantParams; 190 } 191 192 208 public boolean paintDragFeedback(Container container, 209 Container containerDelegate, 210 Component component, 211 LayoutConstraints newConstraints, 212 int newIndex, 213 Graphics g) 214 { 215 Rectangle r = ((AbsoluteLayoutConstraints)newConstraints).getBounds(); 216 int w = r.width; 217 int h = r.height; 218 219 if (w == -1 || h == -1) { 220 Dimension pref = component instanceof javax.swing.JInternalFrame ? 222 component.getSize() : component.getPreferredSize(); 223 if (w == -1) w = pref.width; 224 if (h == -1) h = pref.height; 225 } 226 227 if (w < 4) w = 4; 228 if (h < 4) h = 4; 229 230 g.drawRect(r.x, r.y, w, h); 231 232 return true; 233 } 234 235 245 public int getResizableDirections(Container container, 246 Container containerDelegate, 247 Component component, 248 int index) 249 { 250 return RESIZE_UP | RESIZE_DOWN | RESIZE_LEFT | RESIZE_RIGHT; 251 } 252 253 266 public LayoutConstraints getResizedConstraints(Container container, 267 Container containerDelegate, 268 Component component, 269 int index, 270 Rectangle originalBounds, 271 Insets sizeChanges, 272 Point posInCont) 273 { 274 int x, y, w, h; 275 x = originalBounds.x; 276 y = originalBounds.y; 277 w = originalBounds.width; 278 h = originalBounds.height; 279 280 Dimension prefSize = component.getPreferredSize(); 281 int currentW, currentH; 282 283 LayoutConstraints constr = getConstraints(index); 284 if (constr instanceof AbsoluteLayoutConstraints) { 285 Rectangle r = ((AbsoluteLayoutConstraints)constr).getBounds(); 286 currentW = r.width; 287 currentH = r.height; 288 } 289 else { 290 currentW = computeConstraintSize(w, -1, prefSize.width); 291 currentH = computeConstraintSize(h, -1, prefSize.height); 292 } 293 294 int x2 = x + w; 295 int y2 = y + h; 296 297 if (sizeChanges.left + sizeChanges.right == 0) 298 w = currentW; else { w += sizeChanges.left + sizeChanges.right; 301 w = w <= 0 ? -1 : computeConstraintSize(w, currentW, prefSize.width); 302 303 if (w > 0) { 304 if (formSettings.getApplyGridToSize()) { 305 int gridW = computeGridSize(w, formSettings.getGridX()); 306 x -= sizeChanges.left + 307 (gridW - w) * sizeChanges.left 308 / (sizeChanges.left + sizeChanges.right); 309 w = gridW; 310 } 311 } 312 else if (sizeChanges.left != 0) 313 x = x2 - prefSize.width; 314 } 315 316 if (sizeChanges.top + sizeChanges.bottom == 0) 317 h = currentH; else { h += sizeChanges.top + sizeChanges.bottom; 320 h = h <= 0 ? -1 : computeConstraintSize(h, currentH, prefSize.height); 321 322 if (h > 0) { 323 if (formSettings.getApplyGridToSize()) { 324 int gridH = computeGridSize(h, formSettings.getGridY()); 325 y -= sizeChanges.top + 326 (gridH - h) * sizeChanges.top 327 / (sizeChanges.top + sizeChanges.bottom); 328 h = gridH; 329 } 330 } 331 else if (sizeChanges.top != 0) 332 y = y2 - prefSize.height; 333 } 334 335 return createNewConstraints(constr, x, y, w, h); 336 } 337 338 340 352 protected LayoutConstraints readConstraintsCode(CodeExpression constrExp, 353 CodeGroup constrCode, 354 CodeExpression compExp) 355 { 356 AbsoluteLayoutConstraints constr = 357 new AbsoluteLayoutConstraints(0, 0, -1, -1); 358 359 CodeExpression[] params = constrExp.getOrigin().getCreationParameters(); 360 if (params.length == 4) { 361 constr.readPropertyExpressions(params, 0); 363 } 364 365 return constr; 366 } 367 368 378 protected CodeExpression createConstraintsCode(CodeGroup constrCode, 379 LayoutConstraints constr, 380 CodeExpression compExp, 381 int index) 382 { 383 if (!(constr instanceof AbsoluteLayoutConstraints)) 384 return null; 385 386 AbsoluteLayoutConstraints absConstr = (AbsoluteLayoutConstraints)constr; 387 CodeExpression[] params = absConstr.createPropertyExpressions( 390 getCodeStructure(), 0); 391 return getCodeStructure().createExpression(getConstraintsConstructor(), 392 params); 393 } 394 395 399 protected LayoutConstraints createDefaultConstraints() { 400 return new AbsoluteLayoutConstraints(0, 0, -1, -1); 401 } 402 403 405 protected LayoutConstraints createNewConstraints( 406 LayoutConstraints currentConstr, 407 int x, int y, int w, int h) 408 { 409 return new AbsoluteLayoutConstraints(x, y, w, h); 410 } 411 412 private static int computeConstraintSize(int newSize, 413 int currSize, 414 int prefSize) { 415 return newSize != -1 && (newSize != prefSize 416 || (currSize != -1 && currSize == prefSize)) ? 417 newSize : -1; 418 } 419 420 private static int computeGridSize(int size, int step) { 421 if (step <= 0) return size; 422 int mod = size % step; 423 return mod >= step/2 ? size + step - mod : size - mod; 424 } 425 426 private static Constructor getConstraintsConstructor() { 427 if (constrConstructor == null) { 428 try { 429 constrConstructor = AbsoluteConstraints.class.getConstructor( 430 new Class [] { Integer.TYPE, Integer.TYPE, 431 Integer.TYPE, Integer.TYPE }); 432 } 433 catch (NoSuchMethodException ex) { ex.printStackTrace(); 435 } 436 } 437 return constrConstructor; 438 } 439 440 442 444 public static class AbsoluteLayoutConstraints implements LayoutConstraints { 445 int x, y, w, h; 447 private Node.Property[] properties; 448 boolean nullMode; 449 Component refComponent; 450 451 public AbsoluteLayoutConstraints(int x, int y, int w, int h) 452 { 453 this.x = x; 454 this.y = y; 455 this.w = w; 456 this.h = h; 457 } 458 459 public Node.Property[] getProperties() { 460 if (properties == null) { 461 properties = createProperties(); 462 reinstateProperties(); 463 } 464 return properties; 465 } 466 467 public Object getConstraintsObject() { 468 return new AbsoluteConstraints(x, y, w, h); 469 } 470 471 public LayoutConstraints cloneConstraints() { 472 return new AbsoluteLayoutConstraints(x, y, w, h); 473 } 474 475 477 public Rectangle getBounds() { 478 return new Rectangle(x, y, w, h); 479 } 480 481 protected Node.Property[] createProperties() { 482 return new Node.Property[] { 483 new FormProperty("AbsoluteLayoutConstraints posx", Integer.TYPE, 485 getBundle().getString("PROP_posx"), getBundle().getString("HINT_posx")) { 488 public Object getTargetValue() { 489 return new Integer (x); 490 } 491 public void setTargetValue(Object value) { 492 x = ((Integer )value).intValue(); 493 } 494 public void setPropertyContext( 495 org.netbeans.modules.form.FormPropertyContext ctx) 496 { } }, 499 500 new FormProperty("AbsoluteLayoutConstraints posy", Integer.TYPE, 502 getBundle().getString("PROP_posy"), getBundle().getString("HINT_posy")) { 505 public Object getTargetValue() { 506 return new Integer (y); 507 } 508 public void setTargetValue(Object value) { 509 y = ((Integer )value).intValue(); 510 } 511 public void setPropertyContext( 512 org.netbeans.modules.form.FormPropertyContext ctx) 513 { } }, 516 517 new FormProperty("AbsoluteLayoutConstraints width", Integer.TYPE, 519 getBundle().getString("PROP_width"), getBundle().getString("HINT_width")) { 522 public Object getTargetValue() { 523 return new Integer (w); 524 } 525 public void setTargetValue(Object value) { 526 w = ((Integer )value).intValue(); 527 } 528 public boolean supportsDefaultValue () { 529 return true; 530 } 531 public Object getDefaultValue() { 532 return new Integer (-1); 533 } 534 public PropertyEditor getExpliciteEditor() { 535 return new SizeEditor(); 536 } 537 public Object getValue(String key) { 538 if ("canEditAsText".equals(key)) return Boolean.TRUE; 540 return super.getValue(key); 541 } 542 public String getJavaInitializationString() { 543 if (nullMode && refComponent != null && !isChanged()) 544 return Integer.toString( 545 refComponent.getPreferredSize().width); 546 return super.getJavaInitializationString(); 547 } 548 public void setPropertyContext( 549 org.netbeans.modules.form.FormPropertyContext ctx) 550 { } }, 553 554 new FormProperty("AbsoluteLayoutConstraints height", Integer.TYPE, 556 getBundle().getString("PROP_height"), getBundle().getString("HINT_height")) { 559 public Object getTargetValue() { 560 return new Integer (h); 561 } 562 public void setTargetValue(Object value) { 563 h = ((Integer )value).intValue(); 564 } 565 public boolean supportsDefaultValue () { 566 return true; 567 } 568 public Object getDefaultValue() { 569 return new Integer (-1); 570 } 571 public PropertyEditor getExpliciteEditor() { 572 return new SizeEditor(); 573 } 574 public Object getValue(String key) { 575 if ("canEditAsText".equals(key)) return Boolean.TRUE; 577 return super.getValue(key); 578 } 579 public String getJavaInitializationString() { 580 if (nullMode && refComponent != null && !isChanged()) 581 return Integer.toString( 582 refComponent.getPreferredSize().height); 583 return super.getJavaInitializationString(); 584 } 585 public void setPropertyContext( 586 org.netbeans.modules.form.FormPropertyContext ctx) 587 { } } 590 }; 591 } 592 593 private void reinstateProperties() { 594 try { 595 for (int i=0; i < properties.length; i++) { 596 FormProperty prop = (FormProperty) properties[i]; 597 prop.reinstateProperty(); 598 } 599 } 600 catch(IllegalAccessException e1) {} catch(java.lang.reflect.InvocationTargetException e2) {} } 603 604 615 protected final CodeExpression[] createPropertyExpressions( 616 CodeStructure codeStructure, 617 int shift) 618 { 619 getProperties(); 621 622 CodeExpression xEl = codeStructure.createExpression( 624 FormCodeSupport.createOrigin(properties[shift++])); 625 CodeExpression yEl = codeStructure.createExpression( 626 FormCodeSupport.createOrigin(properties[shift++])); 627 CodeExpression wEl = codeStructure.createExpression( 628 FormCodeSupport.createOrigin(properties[shift++])); 629 CodeExpression hEl = codeStructure.createExpression( 630 FormCodeSupport.createOrigin(properties[shift])); 631 return new CodeExpression[] { xEl, yEl, wEl, hEl }; 632 } 633 634 642 protected final void readPropertyExpressions(CodeExpression[] exps, 643 int shift) 644 { 645 getProperties(); 647 648 for (int i=0; i < exps.length; i++) 650 FormCodeSupport.readPropertyExpression(exps[i], 651 properties[i+shift], 652 false); 653 } 654 } 655 656 658 661 public static final class SizeEditor extends PropertyEditorSupport { 662 663 final Integer prefValue = new Integer (-1); 664 final String prefTag = getBundle().getString("VALUE_preferred"); 666 public String [] getTags() { 667 return new String [] { prefTag }; 668 } 669 670 public String getAsText() { 671 Object value = getValue(); 672 return prefValue.equals(value) ? 673 prefTag : value.toString(); 674 } 675 676 public void setAsText(String str) { 677 if (prefTag.equals(str)) 678 setValue(prefValue); 679 else 680 try { 681 setValue(new Integer (Integer.parseInt(str))); 682 } 683 catch (NumberFormatException e) {} } 685 686 public String getJavaInitializationString() { 687 Object value = getValue(); 688 return value != null ? value.toString() : null; 689 } 690 } 691 } 692 | Popular Tags |