1 19 20 package org.netbeans.modules.form.layoutsupport; 21 22 import java.awt.*; 23 import java.util.*; 24 import java.lang.ref.*; 25 26 import org.openide.loaders.*; 27 import org.openide.filesystems.*; 28 import org.openide.ErrorManager; 29 30 import org.netbeans.modules.form.FormModel; 31 import org.netbeans.modules.form.FormUtils; 32 import org.netbeans.modules.form.palette.PaletteItem; 33 import org.netbeans.modules.form.palette.PaletteUtils; 34 35 40 41 public class LayoutSupportRegistry { 42 43 private static Map containerToLayoutDelegate; 44 private static Map layoutToLayoutDelegate; 45 46 private static boolean needPaletteRescan = true; 47 48 public static final String DEFAULT_SUPPORT = "<default>"; 50 private static FileChangeListener paletteListener; 51 52 private static Map instanceMap; 53 54 private Reference formModelRef; 55 56 58 private LayoutSupportRegistry(FormModel formModel) { 59 this.formModelRef = new WeakReference(formModel); 60 } 61 62 public static LayoutSupportRegistry getRegistry(FormModel formModel) { 63 LayoutSupportRegistry reg; 64 if (instanceMap == null) { 65 instanceMap = new WeakHashMap(); 66 reg = null; 67 } 68 else reg = (LayoutSupportRegistry) instanceMap.get(formModel); 69 70 if (reg == null) { 71 reg = new LayoutSupportRegistry(formModel); 72 instanceMap.put(formModel, reg); 73 } 74 75 return reg; 76 } 77 78 81 public Class getSupportClassForContainer(Class containerClass) { 82 String className = (String ) 83 getContainersMap().get(containerClass.getName()); 84 if (className == null) { 85 className = findSuperClass(getContainersMap(), containerClass); 86 } 93 94 return className != null ? loadClass(className) : null; 95 } 96 97 public String getSupportNameForContainer(String containerClassName) { 98 String className = (String ) getContainersMap().get(containerClassName); 99 if (className == null) { 100 Class containerClass = loadClass(containerClassName); 101 if (containerClass != null) 102 className = findSuperClass(getContainersMap(), containerClass); 103 } 110 111 return className; 112 } 113 114 public Class getSupportClassForLayout(Class layoutClass) { 115 String className = (String ) getLayoutsMap().get(layoutClass.getName()); 116 if (className == null && needPaletteRescan) 117 className = scanPalette(layoutClass.getName()); 118 if (className == null) 119 className = findSuperClass(getLayoutsMap(), layoutClass); 120 121 return className != null ? loadClass(className) : null; 122 } 123 124 public String getSupportNameForLayout(String layoutClassName) { 125 String className = (String ) getLayoutsMap().get(layoutClassName); 126 if (className == null && needPaletteRescan) 127 className = scanPalette(layoutClassName); 128 if (className == null) { 129 Class layoutClass = loadClass(layoutClassName); 130 if (layoutClass != null) 131 className = findSuperClass(getLayoutsMap(), layoutClass); 132 } 133 134 return className; 135 } 136 137 140 public static void registerSupportForContainer( 141 Class containerClass, 142 Class layoutDelegateClass) 143 { 144 getContainersMap().put(containerClass.getName(), 145 layoutDelegateClass.getName()); 146 } 147 148 public static void registerSupportForContainer( 149 String containerClassName, 150 String layoutDelegateClassName) 151 { 152 getContainersMap().put(containerClassName, layoutDelegateClassName); 153 } 154 155 public static void registertSupportForLayout( 156 Class layoutClass, 157 Class layoutDelegateClass) 158 { 159 getLayoutsMap().put(layoutClass.getName(), 160 layoutDelegateClass.getName()); 161 } 162 163 public static void registerSupportForLayout( 164 String layoutClassName, 165 String layoutDelegateClassName) 166 { 167 getLayoutsMap().put(layoutClassName, layoutDelegateClassName); 168 } 169 170 173 public LayoutSupportDelegate createSupportForContainer(Class containerClass) 174 throws ClassNotFoundException , 175 InstantiationException , 176 IllegalAccessException 177 { 178 Class delegateClass = getSupportClassForContainer(containerClass); 179 if (delegateClass == null) 180 return null; 181 182 return (LayoutSupportDelegate) delegateClass.newInstance(); 183 } 184 185 public LayoutSupportDelegate createSupportForLayout(Class layoutClass) 186 throws ClassNotFoundException , 187 InstantiationException , 188 IllegalAccessException 189 { 190 String delegateClassName = getSupportNameForLayout(layoutClass.getName()); 191 if (delegateClassName == null) 192 return null; 193 194 if (delegateClassName == DEFAULT_SUPPORT) 195 return new DefaultLayoutSupport(layoutClass); 196 197 return (LayoutSupportDelegate) 198 loadClass(delegateClassName).newInstance(); 199 } 200 201 public static LayoutSupportDelegate createSupportInstance( 202 Class layoutDelegateClass) 203 throws InstantiationException , IllegalAccessException 204 { 205 return (LayoutSupportDelegate) layoutDelegateClass.newInstance(); 206 } 207 208 211 private String findSuperClass(Map map, Class subClass) { 212 for (Iterator it=map.entrySet().iterator(); it.hasNext(); ) { 213 Map.Entry en = (Map.Entry) it.next(); 214 String className = (String ) en.getKey(); 215 Class keyClass = loadClass(className); 216 if (keyClass != null && keyClass.isAssignableFrom(subClass)) 217 return (String ) en.getValue(); 218 } 219 return null; 220 } 221 222 private static String scanPalette(String wantedClassName) { 223 FileObject paletteFolder = PaletteUtils.getPaletteFolder(); 224 225 boolean newPaletteListener = paletteListener == null; 227 if (newPaletteListener) { 228 paletteListener = new FileChangeAdapter() { 229 public void fileDataCreated(FileEvent fe) { 230 needPaletteRescan = true; 231 } 232 public void fileFolderCreated(FileEvent fe) { 233 needPaletteRescan = true; 234 fe.getFile().addFileChangeListener(this); 235 } 236 public void fileDeleted(FileEvent fe) { 237 fe.getFile().removeFileChangeListener(this); 238 } 239 }; 240 241 paletteFolder.addFileChangeListener(paletteListener); 242 } 243 244 String foundSupportClassName = null; 245 246 FileObject[] paletteCategories = paletteFolder.getChildren(); 247 for (int i=0; i < paletteCategories.length; i++) { 248 FileObject categoryFolder = paletteCategories[i]; 249 if (!categoryFolder.isFolder()) 250 continue; 251 252 if (newPaletteListener) 253 categoryFolder.addFileChangeListener(paletteListener); 254 255 FileObject[] paletteItems = categoryFolder.getChildren(); 256 for (int j=0; j < paletteItems.length; j++) { 257 DataObject itemDO = null; 258 try { 259 itemDO = DataObject.find(paletteItems[j]); 260 } 261 catch (DataObjectNotFoundException ex) { 262 continue; 263 } 264 265 PaletteItem item = (PaletteItem) 266 itemDO.getCookie(PaletteItem.class); 267 if (item == null || !item.isLayout()) 268 continue; 269 270 Class itemClass = item.getComponentClass(); 271 if (itemClass == null) 272 continue; 274 Class delegateClass = null; 275 Class supportedClass = null; 276 277 if (LayoutSupportDelegate.class.isAssignableFrom(itemClass)) { 278 delegateClass = itemClass; 280 try { 281 LayoutSupportDelegate delegate = 282 (LayoutSupportDelegate) delegateClass.newInstance(); 283 supportedClass = delegate.getSupportedClass(); 284 } 285 catch (Exception ex) { 286 org.openide.ErrorManager.getDefault().notify( 287 org.openide.ErrorManager.INFORMATIONAL, ex); 288 continue; } 290 } 291 else if (LayoutManager.class.isAssignableFrom(itemClass)) { 292 supportedClass = itemClass; 294 } 295 296 if (supportedClass != null) { 297 Map map; 298 if (Container.class.isAssignableFrom(supportedClass)) 299 map = getContainersMap(); 300 else if (LayoutManager.class.isAssignableFrom(supportedClass)) 301 map = getLayoutsMap(); 302 else continue; 304 String supportedClassName = supportedClass.getName(); 305 if (map.get(supportedClassName) == null) { 306 String delegateClassName = delegateClass != null ? 307 delegateClass.getName(): 308 DEFAULT_SUPPORT; 309 310 map.put(supportedClassName, delegateClassName); 311 312 if (supportedClassName.equals(wantedClassName)) 313 foundSupportClassName = delegateClassName; 314 } 315 } 316 } 317 } 318 319 needPaletteRescan = false; 320 return foundSupportClassName; 321 } 322 323 private Class loadClass(String className) { 324 try { 325 return FormUtils.loadClass(className, (FormModel)formModelRef.get()); 326 } 327 catch (Exception ex) { 328 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 329 } 330 catch (LinkageError ex) { 331 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 332 } 333 return null; 334 } 335 336 private static Map getContainersMap() { 337 if (containerToLayoutDelegate == null) { 338 containerToLayoutDelegate = new HashMap(); 339 containerToLayoutDelegate.put( 341 "javax.swing.JScrollPane", "org.netbeans.modules.form.layoutsupport.delegates.JScrollPaneSupport"); containerToLayoutDelegate.put( 344 "java.awt.ScrollPane", "org.netbeans.modules.form.layoutsupport.delegates.ScrollPaneSupport"); containerToLayoutDelegate.put( 347 "javax.swing.JSplitPane", "org.netbeans.modules.form.layoutsupport.delegates.JSplitPaneSupport"); containerToLayoutDelegate.put( 350 "javax.swing.JTabbedPane", "org.netbeans.modules.form.layoutsupport.delegates.JTabbedPaneSupport"); containerToLayoutDelegate.put( 353 "javax.swing.JLayeredPane", "org.netbeans.modules.form.layoutsupport.delegates.JLayeredPaneSupport"); containerToLayoutDelegate.put( 356 "javax.swing.JToolBar", "org.netbeans.modules.form.layoutsupport.delegates.JToolBarSupport"); } 359 return containerToLayoutDelegate; 360 } 361 362 private static Map getLayoutsMap() { 363 if (layoutToLayoutDelegate == null) { 364 layoutToLayoutDelegate = new HashMap(); 365 layoutToLayoutDelegate.put( 367 "java.awt.BorderLayout", "org.netbeans.modules.form.layoutsupport.delegates.BorderLayoutSupport"); layoutToLayoutDelegate.put( 370 "java.awt.FlowLayout", "org.netbeans.modules.form.layoutsupport.delegates.FlowLayoutSupport"); layoutToLayoutDelegate.put( 373 "javax.swing.BoxLayout", "org.netbeans.modules.form.layoutsupport.delegates.BoxLayoutSupport"); layoutToLayoutDelegate.put( 376 "java.awt.GridBagLayout", "org.netbeans.modules.form.layoutsupport.delegates.GridBagLayoutSupport"); layoutToLayoutDelegate.put( 379 "java.awt.GridLayout", "org.netbeans.modules.form.layoutsupport.delegates.GridLayoutSupport"); layoutToLayoutDelegate.put( 382 "java.awt.CardLayout", "org.netbeans.modules.form.layoutsupport.delegates.CardLayoutSupport"); layoutToLayoutDelegate.put( 385 "org.netbeans.lib.awtextra.AbsoluteLayout", "org.netbeans.modules.form.layoutsupport.delegates.AbsoluteLayoutSupport"); } 388 return layoutToLayoutDelegate; 389 } 390 } 391 | Popular Tags |