[jbosstools-commits] JBoss Tools SVN: r22511 - in trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console: wizards and 1 other directory.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Thu Jun 3 09:13:42 EDT 2010


Author: dgeraskov
Date: 2010-06-03 09:13:41 -0400 (Thu, 03 Jun 2010)
New Revision: 22511

Modified:
   trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java
   trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties
   trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/wizards/NewConfigurationWizard.java
   trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/wizards/NewConfigurationWizardPage.java
Log:
https://jira.jboss.org/browse/JBIDE-4918

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java	2010-06-03 12:21:57 UTC (rev 22510)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java	2010-06-03 13:13:41 UTC (rev 22511)
@@ -193,10 +193,13 @@
 	public static String NewConfigurationWizardPage_file_already_exist;
 	public static String NewConfigurationWizardPage_file_container_must_be_specified;
 	public static String NewConfigurationWizardPage_file_container_must_exist;
-	public static String NewConfigurationWizardPage_file_extension_must_be_cfgxml;
+	public static String NewConfigurationWizardPage_file_extension_should_be_cfgxml;
 	public static String NewConfigurationWizardPage_file_name;
 	public static String NewConfigurationWizardPage_file_name_must_be_specified;
 	public static String NewConfigurationWizardPage_file_name_must_be_valid;
+	public static String NewConfigurationWizardPage_filefile_name;
+	public static String NewConfigurationWizardPage_fileoutside_classpath;
+	public static String NewConfigurationWizardPage_fileshould_pass_configuration;
 	public static String NewConfigurationWizardPage_hibernate_config_file;
 	public static String NewConfigurationWizardPage_password;
 	public static String NewConfigurationWizardPage_project_must_be_writable;
@@ -437,6 +440,7 @@
 	public static String ConsoleConfigurationTab_create_new;
 	public static String ConsoleConfigurationTab_setup;
 	public static String ConsoleConfigurationTabGroup_archive_classpath_entry_does_not_exist;
+	
 	public static String ConsoleConfigurationTabGroup_classpath_must_be_set_or_restored_to_default;
 	public static String DirectoryBrowseField_filesystem;
 	public static String DirectoryBrowseField_workspace;

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties	2010-06-03 12:21:57 UTC (rev 22510)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties	2010-06-03 13:13:41 UTC (rev 22511)
@@ -188,10 +188,13 @@
 NewConfigurationWizardPage_file_already_exist=File already exists
 NewConfigurationWizardPage_file_container_must_be_specified=File container must be specified
 NewConfigurationWizardPage_file_container_must_exist=File container must exist
-NewConfigurationWizardPage_file_extension_must_be_cfgxml=File extension must be "cfg.xml"
+NewConfigurationWizardPage_file_extension_should_be_cfgxml=File extension should be "cfg.xml"
 NewConfigurationWizardPage_file_name=Fi&le name:
 NewConfigurationWizardPage_file_name_must_be_specified=File name must be specified
 NewConfigurationWizardPage_file_name_must_be_valid=File name must be valid
+NewConfigurationWizardPage_filefile_name=hibernate.cfg.xml
+NewConfigurationWizardPage_fileoutside_classpath=Configuration placed outside CLASSPATH will not be available at run-time
+NewConfigurationWizardPage_fileshould_pass_configuration=To load Configuration from ''{0}'' you''ll have to pass file name to Configuration.configure() method
 NewConfigurationWizardPage_hibernate_config_file=Hibernate Configuration File (cfg.xml)
 NewConfigurationWizardPage_password=&Password:
 NewConfigurationWizardPage_project_must_be_writable=Project must be writable

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/wizards/NewConfigurationWizard.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/wizards/NewConfigurationWizard.java	2010-06-03 12:21:57 UTC (rev 22510)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/wizards/NewConfigurationWizard.java	2010-06-03 13:13:41 UTC (rev 22511)
@@ -22,17 +22,31 @@
 package org.hibernate.eclipse.console.wizards;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Properties;
 
+import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IPackageFragmentRoot;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.wizard.IWizardPage;
 import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.osgi.util.NLS;
 import org.eclipse.ui.INewWizard;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPage;
