use of org.eclipse.jface.text.templates.Template in project webtools.sourceediting by eclipse.
the class NewXMLTemplatesWizardPage method getTemplateString.
/**
* Returns template string to insert.
*
* @return String to insert or null if none is to be inserted
*/
String getTemplateString() {
String templateString = null;
Template template = getSelectedTemplate();
if (template != null) {
TemplateContextType contextType = XMLUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsXML.NEW);
IDocument document = new Document();
TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0);
try {
TemplateBuffer buffer = context.evaluate(template);
templateString = buffer.getString();
} catch (Exception e) {
// $NON-NLS-1$
Logger.log(Logger.WARNING_DEBUG, "Could not create template for new xml", e);
}
}
return templateString;
}
use of org.eclipse.jface.text.templates.Template in project webtools.sourceediting by eclipse.
the class NewXMLTemplatesWizardPage method createControl.
public void createControl(Composite ancestor) {
Composite parent = new Composite(ancestor, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
parent.setLayout(layout);
// create checkbox for user to use XML Template
fUseTemplateButton = new Button(parent, SWT.CHECK);
fUseTemplateButton.setText(XMLWizardsMessages.NewXMLTemplatesWizardPage_4);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
fUseTemplateButton.setLayoutData(data);
fUseTemplateButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
enableTemplates();
}
});
// create composite for Templates table
Composite innerParent = new Composite(parent, SWT.NONE);
GridLayout innerLayout = new GridLayout();
innerLayout.numColumns = 2;
innerLayout.marginHeight = 0;
innerLayout.marginWidth = 0;
innerParent.setLayout(innerLayout);
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
innerParent.setLayoutData(gd);
Label label = new Label(innerParent, SWT.NONE);
label.setText(XMLWizardsMessages.NewXMLTemplatesWizardPage_7);
data = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
label.setLayoutData(data);
// create table that displays templates
Table table = new Table(innerParent, SWT.BORDER | SWT.FULL_SELECTION);
data = new GridData(GridData.FILL_BOTH);
data.widthHint = convertWidthInCharsToPixels(2);
data.heightHint = convertHeightInCharsToPixels(10);
data.horizontalSpan = 2;
table.setLayoutData(data);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableLayout tableLayout = new TableLayout();
table.setLayout(tableLayout);
TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setText(XMLWizardsMessages.NewXMLTemplatesWizardPage_2);
TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setText(XMLWizardsMessages.NewXMLTemplatesWizardPage_3);
fTableViewer = new TableViewer(table);
fTableViewer.setLabelProvider(new TemplateLabelProvider());
fTableViewer.setContentProvider(new TemplateContentProvider());
fTableViewer.setSorter(new ViewerSorter() {
public int compare(Viewer viewer, Object object1, Object object2) {
if ((object1 instanceof Template) && (object2 instanceof Template)) {
Template left = (Template) object1;
Template right = (Template) object2;
int result = left.getName().compareToIgnoreCase(right.getName());
if (result != 0) {
return result;
}
return left.getDescription().compareToIgnoreCase(right.getDescription());
}
return super.compare(viewer, object1, object2);
}
public boolean isSorterProperty(Object element, String property) {
return true;
}
});
fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent e) {
updateViewerInput();
}
});
// create viewer that displays currently selected template's contents
fPatternViewer = doCreateViewer(parent);
fTemplateStore = XMLUIPlugin.getDefault().getTemplateStore();
fTableViewer.setInput(fTemplateStore);
// Create linked text to just to templates preference page
Link link = new Link(parent, SWT.NONE);
link.setText(XMLWizardsMessages.NewXMLTemplatesWizardPage_6);
data = new GridData(SWT.END, SWT.FILL, true, false, 2, 1);
link.setLayoutData(data);
link.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
linkClicked();
}
});
configureTableResizing(innerParent, table, column1, column2);
loadLastSavedPreferences();
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IHelpContextIds.XML_NEWWIZARD_TEMPLATE_HELPID);
Dialog.applyDialogFont(parent);
setControl(parent);
}
use of org.eclipse.jface.text.templates.Template in project webtools.sourceediting by eclipse.
the class XPathTemplateCompletionProcessor method computeCompletionProposals.
/*
* Copied from super class except instead of calling createContext(viewer,
* region) call createContext(viewer, region, offset) instead
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// adjust offset to end of normalized selection
if (selection.getOffset() == offset) {
offset = selection.getOffset() + selection.getLength();
}
String prefix = extractPrefix(viewer, offset);
Region region = new Region(offset - prefix.length(), prefix.length());
TemplateContext context = createContext(viewer, region, offset);
if (context == null) {
return new ICompletionProposal[0];
}
// name of the selection variables {line, word}_selection
// $NON-NLS-1$
context.setVariable("selection", selection.getText());
Template[] templates = getTemplates(context.getContextType().getId());
List matches = new ArrayList();
for (int i = 0; i < templates.length; i++) {
Template template = templates[i];
try {
context.getContextType().validate(template.getPattern());
} catch (TemplateException e) {
continue;
}
if (template.matches(prefix, context.getContextType().getId())) {
matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
}
}
Collections.sort(matches, fgProposalComparator);
return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
use of org.eclipse.jface.text.templates.Template in project dbeaver by serge-rider.
the class SQLSymbolInserter method verifyKey.
@Override
public void verifyKey(VerifyEvent event) {
if (!event.doit) {
return;
}
if (editor.isBlockSelectionModeEnabled()) {
return;
}
IDocument document = sourceViewer.getDocument();
final Point selection = sourceViewer.getSelectedRange();
final int offset = selection.x;
final int length = selection.y;
switch(event.character) {
case '(':
case '[':
if (!closeBrackets) {
return;
}
try {
if (offset < document.getLength() && !Character.isWhitespace(document.getChar(offset + length))) {
return;
}
} catch (BadLocationException e) {
log.debug(e);
return;
}
if (hasCharacterToTheRight(document, offset + length, event.character)) {
return;
}
case '\'':
if (event.character == '\'') {
if (!closeSingleQuotes) {
return;
}
if (hasIdentifierToTheLeft(document, offset) || hasIdentifierToTheRight(document, offset + length)) {
return;
}
}
case '"':
if (event.character == '"') {
if (!closeDoubleQuotes) {
return;
}
if (hasIdentifierToTheLeft(document, offset) || hasIdentifierToTheRight(document, offset + length)) {
return;
}
}
try {
ITypedRegion partition = TextUtilities.getPartition(document, SQLParserPartitions.SQL_PARTITIONING, offset, true);
if (!IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType()) && partition.getOffset() != offset) {
return;
}
if (!editor.validateEditorInputState()) {
return;
}
final char character = event.character;
final char closingCharacter = getPeerCharacter(character);
document.replace(offset, length, String.valueOf(character) + getSelection() + closingCharacter);
SymbolLevel level = new SymbolLevel();
bracketLevelStack.add(level);
LinkedPositionGroup group = new LinkedPositionGroup();
group.addPosition(new LinkedPosition(document, offset + 1, 0, LinkedPositionGroup.NO_STOP));
LinkedModeModel model = new LinkedModeModel();
model.addLinkingListener(this);
model.addGroup(group);
model.forceInstall();
level.offset = offset;
level.length = 2;
// set up position tracking for our magic peers
if (bracketLevelStack.size() == 1) {
document.addPositionCategory(CATEGORY);
document.addPositionUpdater(positionUpdater);
}
level.firstPosition = new Position(offset, 1);
level.secondPosition = new Position(offset + 1, 1);
document.addPosition(CATEGORY, level.firstPosition);
document.addPosition(CATEGORY, level.secondPosition);
level.uI = new EditorLinkedModeUI(model, sourceViewer);
level.uI.setSimpleMode(true);
level.uI.setExitPolicy(new ExitPolicy(closingCharacter, getEscapeCharacter(closingCharacter), bracketLevelStack));
level.uI.setExitPosition(sourceViewer, offset + 2, 0, Integer.MAX_VALUE);
level.uI.setCyclingMode(LinkedModeUI.CYCLE_NEVER);
level.uI.enter();
IRegion newSelection = level.uI.getSelectedRegion();
sourceViewer.setSelectedRange(newSelection.getOffset(), newSelection.getLength());
event.doit = false;
} catch (BadLocationException | BadPositionCategoryException e) {
log.debug(e);
}
break;
case SWT.TAB:
{
try {
int curOffset = offset;
// }
while (curOffset > 0) {
if (!Character.isJavaIdentifierPart(document.getChar(curOffset - 1))) {
break;
}
curOffset--;
}
if (curOffset != offset) {
String templateName = document.get(curOffset, offset - curOffset);
SQLTemplatesPage templatesPage = editor.getTemplatesPage();
Template template = templatesPage.getTemplateStore().findTemplate(templateName);
if (template != null && template.isAutoInsertable()) {
sourceViewer.setSelectedRange(curOffset, offset - curOffset);
templatesPage.insertTemplate(template, document);
event.doit = false;
}
}
} catch (BadLocationException e) {
log.debug(e);
}
break;
}
}
}
use of org.eclipse.jface.text.templates.Template in project flux by eclipse.
the class StubUtility method getMethodBodyContent.
/*
* Don't use this method directly, use CodeGeneration.
*/
public static String getMethodBodyContent(boolean isConstructor, IJavaProject project, String destTypeName, String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
String templateName = isConstructor ? CodeTemplateContextType.CONSTRUCTORSTUB_ID : CodeTemplateContextType.METHODSTUB_ID;
Template template = getCodeTemplate(templateName, project);
if (template == null) {
return bodyStatement;
}
CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
context.setVariable(CodeTemplateContextType.BODY_STATEMENT, bodyStatement);
String str = evaluateTemplate(context, template, new String[] { CodeTemplateContextType.BODY_STATEMENT });
if (str == null && !Strings.containsOnlyWhitespaces(bodyStatement)) {
return bodyStatement;
}
return str;
}
Aggregations