Link Here
|
1 |
package org.eclipse.gmf.internal.codegen.lite.popup.actions; |
2 |
|
3 |
import java.lang.reflect.InvocationTargetException; |
4 |
|
5 |
import org.eclipse.core.resources.IFile; |
6 |
import org.eclipse.core.runtime.CoreException; |
7 |
import org.eclipse.core.runtime.IProgressMonitor; |
8 |
import org.eclipse.core.runtime.IStatus; |
9 |
import org.eclipse.core.runtime.Status; |
10 |
import org.eclipse.emf.common.util.BasicDiagnostic; |
11 |
import org.eclipse.emf.common.util.URI; |
12 |
import org.eclipse.emf.ecore.resource.Resource; |
13 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
14 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
15 |
import org.eclipse.emf.ecore.util.Diagnostician; |
16 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
17 |
import org.eclipse.gmf.codegen.gmfgen.GenDiagram; |
18 |
import org.eclipse.gmf.codegen.gmfgen.GenEditorGenerator; |
19 |
import org.eclipse.gmf.codegen.lite.ui.CodegenRcpUiPlugin; |
20 |
import org.eclipse.gmf.internal.codegen.lite.Generator; |
21 |
import org.eclipse.jface.action.IAction; |
22 |
import org.eclipse.jface.dialogs.MessageDialog; |
23 |
import org.eclipse.jface.dialogs.MessageDialogWithToggle; |
24 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
25 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
26 |
import org.eclipse.jface.preference.IPreferenceStore; |
27 |
import org.eclipse.jface.viewers.ISelection; |
28 |
import org.eclipse.jface.viewers.IStructuredSelection; |
29 |
import org.eclipse.swt.widgets.Shell; |
30 |
import org.eclipse.ui.IObjectActionDelegate; |
31 |
import org.eclipse.ui.IWorkbenchPart; |
32 |
|
33 |
public class ExecuteTemplatesAction implements IObjectActionDelegate, IRunnableWithProgress { |
34 |
private static final String ASK_INFO = "ask_info"; |
35 |
private static final String ASK_OK = "ask_ok"; |
36 |
|
37 |
private IFile mySelection; |
38 |
|
39 |
private IWorkbenchPart myPart; |
40 |
|
41 |
private IStatus myRunStatus; |
42 |
private GenEditorGenerator myGenModel; |
43 |
|
44 |
public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
45 |
myPart = targetPart; |
46 |
} |
47 |
|
48 |
// TODO Jobs |
49 |
public void run(IAction action) { |
50 |
loadGenModel(); |
51 |
assert getGenModel() != null; |
52 |
IStatus isGenModelValid = validateGenModel(); |
53 |
if (!isGenModelValid.isOK()) { |
54 |
if (!MessageDialog.openConfirm(getShell(), action.getText(), formatMessage("generatecode.badsrc", isGenModelValid))) { |
55 |
return; |
56 |
} |
57 |
} |
58 |
|
59 |
try { |
60 |
myRunStatus = Status.CANCEL_STATUS; |
61 |
new ProgressMonitorDialog(getShell()).run(true, true, this); |
62 |
} catch (InvocationTargetException ex) { |
63 |
Throwable targetException = ex.getTargetException(); |
64 |
if (targetException instanceof CoreException) { |
65 |
myRunStatus = ((CoreException)targetException).getStatus(); |
66 |
} else { |
67 |
myRunStatus = new Status(IStatus.ERROR, CodegenRcpUiPlugin.getPluginID(), 0, "Exception occurred while generating code", targetException); |
68 |
} |
69 |
} catch (InterruptedException ex) { |
70 |
myRunStatus = Status.CANCEL_STATUS; |
71 |
} finally { |
72 |
unloadGenModel(); |
73 |
} |
74 |
if (getRunStatus().isOK()) { |
75 |
if (!MessageDialogWithToggle.ALWAYS.equals(getPreferences().getString(ASK_OK))) { |
76 |
MessageDialogWithToggle.openInformation(getShell(), action.getText(), CodegenRcpUiPlugin.getBundleString("generatecode.ok"), CodegenRcpUiPlugin.getBundleString("generatecode.neveragain"), false, getPreferences(), ASK_OK); |
77 |
} |
78 |
} else if (myRunStatus.matches(IStatus.ERROR)) { |
79 |
CodegenRcpUiPlugin.getDefault().getLog().log(getRunStatus()); |
80 |
MessageDialog.openError(getShell(), action.getText(), formatMessage("generatecode.err", getRunStatus())); |
81 |
} else if (myRunStatus.matches(IStatus.WARNING)) { |
82 |
MessageDialog.openWarning(getShell(), action.getText(), formatMessage("generatecode.warn", getRunStatus())); |
83 |
} else if (myRunStatus.matches(IStatus.INFO)) { |
84 |
if (!MessageDialogWithToggle.ALWAYS.equals(getPreferences().getString(ASK_INFO))) { |
85 |
MessageDialogWithToggle.openInformation(getShell(), action.getText(), formatMessage("generatecode.info", getRunStatus()), CodegenRcpUiPlugin.getBundleString("generatecode.neveragain"), false, getPreferences(), ASK_INFO); |
86 |
} |
87 |
} |
88 |
} |
89 |
|
90 |
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { |
91 |
Generator g = new Generator(getGenModel()); |
92 |
g.run(monitor); |
93 |
myRunStatus = g.getRunStatus(); |
94 |
} |
95 |
|
96 |
public void selectionChanged(IAction action, ISelection selection) { |
97 |
mySelection = null; |
98 |
action.setEnabled(false); |
99 |
if (selection instanceof IStructuredSelection == false) { |
100 |
return; |
101 |
} |
102 |
mySelection = (IFile) ((IStructuredSelection) selection).getFirstElement(); |
103 |
action.setEnabled(true); |
104 |
} |
105 |
|
106 |
private static String formatMessage(String bundleStringKey, IStatus status) { |
107 |
return CodegenRcpUiPlugin.formatMessage(bundleStringKey, status); |
108 |
} |
109 |
|
110 |
private IStatus getRunStatus() { |
111 |
return myRunStatus; |
112 |
} |
113 |
|
114 |
private GenEditorGenerator getGenModel() { |
115 |
return myGenModel; |
116 |
} |
117 |
|
118 |
private void loadGenModel() { |
119 |
URI selected = URI.createPlatformResourceURI(mySelection.getFullPath().toString()); |
120 |
ResourceSet srcResSet = new ResourceSetImpl(); |
121 |
// [vano]: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=128998 for a details |
122 |
srcResSet.getLoadOptions().put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); |
123 |
Resource srcRes = srcResSet.getResource(selected, true); |
124 |
Object root = srcRes.getContents().get(0); |
125 |
if (root instanceof GenDiagram) { |
126 |
myGenModel = ((GenDiagram) root).getEditorGen(); |
127 |
} else if (root instanceof GenEditorGenerator) { |
128 |
myGenModel = (GenEditorGenerator) root; |
129 |
} |
130 |
if (myGenModel != null && myGenModel.getDomainGenModel() != null) { |
131 |
myGenModel.getDomainGenModel().reconcile(); |
132 |
} |
133 |
} |
134 |
|
135 |
private void unloadGenModel() { |
136 |
if (myGenModel != null && myGenModel.eResource() != null) { |
137 |
myGenModel.eResource().unload(); |
138 |
} |
139 |
myGenModel = null; |
140 |
} |
141 |
|
142 |
private IStatus validateGenModel() { |
143 |
return BasicDiagnostic.toIStatus(Diagnostician.INSTANCE.validate(getGenModel())); |
144 |
} |
145 |
|
146 |
private Shell getShell() { |
147 |
return myPart.getSite().getShell(); |
148 |
} |
149 |
|
150 |
private static IPreferenceStore getPreferences() { |
151 |
return CodegenRcpUiPlugin.getDefault().getPreferenceStore(); |
152 |
} |
153 |
} |