1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 14:15:23 +02:00

Deprecate model from IGenerator API.

Not all generators need a map to store information used at generate
time. In fact, this probably should be discouraged. We only had it
because Freemarker uses one. But I just wrote a generator that doesn't
use Freemaker and the model map got in the way.

Also adds a post process API so we can do other things in the Wizard
after the generation is done. Especially useful for UI things like
adding generated stuff to Working Sets.

Change-Id: Icd553fd8f6087bd342fca4aec88fb2a5c2d5fa4a
This commit is contained in:
Doug Schaefer 2016-07-07 10:32:18 -04:00
parent 48bd2adbd6
commit 96f4db4667
4 changed files with 75 additions and 14 deletions

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Templates Core
Bundle-SymbolicName: org.eclipse.tools.templates.core
Bundle-Version: 1.0.0.qualifier
Bundle-Version: 1.1.0.qualifier
Bundle-Activator: org.eclipse.tools.templates.core.internal.Activator
Bundle-Vendor: Eclipse CDT
Require-Bundle: org.eclipse.core.runtime,

View file

@ -13,10 +13,43 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
/**
* Interface used by the Template Wizard to call on the generator to generate. Also provides other
* utility methods as necessary, but that should be limited.
*/
public interface IGenerator {
void generate(Map<String, Object> model, IProgressMonitor monitor) throws CoreException;
/**
* Generate.
*
* @param model
* @param monitor
* @throws CoreException
* @deprecated The generator should manage it's own model.
*/
@Deprecated
default void generate(Map<String, Object> model, IProgressMonitor monitor) throws CoreException {
if (model.isEmpty()) {
generate(monitor);
}
}
IFile[] getFilesToOpen();
/**
* Generate.
*
* @param monitor
* @throws CoreException
*/
default void generate(IProgressMonitor monitor) throws CoreException {
};
/**
* Return which files should be opened in the workbench when the generation is complete.
*
* @return files to open
*/
default IFile[] getFilesToOpen() {
return new IFile[0];
}
}

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Ui
Bundle-SymbolicName: org.eclipse.tools.templates.ui;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Version: 1.1.0.qualifier
Bundle-Activator: org.eclipse.tools.templates.ui.internal.Activator
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,

View file

@ -24,17 +24,52 @@ import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
/**
* The wizard component of a template. Takes over when the template is selected in the from the
* Template Selection Page in the parent wizard.
*/
public abstract class TemplateWizard extends BasicNewResourceWizard {
/**
* The generator to be called when the wizard is finished.
*
* @return generator
*/
protected abstract IGenerator getGenerator();
/**
* Populate the model.
*
* @param model
* @deprecated The subclass should initialize the generator with information in the
* getGenerator() method.
*/
@Deprecated
protected void populateModel(Map<String, Object> model) {
// nothing by default
}
/**
* Perform additional UI actions after the generation is complete.
*
* @param generator
*/
protected void postProcess(IGenerator generator) {
try {
IWorkbenchPage activePage = getWorkbench().getActiveWorkbenchWindow().getActivePage();
for (IFile file : generator.getFilesToOpen()) {
IDE.openEditor(activePage, file);
}
} catch (PartInitException e) {
Activator.log(e);
}
}
@Override
public boolean performFinish() {
IGenerator generator = getGenerator();
// TODO remove the model in 2.0. The getGenerator method should have
// initialized this.
Map<String, Object> model = new HashMap<>();
populateModel(model);
@ -45,20 +80,13 @@ public abstract class TemplateWizard extends BasicNewResourceWizard {
throws CoreException, InvocationTargetException, InterruptedException {
monitor.beginTask("Generating project", 1); //$NON-NLS-1$
generator.generate(model, monitor);
monitor.done();
getWorkbench().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
try {
IWorkbenchPage activePage = getWorkbench().getActiveWorkbenchWindow().getActivePage();
for (IFile file : generator.getFilesToOpen()) {
IDE.openEditor(activePage, file);
}
} catch (PartInitException e) {
Activator.log(e);
}
postProcess(generator);
}
});
monitor.done();
}
@Override