[jbosstools-commits] JBoss Tools SVN: r41653 - in trunk/jst/plugins/org.jboss.tools.jst.web.ui: src/org/jboss/tools/jst/web/ui/wizards/newfile and 1 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Jun 1 21:25:54 EDT 2012


Author: scabanovich
Date: 2012-06-01 21:25:52 -0400 (Fri, 01 Jun 2012)
New Revision: 41653

Added:
   trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-as7.xml
   trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-default.xml
Modified:
   trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/Messages.java
   trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/messages.properties
   trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/wizards/newfile/NewDSXMLWizard.java
Log:
JBIDE-11394
https://issues.jboss.org/browse/JBIDE-11394
Added 'Register in persistence.xml' checkbox, and initial implementation.



Modified: trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/Messages.java
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/Messages.java	2012-06-02 00:03:13 UTC (rev 41652)
+++ trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/Messages.java	2012-06-02 01:25:52 UTC (rev 41653)
@@ -102,6 +102,7 @@
 	public static String NewDSXMLWizard_PROFILE_NOT_FOUND;
 	public static String NewDSXMLWizard_DRIVER_NOT_FOUND;
 	public static String NewDSXMLWizard_JAR_LIST_NOT_SET;
+	public static String NewDSXMLWizard_REGISTER_FILE_LABEL;
 	static {
 		// initialize resource bundle
 		NLS.initializeMessages(BUNDLE_NAME, Messages.class);

Modified: trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/messages.properties
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/messages.properties	2012-06-02 00:03:13 UTC (rev 41652)
+++ trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/messages.properties	2012-06-02 01:25:52 UTC (rev 41653)
@@ -95,3 +95,4 @@
 NewDSXMLWizard_JAR_LIST_NOT_SET=Jar list for the connection profile is not set.
 NewDSXMLWizard_PARENT_FOLDER_LABEL=Parent fold&er:
 NewDSXMLWizard_FILE_NAME_LABEL=File na&me:
+NewDSXMLWizard_REGISTER_FILE_LABEL=Register in persistence.xml

Modified: trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/wizards/newfile/NewDSXMLWizard.java
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/wizards/newfile/NewDSXMLWizard.java	2012-06-02 00:03:13 UTC (rev 41652)
+++ trunk/jst/plugins/org.jboss.tools.jst.web.ui/src/org/jboss/tools/jst/web/ui/wizards/newfile/NewDSXMLWizard.java	2012-06-02 01:25:52 UTC (rev 41653)
@@ -33,6 +33,7 @@
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IWorkspace;
@@ -89,6 +90,7 @@
 import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelProvider;
 import org.eclipse.wst.common.frameworks.datamodel.DataModelFactory;
 import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
+import org.jboss.tools.common.model.util.EclipseResourceUtil;
 import org.jboss.tools.common.ui.IValidator;
 import org.jboss.tools.common.ui.ValidatorFactory;
 import org.jboss.tools.common.ui.widget.editor.ButtonFieldEditor;
@@ -203,8 +205,10 @@
 		private IFieldEditor folderEditor;
 		private String initialFileName = ""; //$NON-NLS-1$
 		private IFieldEditor fileNameEditor;
+		private IFieldEditor registerEditor;
 
 		IDataModel model;
+		FilterSetCollection viewFilterSetCollection;
 		
 
 		public WizardNewDSXMLFileCreationPage(String pageName, IStructuredSelection selection) {
@@ -243,7 +247,7 @@
 				File dataSourceDsFile = new File(homePath, templatePath);
 
 				//2. Create filter set for Ant.
-				FilterSetCollection viewFilterSetCollection = new FilterSetCollection();
+				viewFilterSetCollection = new FilterSetCollection();
 
 				// Do it by reusing data model provider.
 				model = DataModelFactory.createDataModel(new DSDataModelProvider());
@@ -373,6 +377,9 @@
 				}
 			});
 			
+			registerEditor = IFieldEditorFactory.INSTANCE.createCheckboxEditor("register", Messages.NewDSXMLWizard_REGISTER_FILE_LABEL, true); //$NON-NLS-1$
+			registerEditor.doFillIntoGrid(q);
+			
 			setControl(topLevel);
 			setPageComplete(validatePage());
 		}
@@ -515,8 +522,111 @@
 				return null;
 			}
 			newFile = newFileHandle;
