Author: dgeraskov
Date: 2009-10-27 11:49:17 -0400 (Tue, 27 Oct 2009)
New Revision: 18308
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/wizards/NewHibernateMappingElementsSelectionPage2.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/wizards/NewHibernateMappingFileWizard.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-5095
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/wizards/NewHibernateMappingElementsSelectionPage2.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/wizards/NewHibernateMappingElementsSelectionPage2.java 2009-10-27
15:43:23 UTC (rev 18307)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/wizards/NewHibernateMappingElementsSelectionPage2.java 2009-10-27
15:49:17 UTC (rev 18308)
@@ -15,6 +15,7 @@
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableItem;
import org.hibernate.eclipse.jdt.ui.internal.JdtUiMessages;
@@ -27,6 +28,8 @@
AddRemoveTableComposite addRemoveTableComposite;
+ Button createEmpty;
+
private IStructuredSelection selection;
public NewHibernateMappingElementsSelectionPage2(String pageName, IStructuredSelection
selection) {
@@ -36,8 +39,16 @@
}
public void createControl(Composite parent) {
+ /*Composite container = new Composite(parent, SWT.NULL);
+ GridLayout layout = new GridLayout();
+
+ container.setLayout(layout);
+ layout.verticalSpacing = 10;*/
+
addRemoveTableComposite = new AddRemoveTableComposite(parent, SWT.NONE);
addRemoveTableComposite.getTableViewer().setInput(selection.toArray());
+ createEmpty = new Button(addRemoveTableComposite, SWT.CHECK);
+ createEmpty.setText(JdtUiMessages.NewHibernateMappingElementsSelectionPage2_createEmpty);
setControl(addRemoveTableComposite);
}
@@ -45,9 +56,13 @@
TableItem[] items = addRemoveTableComposite.getTableViewer().getTable().getItems();
Object[] data = new Object[items.length];
for (int i = 0; i < items.length; i++) {
- data[i] = items[i].getData();
+ data[i] = items[i].getData();
}
return new StructuredSelection(data);
}
+
+ public boolean createEmpty(){
+ return createEmpty.getSelection();
+ }
}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/wizards/NewHibernateMappingFileWizard.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/wizards/NewHibernateMappingFileWizard.java 2009-10-27
15:43:23 UTC (rev 18307)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/wizards/NewHibernateMappingFileWizard.java 2009-10-27
15:49:17 UTC (rev 18308)
@@ -10,6 +10,11 @@
******************************************************************************/
package org.hibernate.eclipse.jdt.ui.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.Arrays;
import java.util.Collection;
@@ -20,6 +25,7 @@
import java.util.Set;
import java.util.Map.Entry;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
@@ -42,10 +48,16 @@
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.IWizardContainer;
+import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
+import org.eclipse.ui.ide.IDE;
import org.hibernate.cfg.Configuration;
import org.hibernate.console.ImageConstants;
import org.hibernate.eclipse.console.HibernateConsoleMessages;
@@ -74,10 +86,12 @@
private IStructuredSelection selection;
private NewHibernateMappingElementsSelectionPage2 page0 = null;
+
+ private WizardNewFileCreationPage cPage;
//private NewHibernateMappingElementsSelectionPage page1 = null;
- private NewHibernateMappingFilePage page2 = null;
+ private NewHibernateMappingFilePage page2 = null;
public NewHibernateMappingFileWizard(){
setDefaultPageImageDescriptor(EclipseImages.getImageDescriptor(ImageConstants.NEW_WIZARD)
);
@@ -89,6 +103,10 @@
public void addPages() {
super.addPages();
+ if (selection == null) {
+ selection = new StructuredSelection();
+ }
+
page0 = new
NewHibernateMappingElementsSelectionPage2(HibernateConsoleMessages.NewHibernateMappingFileWizard_create_hibernate_xml_mapping_file,
selection);
addPage(page0);
@@ -96,11 +114,25 @@
//page1.setTitle(
HibernateConsoleMessages.NewHibernateMappingFileWizard_create_hibernate_xml_mapping_file
);
//page1.setDescription(
HibernateConsoleMessages.NewHibernateMappingFileWizard_create_new_xml_mapping_file );
//addPage(page1);
+
+ cPage = new WizardNewFileCreationPage( "Ccfgxml", selection ); //$NON-NLS-1$
+ cPage.setTitle(
HibernateConsoleMessages.NewHibernateMappingFileWizard_create_hibernate_xml_mapping_file
);
+ cPage.setDescription(
HibernateConsoleMessages.NewHibernateMappingFileWizard_create_hibernate_xml_mapping_file
);
+ cPage.setFileName("hibernate.hbm.xml"); //$NON-NLS-1$
+ addPage( cPage );
+
page2 = new NewHibernateMappingFilePage();
- addPage(page2);
+ addPage(page2);
}
-
+
@Override
+ public IWizardPage getNextPage(IWizardPage page) {
+ if (page == page0 && !page0.createEmpty()) return page2;
+ if (page == cPage) return null;
+ return super.getNextPage(page);
+ }
+
+ @Override
public void setContainer(IWizardContainer wizardContainer) {
if (getContainer() instanceof WizardDialog) {
((WizardDialog) getContainer()).removePageChangingListener(this);
@@ -174,38 +206,115 @@
@Override
public boolean performFinish() {
- updateCompilationUnits();
- Map<IJavaProject, Configuration> configs = createConfigurations();
- for (Entry<IJavaProject, Configuration> entry : configs.entrySet()) {
- Configuration config = entry.getValue();
- HibernateMappingGlobalSettings hmgs = new HibernateMappingGlobalSettings();
-
- IResource container;
+ if (page0.createEmpty()){
+ final IFile file = cPage.createNewFile();
+ IRunnableWithProgress op = new IRunnableWithProgress() {
+ public void run(IProgressMonitor monitor) throws InvocationTargetException {
+ try {
+ doFinish(file, monitor);
+ } catch (CoreException e) {
+ throw new InvocationTargetException(e);
+ } finally {
+ monitor.done();
+ }
+ }
+ };
try {
- container = entry.getKey().getPackageFragmentRoots().length > 0
- ? entry.getKey().getPackageFragmentRoots()[0].getResource()
- : entry.getKey().getResource();
+ getContainer().run(true, false, op);
+ } catch (InterruptedException e) {
+ return false;
+ } catch (InvocationTargetException e) {
+ Throwable realException = e.getTargetException();
+ HibernateConsolePlugin.getDefault().showError(getShell(),
HibernateConsoleMessages.NewReverseEngineeringFileWizard_error, realException);
+ return false;
+ }
+ return true;
+ } else {
+ updateCompilationUnits();
+ Map<IJavaProject, Configuration> configs = createConfigurations();
+ for (Entry<IJavaProject, Configuration> entry : configs.entrySet()) {
+ Configuration config = entry.getValue();
+ HibernateMappingGlobalSettings hmgs = new HibernateMappingGlobalSettings();
- HibernateMappingExporter hce = new HibernateMappingExporter(config,
- container.getLocation().toFile());
+ IResource container;
+ try {
+ container = entry.getKey().getPackageFragmentRoots().length > 0
+ ? entry.getKey().getPackageFragmentRoots()[0].getResource()
+ : entry.getKey().getResource();
- hce.setGlobalSettings(hmgs);
- //hce.setForEach("entity");
- //hce.setFilePattern(file.getName());
+ HibernateMappingExporter hce = new HibernateMappingExporter(config,
+ container.getLocation().toFile());
+
+ hce.setGlobalSettings(hmgs);
+ //hce.setForEach("entity");
+ //hce.setFilePattern(file.getName());
+ try {
+ hce.start();
+ } catch (Exception e){
+ e.getCause().printStackTrace();
+ }
+ container.refreshLocal(IResource.DEPTH_INFINITE, null);
+ } catch (JavaModelException e1) {
+ HibernateConsolePlugin.getDefault().log(e1);
+ } catch (CoreException e) {
+ HibernateConsolePlugin.getDefault().log(e);
+ }
+ }
+ return true;
+ }
+ }
+
+ /**
+ * 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 doFinish(
+ final IFile file, IProgressMonitor monitor)
+ throws CoreException {
+ // create a sample file
+ monitor.beginTask(HibernateConsoleMessages.NewReverseEngineeringFileWizard_creating +
file.getName(), 2);
+ try {
+ InputStream stream = openContentStream();
+ 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 =
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
- hce.start();
- } catch (Exception e){
- e.getCause().printStackTrace();
+ IDE.openEditor(page, file, true);
+ } catch (PartInitException e) {
}
- container.refreshLocal(IResource.DEPTH_INFINITE, null);
- } catch (JavaModelException e1) {
- HibernateConsolePlugin.getDefault().log(e1);
- } catch (CoreException e) {
- HibernateConsolePlugin.getDefault().log(e);
}
- }
- return true;
+ });
+ monitor.worked(1);
}
+
+ private InputStream openContentStream() {
+ StringWriter sw = new StringWriter();
+ sw.write("<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\r\n" + //$NON-NLS-1$
+ "<!DOCTYPE hibernate-mapping PUBLIC \"-//Hibernate/Hibernate
Mapping DTD 3.0//EN\"
\"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\" >\r\n" +
//$NON-NLS-1$
+ "<hibernate-mapping>\r\n</hibernate-mapping>");
//$NON-NLS-1$
+ try {
+ return new ByteArrayInputStream(sw.toString().getBytes("UTF-8") );
//$NON-NLS-1$
+ } catch (UnsupportedEncodingException uec) {
+
HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.NewConfigurationWizard_problems_converting_to_utf8,
uec);
+ return new ByteArrayInputStream(sw.toString().getBytes() );
+ }
+ }
protected void initEntitiesInfo(){
if (selectionCU.size() == 0) {
@@ -285,7 +394,6 @@
Map<IJavaProject, Configuration> configs = actor.createConfigurations();
return configs;
}
-
protected void updateCompilationUnits(){
Assert.isNotNull(page0.getSelection(),
HibernateConsoleMessages.NewHibernateMappingFileWizard_selection_cant_be_empty);