mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-26 02:15:31 +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:
parent
48bd2adbd6
commit
96f4db4667
4 changed files with 75 additions and 14 deletions
|
@ -2,7 +2,7 @@ Manifest-Version: 1.0
|
||||||
Bundle-ManifestVersion: 2
|
Bundle-ManifestVersion: 2
|
||||||
Bundle-Name: Templates Core
|
Bundle-Name: Templates Core
|
||||||
Bundle-SymbolicName: org.eclipse.tools.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-Activator: org.eclipse.tools.templates.core.internal.Activator
|
||||||
Bundle-Vendor: Eclipse CDT
|
Bundle-Vendor: Eclipse CDT
|
||||||
Require-Bundle: org.eclipse.core.runtime,
|
Require-Bundle: org.eclipse.core.runtime,
|
||||||
|
|
|
@ -13,10 +13,43 @@ import org.eclipse.core.resources.IFile;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IProgressMonitor;
|
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 {
|
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];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ Manifest-Version: 1.0
|
||||||
Bundle-ManifestVersion: 2
|
Bundle-ManifestVersion: 2
|
||||||
Bundle-Name: Ui
|
Bundle-Name: Ui
|
||||||
Bundle-SymbolicName: org.eclipse.tools.templates.ui;singleton:=true
|
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
|
Bundle-Activator: org.eclipse.tools.templates.ui.internal.Activator
|
||||||
Require-Bundle: org.eclipse.ui,
|
Require-Bundle: org.eclipse.ui,
|
||||||
org.eclipse.core.runtime,
|
org.eclipse.core.runtime,
|
||||||
|
|
|
@ -24,17 +24,52 @@ import org.eclipse.ui.actions.WorkspaceModifyOperation;
|
||||||
import org.eclipse.ui.ide.IDE;
|
import org.eclipse.ui.ide.IDE;
|
||||||
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
|
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 {
|
public abstract class TemplateWizard extends BasicNewResourceWizard {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The generator to be called when the wizard is finished.
|
||||||
|
*
|
||||||
|
* @return generator
|
||||||
|
*/
|
||||||
protected abstract IGenerator getGenerator();
|
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) {
|
protected void populateModel(Map<String, Object> model) {
|
||||||
// nothing by default
|
// 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
|
@Override
|
||||||
public boolean performFinish() {
|
public boolean performFinish() {
|
||||||
IGenerator generator = getGenerator();
|
IGenerator generator = getGenerator();
|
||||||
|
// TODO remove the model in 2.0. The getGenerator method should have
|
||||||
|
// initialized this.
|
||||||
Map<String, Object> model = new HashMap<>();
|
Map<String, Object> model = new HashMap<>();
|
||||||
populateModel(model);
|
populateModel(model);
|
||||||
|
|
||||||
|
@ -45,20 +80,13 @@ public abstract class TemplateWizard extends BasicNewResourceWizard {
|
||||||
throws CoreException, InvocationTargetException, InterruptedException {
|
throws CoreException, InvocationTargetException, InterruptedException {
|
||||||
monitor.beginTask("Generating project", 1); //$NON-NLS-1$
|
monitor.beginTask("Generating project", 1); //$NON-NLS-1$
|
||||||
generator.generate(model, monitor);
|
generator.generate(model, monitor);
|
||||||
monitor.done();
|
|
||||||
getWorkbench().getDisplay().asyncExec(new Runnable() {
|
getWorkbench().getDisplay().asyncExec(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
postProcess(generator);
|
||||||
IWorkbenchPage activePage = getWorkbench().getActiveWorkbenchWindow().getActivePage();
|
|
||||||
for (IFile file : generator.getFilesToOpen()) {
|
|
||||||
IDE.openEditor(activePage, file);
|
|
||||||
}
|
|
||||||
} catch (PartInitException e) {
|
|
||||||
Activator.log(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
monitor.done();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Add table
Reference in a new issue