use of org.bndtools.templating.Template in project bndtools by bndtools.
the class IconLoaderJob method run.
@Override
protected IStatus run(IProgressMonitor monitor) {
SubMonitor progress = SubMonitor.convert(monitor, templates.size());
Map<Template, byte[]> batch = new IdentityHashMap<>();
for (Template template : templates) {
InputStream iconStream = null;
try {
URI iconUri = template.getIcon();
if (iconUri != null) {
iconStream = iconUri.toURL().openStream();
byte[] bytes = IO.read(iconStream);
batch.put(template, bytes);
if (batch.size() >= batchLimit) {
processBatch(batch);
batch = new IdentityHashMap<>();
}
}
} catch (Exception e) {
log.log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error reading icon for template '" + template.getName() + "'", e));
} finally {
IO.close(iconStream);
}
progress.worked(1);
}
processBatch(batch);
return Status.OK_STATUS;
}
use of org.bndtools.templating.Template in project bndtools by bndtools.
the class TemplateSelectionWizardPage method createControl.
@Override
public void createControl(Composite parent) {
// $NON-NLS-1$
setImageDescriptor(Plugin.imageDescriptorFromPlugin("icons/bndtools-wizban.png"));
GridData gd;
Composite composite = new Composite(parent, SWT.NULL);
setControl(composite);
composite.setLayout(new GridLayout(1, false));
Control headerControl = createHeaderControl(composite);
if (headerControl != null)
headerControl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
new Label(composite, SWT.NONE).setText("Select Template:");
tree = new Tree(composite, SWT.BORDER | SWT.FULL_SELECTION);
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.heightHint = 150;
tree.setLayoutData(gd);
defaultTemplateImage = AbstractUIPlugin.imageDescriptorFromPlugin(Plugin.PLUGIN_ID, "icons/template.gif").createImage(parent.getDisplay());
viewer = new TreeViewer(tree);
contentProvider = new RepoTemplateContentProvider(false);
viewer.setContentProvider(contentProvider);
viewer.setLabelProvider(new RepoTemplateLabelProvider(loadedImages, defaultTemplateImage));
viewer.addFilter(latestFilter);
setTemplates(emptyTemplate != null ? Collections.singletonList(emptyTemplate) : Collections.<Template>emptyList());
btnLatestOnly = new Button(composite, SWT.CHECK);
btnLatestOnly.setText("Show latest versions only");
btnLatestOnly.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
btnLatestOnly.setSelection(true);
new Label(composite, SWT.NONE).setText("Description:");
Composite cmpDescription = new Composite(composite, SWT.BORDER);
cmpDescription.setBackground(tree.getBackground());
txtDescription = new ScrolledFormText(cmpDescription, SWT.V_SCROLL | SWT.H_SCROLL, false);
FormText formText = new FormText(txtDescription, SWT.NO_FOCUS);
txtDescription.setFormText(formText);
txtDescription.setBackground(tree.getBackground());
formText.setBackground(tree.getBackground());
formText.setForeground(tree.getForeground());
formText.setFont("fixed", JFaceResources.getTextFont());
formText.setFont("italic", JFaceResources.getFontRegistry().getItalic(""));
GridData gd_cmpDescription = new GridData(SWT.FILL, SWT.FILL, true, true);
gd_cmpDescription.heightHint = 25;
cmpDescription.setLayoutData(gd_cmpDescription);
GridLayout layout_cmpDescription = new GridLayout(1, false);
cmpDescription.setLayout(layout_cmpDescription);
GridData gd_txtDescription = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
gd_txtDescription.heightHint = 25;
txtDescription.setLayoutData(gd_txtDescription);
Hyperlink linkRetina = new Hyperlink(composite, SWT.NONE);
linkRetina.setText("Why is this text blurred?");
linkRetina.setUnderlined(true);
linkRetina.setForeground(JFaceColors.getHyperlinkText(getShell().getDisplay()));
linkRetina.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
Object element = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
setTemplate(element instanceof Template ? (Template) element : null);
getContainer().updateButtons();
}
});
viewer.addOpenListener(new IOpenListener() {
@Override
public void open(OpenEvent event) {
Object element = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
setTemplate(element instanceof Template ? (Template) element : null);
getContainer().updateButtons();
IWizardPage nextPage = getNextPage();
if (nextPage != null && selected != null)
getContainer().showPage(nextPage);
}
});
btnLatestOnly.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
boolean latestOnly = btnLatestOnly.getSelection();
if (latestOnly)
viewer.addFilter(latestFilter);
else
viewer.removeFilter(latestFilter);
}
});
linkRetina.addHyperlinkListener(new HyperlinkAdapter() {
@Override
public void linkActivated(HyperlinkEvent ev) {
try {
IWorkbenchBrowserSupport browser = PlatformUI.getWorkbench().getBrowserSupport();
browser.getExternalBrowser().openURL(new URL("https://github.com/bndtools/bndtools/wiki/Blurry-Form-Text-on-High-Resolution-Displays"));
} catch (Exception e) {
log.log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Browser open error", e));
}
}
});
txtDescription.getFormText().addHyperlinkListener(new HyperlinkAdapter() {
@Override
public void linkActivated(HyperlinkEvent ev) {
try {
PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL((String) ev.getHref()));
} catch (Exception ex) {
log.log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Browser open error", ex));
}
}
});
}
use of org.bndtools.templating.Template in project bndtools by bndtools.
the class TemplateSelectionWizardPage method setTemplates.
private void setTemplates(final Collection<Template> templates) {
viewer.setInput(templates);
viewer.expandAll();
Template templateToSelect = null;
if (viewer.getFilters().length == 0) {
templateToSelect = contentProvider.getFirstTemplate();
} else {
for (Object element : contentProvider.getElements(null)) {
if (element instanceof Category) {
Object[] filteredTemplates = latestFilter.filter(viewer, element, contentProvider.getChildren(element));
if (filteredTemplates.length > 0) {
templateToSelect = (Template) filteredTemplates[0];
break;
}
} else {
templateToSelect = (Template) element;
break;
}
}
}
if (templateToSelect == null) {
return;
}
viewer.setSelection(new StructuredSelection(templateToSelect));
}
use of org.bndtools.templating.Template in project bndtools by bndtools.
the class ReposTemplateLoaderTest method testProcessTemplate.
public void testProcessTemplate() throws Exception {
List<Template> templates = loader.findTemplates("test1", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
assertEquals(1, templates.size());
Template template = templates.get(0);
Map<String, List<Object>> parameters = new HashMap<>();
parameters.put("projectName", Collections.<Object>singletonList("org.example.foo"));
parameters.put("srcDir", Collections.<Object>singletonList("src/main/java"));
parameters.put("basePackageDir", Collections.<Object>singletonList("org/example/foo"));
ResourceMap outputs = template.generateOutputs(parameters);
assertEquals(5, outputs.size());
Entry<String, Resource> entry;
Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
entry = iter.next();
assertEquals("src/main/java/org/example/foo/Activator.java", entry.getKey());
assertEquals("package org.example.foo; public class Activator {}", IO.collect(entry.getValue().getContent()));
entry = iter.next();
assertEquals("pic.jpg", entry.getKey());
// Check the digest of the pic to ensure it didn't get damaged by the templating engine
DigestInputStream digestStream = new DigestInputStream(entry.getValue().getContent(), MessageDigest.getInstance("SHA-256"));
IO.drain(digestStream);
byte[] digest = digestStream.getMessageDigest().digest();
assertEquals("ea5d770bc2deddb1f9a20df3ad337bdc1490ba7b35fa41c33aa4e9a534e82ada", Hex.toHexString(digest).toLowerCase());
entry = iter.next();
assertEquals("src/main/java/", entry.getKey());
entry = iter.next();
assertEquals("src/main/java/org/example/foo/", entry.getKey());
entry = iter.next();
assertEquals("bnd.bnd", entry.getKey());
assertEquals("Bundle-SymbolicName: org.example.foo", IO.collect(entry.getValue().getContent()));
}
use of org.bndtools.templating.Template in project bndtools by bndtools.
the class ReposTemplateLoaderTest method testAlternateDelimiters.
public void testAlternateDelimiters() throws Exception {
List<Template> templates = loader.findTemplates("test2", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
assertEquals(1, templates.size());
Template template = templates.get(0);
Map<String, List<Object>> parameters = new HashMap<>();
parameters.put("projectName", Collections.<Object>singletonList("org.example.foo"));
parameters.put("srcDir", Collections.<Object>singletonList("src/main/java"));
parameters.put("basePackageDir", Collections.<Object>singletonList("org/example/foo"));
ResourceMap outputs = template.generateOutputs(parameters);
assertEquals(5, outputs.size());
Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
Entry<String, Resource> entry;
entry = iter.next();
assertEquals("src/main/java/org/example/foo/Activator.java", entry.getKey());
assertEquals("package org.example.foo; public class Activator {}", IO.collect(entry.getValue().getContent()));
entry = iter.next();
assertEquals("pic.jpg", entry.getKey());
// Check the digest of the pic to ensure it didn't get damaged by the templating engine
DigestInputStream digestStream = new DigestInputStream(entry.getValue().getContent(), MessageDigest.getInstance("SHA-256"));
IO.drain(digestStream);
byte[] digest = digestStream.getMessageDigest().digest();
assertEquals("ea5d770bc2deddb1f9a20df3ad337bdc1490ba7b35fa41c33aa4e9a534e82ada", Hex.toHexString(digest).toLowerCase());
entry = iter.next();
assertEquals("src/main/java/", entry.getKey());
entry = iter.next();
assertEquals("src/main/java/org/example/foo/", entry.getKey());
entry = iter.next();
assertEquals("bnd.bnd", entry.getKey());
assertEquals("Bundle-SymbolicName: org.example.foo", IO.collect(entry.getValue().getContent()));
}
Aggregations