KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > contentassist > TypeCompletionProposal


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.contentassist;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.jface.text.AbstractReusableInformationControlCreator;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IInformationControl;
19 import org.eclipse.jface.text.IInformationControlCreator;
20 import org.eclipse.jface.text.contentassist.ICompletionProposal;
21 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3;
22 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension5;
23 import org.eclipse.jface.text.contentassist.IContextInformation;
24 import org.eclipse.pde.internal.ui.editor.contentassist.display.BrowserInformationControl;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.editors.text.EditorsUI;
30
31 public class TypeCompletionProposal implements ICompletionProposal, ICompletionProposalExtension3, ICompletionProposalExtension5 {
32
33     protected String JavaDoc fReplacementString;
34     protected Image fImage;
35     protected String JavaDoc fDisplayString;
36     protected int fBeginInsertPoint;
37     protected int fLength;
38     protected String JavaDoc fAdditionalInfo;
39     private IInformationControlCreator fCreator;
40     
41     public TypeCompletionProposal(String JavaDoc replacementString, Image image, String JavaDoc displayString) {
42         this(replacementString, image, displayString, 0, 0);
43     }
44     
45     public TypeCompletionProposal(String JavaDoc replacementString, Image image, String JavaDoc displayString, int startOffset, int length) {
46         Assert.isNotNull(replacementString);
47         
48         fReplacementString = replacementString;
49         fImage = image;
50         fDisplayString = displayString;
51         fBeginInsertPoint = startOffset;
52         fLength = length;
53     }
54
55     /* (non-Javadoc)
56      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
57      */

58     public void apply(IDocument document) {
59         if (fLength == -1) {
60             String JavaDoc current = document.get();
61             fLength = current.length();
62         }
63         try {
64             document.replace(fBeginInsertPoint, fLength, fReplacementString);
65         } catch (BadLocationException e) {
66             // DEBUG
67
// e.printStackTrace();
68
}
69     }
70
71     /* (non-Javadoc)
72      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
73      */

74     public String JavaDoc getAdditionalProposalInfo() {
75         // No additional proposal information
76
return null;
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
81      */

82     public IContextInformation getContextInformation() {
83         // No context information
84
return null;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
89      */

90     public String JavaDoc getDisplayString() {
91         return fDisplayString;
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
96      */

97     public Image getImage() {
98         return fImage;
99     }
100
101     /* (non-Javadoc)
102      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
103      */

104     public Point getSelection(IDocument document) {
105         if (fReplacementString.equals("\"\"")) //$NON-NLS-1$
106
return new Point(fBeginInsertPoint + 1, 0);
107         return new Point(fBeginInsertPoint + fReplacementString.length(), 0);
108     }
109     
110     /**
111      * @return
112      */

113     public String JavaDoc getReplacementString() {
114         return fReplacementString;
115     }
116
117     public Object JavaDoc getAdditionalProposalInfo(IProgressMonitor monitor) {
118         return fAdditionalInfo;
119     }
120     
121     public void setAdditionalProposalInfo(String JavaDoc info) {
122         fAdditionalInfo = info;
123     }
124
125     public IInformationControlCreator getInformationControlCreator() {
126         if (!BrowserInformationControl.isAvailable(null))
127             return null;
128         
129         if (fCreator == null) {
130             fCreator= new AbstractReusableInformationControlCreator() {
131                 
132                 /*
133                  * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractReusableInformationControlCreator#doCreateInformationControl(org.eclipse.swt.widgets.Shell)
134                  */

135                 public IInformationControl doCreateInformationControl(Shell parent) {
136                     return new BrowserInformationControl(parent, SWT.NO_TRIM | SWT.TOOL, SWT.NONE, EditorsUI.getTooltipAffordanceString());
137                 }
138             };
139         }
140         return fCreator;
141     }
142
143     public int getPrefixCompletionStart(IDocument document, int completionOffset) {
144         return fBeginInsertPoint;
145     }
146
147     public CharSequence JavaDoc getPrefixCompletionText(IDocument document, int completionOffset) {
148         return fReplacementString;
149     }
150
151 }
Popular Tags