| /******************************************************************************* |
| * Copyright (c) 2007, 2008 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.pde.api.tools.ui.internal.completion; |
| |
| import org.eclipse.jdt.core.CompletionContext; |
| import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; |
| import org.eclipse.jface.text.BadLocationException; |
| import org.eclipse.jface.text.DocumentEvent; |
| import org.eclipse.jface.text.IDocument; |
| import org.eclipse.jface.text.IInformationControlCreator; |
| import org.eclipse.jface.text.ITextViewer; |
| import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2; |
| import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3; |
| import org.eclipse.jface.text.contentassist.IContextInformation; |
| import org.eclipse.pde.api.tools.ui.internal.ApiUIPlugin; |
| import org.eclipse.swt.graphics.Image; |
| import org.eclipse.swt.graphics.Point; |
| |
| /** |
| * This class provides a completion proposal for javadoc type extensions, more specifically |
| * it is used to contribute the following tags: |
| * |
| * @since 1.0.0 |
| */ |
| public class APIToolsJavadocCompletionProposal implements IJavaCompletionProposal, ICompletionProposalExtension2, ICompletionProposalExtension3 { |
| |
| private String fReplaceText = null; |
| private String fDisplayText = null; |
| private Image fImage = null; |
| private CompletionContext fContext = null; |
| |
| /** |
| * Constructor |
| * @param replacetext |
| * @param displaytext |
| * @param image |
| */ |
| public APIToolsJavadocCompletionProposal(CompletionContext context, String replacetext, String displaytext, Image image) { |
| fContext = context; |
| fReplaceText = replacetext; |
| fImage = image; |
| fDisplayText = displaytext; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance() |
| */ |
| public int getRelevance() { |
| return 0; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument) |
| */ |
| public void apply(IDocument document) {} |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo() |
| */ |
| public String getAdditionalProposalInfo() { |
| int index = fReplaceText.indexOf(fDisplayText); |
| if(index > -1) { |
| return fReplaceText.substring(index + fDisplayText.length()); |
| } |
| return null; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation() |
| */ |
| public IContextInformation getContextInformation() { |
| return null; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString() |
| */ |
| public String getDisplayString() { |
| return fDisplayText; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage() |
| */ |
| public Image getImage() { |
| return fImage; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument) |
| */ |
| public Point getSelection(IDocument document) { |
| return null; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int) |
| */ |
| public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { |
| try { |
| IDocument doc = viewer.getDocument(); |
| int start = fContext.getTokenStart(); |
| doc.replace(start, offset-start, fReplaceText); |
| } catch (BadLocationException e) { |
| ApiUIPlugin.log(e); |
| } |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean) |
| */ |
| public void selected(ITextViewer viewer, boolean smartToggle) {} |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer) |
| */ |
| public void unselected(ITextViewer viewer) {} |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent) |
| */ |
| public boolean validate(IDocument document, int offset, DocumentEvent event) { |
| try { |
| int start = fContext.getTokenStart(); |
| int length = offset-start; |
| String prefix = document.get(start, length); |
| if(length <= fDisplayText.length()) { |
| if(prefix.equals(fDisplayText.substring(0, length))) { |
| return true; |
| } |
| } |
| } |
| catch (BadLocationException e) {} |
| return false; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getInformationControlCreator() |
| */ |
| public IInformationControlCreator getInformationControlCreator() { |
| return null; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getPrefixCompletionStart(org.eclipse.jface.text.IDocument, int) |
| */ |
| public int getPrefixCompletionStart(IDocument document, int completionOffset) { |
| return fContext.getTokenStart(); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getPrefixCompletionText(org.eclipse.jface.text.IDocument, int) |
| */ |
| public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) { |
| if(validate(document, completionOffset, null)) { |
| return fReplaceText; |
| } |
| return null; |
| } |
| } |