001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.contentassist;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
015: import org.eclipse.jface.text.BadLocationException;
016: import org.eclipse.jface.text.IDocument;
017: import org.eclipse.jface.text.IInformationControl;
018: import org.eclipse.jface.text.IInformationControlCreator;
019: import org.eclipse.jface.text.contentassist.ICompletionProposal;
020: import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3;
021: import org.eclipse.jface.text.contentassist.ICompletionProposalExtension5;
022: import org.eclipse.jface.text.contentassist.IContextInformation;
023: import org.eclipse.pde.internal.ui.editor.contentassist.display.BrowserInformationControl;
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.graphics.Image;
026: import org.eclipse.swt.graphics.Point;
027: import org.eclipse.swt.widgets.Shell;
028: import org.eclipse.ui.editors.text.EditorsUI;
029:
030: public class TypeCompletionProposal implements ICompletionProposal,
031: ICompletionProposalExtension3, ICompletionProposalExtension5 {
032:
033: protected String fReplacementString;
034: protected Image fImage;
035: protected String fDisplayString;
036: protected int fBeginInsertPoint;
037: protected int fLength;
038: protected String fAdditionalInfo;
039: private IInformationControlCreator fCreator;
040:
041: public TypeCompletionProposal(String replacementString,
042: Image image, String displayString) {
043: this (replacementString, image, displayString, 0, 0);
044: }
045:
046: public TypeCompletionProposal(String replacementString,
047: Image image, String displayString, int startOffset,
048: int length) {
049: Assert.isNotNull(replacementString);
050:
051: fReplacementString = replacementString;
052: fImage = image;
053: fDisplayString = displayString;
054: fBeginInsertPoint = startOffset;
055: fLength = length;
056: }
057:
058: /* (non-Javadoc)
059: * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
060: */
061: public void apply(IDocument document) {
062: if (fLength == -1) {
063: String current = document.get();
064: fLength = current.length();
065: }
066: try {
067: document.replace(fBeginInsertPoint, fLength,
068: fReplacementString);
069: } catch (BadLocationException e) {
070: // DEBUG
071: // e.printStackTrace();
072: }
073: }
074:
075: /* (non-Javadoc)
076: * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
077: */
078: public String getAdditionalProposalInfo() {
079: // No additional proposal information
080: return null;
081: }
082:
083: /* (non-Javadoc)
084: * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
085: */
086: public IContextInformation getContextInformation() {
087: // No context information
088: return null;
089: }
090:
091: /* (non-Javadoc)
092: * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
093: */
094: public String getDisplayString() {
095: return fDisplayString;
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
100: */
101: public Image getImage() {
102: return fImage;
103: }
104:
105: /* (non-Javadoc)
106: * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
107: */
108: public Point getSelection(IDocument document) {
109: if (fReplacementString.equals("\"\"")) //$NON-NLS-1$
110: return new Point(fBeginInsertPoint + 1, 0);
111: return new Point(fBeginInsertPoint
112: + fReplacementString.length(), 0);
113: }
114:
115: /**
116: * @return
117: */
118: public String getReplacementString() {
119: return fReplacementString;
120: }
121:
122: public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
123: return fAdditionalInfo;
124: }
125:
126: public void setAdditionalProposalInfo(String info) {
127: fAdditionalInfo = info;
128: }
129:
130: public IInformationControlCreator getInformationControlCreator() {
131: if (!BrowserInformationControl.isAvailable(null))
132: return null;
133:
134: if (fCreator == null) {
135: fCreator = new AbstractReusableInformationControlCreator() {
136:
137: /*
138: * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractReusableInformationControlCreator#doCreateInformationControl(org.eclipse.swt.widgets.Shell)
139: */
140: public IInformationControl doCreateInformationControl(
141: Shell parent) {
142: return new BrowserInformationControl(parent,
143: SWT.NO_TRIM | SWT.TOOL, SWT.NONE, EditorsUI
144: .getTooltipAffordanceString());
145: }
146: };
147: }
148: return fCreator;
149: }
150:
151: public int getPrefixCompletionStart(IDocument document,
152: int completionOffset) {
153: return fBeginInsertPoint;
154: }
155:
156: public CharSequence getPrefixCompletionText(IDocument document,
157: int completionOffset) {
158: return fReplacementString;
159: }
160:
161: }
|