1 19 20 package org.netbeans.modules.form.layoutsupport; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.util.*; 25 import java.lang.reflect.Method ; 26 27 import org.openide.ErrorManager; 28 import org.openide.nodes.Node; 29 import org.openide.util.Utilities; 30 31 import org.netbeans.modules.form.*; 32 import org.netbeans.modules.form.codestructure.*; 33 34 64 65 public abstract class AbstractLayoutSupport implements LayoutSupportDelegate 66 { 67 68 private static String iconURL = 69 "org/netbeans/modules/form/layoutsupport/resources/AbstractLayout.gif"; 71 private static String icon32URL = 72 "org/netbeans/modules/form/layoutsupport/resources/AbstractLayout32.gif"; 74 private static Method simpleAddMethod = null; 75 private static Method addWithConstraintsMethod = null; 76 private static Method setLayoutMethod = null; 77 78 80 private LayoutSupportContext layoutContext; 81 82 private java.util.List componentCodeExpressions; 83 private java.util.List componentCodeGroups; 84 private java.util.List componentConstraints; 85 86 private BeanCodeManager layoutBeanCode; 87 private CodeGroup setLayoutCode; 88 89 private MetaLayout metaLayout; 90 private FormProperty[] allProperties; 91 92 95 109 public void initialize(LayoutSupportContext layoutContext, 110 LayoutManager lmInstance, 111 boolean fromCode) 112 throws Exception 113 { 114 if (this.layoutContext == layoutContext) { 115 if (setLayoutCode != null) 117 setLayoutCode.removeAll(); 118 else setLayoutCode = 119 layoutContext.getCodeStructure().createCodeGroup(); 120 121 readLayoutCode(setLayoutCode); return; 123 } 124 125 this.layoutContext = layoutContext; 126 clean(); 127 128 Class cls = getSupportedClass(); 129 if (cls != null && LayoutManager.class.isAssignableFrom(cls)) { 130 boolean defaultInstance = lmInstance == null; 132 if (lmInstance == null || !lmInstance.getClass().equals(cls)) { 133 lmInstance = createDefaultLayoutInstance(); 135 defaultInstance = true; 136 } 137 138 if (lmInstance != null) 139 metaLayout = new MetaLayout(this, lmInstance, defaultInstance); 140 } 141 else metaLayout = null; 142 143 readLayoutCode(setLayoutCode); 145 146 if (fromCode) { 147 CodeGroup componentCode = null; 149 Iterator it = CodeStructure.getDefinedStatementsIterator( 150 getActiveContainerCodeExpression()); 151 while (it.hasNext()) { 152 if (componentCode == null) 153 componentCode = 154 layoutContext.getCodeStructure().createCodeGroup(); 155 156 CodeStatement statement = (CodeStatement) it.next(); 157 CodeExpression compExp = readComponentCode(statement, 158 componentCode); 159 if (compExp != null) { 160 componentCodeExpressions.add(compExp); 161 componentCodeGroups.add(componentCode); 162 componentCode = null; 163 164 if (componentConstraints.size() 165 < componentCodeExpressions.size()) 166 componentConstraints.add(null); 167 } 168 } 169 } 170 } 171 172 176 public boolean isDedicated() { 177 Class cls = getSupportedClass(); 178 return cls != null && !LayoutManager.class.isAssignableFrom(cls); 179 } 180 181 188 public boolean checkEmptyContainer(Container cont) { 189 return true; 190 } 191 192 198 public boolean shouldHaveNode() { 199 Class cls = getSupportedClass(); 200 return cls == null || LayoutManager.class.isAssignableFrom(cls); 201 } 202 203 207 public String getDisplayName() { 208 Class cls = getSupportedClass(); 209 String name; 210 211 if (cls != null) { 212 name = cls.getName(); 213 int lastdot = name.lastIndexOf('.'); 214 if (lastdot > 0) 215 name = name.substring(lastdot + 1); 216 } 217 else name = "null"; 219 return name; 220 } 221 222 229 public Image getIcon(int type) { 230 if (metaLayout != null) { 231 Image icon = metaLayout.getBeanInfo().getIcon(type); 232 if (icon != null) 233 return icon; 234 } 235 236 switch (type) { 237 case BeanInfo.ICON_COLOR_16x16: 238 case BeanInfo.ICON_MONO_16x16: 239 return Utilities.loadImage(iconURL); 240 default: 241 return Utilities.loadImage(icon32URL); 242 } 243 } 244 245 251 public Node.PropertySet[] getPropertySets() { 252 Node.PropertySet[] propertySets; 253 254 FormProperty[] properties = getProperties(); 255 if (properties == null) { 256 propertySets = metaLayout != null ? 257 metaLayout.getProperties() : null; 258 } 259 else { propertySets = new Node.PropertySet[1]; 261 propertySets[0] = new Node.PropertySet( 262 "properties", FormUtils.getBundleString("CTL_PropertiesTab"), FormUtils.getBundleString("CTL_PropertiesTabHint")) { 266 public Node.Property[] getProperties() { 267 return AbstractLayoutSupport.this.getProperties(); 268 } 269 }; 270 } 271 272 if (propertySets != null) { 273 ArrayList allPropsList = new ArrayList(); 274 for (int i=0; i < propertySets.length; i++) { 275 Node.Property[] props = propertySets[i].getProperties(); 276 for (int j=0; j < props.length; j++) 277 if (props[j] instanceof FormProperty) 278 allPropsList.add(props[j]); 279 } 280 allProperties = new FormProperty[allPropsList.size()]; 281 allPropsList.toArray(allProperties); 282 } 283 else { 284 allProperties = new FormProperty[0]; 285 propertySets = new Node.PropertySet[0]; 286 } 287 288 return propertySets; 289 } 290 291 297 public Class getCustomizerClass() { 298 return metaLayout == null ? null : 299 metaLayout.getBeanInfo().getBeanDescriptor().getCustomizerClass(); 300 } 301 302 310 public Component getSupportCustomizer() { 311 return null; 312 } 313 314 318 public CodeGroup getLayoutCode() { 319 return setLayoutCode; 320 } 321 322 326 public CodeGroup getComponentCode(int index) { 327 return (CodeGroup) componentCodeGroups.get(index); 328 } 329 330 334 public CodeExpression getComponentCodeExpression(int index) { 335 return (CodeExpression) componentCodeExpressions.get(index); 336 } 337 338 341 public int getComponentCount() { 342 return componentCodeExpressions != null ? 343 componentCodeExpressions.size() : 0; 344 } 345 346 359 public void acceptNewComponents(CodeExpression[] compExpressions, 360 LayoutConstraints[] constraints, 361 int index) 362 { 363 } 364 365 373 public void acceptContainerLayoutChange(PropertyChangeEvent ev) 374 throws PropertyVetoException 375 { 376 if (layoutBeanCode != null) 379 layoutBeanCode.updateCode(); 380 } 381 382 391 public void acceptComponentLayoutChange(int index, PropertyChangeEvent ev) 392 throws PropertyVetoException 393 { 394 } 395 396 407 public void addComponents(CodeExpression[] newCompExps, 408 LayoutConstraints[] newConstraints, 409 int index) 410 { 411 if (index < 0 || index > componentCodeExpressions.size()) 412 index = componentCodeExpressions.size(); 413 414 CodeStructure codeStructure = layoutContext.getCodeStructure(); 415 416 for (int i=0; i < newCompExps.length; i++) { 417 int ii = index + i; 418 419 CodeExpression compExp = newCompExps[i]; 420 componentCodeExpressions.add(ii, compExp); 421 422 LayoutConstraints constr = newConstraints != null ? 423 newConstraints[i] : null; 424 if (constr == null) 425 constr = createDefaultConstraints(); 426 427 componentConstraints.add(ii, constr); 428 429 CodeGroup componentCode = 430 codeStructure.createCodeGroup(); 431 createComponentCode(componentCode, compExp, ii); 432 componentCodeGroups.add(ii, componentCode); 433 } 434 } 435 436 440 public void removeComponent(int index) { 441 componentCodeExpressions.remove(index); 442 componentCodeGroups.remove(index); 443 componentConstraints.remove(index); 444 } 445 446 449 public void removeAll() { 450 if (componentCodeExpressions != null) 451 componentCodeExpressions.clear(); 452 if (componentCodeGroups != null) 453 componentCodeGroups.clear(); 454 if (componentConstraints != null) 455 componentConstraints.clear(); 456 } 457 458 467 public boolean isLayoutChanged(Container defaultContainer, 468 Container defaultContainerDelegate) 469 { 470 if (isDedicated()) 471 return false; 472 473 Class layoutClass = getSupportedClass(); 474 LayoutManager lm = defaultContainerDelegate.getLayout(); 475 476 if (layoutClass == null) 477 return lm != null; 478 if (lm == null) 479 return true; 480 481 if (!layoutClass.isAssignableFrom(lm.getClass())) 483 return true; 484 485 FormProperty[] props = getAllProperties(); 486 for (int i=0; i < props.length; i++) 487 if (props[i].isChanged()) 488 return true; 489 490 return false; 491 } 492 493 497 public LayoutConstraints getConstraints(int index) { 498 return index < 0 || index >= componentConstraints.size() ? null : 499 (LayoutConstraints) componentConstraints.get(index); 500 } 501 502 512 public void convertConstraints(LayoutConstraints[] previousConstraints, 513 LayoutConstraints[] currentConstraints, 514 Component[] components) 515 { 516 } 517 518 524 public void setLayoutToContainer(Container container, 525 Container containerDelegate) 526 { 527 if (isDedicated()) 528 return; 529 530 LayoutManager lm = null; 531 try { 532 if (containerDelegate == layoutContext.getPrimaryContainerDelegate()) { 533 if (metaLayout != null) lm = (LayoutManager) metaLayout.getBeanInstance(); 535 } 536 else { lm = cloneLayoutInstance(container, containerDelegate); 538 } 539 } 540 catch (Exception ex) { ex.printStackTrace(); 542 } 543 544 if (lm != null) 545 containerDelegate.setLayout(lm); 546 } 547 548 556 public void addComponentsToContainer(Container container, 557 Container containerDelegate, 558 Component[] components, 559 int index) 560 { 561 for (int i=0; i < components.length; i++) { 562 LayoutConstraints constr = getConstraints(i + index); 563 if (constr != null) 564 containerDelegate.add(components[i], 565 constr.getConstraintsObject(), 566 i + index); 567 else 568 containerDelegate.add(components[i], i + index); 569 } 570 } 571 572 580 public boolean removeComponentFromContainer(Container container, 581 Container containerDelegate, 582 Component component) 583 { 584 containerDelegate.remove(component); 585 component.setBounds(0, 0, 0, 0); 586 return true; 587 } 588 589 596 public boolean clearContainer(Container container, 597 Container containerDelegate) 598 { 599 Component[] components = containerDelegate.getComponents(); 600 containerDelegate.removeAll(); 601 for (int i=0; i < components.length; i++) 602 components[i].setBounds(0, 0, 0, 0); 603 return true; 604 } 605 606 615 public void processMouseClick(Point p, 616 Container container, 617 Container containerDelegate) 618 { 619 } 620 621 627 public void selectComponent(int index) { 628 } 629 630 639 public void arrangeContainer(Container container, 640 Container containerDelegate) 641 { 642 } 643 644 663 public LayoutConstraints getNewConstraints(Container container, 664 Container containerDelegate, 665 Component component, 666 int index, 667 Point posInCont, 668 Point posInComp) 669 { 670 return null; 671 } 672 673 691 public int getNewIndex(Container container, 692 Container containerDelegate, 693 Component component, 694 int index, 695 Point posInCont, 696 Point posInComp) 697 { 698 return -1; 699 } 700 701 706 public String getAssistantContext() { 707 return null; 708 } 709 710 715 public Object [] getAssistantParams() { 716 return null; 717 } 718 719 736 public boolean paintDragFeedback(Container container, 737 Container containerDelegate, 738 Component component, 739 LayoutConstraints newConstraints, 740 int newIndex, 741 Graphics g) 742 { 743 return false; 744 } 745 746 756 public int getResizableDirections(Container container, 757 Container containerDelegate, 758 Component component, 759 int index) 760 { 761 return 0; 762 } 763 764 777 public LayoutConstraints getResizedConstraints(Container container, 778 Container containerDelegate, 779 Component component, 780 int index, 781 Rectangle originalBounds, 782 Insets sizeChanges, 783 Point posInCont) 784 { 785 return null; 786 } 787 788 795 public LayoutSupportDelegate cloneLayoutSupport( 796 LayoutSupportContext targetContext, 797 CodeExpression[] targetComponents) 798 { 799 AbstractLayoutSupport clone = createLayoutSupportInstance(); 800 try { 801 clone.initialize(targetContext, null, false); 802 } 803 catch (Exception ex) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 805 return null; 806 } 807 808 FormProperty[] sourceProperties = getAllProperties(); 809 FormProperty[] targetProperties = clone.getAllProperties(); 810 FormUtils.copyProperties(sourceProperties, 811 targetProperties, 812 FormUtils.CHANGED_ONLY 813 | FormUtils.DISABLE_CHANGE_FIRING); 814 815 int compCount = getComponentCount(); 816 LayoutConstraints[] constraints = new LayoutConstraints[compCount]; 817 for (int i=0; i < compCount; i++) { 818 LayoutConstraints constr = getConstraints(i); 819 constraints[i] = constr != null ? constr.cloneConstraints() : null; 820 } 821 822 clone.addComponents(targetComponents, constraints, 0); 823 824 return clone; 825 } 826 827 830 835 protected LayoutManager createDefaultLayoutInstance() 836 throws Exception 837 { 838 return (LayoutManager) 839 CreationFactory.createDefaultInstance(getSupportedClass()); 840 } 841 842 853 protected LayoutManager cloneLayoutInstance(Container container, 854 Container containerDelegate) 855 throws Exception 856 { 857 return metaLayout == null ? null : 858 (LayoutManager) metaLayout.cloneBeanInstance(null); 859 } 860 861 865 protected AbstractLayoutSupport createLayoutSupportInstance() { 866 try { 867 return (AbstractLayoutSupport) getClass().newInstance(); 868 } 869 catch (Exception ex) { return null; 871 } 872 } 873 874 882 protected CodeExpression getActiveContainerCodeExpression() { 883 return layoutContext.getContainerDelegateCodeExpression(); 884 } 885 886 888 protected void clean() { 889 if (componentCodeExpressions != null) 890 componentCodeExpressions.clear(); 891 else componentCodeExpressions = new ArrayList(); 892 893 if (componentCodeGroups != null) 894 componentCodeGroups.clear(); 895 else componentCodeGroups = new ArrayList(); 896 897 if (componentConstraints != null) 898 componentConstraints.clear(); 899 else componentConstraints = new ArrayList(); 900 901 if (setLayoutCode != null) 902 setLayoutCode.removeAll(); 903 else setLayoutCode = layoutContext.getCodeStructure().createCodeGroup(); 904 905 layoutBeanCode = null; 906 metaLayout = null; 907 908 allProperties = null; 909 } 910 911 921 protected void readLayoutCode(CodeGroup layoutCode) { 922 if (isDedicated()) 923 return; 924 925 CodeGroup initLayoutCode = 926 getCodeStructure().createCodeGroup(); 927 CodeStatement setLayoutStatement = null; 928 929 Iterator it = CodeStructure.getDefinedStatementsIterator( 930 getActiveContainerCodeExpression()); 931 CodeStatement[] statements = CodeStructure.filterStatements( 932 it, getSetLayoutMethod()); 933 if (statements.length > 0) { setLayoutStatement = statements[0]; 935 readInitLayoutCode(setLayoutStatement.getStatementParameters()[0], 936 initLayoutCode); 937 } 938 else { CodeExpression layoutExp = createInitLayoutCode(initLayoutCode); 940 if (layoutExp != null) 941 setLayoutStatement = CodeStructure.createStatement( 942 getActiveContainerCodeExpression(), 943 getSetLayoutMethod(), 944 new CodeExpression[] { layoutExp }); 945 } 946 947 if (setLayoutStatement != null) { 948 layoutCode.addGroup(initLayoutCode); 949 layoutCode.addStatement(setLayoutStatement); 950 } 951 } 952 953 959 protected void readInitLayoutCode(CodeExpression layoutExp, 960 CodeGroup initLayoutCode) 961 { 962 if (metaLayout == null) 963 return; 964 965 layoutBeanCode = new BeanCodeManager( 966 getSupportedClass(), 967 getAllProperties(), 968 CreationDescriptor.PLACE_ALL | CreationDescriptor.CHANGED_ONLY, 969 false, false, layoutExp, 972 initLayoutCode); 973 } 974 975 981 protected CodeExpression createInitLayoutCode(CodeGroup initLayoutCode) { 982 if (metaLayout == null) 983 return null; 984 985 layoutBeanCode = new BeanCodeManager( 986 getSupportedClass(), 987 getAllProperties(), 988 CreationDescriptor.PLACE_ALL | CreationDescriptor.CHANGED_ONLY, 989 false, 990 layoutContext.getCodeStructure(), 991 CodeVariable.LOCAL, 992 initLayoutCode); 993 994 return layoutBeanCode.getCodeExpression(); 995 } 996 997 1008 protected CodeExpression readComponentCode(CodeStatement statement, 1009 CodeGroup componentCode) 1010 { 1011 CodeExpression compExp; 1012 CodeGroup constrCode; 1013 LayoutConstraints constr; 1014 1015 if (getSimpleAddMethod().equals(statement.getMetaObject())) { 1017 compExp = statement.getStatementParameters()[0]; 1018 constrCode = null; 1019 constr = null; 1020 } 1021 else if (getAddWithConstraintsMethod().equals( 1022 statement.getMetaObject())) 1023 { 1024 CodeExpression[] params = statement.getStatementParameters(); 1025 1026 compExp = params[0]; 1027 constrCode = getCodeStructure().createCodeGroup(); 1028 constr = readConstraintsCode(params[1], constrCode, compExp); 1029 } 1030 else return null; 1031 1032 componentConstraints.add(constr); 1033 if (constrCode != null) 1034 componentCode.addGroup(constrCode); 1035 componentCode.addStatement(statement); 1036 1037 return compExp; 1038 } 1039 1040 1050 protected LayoutConstraints readConstraintsCode(CodeExpression constrExp, 1051 CodeGroup constrCode, 1052 CodeExpression compExp) 1053 { 1054 return null; } 1056 1057 1065 protected void createComponentCode(CodeGroup componentCode, 1066 CodeExpression compExp, 1067 int index) 1068 { 1069 CodeGroup constrCode = getCodeStructure().createCodeGroup(); 1070 LayoutConstraints constr = getConstraints(index); 1071 1072 CodeExpression constrExp = createConstraintsCode( 1074 constrCode, constr, compExp, index); 1075 1076 CodeStatement compAddStatement; 1078 if (constrExp != null) { compAddStatement = CodeStructure.createStatement( 1080 getActiveContainerCodeExpression(), 1081 getAddWithConstraintsMethod(), 1082 new CodeExpression[] { compExp, constrExp }); 1083 } 1084 else { compAddStatement = CodeStructure.createStatement( 1086 getActiveContainerCodeExpression(), 1087 getSimpleAddMethod(), 1088 new CodeExpression[] { compExp }); 1089 } 1090 1091 componentCode.addGroup(constrCode); 1092 componentCode.addStatement(compAddStatement); 1093 } 1094 1095 1102 protected CodeExpression createConstraintsCode(CodeGroup constrCode, 1103 LayoutConstraints constr, 1104 CodeExpression compExp, 1105 int index) 1106 { 1107 return null; } 1109 1110 1115 protected LayoutConstraints createDefaultConstraints() { 1116 return null; } 1118 1119 1122 protected Node.Property getProperty(String propName) { 1123 return metaLayout == null ? null : 1124 metaLayout.getPropertyByName(propName); 1125 } 1126 1127 1135 protected FormProperty[] getProperties() { 1136 return null; } 1138 1139 1142 1146 protected final LayoutSupportContext getLayoutContext() { 1147 return layoutContext; 1148 } 1149 1150 1154 protected final CodeStructure getCodeStructure() { 1155 return layoutContext.getCodeStructure(); 1156 } 1157 1158 1162 protected final java.util.List getConstraintsList() { 1163 return componentConstraints; 1164 } 1165 1166 1169 protected final FormProperty[] getAllProperties() { 1170 if (allProperties == null) 1171 getPropertySets(); 1172 1173 return allProperties; 1174 } 1175 1176 1179 protected final void updateLayoutInstance() { 1180 Container cont = layoutContext.getPrimaryContainer(); 1181 Container contDel = layoutContext.getPrimaryContainerDelegate(); 1182 1183 LayoutManager lm = null; 1184 try { 1185 lm = cloneLayoutInstance(cont, contDel); 1186 } 1187 catch (Exception ex) { ex.printStackTrace(); 1189 } 1190 1191 if (lm != null && metaLayout != null) 1192 metaLayout.updateInstance(lm); 1193 1194 layoutContext.updatePrimaryContainer(); 1195 } 1196 1197 1201 protected final CodeStatement getSetLayoutStatement() { 1202 Iterator it = CodeStructure.getDefinedStatementsIterator( 1203 getActiveContainerCodeExpression()); 1204 CodeStatement[] found = CodeStructure.filterStatements( 1205 it, getSetLayoutMethod()); 1206 return found != null && found.length > 0 ? found[0] : null; 1207 } 1208 1209 1212 1214 protected static ResourceBundle getBundle() { 1215 return org.openide.util.NbBundle.getBundle(AbstractLayoutSupport.class); 1216 } 1217 1218 1223 protected static Method getSimpleAddMethod() { 1224 if (simpleAddMethod == null) { 1225 try { 1226 simpleAddMethod = Container.class.getMethod( 1227 "add", new Class [] { Component.class }); 1229 } 1230 catch (NoSuchMethodException ex) { ex.printStackTrace(); 1232 } 1233 } 1234 return simpleAddMethod; 1235 } 1236 1237 1243 protected static Method getAddWithConstraintsMethod() { 1244 if (addWithConstraintsMethod == null) { 1245 try { 1246 addWithConstraintsMethod = Container.class.getMethod( 1247 "add", new Class [] { Component.class, 1249 Object .class }); 1250 } 1251 catch (NoSuchMethodException ex) { ex.printStackTrace(); 1253 } 1254 } 1255 return addWithConstraintsMethod; 1256 } 1257 1258 1264 protected static Method getSetLayoutMethod() { 1265 if (setLayoutMethod == null) { 1266 try { 1267 setLayoutMethod = Container.class.getMethod( 1268 "setLayout", new Class [] { LayoutManager.class }); 1270 } 1271 catch (NoSuchMethodException ex) { ex.printStackTrace(); 1273 } 1274 } 1275 return setLayoutMethod; 1276 } 1277} 1278 | Popular Tags |