@@ -72,6 +86,7 @@
 
         public ExtendedWizardNewFileCreationPage(String pageName, IStructuredSelection selection) {
             super(pageName, selection);
+            setAllowExistingResources(false);
         }
 
         boolean firstTime = true;
@@ -82,6 +97,51 @@
                 firstTime = false;
             }
         }
+        
+        @Override
+        protected boolean validatePage() {
+        	if (super.validatePage()){
+        		String fileName = getFileName();
+        		if (!fileName.endsWith(".cfg.xml") ) { //$NON-NLS-1$
+        			setMessage(HibernateConsoleMessages.NewConfigurationWizardPage_file_extension_should_be_cfgxml, WARNING);
+                } else if (!fileName.equals(HibernateConsoleMessages.NewConfigurationWizardPage_filefile_name)){
+                	setMessage(NLS.bind(HibernateConsoleMessages.NewConfigurationWizardPage_fileshould_pass_configuration,
+                			fileName), WARNING);
+                }
+        		IPath path = getContainerFullPath();
+        		IContainer container = ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
+        		
+        		try {
+					if (container != null && container.getProject().hasNature(JavaCore.NATURE_ID)) {
+						IJavaProject proj = JavaCore.create(container.getProject());
+						IPath projRelPath = container.getProjectRelativePath();
+						IPackageFragmentRoot[] roots;
+
+						roots = proj.getAllPackageFragmentRoots();
+						boolean found = false;
+						for (IPackageFragmentRoot root : roots) {
+							if (root.isArchive()) continue;
+							if (root.getResource() != null && root.getResource().getProjectRelativePath().isPrefixOf(projRelPath)){
+								if (!root.getResource().getProjectRelativePath().equals(projRelPath)){
+				                	//this is not "src" folder
+									setMessage(NLS.bind(HibernateConsoleMessages.NewConfigurationWizardPage_fileshould_pass_configuration,
+				                			fileName), WARNING);
+								}
+								found = true;
+								break;
+							}
+						}
+						if (!found){
+							setMessage(HibernateConsoleMessages.NewConfigurationWizardPage_fileoutside_classpath, WARNING);
+						}										
+					}
+				} catch (CoreException e) {
+					HibernateConsolePlugin.getDefault().log(e);
+				}
+        		return true;
+        	}
+        	return false;
+        }
     }
     
 	/**
@@ -122,11 +182,39 @@
 	 * using wizard as execution context.
 	 */
 	public boolean performFinish() {
+		final Properties props = new Properties();
+        putIfNotNull(props, Environment.SESSION_FACTORY_NAME, connectionInfoPage.getSessionFactoryName() );
+        putIfNotNull(props, Environment.DIALECT, connectionInfoPage.getDialect() );
+        putIfNotNull(props, Environment.DRIVER, connectionInfoPage.getDriver() );
+        putIfNotNull(props, Environment.URL, connectionInfoPage.getConnectionURL() );
+        putIfNotNull(props, Environment.USER, connectionInfoPage.getUsername() );
+        putIfNotNull(props, Environment.PASS, connectionInfoPage.getPassword() );
+        putIfNotNull(props, Environment.DEFAULT_CATALOG, connectionInfoPage.getDefaultCatalog() );
+        putIfNotNull(props, Environment.DEFAULT_SCHEMA, connectionInfoPage.getDefaultSchema() );
         final IFile file = cPage.createNewFile();
 
-        openHibernateCfgXml(file);
-
+		IRunnableWithProgress op = new IRunnableWithProgress() {
+			public void run(IProgressMonitor monitor) throws InvocationTargetException {
+				try {
+					createHibernateCfgXml(file, props, monitor);
+				} catch (CoreException e) {
+					throw new InvocationTargetException(e);
+				} finally {
+					monitor.done();
+				}
+			}
+		};
 		try {
+			getContainer().run(true, false, op);
+		} catch (InterruptedException e) {
+			return false;
+		} catch (InvocationTargetException e) {
+			Throwable realException = e.getTargetException();
+			HibernateConsolePlugin.getDefault().showError(getShell(), HibernateConsoleMessages.NewConfigurationWizard_error, realException);
+			return false;
+		}
+		
+		try {
 			if (connectionInfoPage.isCreateConsoleConfigurationEnabled()) {
 				confPage.performFinish();
 			} else {
@@ -159,7 +247,30 @@
         }
     }
 
-	private void openHibernateCfgXml(final IFile file) {
+    /**
+	 * The worker method. It will find the container, create the
+	 * file if missing or just replace its contents, and open
+	 * the editor on the newly created file.
+     * @param file
+     * @param props
+	 */
+	private void createHibernateCfgXml(
+		final IFile file, Properties props, IProgressMonitor monitor)
+		throws CoreException {
+		// create a sample file
+		monitor.beginTask(HibernateConsoleMessages.NewConfigurationWizard_creating + file.getName(), 2);
+		try {
+			InputStream stream = openContentStream(props);
+			if (file.exists() ) {
+                file.setContents(stream, true, true, monitor);
+			} else {
+				file.create(stream, true, monitor);
+			}
+			stream.close();
+		} catch (IOException e) {
+		}
+		monitor.worked(1);
+		monitor.setTaskName(HibernateConsoleMessages.NewConfigurationWizard_open_file_for_editing);
 		getShell().getDisplay().asyncExec(new Runnable() {
 			public void run() {
 				IWorkbenchPage page =
@@ -170,6 +281,7 @@
 				}
 			}
 		});
+		monitor.worked(1);
 	}
 
 	/**
@@ -197,6 +309,32 @@
 	 */
 	public void init(IWorkbench workbench, IStructuredSelection selection) {
 		this.selection = selection;
+		if (!selection.isEmpty()){
+			IJavaProject jproj = null; 
+			if (selection.getFirstElement() instanceof IJavaProject) {
+				jproj = (IJavaProject) selection.getFirstElement();
+			}
+			if (selection.getFirstElement() instanceof IProject){
+				try {
+					if (((IProject)selection.getFirstElement()).getNature(JavaCore.NATURE_ID) != null) {
+						jproj =  (IJavaProject)((IProject)selection.getFirstElement()).getNature(JavaCore.NATURE_ID);
+					}
+				} catch (CoreException e) {
+					HibernateConsolePlugin.getDefault().log(e);
+				}
+			}
+			if (jproj != null){
+				IPackageFragmentRoot[] roots;
+				try {
+					roots = jproj.getAllPackageFragmentRoots();
+					if (roots.length > 0){
+						this.selection = new StructuredSelection(roots[0]);
+					}
+				} catch (JavaModelException e) {
+					HibernateConsolePlugin.getDefault().log(e);
+				}				
+			};			
+		}		
 	}
 
 	public IWizardPage getNextPage(IWizardPage page) {

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/wizards/NewConfigurationWizardPage.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/wizards/NewConfigurationWizardPage.java	2010-06-03 12:21:57 UTC (rev 22510)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/wizards/NewConfigurationWizardPage.java	2010-06-03 13:13:41 UTC (rev 22511)
@@ -21,8 +21,6 @@
  */
 package org.hibernate.eclipse.console.wizards;
 
-import org.eclipse.core.resources.IContainer;
-import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.Path;
@@ -315,17 +313,7 @@
             updateStatus(HibernateConsoleMessages.NewConfigurationWizardPage_file_name_must_be_valid);
             return;
         }
-        if (!fileName.endsWith(".cfg.xml") ) { //$NON-NLS-1$
-            updateStatus(HibernateConsoleMessages.NewConfigurationWizardPage_file_extension_must_be_cfgxml);
-            return;
-        }
 
-        IFile file = ( (IContainer) container).getFile(new Path(fileName) );
-        if(file.exists() ) {
-            updateStatus(HibernateConsoleMessages.NewConfigurationWizardPage_file_already_exist);
-            return;
-        }
-
         // TODO: check for driver class availability.
         updateStatus(null);
     }



More information about the jbosstools-commits mailing list