/***********************************************************************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
* available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors: IBM Corporation - initial API and implementation
**********************************************************************************************************************************/
package net.sourceforge.phpeclipse.wiki.editor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaSection;
import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaText;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.jface.text.templates.DocumentTemplateContext;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.eclipse.jface.text.templates.TemplateException;
import org.eclipse.jface.text.templates.TemplateProposal;
import org.eclipse.swt.graphics.Image;
public class WikiCompletionProcessor implements IContentAssistProcessor {
private static final String TEMPLATE_ICON = "icons/template.gif";
// private static final String PREPARATION_TEMPLATE_CTX = "net.sourceforge.phpeclipse.wiki.editor.preparation";
private static final String INGREDIENTS_TEMPLATE_CTX = "net.sourceforge.phpeclipse.wiki.editor.templates";
private static final class ProposalComparator implements Comparator {
public int compare(Object o1, Object o2) {
return ((TemplateProposal) o2).getRelevance() - ((TemplateProposal) o1).getRelevance();
}
}
private static final Comparator fgProposalComparator = new ProposalComparator();
private final WikiEditor fEditor;
public WikiCompletionProcessor(WikiEditor editor) {
fEditor = editor;
}
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
WikipediaSection section = fEditor.getSection();
if (section == null)
return null;
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// adjust offset to start of normalized selection:
if (selection.getOffset() != offset)
offset = selection.getOffset();
String prefix = getPrefix(viewer, offset);
Region region = new Region(offset - prefix.length(), prefix.length() + selection.getLength());
ICompletionProposal[] templateProposals = computeTemplateProposals(viewer, region, section, prefix);
// ICompletionProposal[] ingredientProposals = computeIngredientsProposals(viewer, region, recipe, prefix);
// ICompletionProposal[] titleProposals = computeTitleProposals(viewer, region, recipe, prefix);
List result = new ArrayList();
// result.addAll(Arrays.asList(ingredientProposals));
result.addAll(Arrays.asList(templateProposals));
// result.addAll(Arrays.asList(titleProposals));
return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]);
}
private ICompletionProposal[] computeTitleProposals(ITextViewer viewer, IRegion region, WikipediaText recipe, String prefix) {
List props = new ArrayList(2);
// if (recipe.getIngredientsSection() == null)
// props.add(createTitleProposal("Zutaten:", region, viewer));
// if (recipe.getPreparationSection() == null)
// props.add(createTitleProposal("Zubereitung:", region, viewer));
return (ICompletionProposal[]) props.toArray(new ICompletionProposal[props.size()]);
}
private CompletionProposal createTitleProposal(String name, IRegion region, ITextViewer viewer) {
String lineDelimiter = TextUtilities.getDefaultLineDelimiter(viewer.getDocument());
return new CompletionProposal(name + lineDelimiter, region.getOffset(), region.getLength(), region.getOffset() + name.length()
+ lineDelimiter.length(), null, name, null, null);
}
private ICompletionProposal[] computeIngredientsProposals(ITextViewer viewer, IRegion region, WikipediaText recipe, String prefix) {
return new ICompletionProposal[0];
// Step[] steps= recipe.getSteps();
// if (steps == null || steps.length == 0)
// return new ICompletionProposal[0];
//
// int offset= region.getOffset();
// Step first= steps[0];
// if (offset < first.getOffset())
// return new ICompletionProposal[0];
//
// Ingredient[] ingredients= recipe.getIngredients();
// if (ingredients == null)
// return new ICompletionProposal[0];
//
// prefix= prefix.toLowerCase();
//
// ArrayList proposals= new ArrayList();
// for (int i= 0; i < ingredients.length; i++) {
// String ingredient= ingredients[i].getName();
// if (ingredient.toLowerCase().startsWith(prefix))
// proposals.add(new CompletionProposal(ingredient, offset, region.getLength(), ingredient.length()));
// }
// return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
}
private TemplateContextType getContextType(WikipediaSection section, int offset) {
// if (recipe.getPreparationSection() != null && recipe.getPreparationSection().getOffset() < offset)
// return WikiEditorPlugin.getDefault().getContextTypeRegistry().getContextType(PREPARATION_TEMPLATE_CTX);
// else
return WikiEditorPlugin.getDefault().getContextTypeRegistry().getContextType(INGREDIENTS_TEMPLATE_CTX);
}
/**
* Creates a concrete template context for the given region in the document. This involves finding out which context fType is valid
* at the given location, and then creating a context of this fType. The default implementation returns a
* DocumentTemplateContext
for the context fType at the given location.
*
* @param viewer
* the viewer for which the context is created
* @param region
* the region into document
for which the context is created
* @return a template context that can handle template insertion at the given location, or null
*/
private TemplateContext createContext(ITextViewer viewer, IRegion region, WikipediaSection recipe) {
TemplateContextType contextType = getContextType(recipe, region.getOffset());
if (contextType != null) {
IDocument document = viewer.getDocument();
return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength());
}
return null;
}
private ICompletionProposal[] computeTemplateProposals(ITextViewer viewer, IRegion region, WikipediaSection recipe, String prefix) {
TemplateContext context = createContext(viewer, region, recipe);
if (context == null)
return new ICompletionProposal[0];
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
context.setVariable("selection", selection.getText()); // name of the selection variables {line, word}_selection
String id = context.getContextType().getId();
Template[] templates = WikiEditorPlugin.getDefault().getTemplateStore().getTemplates(id);
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;
}
int relevance = getRelevance(template, prefix);
if (relevance > 0) {
matches.add(new TemplateProposal(template, context, region, getImage(template), relevance));
}
}
Collections.sort(matches, fgProposalComparator);
return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
/**
* Returns the relevance of a template given a prefix. The default implementation returns a number greater than zero if the
* template name starts with the prefix, and zero otherwise.
*
* @param template
* the template to compute the relevance for
* @param prefix
* the prefix after which content assist was requested
* @return the relevance of template
* @see #getPrefix(ITextViewer, int)
*/
private int getRelevance(Template template, String prefix) {
if (template.getName().startsWith(prefix))
return 90;
return 0;
}
private String getPrefix(ITextViewer viewer, int offset) {
int i = offset;
IDocument document = viewer.getDocument();
if (i > document.getLength())
return "";
try {
while (i > 0) {
char ch = document.getChar(i - 1);
if (!Character.isLetterOrDigit(ch))
break;
i--;
}
return document.get(i, offset - i);
} catch (BadLocationException e) {
return "";
}
}
/**
* Always return the default image.
*/
private Image getImage(Template template) {
ImageRegistry registry = WikiEditorPlugin.getDefault().getImageRegistry();
Image image = registry.get(TEMPLATE_ICON);
if (image == null) {
ImageDescriptor desc = WikiEditorPlugin.imageDescriptorFromPlugin("net.sourceforge.phpeclipse.wiki.editor", TEMPLATE_ICON);
registry.put(TEMPLATE_ICON, desc);
image = registry.get(TEMPLATE_ICON);
}
return image;
}
public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
return null;
}
public char[] getCompletionProposalAutoActivationCharacters() {
return null;
}
public char[] getContextInformationAutoActivationCharacters() {
return null;
}
public String getErrorMessage() {
return null;
}
public IContextInformationValidator getContextInformationValidator() {
return null;
}
}