Author: vyemialyanchyk
Date: 2010-08-10 13:01:22 -0400 (Tue, 10 Aug 2010)
New Revision: 24028
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/codegen/ExportAntCodeGenWizardPage.java
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/launch/CodeGenXMLFactory.java
Log:
https://jira.jboss.org/browse/JBIDE-6620 - fixed
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/codegen/ExportAntCodeGenWizardPage.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/codegen/ExportAntCodeGenWizardPage.java 2010-08-10
17:01:01 UTC (rev 24027)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/codegen/ExportAntCodeGenWizardPage.java 2010-08-10
17:01:22 UTC (rev 24028)
@@ -12,14 +12,25 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IWorkspace;
+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.debug.core.ILaunchConfiguration;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.ComboDialogField;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
+import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IMessageProvider;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
@@ -27,6 +38,13 @@
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
+import org.eclipse.ui.ide.undo.CreateFileOperation;
+import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
+import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
+import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
+import org.hibernate.console.ConsoleConfiguration;
+import org.hibernate.console.KnownConfigurations;
+import org.hibernate.console.preferences.ConsoleConfigurationPreferences;
import org.hibernate.eclipse.console.HibernateConsoleMessages;
import org.hibernate.eclipse.console.utils.LaunchHelper;
import org.hibernate.eclipse.launch.CodeGenXMLFactory;
@@ -40,6 +58,8 @@
protected ComboDialogField consoleConfigurationName;
+ protected CodeGenXMLFactory codeGenXMLFactory = null;
+
/**
* Creates a new file creation (Ant code generation) wizard page. If the initial
resource
* selection contains exactly one container resource then it will be used as the
default
@@ -123,9 +143,7 @@
res = false;
} else {
String checkMessage = checkCodeGenLaunchConfig(lc);
- if (checkMessage != null) {
- checkMessage =
HibernateConsoleMessages.ExportAntCodeGenWizardPage_error_in_hibernate_code_generation_configuration
- + " " + checkMessage; //$NON-NLS-1$
+ if (checkMessage != null) {
setMessage(checkMessage, IMessageProvider.WARNING);
}
}
@@ -134,14 +152,45 @@
return res;
}
+ protected ConsoleConfigurationPreferences getConsoleConfigPreferences(String
consoleConfigName) {
+ ConsoleConfiguration consoleConfig =
KnownConfigurations.getInstance().find(consoleConfigName);
+ if (consoleConfig == null) {
+ return null;
+ }
+ return consoleConfig.getPreferences();
+ }
+
protected String checkCodeGenLaunchConfig(ILaunchConfiguration lc) {
String checkMessage = null;
+ ExporterAttributes attributes = null;
try {
- ExporterAttributes attributes = new ExporterAttributes(lc);
+ attributes = new ExporterAttributes(lc);
checkMessage = attributes.checkExporterAttributes();
} catch (CoreException e) {
checkMessage = e.getMessage();
}
+ if (checkMessage != null) {
+ checkMessage =
NLS.bind(HibernateConsoleMessages.ExportAntCodeGenWizardPage_error_in_hibernate_code_generation_configuration,
+ checkMessage);
+ }
+ if (checkMessage == null && attributes != null) {
+ String consoleConfigName = attributes.getConsoleConfigurationName();
+ ConsoleConfigurationPreferences consoleConfigPrefs =
+ getConsoleConfigPreferences(consoleConfigName);
+ String connProfileName = consoleConfigPrefs == null ? null :
+ consoleConfigPrefs.getConnectionProfileName();
+ if (!CodeGenXMLFactory.isEmpty(connProfileName)) {
+ IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ String externalPropFileName = CodeGenXMLFactory.propFileNameSuffix;
+ externalPropFileName = getFileName() + "." + externalPropFileName;
//$NON-NLS-1$
+ String problemMessage =
NLS.bind(HibernateConsoleMessages.ExportAntCodeGenWizardPage_warning,
+ externalPropFileName);
+ IPath resourcePath = getContainerFullPath().append(externalPropFileName);
+ if (workspace.getRoot().getFile(resourcePath).exists()) {
+ checkMessage = problemMessage;
+ }
+ }
+ }
return checkMessage;
}
@@ -164,8 +213,83 @@
if (lc == null) {
return null;
}
- final CodeGenXMLFactory codeGenXMLFactory = new CodeGenXMLFactory(lc);
+ codeGenXMLFactory = new CodeGenXMLFactory(lc);
+ String externalPropFileName = CodeGenXMLFactory.propFileNameSuffix;
+ externalPropFileName = getFileName() + "." + externalPropFileName;
//$NON-NLS-1$
+ codeGenXMLFactory.setExternalPropFileName(externalPropFileName);
String buildXml = codeGenXMLFactory.createCodeGenXML();
return new ByteArrayInputStream(buildXml.getBytes());
}
+
+ public IFile createNewFile() {
+ codeGenXMLFactory = null;
+ IFile res = super.createNewFile();
+ if (codeGenXMLFactory != null && res != null) {
+ final String propFileContentPreSave = codeGenXMLFactory.getPropFileContentPreSave();
+ if (!CodeGenXMLFactory.isEmpty(propFileContentPreSave)) {
+ IPath path = res.getFullPath();
+ path = path.removeLastSegments(1);
+ path = path.append(codeGenXMLFactory.getExternalPropFileName());
+ final IFile newFileHandle = createFileHandle(path);
+ final InputStream initialContents = new ByteArrayInputStream(
+ propFileContentPreSave.getBytes());
+ IRunnableWithProgress op = new IRunnableWithProgress() {
+ public void run(IProgressMonitor monitor) {
+ CreateFileOperation op = new CreateFileOperation(newFileHandle, null,
+ initialContents,
+ IDEWorkbenchMessages.WizardNewFileCreationPage_title);
+ try {
+ // see bug
https://bugs.eclipse.org/bugs/show_bug.cgi?id=219901
+ // directly execute the operation so that the undo state is
+ // not preserved. Making this undoable resulted in too many
+ // accidental file deletions.
+ 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()));
+ }
+ }
+ });
+ }
+ }
+ };
+ try {
+ getContainer().run(true, true, op);
+ } catch (InterruptedException e) {
+ } 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 res;
+ }
}
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-08-10
17:01:01 UTC (rev 24027)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java 2010-08-10
17:01:22 UTC (rev 24028)
@@ -385,6 +385,7 @@
public static String CodeGenerationSettingsTab_use_custom_templates;
public static String CodeGenerationSettingsTab_use_existing;
public static String CodeGenerationSettingsTab_workspace;
+ public static String CodeGenerationSettingsTab_use_generation_in_external_process;
public static String ConsoleConfigurationLaunchDelegate_direct_launch_not_supported;
public static String ConsoleConfigurationMainTab_annotations;
public static String ConsoleConfigurationMainTab_cfg_xml;
@@ -523,6 +524,11 @@
public static String
ExportAntCodeGenWizardPage_empty_hibernate_code_generation_configuration;
public static String
ExportAntCodeGenWizardPage_cannot_find_selected_hibernate_code_generation_configuration;
public static String
ExportAntCodeGenWizardPage_error_in_hibernate_code_generation_configuration;
+ public static String ExportAntCodeGenWizardPage_warning;
+
+ public static String QueryParametersPage_add_query_parameter_tooltip;
+ public static String QueryParametersPage_remove_query_parameter_tooltip;
+ public static String QueryParametersPage_enable_disable_parameters_tooltip;
//
static {
// initialize resource bundle
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-08-10
17:01:01 UTC (rev 24027)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties 2010-08-10
17:01:22 UTC (rev 24028)
@@ -388,6 +388,7 @@
CodeGenerationSettingsTab_use_custom_templates=Use custom templates (for custom file
generation)
CodeGenerationSettingsTab_use_existing=Use &existing...
CodeGenerationSettingsTab_workspace=&Workspace...
+CodeGenerationSettingsTab_use_generation_in_external_process=Use generation in external
process
ConsoleConfigurationLaunchDelegate_direct_launch_not_supported=Direct launch not
supported
ConsoleConfigurationMainTab_annotations=Annotations (jdk 1.5+)
ConsoleConfigurationMainTab_cfg_xml=cfg.xml
@@ -522,4 +523,9 @@
ExportAntCodeGenWizardPage_hibernate_code_generation_configurations=Hibernate Code
Generation Configurations:
ExportAntCodeGenWizardPage_empty_hibernate_code_generation_configuration=Hibernate Code
Generation Configuration is empty
ExportAntCodeGenWizardPage_cannot_find_selected_hibernate_code_generation_configuration=Can't
find selected Hibernate Code Generation Configuration
-ExportAntCodeGenWizardPage_error_in_hibernate_code_generation_configuration=Error in
Hibernate Code Generation Configuration:
+ExportAntCodeGenWizardPage_error_in_hibernate_code_generation_configuration=Error in
Hibernate Code Generation Configuration: ''{0}''
+ExportAntCodeGenWizardPage_warning=Warning: ''{0}'' already exists.
+
+QueryParametersPage_add_query_parameter_tooltip=Add Query Parameter
+QueryParametersPage_remove_query_parameter_tooltip=Remove Query Parameter
+QueryParametersPage_enable_disable_parameters_tooltip=Enable/Disable Parameters
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenXMLFactory.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenXMLFactory.java 2010-08-10
17:01:01 UTC (rev 24027)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenXMLFactory.java 2010-08-10
17:01:22 UTC (rev 24028)
@@ -13,6 +13,7 @@
import java.io.ByteArrayOutputStream;
import java.net.URISyntaxException;
import java.net.URL;
+import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -58,14 +59,26 @@
*/
public static final long versionUID4PropFile = 1841714864553304000L;
/**
+ */
+ public static final String propFileNameSuffix = "hibernate.properties";
//$NON-NLS-1$
+ /**
* presave generated Hibernate Properties file content,
* this is necessary to proper content formating
*/
protected String propFileContentPreSave = ""; //$NON-NLS-1$
/**
- * generate Ant scrip from this launch configuration
+ * generate Ant script from this launch configuration
*/
protected ILaunchConfiguration lc = null;
+ /**
+ * generate external Hibernate Properties file or
+ * put generated properties into Ant script directly
+ */
+ protected boolean externalPropFile = true;
+ /**
+ * file name for generated properties file
+ */
+ protected String externalPropFileName = propFileNameSuffix;
public CodeGenXMLFactory(ILaunchConfiguration lc) {
this.lc = lc;
@@ -115,52 +128,73 @@
String generateHibernatePropeties = null;
String connProfileName = consoleConfigPrefs == null ? null :
consoleConfigPrefs.getConnectionProfileName();
- if (!isEmpty(connProfileName)) {
- IConnectionProfile profile =
ProfileManager.getInstance().getProfileByName(connProfileName);
- if (profile != null) {
- StringBuilder propFileContent = new StringBuilder();
- DriverInstance driverInstance =
ConnectionProfileUtil.getDriverDefinition(connProfileName);
- final Properties cpProperties = profile.getProperties(profile.getProviderId());
+ IConnectionProfile profile = getConnectionProfile(connProfileName);
+ if (profile != null) {
+ StringBuilder propFileContent = new StringBuilder();
+ String driverClass = getDriverClass(connProfileName);
+ final Properties cpProperties = profile.getProperties(profile.getProviderId());
+ //
+ String url =
cpProperties.getProperty("org.eclipse.datatools.connectivity.db.URL");
//$NON-NLS-1$
+ //
+ String user =
cpProperties.getProperty("org.eclipse.datatools.connectivity.db.username");
//$NON-NLS-1$
+ //
+ String pass =
cpProperties.getProperty("org.eclipse.datatools.connectivity.db.password");
//$NON-NLS-1$
+ //
+ String dialectName = consoleConfigPrefs.getDialectName();
+ if (consoleConfigPrefs.getPropertyFile() != null) {
+ props = consoleConfigPrefs.getProperties();
+ props.setProperty(Environment.DRIVER, driverClass);
+ props.setProperty(Environment.URL, url);
+ props.setProperty(Environment.USER, user);
+ props.setProperty(Environment.PASS, pass);
+ if (StringHelper.isNotEmpty(dialectName)) {
+ props.setProperty(Environment.DIALECT, dialectName);
+ }
+ // output keys in sort order
+ Object[] keys = props.keySet().toArray();
+ Arrays.sort(keys);
+ for (Object obj : keys) {
+ addIntoPropFileContent(propFileContent, obj.toString(),
props.getProperty(obj.toString()));
+ }
+ } else {
//
/** /
String driverURL = getConnectionProfileDriverURL(connProfileName);
- el = root.addElement(CGS.PROPERTY);
- el.addAttribute(CGS.NAME, "jdbc.driver"); //$NON-NLS-1$
- el.addAttribute(CGS.LOCATION, driverURL);
+ el = root.addElement(CodeGenerationStrings.PROPERTY);
+ el.addAttribute(CodeGenerationStrings.NAME, "jdbc.driver"); //$NON-NLS-1$
+ el.addAttribute(CodeGenerationStrings.LOCATION, driverURL);
/**/
//
- String driverClass =
driverInstance.getProperty("org.eclipse.datatools.connectivity.db.driverClass");
//$NON-NLS-1$
el = root.addElement(CodeGenerationStrings.PROPERTY);
el.addAttribute(CodeGenerationStrings.NAME, Environment.DRIVER);
el.addAttribute(CodeGenerationStrings.VALUE, driverClass);
addIntoPropFileContent(propFileContent, Environment.DRIVER);
//
- String url =
cpProperties.getProperty("org.eclipse.datatools.connectivity.db.URL");
//$NON-NLS-1$
el = root.addElement(CodeGenerationStrings.PROPERTY);
el.addAttribute(CodeGenerationStrings.NAME, Environment.URL);
el.addAttribute(CodeGenerationStrings.VALUE, url);
addIntoPropFileContent(propFileContent, Environment.URL);
//
- String user =
cpProperties.getProperty("org.eclipse.datatools.connectivity.db.username");
//$NON-NLS-1$
el = root.addElement(CodeGenerationStrings.PROPERTY);
el.addAttribute(CodeGenerationStrings.NAME, Environment.USER);
el.addAttribute(CodeGenerationStrings.VALUE, user);
addIntoPropFileContent(propFileContent, Environment.USER);
//
- String pass =
cpProperties.getProperty("org.eclipse.datatools.connectivity.db.password");
//$NON-NLS-1$
el = root.addElement(CodeGenerationStrings.PROPERTY);
el.addAttribute(CodeGenerationStrings.NAME, Environment.PASS);
el.addAttribute(CodeGenerationStrings.VALUE, pass);
addIntoPropFileContent(propFileContent, Environment.PASS);
//
- String dialectName = consoleConfigPrefs.getDialectName();
if (StringHelper.isNotEmpty(dialectName)) {
el = root.addElement(CodeGenerationStrings.PROPERTY);
el.addAttribute(CodeGenerationStrings.NAME, Environment.DIALECT);
el.addAttribute(CodeGenerationStrings.VALUE, dialectName);
addIntoPropFileContent(propFileContent, Environment.DIALECT);
}
- //
+ }
+ if (externalPropFile) {
+ hibernatePropFile = externalPropFileName;
+ } else {
hibernatePropFile = "hibernatePropFile"; //$NON-NLS-1$
el = root.addElement(CodeGenerationStrings.PROPERTY);
el.addAttribute(CodeGenerationStrings.NAME, hibernatePropFile);
@@ -174,9 +208,8 @@
Element echo = target.addElement(CodeGenerationStrings.ECHO);
echo.addAttribute(CodeGenerationStrings.FILE, hibernatePropFile);
echo.addText(getPropFileContentStubUID());
- //echo.addText(propFileContent.toString());
- propFileContentPreSave = propFileContent.toString();
}
+ propFileContentPreSave = propFileContent.toString().trim();
}
// all jars from libraries should be here
String toolslibID = "toolslib"; //$NON-NLS-1$
@@ -187,11 +220,11 @@
if (customClassPathURLs[i] == null) {
continue;
}
- // what is right here: CGS.PATH or CGS.PATHELEMENT?
+ // what is right here: CodeGenerationStrings.PATH or
CodeGenerationStrings.PATHELEMENT?
//
http://www.redhat.com/docs/en-US/JBoss_Developer_Studio/en/hibernatetools...
- // use CGS.PATH - so may be error in documentation?
+ // use CodeGenerationStrings.PATH - so may be error in documentation?
Element pathItem = toolslib.addElement(CodeGenerationStrings.PATH);
- //Element pathItem = toolslib.addElement(CGS.PATHELEMENT);
+ //Element pathItem = toolslib.addElement(CodeGenerationStrings.PATHELEMENT);
String strPathItem = customClassPathURLs[i].getPath();
try {
strPathItem = (new java.io.File(customClassPathURLs[i].toURI())).getPath();
@@ -307,10 +340,37 @@
return root;
}
+ public IConnectionProfile getConnectionProfile(String connProfileName) {
+ IConnectionProfile profile = null;
+ if (!isEmpty(connProfileName)) {
+ profile = ProfileManager.getInstance().getProfileByName(connProfileName);
+ }
+ return profile;
+ }
+
+ public DriverInstance getDriverInstance(String connProfileName) {
+ DriverInstance driverInstance = null;
+ if (!isEmpty(connProfileName)) {
+ driverInstance = ConnectionProfileUtil.getDriverDefinition(connProfileName);
+ }
+ return driverInstance;
+ }
+
+ public String getDriverClass(String connProfileName) {
+ DriverInstance driverInstance = getDriverInstance(connProfileName);
+ String driverClass = driverInstance != null ?
+ driverInstance.getProperty("org.eclipse.datatools.connectivity.db.driverClass")
: ""; //$NON-NLS-1$ //$NON-NLS-2$
+ return driverClass;
+ }
+
public void addIntoPropFileContent(StringBuilder pfc, String str) {
pfc.append(NL + str + "=${" + str + "}"); //$NON-NLS-1$
//$NON-NLS-2$
}
+ public void addIntoPropFileContent(StringBuilder pfc, String name, String value) {
+ pfc.append(NL + name + "=" + value); //$NON-NLS-1$
+ }
+
public ConsoleConfigurationPreferences getConsoleConfigPreferences(String
consoleConfigName) {
ConsoleConfiguration consoleConfig =
KnownConfigurations.getInstance().find(consoleConfigName);
if (consoleConfig == null) {
@@ -342,7 +402,7 @@
return driverURL;
}
- public boolean isEmpty(String str) {
+ public static boolean isEmpty(String str) {
return (str == null || str.length() == 0);
}
@@ -362,4 +422,17 @@
getPropFileContentStubUID(), getPropFileContentPreSave()).trim();
return res;
}
+
+ public void setExternalPropFile(boolean externalPropFile) {
+ this.externalPropFile = externalPropFile;
+ }
+
+ public void setExternalPropFileName(String externalPropFileName) {
+ this.externalPropFileName = externalPropFileName;
+ }
+
+ public String getExternalPropFileName() {
+ return externalPropFileName;
+ }
+
}