+			
+			if("true".equals(registerEditor.getValueAsString())) { //$NON-NLS-1$
+				final IFile persistenceFile = findPersistenceXMLHandle();
+				if(persistenceFile != null) {
+					final InputStream content = getPersistanceXMLContent(persistenceFile);
+					
+					op = new IRunnableWithProgress() {
+						public void run(IProgressMonitor monitor) {
+							if(!persistenceFile.exists()) {
+								CreateFileOperation op = new CreateFileOperation(persistenceFile,
+										null, content, IDEWorkbenchMessages.WizardNewFileCreationPage_title);
+								try {
+									op.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
+								} catch (final ExecutionException e) {
+									getContainer().getShell().getDisplay().syncExec(
+										new Runnable() {
+											public void run() {
+												if (e.getCause() instanceof CoreException) {
+													ErrorDialog.openError(getContainer().getShell(), // Was
+																	// Utilities.getFocusShell()
+																	IDEWorkbenchMessages.WizardNewFileCreationPage_errorTitle,
+																	null, // no special
+																	// message
+																	((CoreException) e.getCause()).getStatus());
+												} else {
+													IDEWorkbenchPlugin.log(getClass(), "createNewFile()", e.getCause()); //$NON-NLS-1$
+													MessageDialog.openError(getContainer().getShell(),
+																	IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorTitle,
+																	NLS.bind(IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorMessage,
+																					e.getCause().getMessage()));
+												}
+											}
+										});
+								}
+							} else {
+								try {
+									persistenceFile.setContents(content, IResource.FORCE, monitor);
+								} catch (CoreException e) {
+									WebUiPlugin.getDefault().logError(e);
+								}
+							}
+						}
+					};
+					try {
+						getContainer().run(true, true, op);
+					} catch (InterruptedException e) {
+						return null;
+					} catch (InvocationTargetException e) {
+						// Execution Exceptions are handled above but we may still get
+						// unexpected runtime errors.
+						IDEWorkbenchPlugin.log(getClass(), "createNewFile()", e.getTargetException()); //$NON-NLS-1$
+						MessageDialog.open(MessageDialog.ERROR,
+										getContainer().getShell(),
+										IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorTitle,
+										NLS.bind(IDEWorkbenchMessages.WizardNewFileCreationPage_internalErrorMessage,
+														e.getTargetException().getMessage()), SWT.SHEET);
+					}
+				}
+			}
 			return newFile;
 		}
+
+		private IFile findPersistenceXMLHandle() {
+			IPath containerPath = getContainerFullPath();
+			IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(containerPath);
+			Set<IFolder> srcs = EclipseResourceUtil.getSourceFolders(folder.getProject());
+			IFolder src = (IFolder)EclipseResourceUtil.getJavaSourceRoot(folder.getProject());
+			if(src != null) {
+				IFile result = src.getFile("META-INF/persistence.xml"); //$NON-NLS-1$
+				if(!result.exists()) {
+					for (IFolder f: srcs) {
+						IFile r = f.getFile("META-INF/persistence.xml"); //$NON-NLS-1$
+						if(r.exists()) {
+							return r;
+						}
+					}
+				}
+				//handle to be created
+				return result;
+			}
+			
+			return null;
+		}
+	
+		private InputStream getPersistanceXMLContent(IFile persistenceFile) {
+			try {
+				File homePath = DSDataModelProvider.getTemplatesFolder();
+				String templatePath = (NewDSXMLWizardFactory.AS_7_TEMPLATE.equals(templateSelEditor.getValueAsString()))
+					? "/Datasource/persistence-xml-as7.xml" //$NON-NLS-1$
+					: "/Datasource/persistence-xml-default.xml"; //$NON-NLS-1$
+				File persistenceFileTemplate = new File(homePath, templatePath);
+				StringResource sr = new StringResource();
+
+				ResourceUtils.copyResource(new FileResource(persistenceFileTemplate), sr, viewFilterSetCollection,
+						null, true, false, false, null, null, null, false);
+				
+				// 4. Return input stream for new ds file taken from StringResource.
+				return sr.getInputStream();
+			} catch (IOException e) {
+				WebUiPlugin.getDefault().logError(e);
+			}
+
+			return null;
+		}
+
 	}
 
 	/**

Added: trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-as7.xml
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-as7.xml	                        (rev 0)
+++ trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-as7.xml	2012-06-02 01:25:52 UTC (rev 41653)
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence version="2.0"
+   xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+   xsi:schemaLocation="
+        http://java.sun.com/xml/ns/persistence
+        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
+   <persistence-unit name="primary">
+      <!-- If you are running in a production environment, add a managed 
+         data source, this example data source is just for development and testing! -->
+      <!-- The datasource is deployed as ${ds-deployment-location}, you
+         can find it in the source at ${ds-source-location} -->
+      <jta-data-source>java:jboss/datasources/@datasourcename@</jta-data-source>
+      <properties>
+         <!-- Properties for Hibernate -->
+         <!--
+         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
+         -->
+         <!-- Print's out the SQL statement Hibernate is using to the console, useful for debugging -->
+         <property name="hibernate.show_sql" value="false" />
+      </properties>
+   </persistence-unit>
+</persistence>


Property changes on: trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-as7.xml
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-default.xml
===================================================================
--- trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-default.xml	                        (rev 0)
+++ trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-default.xml	2012-06-02 01:25:52 UTC (rev 41653)
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence version="2.0"
+   xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+   xsi:schemaLocation="
+        http://java.sun.com/xml/ns/persistence
+        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
+   <persistence-unit name="primary">
+      <!-- If you are running in a production environment, add a managed 
+         data source, this example data source is just for development and testing! -->
+      <!-- The datasource is deployed as ${ds-deployment-location}, you
+         can find it in the source at ${ds-source-location} -->
+      <jta-data-source>@projectName at Datasource</jta-data-source>
+      <properties>
+         <!-- Properties for Hibernate -->
+         <!--
+         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
+         -->
+         <!-- Print's out the SQL statement Hibernate is using to the console, useful for debugging -->
+         <property name="hibernate.show_sql" value="false" />
+      </properties>
+   </persistence-unit>
+</persistence>


Property changes on: trunk/jst/plugins/org.jboss.tools.jst.web.ui/templates/Datasource/persistence-xml-default.xml
___________________________________________________________________
Added: svn:mime-type
   + text/plain



More information about the jbosstools-commits mailing list