1 19 20 package org.netbeans.modules.j2ee.persistence.wizard; 21 22 import java.awt.Component ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import javax.swing.JButton ; 26 import javax.swing.JLabel ; 27 import java.awt.Container ; 28 import java.io.IOException ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import javax.swing.JComponent ; 32 import java.util.Vector ; 33 import java.util.Iterator ; 34 import java.util.Collection ; 35 import java.util.Collections ; 36 import java.util.HashMap ; 37 import java.util.HashSet ; 38 import java.util.List ; 39 import java.util.Map ; 40 import java.util.Set ; 41 import org.netbeans.api.java.classpath.ClassPath; 42 import org.netbeans.api.java.project.JavaProjectConstants; 43 import org.netbeans.api.java.queries.UnitTestForSourceQuery; 44 import org.netbeans.api.project.Project; 45 import org.netbeans.api.project.ProjectUtils; 46 import org.netbeans.api.project.SourceGroup; 47 import org.netbeans.api.project.libraries.Library; 48 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 49 import org.netbeans.modules.j2ee.persistence.provider.InvalidPersistenceXmlException; 50 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 51 import org.netbeans.modules.j2ee.persistence.spi.moduleinfo.JPAModuleInfo; 52 import org.netbeans.modules.j2ee.persistence.unit.PUDataObject; 53 import org.netbeans.modules.j2ee.persistence.wizard.entity.WrapperPanel; 54 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanel.TableGeneration; 55 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanel; 56 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanelDS; 57 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanelJdbc; 58 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 59 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 60 import org.openide.DialogDescriptor; 61 import org.openide.DialogDisplayer; 62 import org.openide.ErrorManager; 63 import org.openide.WizardDescriptor; 64 import org.openide.filesystems.FileObject; 65 import org.openide.filesystems.URLMapper; 66 import org.openide.util.NbBundle; 67 68 71 public class Util { 72 73 76 public static void changeLabelInComponent(JComponent component, String oldLabel, String newLabel) { 77 JLabel label = findLabel(component, oldLabel); 78 if(label != null) { 79 label.setText(newLabel); 80 } 81 } 82 83 86 public static void hideLabelAndLabelFor(JComponent component, String lab) { 87 JLabel label = findLabel(component, lab); 88 if(label != null) { 89 label.setVisible(false); 90 Component c = label.getLabelFor(); 91 if(c != null) { 92 c.setVisible(false); 93 } 94 } 95 } 96 97 100 public static void getAllComponents( Component [] components, Collection allComponents ) { 101 for( int i = 0; i < components.length; i++ ) { 102 if( components[i] != null ) { 103 allComponents.add( components[i] ); 104 if( ( ( Container )components[i] ).getComponentCount() != 0 ) { 105 getAllComponents( ( ( Container )components[i] ).getComponents(), allComponents ); 106 } 107 } 108 } 109 } 110 111 114 public static JLabel findLabel(JComponent comp, String labelText) { 115 Vector allComponents = new Vector (); 116 getAllComponents(comp.getComponents(), allComponents); 117 Iterator iterator = allComponents.iterator(); 118 while(iterator.hasNext()) { 119 Component c = (Component )iterator.next(); 120 if(c instanceof JLabel ) { 121 JLabel label = (JLabel )c; 122 if(label.getText().equals(labelText)) { 123 return label; 124 } 125 } 126 } 127 return null; 128 } 129 130 133 public static String getClassName(String fqClassName) { 134 int dot = fqClassName.lastIndexOf("."); if (dot >= 0 && dot < fqClassName.length() - 1) { 136 return fqClassName.substring(dot + 1); 137 } else { 138 return fqClassName; 139 } 140 } 141 142 145 public static String getPackageName(String fqClassName) { 146 int dot = fqClassName.lastIndexOf("."); if (dot >= 0 && dot < fqClassName.length() - 1) { 148 return fqClassName.substring(0, dot); 149 } else { 150 return ""; } 152 } 153 154 158 public static SourceGroup getClassSourceGroup(Project project, String fqClassName) { 159 String classFile = fqClassName.replace('.', '/') + ".java"; SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 161 162 for (SourceGroup sourceGroup : sourceGroups) { 163 FileObject classFO = sourceGroup.getRootFolder().getFileObject(classFile); 164 if (classFO != null) { 165 return sourceGroup; 166 } 167 } 168 return null; 169 } 170 171 178 public static SourceGroup[] getJavaSourceGroups(Project project) { 179 SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups( 180 JavaProjectConstants.SOURCES_TYPE_JAVA); 181 Set testGroups = getTestSourceGroups(project, sourceGroups); 182 List result = new ArrayList (); 183 for (int i = 0; i < sourceGroups.length; i++) { 184 if (!testGroups.contains(sourceGroups[i])) { 185 result.add(sourceGroups[i]); 186 } 187 } 188 return (SourceGroup[]) result.toArray(new SourceGroup[result.size()]); 189 } 190 191 private static Set getTestSourceGroups(Project project, SourceGroup[] sourceGroups) { 192 Map foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups); 193 Set testGroups = new HashSet (); 194 for (int i = 0; i < sourceGroups.length; i++) { 195 testGroups.addAll(getTestTargets(sourceGroups[i], foldersToSourceGroupsMap)); 196 } 197 return testGroups; 198 } 199 200 private static Map createFoldersToSourceGroupsMap(final SourceGroup[] sourceGroups) { 201 Map result; 202 if (sourceGroups.length == 0) { 203 result = Collections.EMPTY_MAP; 204 } else { 205 result = new HashMap (2 * sourceGroups.length, .5f); 206 for (int i = 0; i < sourceGroups.length; i++) { 207 SourceGroup sourceGroup = sourceGroups[i]; 208 result.put(sourceGroup.getRootFolder(), sourceGroup); 209 } 210 } 211 return result; 212 } 213 214 private static List getFileObjects(URL [] urls) { 215 List result = new ArrayList (); 216 for (int i = 0; i < urls.length; i++) { 217 FileObject sourceRoot = URLMapper.findFileObject(urls[i]); 218 if (sourceRoot != null) { 219 result.add(sourceRoot); 220 } else { 221 int severity = ErrorManager.INFORMATIONAL; 222 if (ErrorManager.getDefault().isNotifiable(severity)) { 223 ErrorManager.getDefault().notify(severity, new IllegalStateException ( 224 "No FileObject found for the following URL: " + urls[i])); } 226 } 227 } 228 return result; 229 } 230 231 private static List getTestTargets(SourceGroup sourceGroup, Map foldersToSourceGroupsMap) { 232 final URL [] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder()); 233 if (rootURLs.length == 0) { 234 return new ArrayList (); 235 } 236 List result = new ArrayList (); 237 List sourceRoots = getFileObjects(rootURLs); 238 for (int i = 0; i < sourceRoots.size(); i++) { 239 FileObject sourceRoot = (FileObject) sourceRoots.get(i); 240 SourceGroup srcGroup = (SourceGroup) foldersToSourceGroupsMap.get(sourceRoot); 241 if (srcGroup != null) { 242 result.add(srcGroup); 243 } 244 } 245 return result; 246 } 247 248 public static ClassPath getFullClasspath(FileObject fo) { 249 FileObject[] sourceRoots = ClassPath.getClassPath(fo, ClassPath.SOURCE).getRoots(); 250 FileObject[] bootRoots = ClassPath.getClassPath(fo, ClassPath.BOOT).getRoots(); 251 FileObject[] compileRoots = ClassPath.getClassPath(fo, ClassPath.COMPILE).getRoots(); 252 FileObject[] roots = new FileObject[sourceRoots.length + bootRoots.length + compileRoots.length]; 253 for (int i = 0; i < sourceRoots.length; i++) { 254 roots[i] = sourceRoots[i]; 255 } 256 for (int i = 0; i < bootRoots.length; i++) { 257 roots[sourceRoots.length + i] = bootRoots[i]; 258 } 259 for (int i = 0; i < compileRoots.length; i++) { 260 roots[sourceRoots.length + bootRoots.length + i] = compileRoots[i]; 261 } 262 return ClassPathSupport.createClassPath(roots); 263 } 264 265 267 private static final String WIZARD_PANEL_CONTENT_DATA = "WizardPanel_contentData"; private static final String WIZARD_PANEL_CONTENT_SELECTED_INDEX = "WizardPanel_contentSelectedIndex"; 270 public static void mergeSteps(WizardDescriptor wizard, WizardDescriptor.Panel[] panels, String [] steps) { 271 Object prop = wizard.getProperty(WIZARD_PANEL_CONTENT_DATA); 272 String [] beforeSteps; 273 int offset; 274 if (prop instanceof String []) { 275 beforeSteps = (String []) prop; 276 offset = beforeSteps.length; 277 if (offset > 0 && ("...".equals(beforeSteps[offset - 1]))) { offset--; 279 } 280 } else { 281 beforeSteps = null; 282 offset = 0; 283 } 284 String [] resultSteps = new String [ (offset) + panels.length]; 285 for (int i = 0; i < offset; i++) { 286 resultSteps[i] = beforeSteps[i]; 287 } 288 setSteps(panels, steps, resultSteps, offset); 289 } 290 291 private static void setSteps(WizardDescriptor.Panel[] panels, String [] steps, String [] resultSteps, int offset) { 292 int n = steps == null ? 0 : steps.length; 293 for (int i = 0; i < panels.length; i++) { 294 final JComponent component = (JComponent ) panels[i].getComponent(); 295 String step = i < n ? steps[i] : null; 296 if (step == null) { 297 step = component.getName(); 298 } 299 component.putClientProperty(WIZARD_PANEL_CONTENT_DATA, resultSteps); 300 component.putClientProperty(WIZARD_PANEL_CONTENT_SELECTED_INDEX, new Integer (i)); 301 component.getAccessibleContext().setAccessibleDescription(step); 302 resultSteps[i + offset] = step; 303 } 304 } 305 306 public static void setSteps(WizardDescriptor.Panel[] panels, String [] steps) { 307 setSteps(panels, steps, steps, 0); 308 } 309 310 public static boolean isSupportedJavaEEVersion(Project project) { 311 JPAModuleInfo moduleInfo = project.getLookup().lookup(JPAModuleInfo.class); 312 if (moduleInfo == null){ 313 return false; 314 } 315 if (JPAModuleInfo.ModuleType.EJB == moduleInfo.getType() 316 && "3.0".equals(moduleInfo.getVersion())){ 317 return true; 318 } 319 if (JPAModuleInfo.ModuleType.WEB == moduleInfo.getType() 320 && "2.5".equals(moduleInfo.getVersion())){ 321 return true; 322 } 323 return false; 324 } 325 326 public static boolean isEjbModule(Project project) { 327 JPAModuleInfo moduleInfo = project.getLookup().lookup(JPAModuleInfo.class); 328 if (moduleInfo == null){ 329 return false; 330 } 331 return JPAModuleInfo.ModuleType.EJB == moduleInfo.getType(); 332 } 333 334 335 public static boolean isEjb21Module(Project project) { 336 JPAModuleInfo moduleInfo = project.getLookup().lookup(JPAModuleInfo.class); 337 if (moduleInfo == null){ 338 return false; 339 } 340 341 return JPAModuleInfo.ModuleType.EJB == moduleInfo.getType() 342 && "2.1".equals(moduleInfo.getVersion()); 343 } 344 345 348 public static boolean isJavaSE(Project project){ 349 return project.getLookup().lookup(JPAModuleInfo.class) == null; 350 } 351 352 361 public static PersistenceUnit buildPersistenceUnitUsingWizard(Project project, 362 String preselectedDB, TableGeneration tableGeneration){ 363 364 boolean isContainer = Util.isSupportedJavaEEVersion(project); 365 PersistenceUnitWizardPanel panel; 366 if (isContainer) { 367 panel = new PersistenceUnitWizardPanelDS(project, null, true, tableGeneration); 368 } else { 369 panel = new PersistenceUnitWizardPanelJdbc(project, null, true, tableGeneration); 370 } 371 if (preselectedDB != null) { 372 panel.setPreselectedDB(preselectedDB); 373 } 374 375 final JButton createPUButton = new JButton (NbBundle.getMessage(Util.class,"LBL_CreatePersistenceUnitButton")); 376 createPUButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(Util.class,"ACSD_CreatePersistenceUnitButton")); 377 Object [] buttons = new Object [] { createPUButton, DialogDescriptor.CANCEL_OPTION }; 378 379 final DialogDescriptor nd = new DialogDescriptor( 380 new WrapperPanel(panel), 381 NbBundle.getMessage(Util.class, "LBL_CreatePersistenceUnit"), 382 true, 383 buttons, 384 DialogDescriptor.OK_OPTION, 385 DialogDescriptor.DEFAULT_ALIGN, 386 null, 387 null 388 ); 389 panel.addPropertyChangeListener(new PropertyChangeListener () { 390 public void propertyChange(PropertyChangeEvent evt) { 391 if (evt.getPropertyName().equals(PersistenceUnitWizardPanel.IS_VALID)) { 392 Object newvalue = evt.getNewValue(); 393 if ((newvalue != null) && (newvalue instanceof Boolean )) { 394 nd.setValid(((Boolean )newvalue).booleanValue()); 395 createPUButton.setEnabled(((Boolean )newvalue).booleanValue()); 396 } 397 } 398 } 399 }); 400 if (!panel.isValidPanel()) { 401 nd.setValid(false); 402 createPUButton.setEnabled(false); 403 } 404 Object result = DialogDisplayer.getDefault().notify(nd); 405 if (result == createPUButton) { 406 PersistenceUnit punit = new PersistenceUnit(); 407 if (isContainer) { 408 PersistenceUnitWizardPanelDS puPanel = (PersistenceUnitWizardPanelDS) panel; 409 if (puPanel.getDatasource() != null && !"".equals(puPanel.getDatasource().trim())){ 410 if (puPanel.isJTA()) { 411 punit.setJtaDataSource(puPanel.getDatasource()); 412 } else { 413 if (puPanel.isNonDefaultProviderEnabled()) { 414 punit.setNonJtaDataSource(puPanel.getDatasource()); 415 } 416 punit.setTransactionType("RESOURCE_LOCAL"); 417 } 418 } 419 if (puPanel.isNonDefaultProviderEnabled()) { 420 punit.setProvider(puPanel.getNonDefaultProvider()); 421 } 422 } else { 423 PersistenceUnitWizardPanelJdbc puJdbc = (PersistenceUnitWizardPanelJdbc) panel; 424 punit = ProviderUtil.buildPersistenceUnit(puJdbc.getPersistenceUnitName(), puJdbc.getSelectedProvider(), puJdbc.getPersistenceConnection()); 425 punit.setTransactionType("RESOURCE_LOCAL"); if (puJdbc.getPersistenceLibrary() != null){ 427 addLibraryToProject(project, puJdbc.getPersistenceLibrary()); 428 } 429 } 430 punit.setName(panel.getPersistenceUnitName()); 431 ProviderUtil.setTableGeneration(punit, panel.getTableGeneration(), project); 432 return punit; 433 } 434 return null; 435 436 } 437 438 453 public static boolean createPersistenceUnitUsingWizard(Project project, 454 String preselectedDB, TableGeneration tableGeneration) throws InvalidPersistenceXmlException { 455 456 PersistenceUnit punit = buildPersistenceUnitUsingWizard(project, preselectedDB, tableGeneration); 457 if (punit == null){ 458 return false; 459 } 460 PUDataObject pud = ProviderUtil.getPUDataObject(project); 461 pud.addPersistenceUnit(punit); 462 pud.save(); 463 return true; 464 } 465 466 480 public static boolean createPersistenceUnitUsingWizard(Project project, String preselectedDB) throws InvalidPersistenceXmlException { 481 return createPersistenceUnitUsingWizard(project, preselectedDB, TableGeneration.CREATE); 482 } 483 484 public static void addLibraryToProject(Project project, Library library) { 485 ProjectClassPathExtender pcpe = (ProjectClassPathExtender) project.getLookup().lookup(ProjectClassPathExtender.class); 486 if (pcpe != null) { 487 try { 488 pcpe.addLibrary(library); 489 } catch (IOException ex) { 490 ErrorManager.getDefault().notify(ex); 491 } 492 } 493 } 494 495 public static String simpleClassName(String fqn) { 496 int lastDot = fqn.lastIndexOf('.'); 497 return lastDot > 0 ? fqn.substring(lastDot + 1) : fqn; 498 } 499 500 } 501 | Popular Tags |