|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2011 Oracle. All rights reserved. |
2 |
* Copyright (c) 2007, 2012 Oracle. All rights reserved. |
| 3 |
* This program and the accompanying materials are made available under the |
3 |
* This program and the accompanying materials are made available under the |
| 4 |
* terms of the Eclipse Public License v1.0, which accompanies this distribution |
4 |
* terms of the Eclipse Public License v1.0, which accompanies this distribution |
| 5 |
* and is available at http://www.eclipse.org/legal/epl-v10.html. |
5 |
* and is available at http://www.eclipse.org/legal/epl-v10.html. |
|
Lines 9-36
Link Here
|
| 9 |
*******************************************************************************/ |
9 |
*******************************************************************************/ |
| 10 |
package org.eclipse.jpt.jpa.eclipselink.ui.internal.ddlgen.wizards; |
10 |
package org.eclipse.jpt.jpa.eclipselink.ui.internal.ddlgen.wizards; |
| 11 |
|
11 |
|
|
|
12 |
import org.eclipse.core.resources.IContainer; |
| 13 |
import org.eclipse.core.resources.IFile; |
| 14 |
import org.eclipse.core.resources.IResource; |
| 15 |
import org.eclipse.core.resources.IResourceRuleFactory; |
| 16 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 17 |
import org.eclipse.core.resources.WorkspaceJob; |
| 18 |
import org.eclipse.core.runtime.CoreException; |
| 19 |
import org.eclipse.core.runtime.IPath; |
| 20 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 21 |
import org.eclipse.core.runtime.IStatus; |
| 22 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 23 |
import org.eclipse.core.runtime.OperationCanceledException; |
| 24 |
import org.eclipse.core.runtime.Path; |
| 25 |
import org.eclipse.core.runtime.Status; |
| 26 |
import org.eclipse.core.runtime.SubMonitor; |
| 27 |
import org.eclipse.jdt.core.IJavaProject; |
| 28 |
import org.eclipse.jface.dialogs.IDialogConstants; |
| 29 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 12 |
import org.eclipse.jface.wizard.Wizard; |
30 |
import org.eclipse.jface.wizard.Wizard; |
|
|
31 |
import org.eclipse.jpt.common.core.gen.JptGenerator; |
| 32 |
import org.eclipse.jpt.common.core.gen.LaunchConfigListener; |
| 33 |
import org.eclipse.jpt.common.ui.internal.dialogs.OptionalMessageDialog; |
| 34 |
import org.eclipse.jpt.common.ui.internal.util.SWTUtil; |
| 35 |
import org.eclipse.jpt.common.utility.internal.StringTools; |
| 36 |
import org.eclipse.jpt.common.utility.internal.SynchronizedBoolean; |
| 13 |
import org.eclipse.jpt.jpa.core.JpaProject; |
37 |
import org.eclipse.jpt.jpa.core.JpaProject; |
| 14 |
import org.eclipse.jpt.jpa.db.ConnectionProfile; |
38 |
import org.eclipse.jpt.jpa.db.ConnectionProfile; |
| 15 |
import org.eclipse.jpt.jpa.eclipselink.core.context.persistence.OutputMode; |
39 |
import org.eclipse.jpt.jpa.eclipselink.core.context.persistence.OutputMode; |
|
|
40 |
import org.eclipse.jpt.jpa.eclipselink.core.internal.ddlgen.EclipseLinkDDLGenerator; |
| 41 |
import org.eclipse.jpt.jpa.eclipselink.ui.JptJpaEclipseLinkUiPlugin; |
| 42 |
import org.eclipse.jpt.jpa.eclipselink.ui.internal.EclipseLinkUiMessages; |
| 43 |
import org.eclipse.jpt.jpa.ui.JptJpaUiPlugin; |
| 16 |
import org.eclipse.jpt.jpa.ui.internal.JptUiMessages; |
44 |
import org.eclipse.jpt.jpa.ui.internal.JptUiMessages; |
| 17 |
import org.eclipse.jpt.jpa.ui.internal.wizards.DatabaseSchemaWizardPage; |
45 |
import org.eclipse.jpt.jpa.ui.internal.wizards.DatabaseSchemaWizardPage; |
|
|
46 |
import org.eclipse.osgi.util.NLS; |
| 47 |
import org.eclipse.swt.widgets.Composite; |
| 48 |
import org.eclipse.swt.widgets.Display; |
| 49 |
import org.eclipse.swt.widgets.Shell; |
| 50 |
import org.eclipse.ui.IWorkbenchPage; |
| 51 |
import org.eclipse.ui.PartInitException; |
| 52 |
import org.eclipse.ui.PlatformUI; |
| 53 |
import org.eclipse.ui.ide.IDE; |
| 18 |
|
54 |
|
| 19 |
/** |
55 |
/** |
| 20 |
* GenerateDDLWizard |
56 |
* GenerateDDLWizard |
| 21 |
*/ |
57 |
*/ |
| 22 |
public class GenerateDDLWizard extends Wizard { |
58 |
public class GenerateDDLWizard extends Wizard { |
| 23 |
|
59 |
|
|
|
60 |
private static final String CR = StringTools.CR; |
| 61 |
|
| 24 |
private JpaProject jpaProject; |
62 |
private JpaProject jpaProject; |
| 25 |
|
63 |
private String puName; |
|
|
64 |
private JptGenerator ddlGenerator; |
| 65 |
|
| 26 |
private DatabaseSchemaWizardPage dbSettingsPage; |
66 |
private DatabaseSchemaWizardPage dbSettingsPage; |
| 27 |
private GenerationOutputModeWizardPage generationOutputModePage; |
67 |
private GenerationOutputModeWizardPage generationOutputModePage; |
| 28 |
|
68 |
|
| 29 |
// ********** constructor ********** |
69 |
// ********** constructor ********** |
| 30 |
|
70 |
|
| 31 |
public GenerateDDLWizard(JpaProject jpaProject) { |
71 |
public GenerateDDLWizard(JpaProject jpaProject, String puName) { |
| 32 |
super(); |
72 |
super(); |
| 33 |
this.jpaProject = jpaProject; |
73 |
this.jpaProject = jpaProject; |
|
|
74 |
this.puName = puName; |
| 34 |
this.setWindowTitle(JptUiMessages.GenerateDDLWizard_title); |
75 |
this.setWindowTitle(JptUiMessages.GenerateDDLWizard_title); |
| 35 |
} |
76 |
} |
| 36 |
|
77 |
|
|
Lines 49-62
Link Here
|
| 49 |
|
90 |
|
| 50 |
@Override |
91 |
@Override |
| 51 |
public boolean performFinish() { |
92 |
public boolean performFinish() { |
| 52 |
return (this.getJpaProjectConnectionProfile() != null); |
93 |
OutputMode outputMode = this.getOutputMode(); |
|
|
94 |
|
| 95 |
if((outputMode != OutputMode.sql_script) && ( ! this.displayGeneratingDDLWarning())) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
this.scheduleGenerateDDLJob(outputMode); |
| 100 |
return true; |
| 53 |
} |
101 |
} |
| 54 |
|
102 |
|
| 55 |
@Override |
103 |
@Override |
| 56 |
public boolean canFinish() { |
104 |
public boolean canFinish() { |
| 57 |
return this.dbSettingsPageCanFinish() && this.generationOutputModePageCanFinish(); |
105 |
return this.dbSettingsPageCanFinish() && this.generationOutputModePageCanFinish(); |
| 58 |
} |
106 |
} |
| 59 |
|
107 |
|
|
|
108 |
protected void scheduleGenerateDDLJob(OutputMode outputMode) { |
| 109 |
|
| 110 |
WorkspaceJob generateDDLJob = this.buildGenerateDDLJob(this.puName, this.jpaProject, outputMode); |
| 111 |
generateDDLJob.schedule(); |
| 112 |
} |
| 113 |
|
| 114 |
protected WorkspaceJob buildGenerateDDLJob(String puName, JpaProject project, OutputMode outputMode) { |
| 115 |
return new GenerateDDLJob(puName, project, outputMode); |
| 116 |
} |
| 117 |
|
| 60 |
// ********** intra-wizard methods ********** |
118 |
// ********** intra-wizard methods ********** |
| 61 |
|
119 |
|
| 62 |
public OutputMode getOutputMode() { |
120 |
public OutputMode getOutputMode() { |
|
Lines 76-79
Link Here
|
| 76 |
private ConnectionProfile getJpaProjectConnectionProfile() { |
134 |
private ConnectionProfile getJpaProjectConnectionProfile() { |
| 77 |
return this.jpaProject.getConnectionProfile(); |
135 |
return this.jpaProject.getConnectionProfile(); |
| 78 |
} |
136 |
} |
|
|
137 |
|
| 138 |
private boolean displayGeneratingDDLWarning() { |
| 139 |
if (!OptionalMessageDialog.isDialogEnabled(OverwriteConfirmerDialog.ID)) { |
| 140 |
return true; |
| 141 |
} else { |
| 142 |
OverwriteConfirmerDialog dialog = new OverwriteConfirmerDialog(this.getCurrentShell()); |
| 143 |
return dialog.open() == IDialogConstants.YES_ID; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
private Shell getCurrentShell() { |
| 148 |
return Display.getCurrent().getActiveShell(); |
| 149 |
} |
| 150 |
|
| 151 |
private JptGenerator getDDLGenerator() { |
| 152 |
return ddlGenerator; |
| 153 |
} |
| 154 |
|
| 155 |
protected void setDDLGenerator(JptGenerator ddlGenerator) { |
| 156 |
this.ddlGenerator = ddlGenerator; |
| 157 |
} |
| 158 |
|
| 159 |
// ********** overwrite dialog ********** |
| 160 |
|
| 161 |
static class OverwriteConfirmerDialog extends OptionalMessageDialog { |
| 162 |
|
| 163 |
private static final String ID= "dontShowOverwriteExistingTablesFromClasses.warning"; //$NON-NLS-1$ |
| 164 |
|
| 165 |
OverwriteConfirmerDialog(Shell parent) { |
| 166 |
super(ID, parent, |
| 167 |
EclipseLinkUiMessages.EclipseLinkDDLGeneratorUi_generatingDDLWarningTitle, |
| 168 |
NLS.bind(EclipseLinkUiMessages.EclipseLinkDDLGeneratorUi_generatingDDLWarningMessage, CR, CR + CR), |
| 169 |
MessageDialog.WARNING, |
| 170 |
new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL}, |
| 171 |
1); |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
protected void createButtonsForButtonBar(Composite parent) { |
| 176 |
this.createButton(parent, IDialogConstants.YES_ID, IDialogConstants.YES_LABEL, false); |
| 177 |
this.createButton(parent, IDialogConstants.NO_ID, IDialogConstants.NO_LABEL, true); |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
// ********** generate ddl job ********** |
| 183 |
|
| 184 |
protected static class GenerateDDLJob extends WorkspaceJob { |
| 185 |
protected final String puName; |
| 186 |
protected final JpaProject jpaProject; |
| 187 |
protected final OutputMode outputMode; |
| 188 |
private final SynchronizedBoolean generationCompleted; |
| 189 |
private boolean generationSuccessful; |
| 190 |
|
| 191 |
public GenerateDDLJob(String puName, JpaProject jpaProject, OutputMode outputMode) { |
| 192 |
super(EclipseLinkUiMessages.ECLIPSELINK_GENERATE_TABLES_JOB); |
| 193 |
this.puName = puName; |
| 194 |
this.jpaProject = jpaProject; |
| 195 |
this.outputMode = outputMode; |
| 196 |
this.generationCompleted = new SynchronizedBoolean(false); |
| 197 |
this.generationSuccessful = false; |
| 198 |
|
| 199 |
IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory(); |
| 200 |
this.setRule(ruleFactory.modifyRule(this.jpaProject.getProject())); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException { |
| 205 |
SubMonitor sm = SubMonitor.convert(monitor, EclipseLinkUiMessages.ECLIPSELINK_GENERATE_TABLES_TASK, 1); |
| 206 |
try { |
| 207 |
this.ddlGeneratorGenerate(sm.newChild(1)); |
| 208 |
} |
| 209 |
catch (OperationCanceledException e) { |
| 210 |
return Status.CANCEL_STATUS; |
| 211 |
} |
| 212 |
catch (RuntimeException re) { |
| 213 |
this.logException(re); |
| 214 |
} |
| 215 |
return Status.OK_STATUS; |
| 216 |
} |
| 217 |
|
| 218 |
protected void ddlGeneratorGenerate(IProgressMonitor monitor) { |
| 219 |
JptGenerator ddlGenerator = this.buildDDLGenerator(this.puName, this.jpaProject, this.outputMode); |
| 220 |
|
| 221 |
LaunchConfigListener launchListener = this.buildLaunchListener(); |
| 222 |
ddlGenerator.addLaunchConfigListener(launchListener); |
| 223 |
ddlGenerator.generate(monitor); |
| 224 |
try { |
| 225 |
this.generationCompleted.waitUntilTrue(); |
| 226 |
} |
| 227 |
catch (InterruptedException e) { |
| 228 |
this.logException(e); |
| 229 |
} |
| 230 |
finally { |
| 231 |
ddlGenerator.removeLaunchConfigListener(launchListener); |
| 232 |
} |
| 233 |
|
| 234 |
this.postGenerate(this.generationSuccessful); |
| 235 |
} |
| 236 |
|
| 237 |
protected void postGenerate(boolean generationSuccessful) { |
| 238 |
if( ! generationSuccessful) { |
| 239 |
this.displayError("Generation Failed"); //TODO |
| 240 |
return; |
| 241 |
} |
| 242 |
if((this.outputMode != OutputMode.database) && generationSuccessful) { |
| 243 |
this.refreshProject(); |
| 244 |
this.openGeneratedSqlFile(); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
private void refreshProject() { |
| 249 |
try { |
| 250 |
this.jpaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); |
| 251 |
} |
| 252 |
catch (CoreException e) { |
| 253 |
this.logException(e); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
private LaunchConfigListener buildLaunchListener() { |
| 258 |
return new LaunchConfigListener() { |
| 259 |
|
| 260 |
public void launchCompleted(boolean generationSuccessful) { |
| 261 |
GenerateDDLJob.this.generationSuccessful = generationSuccessful; |
| 262 |
GenerateDDLJob.this.generationCompleted.setTrue(); |
| 263 |
} |
| 264 |
}; |
| 265 |
} |
| 266 |
|
| 267 |
protected JptGenerator buildDDLGenerator(String puName, JpaProject project, OutputMode outputMode) { |
| 268 |
return new EclipseLinkDDLGenerator(puName, project, outputMode); |
| 269 |
} |
| 270 |
|
| 271 |
private void openGeneratedSqlFile() { |
| 272 |
|
| 273 |
IJavaProject javaProject = this.jpaProject.getJavaProject(); |
| 274 |
IPath projecName = new Path(javaProject.getProject().getName()); |
| 275 |
IContainer container = (IContainer)ResourcesPlugin.getWorkspace().getRoot().findMember(projecName); |
| 276 |
IFile sqlFile = container.getFile(new Path("createDDL.sql")); //TODO |
| 277 |
|
| 278 |
this.openEditor(sqlFile); |
| 279 |
} |
| 280 |
|
| 281 |
private void openEditor(final IFile file) { |
| 282 |
if(file != null) { |
| 283 |
SWTUtil.asyncExec(new Runnable() { |
| 284 |
public void run() { |
| 285 |
try { |
| 286 |
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 287 |
IDE.openEditor(page, file, true); |
| 288 |
} |
| 289 |
catch (PartInitException e) { |
| 290 |
JptJpaUiPlugin.log(e); |
| 291 |
} |
| 292 |
} |
| 293 |
}); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
protected void logException(Exception exception) { |
| 298 |
String msg = exception.getMessage(); |
| 299 |
String message = (msg == null) ? exception.toString() : msg; |
| 300 |
this.displayError(message); |
| 301 |
JptJpaEclipseLinkUiPlugin.log(exception); |
| 302 |
} |
| 303 |
|
| 304 |
private void displayError(final String message) { |
| 305 |
SWTUtil.syncExec(new Runnable() { |
| 306 |
public void run() { |
| 307 |
MessageDialog.openError( |
| 308 |
getShell(), |
| 309 |
EclipseLinkUiMessages.EclipseLinkDDLGeneratorUi_error, |
| 310 |
message |
| 311 |
); |
| 312 |
} |
| 313 |
}); |
| 314 |
} |
| 315 |
|
| 316 |
protected Shell getShell() { |
| 317 |
Display display = Display.getCurrent(); |
| 318 |
Shell shell = (display == null) ? null : display.getActiveShell(); |
| 319 |
if (shell == null && display != null) { |
| 320 |
Shell[] shells = display.getShells(); |
| 321 |
if (shells.length > 0) |
| 322 |
shell = shells[0]; |
| 323 |
} |
| 324 |
return shell; |
| 325 |
} |
| 326 |
} |
| 79 |
} |
327 |
} |