1 19 20 package org.netbeans.modules.form; 21 22 import java.awt.*; 23 import javax.swing.*; 24 import java.util.ArrayList ; 25 import java.lang.reflect.Method ; 26 import org.netbeans.modules.form.fakepeer.FakePeerSupport; 27 28 import org.netbeans.modules.form.layoutsupport.*; 29 import org.openide.ErrorManager; 30 31 32 public class RADVisualContainer extends RADVisualComponent implements ComponentContainer { 33 private ArrayList <RADVisualComponent> subComponents = new ArrayList (10); 34 private LayoutSupportManager layoutSupport; private LayoutNode layoutNode; 37 private RADMenuComponent containerMenu; 38 39 private Method containerDelegateGetter; 40 private boolean noContainerDelegate; 41 42 43 52 protected void setBeanInstance(Object beanInstance) { 53 56 containerDelegateGetter = null; 57 noContainerDelegate = false; 58 59 super.setBeanInstance(beanInstance); 60 61 if (layoutSupport != null) layoutSupport = new LayoutSupportManager(this, getFormModel().getCodeStructure()); 63 } 64 65 void setInModel(boolean in) { 66 boolean alreadyIn = isInModel(); 67 super.setInModel(in); 68 if (in && !alreadyIn && layoutSupport != null) { 69 try { 71 layoutSupport.initializeLayoutDelegate(); 72 } 73 catch (Exception ex) { 74 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 76 layoutSupport.setUnknownLayoutDelegate(false); 77 } 78 } 79 } 80 81 public void setLayoutSupportDelegate(LayoutSupportDelegate layoutDelegate, 82 LayoutManager lmInstance) 83 throws Exception 84 { 85 layoutSupport.setLayoutDelegate(layoutDelegate, lmInstance, false); 86 setLayoutNodeReference(null); 87 } 88 89 public LayoutSupportManager getLayoutSupport() { 90 return layoutSupport; 91 } 92 93 97 public static boolean isFreeDesignContainer(RADComponent metacomp) { 98 return metacomp instanceof RADVisualContainer 99 && ((RADVisualContainer)metacomp).getLayoutSupport() == null; 100 } 101 102 public static boolean isInFreeDesign(RADComponent metacomp) { 103 return metacomp instanceof RADVisualComponent 104 && isFreeDesignContainer(metacomp.getParentComponent()); 105 } 106 107 void setOldLayoutSupport(boolean old) { 108 if (old) { 109 if (layoutSupport == null) { 110 layoutSupport = new LayoutSupportManager(this, getFormModel().getCodeStructure()); 111 } 112 } 113 else { 114 if (layoutSupport != null) { try { 116 layoutSupport.setLayoutDelegate(null, null, false); 117 } catch (Exception ex) { 118 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 119 } 120 refillContainerInstance(); 121 } 122 layoutSupport = null; 123 setLayoutNodeReference(null); 124 } 125 } 126 127 private void refillContainerInstance() { 128 Container cont = getContainerDelegate(getBeanInstance()); 129 cont.removeAll(); 130 cont.setLayout(null); for (RADVisualComponent sub : subComponents) { 132 Component comp = (Component) sub.getBeanInstance(); 133 FakePeerSupport.attachFakePeer(comp); 134 if (comp instanceof Container) 135 FakePeerSupport.attachFakePeerRecursively((Container)comp); 136 cont.add(comp); 137 } 138 } 139 140 144 public boolean hasDedicatedLayoutSupport() { 145 return layoutSupport != null && layoutSupport.isDedicated(); 146 } 147 148 152 153 public Container getContainerDelegate(Object container) { 154 if (container instanceof RootPaneContainer 155 && container.getClass().getName().startsWith("javax.swing.")) return ((RootPaneContainer)container).getContentPane(); 157 if (container.getClass().equals(JRootPane.class)) 158 return ((JRootPane)container).getContentPane(); 159 160 Container containerDelegate = (Container) container; 161 Method m = getContainerDelegateMethod(); 162 if (m != null) { 163 try { 164 containerDelegate = 165 (Container) m.invoke(container, new Object [0]); 166 } 167 catch (Exception ex) { 168 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 169 } 170 } 171 return containerDelegate; 172 } 173 174 public Method getContainerDelegateMethod() { 175 if (containerDelegateGetter == null && !noContainerDelegate) { 176 String delegateGetterName = getContainerDelegateGetterName(); 177 if (delegateGetterName == null 178 && (RootPaneContainer.class.isAssignableFrom(getBeanClass()) 179 || JRootPane.class.isAssignableFrom(getBeanClass()))) 180 delegateGetterName = "getContentPane"; 182 if (delegateGetterName != null) { 183 try { 184 containerDelegateGetter = 185 getBeanClass().getMethod( 186 delegateGetterName, new Class [0]); 187 } 188 catch (NoSuchMethodException ex) { 189 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 190 } 191 } 192 else noContainerDelegate = true; 193 } 194 return containerDelegateGetter; 195 } 196 197 String getContainerDelegateGetterName() { 198 Object value = getBeanInfo().getBeanDescriptor() 199 .getValue("containerDelegate"); 201 if (value instanceof String ) 202 return (String ) value; 203 else 204 return null; 205 } 206 207 public void setLayoutNodeReference(LayoutNode node) { 208 this.layoutNode = node; 209 } 210 211 public LayoutNode getLayoutNodeReference() { 212 return layoutNode; 213 } 214 215 boolean shouldHaveLayoutNode() { 216 return layoutSupport != null && layoutSupport.shouldHaveNode(); 217 } 218 219 RADMenuComponent getContainerMenu() { 220 return containerMenu; 221 } 222 223 boolean canHaveMenu(Class menuClass) { 224 return (JMenuBar.class.isAssignableFrom(menuClass) 225 && RootPaneContainer.class.isAssignableFrom(getBeanClass())) 226 || 227 (MenuBar.class.isAssignableFrom(menuClass) 228 && Frame.class.isAssignableFrom(getBeanClass()) 229 && !JFrame.class.isAssignableFrom(getBeanClass())); 230 } 231 232 235 236 public RADVisualComponent[] getSubComponents() { 237 RADVisualComponent[] components = new RADVisualComponent[subComponents.size()]; 238 subComponents.toArray(components); 239 return components; 240 } 241 242 public RADVisualComponent getSubComponent(int index) { 243 return (RADVisualComponent) subComponents.get(index); 244 } 245 246 248 249 public RADComponent[] getSubBeans() { 250 int n = subComponents.size(); 251 if (containerMenu != null) 252 n++; 253 254 RADComponent[] components = new RADComponent[n]; 255 subComponents.toArray(components); 256 if (containerMenu != null) 257 components[n-1] = containerMenu; 258 259 return components; 260 } 261 262 public void initSubComponents(RADComponent[] initComponents) { 263 if (subComponents == null) 264 subComponents = new ArrayList (initComponents.length); 265 else { 266 subComponents.clear(); 267 subComponents.ensureCapacity(initComponents.length); 268 } 269 270 for (int i=0; i < initComponents.length; i++) { 271 RADComponent metacomp = initComponents[i]; 272 273 if (metacomp instanceof RADVisualComponent) 274 subComponents.add((RADVisualComponent)metacomp); 275 else if (metacomp instanceof RADMenuComponent) 276 containerMenu = (RADMenuComponent) metacomp; else 278 continue; 280 metacomp.setParentComponent(this); 281 } 282 283 if (layoutSupport == null) 284 refillContainerInstance(); 285 } 286 287 public void reorderSubComponents(int[] perm) { 288 RADVisualComponent[] components = new RADVisualComponent[subComponents.size()]; 289 LayoutConstraints[] constraints; 290 if (layoutSupport != null) { 291 layoutSupport.removeAll(); 292 constraints = new LayoutConstraints[subComponents.size()]; 293 } 294 else constraints = null; 295 296 for (int i=0; i < perm.length; i++) { 297 RADVisualComponent metacomp = subComponents.get(i); 298 components[perm[i]] = metacomp; 299 if (constraints != null) 300 constraints[perm[i]] = layoutSupport.getStoredConstraints(metacomp); 301 } 302 303 subComponents.clear(); 304 subComponents.addAll(java.util.Arrays.asList(components)); 305 306 if (layoutSupport != null) { 307 layoutSupport.addComponents(components, constraints, 0); 308 } 309 else { 310 refillContainerInstance(); 311 } 312 } 313 314 public void add(RADComponent comp) { 315 add(comp, -1); 316 } 317 318 public void add(RADComponent metacomp, int index) { 319 RADVisualComponent visual = metacomp instanceof RADVisualComponent ? 320 (RADVisualComponent) metacomp : null; 321 if (visual != null) { 322 if (index == -1) { 323 index = subComponents.size(); 324 subComponents.add(visual); 325 } else { 326 subComponents.add(index, visual); 327 } 328 if (layoutSupport == null) { 329 Component comp = (Component) visual.getBeanInstance(); 330 FakePeerSupport.attachFakePeer(comp); 331 if (comp instanceof Container) 332 FakePeerSupport.attachFakePeerRecursively((Container)comp); 333 getContainerDelegate(getBeanInstance()).add(comp, index); 334 } 335 } 336 else if (metacomp instanceof RADMenuComponent) 337 containerMenu = (RADMenuComponent) metacomp; else 339 return; 341 metacomp.setParentComponent(this); 342 if (visual != null) { visual.getConstraintsProperties(); 344 } 345 } 346 347 public void remove(RADComponent comp) { 348 if (comp instanceof RADVisualComponent) { 349 int index = subComponents.indexOf(comp); 350 if (layoutSupport != null) { 351 layoutSupport.removeComponent((RADVisualComponent) comp, index); 352 } 353 else { 354 getContainerDelegate(getBeanInstance()).remove(index); 355 } 356 if (subComponents.remove(comp)) 357 comp.setParentComponent(null); 358 } 359 else if (comp == containerMenu) { 360 containerMenu = null; 361 comp.setParentComponent(null); 362 } 363 else return; 364 } 365 366 public int getIndexOf(RADComponent comp) { 367 if (comp != null && comp == containerMenu) 368 return subComponents.size(); 369 370 return subComponents.lastIndexOf(comp); 371 } 372 } 373 | Popular Tags |