KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > AntCompletionProposal


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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
12 package org.eclipse.ant.internal.ui.editor;
13
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.DocumentEvent;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.contentassist.ICompletionProposal;
19 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
20 import org.eclipse.jface.text.contentassist.IContextInformation;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.Point;
23
24 public class AntCompletionProposal implements ICompletionProposal, ICompletionProposalExtension2 {
25
26     public static final int TAG_CLOSING_PROPOSAL= 0;
27     public static final int TASK_PROPOSAL= 1;
28     public static final int PROPERTY_PROPOSAL= 2;
29     
30     /** The string to be displayed in the completion proposal popup */
31     private String JavaDoc fDisplayString;
32     /** The replacement string */
33     private String JavaDoc fReplacementString;
34     /** The replacement offset */
35     private int fReplacementOffset;
36     /** The replacement length */
37     private int fReplacementLength;
38     /** The cursor position after this proposal has been applied */
39     private int fCursorPosition;
40     /** The image to be displayed in the completion proposal popup */
41     private Image fImage;
42     /** The additional info of this proposal */
43     private String JavaDoc fAdditionalProposalInfo;
44     
45     private int fType;
46     
47     /**
48      * Creates a new Ant completion proposal. All fields are initialized based on the provided information.
49      *
50      * @param replacementString the actual string to be inserted into the document
51      * @param replacementOffset the offset of the text to be replaced
52      * @param replacementLength the length of the text to be replaced
53      * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
54      * @param image the image to display for this proposal
55      * @param displayString the string to be displayed for the proposal
56      * @param additionalProposalInfo the additional information associated with this proposal
57      * @param type the type of this proposal
58      */

59     public AntCompletionProposal(String JavaDoc replacementString, int replacementOffset, int replacementLength, int cursorPosition, Image image, String JavaDoc displayString, String JavaDoc additionalProposalInfo, int type) {
60         fReplacementString= replacementString;
61         fReplacementOffset= replacementOffset;
62         fReplacementLength= replacementLength;
63         fCursorPosition= cursorPosition;
64         fImage= image;
65         fDisplayString= displayString;
66         fAdditionalProposalInfo= additionalProposalInfo;
67         fType= type;
68     }
69
70     
71     /* (non-Javadoc)
72      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
73      */

74     public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
75         apply(viewer.getDocument());
76
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
81      */

82     public void selected(ITextViewer viewer, boolean smartToggle) {
83     }
84
85     /* (non-Javadoc)
86      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
87      */

88     public void unselected(ITextViewer viewer) {
89     }
90
91     /* (non-Javadoc)
92      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
93      */

94     public boolean validate(IDocument document, int offset, DocumentEvent event) {
95         String JavaDoc enteredText= ""; //$NON-NLS-1$
96
try {
97             enteredText = document.get(fReplacementOffset, offset-fReplacementOffset);
98         } catch (BadLocationException e) {
99         }
100         int enteredLength= enteredText.length();
101         if (fType == TASK_PROPOSAL && enteredText.startsWith("<")) { //$NON-NLS-1$
102
enteredText= enteredText.substring(1);
103         } else if (fType == PROPERTY_PROPOSAL) {
104             if(enteredText.startsWith("${")) { //$NON-NLS-1$
105
enteredText= enteredText.substring(2);
106             }
107             if(enteredText.startsWith("$")) { //$NON-NLS-1$
108
enteredText= enteredText.substring(1);
109             }
110         } else if (fType == TAG_CLOSING_PROPOSAL) {
111             if (enteredText.startsWith("</")) { //$NON-NLS-1$
112
enteredText= enteredText.substring(2);
113             } else if (enteredText.startsWith("/")) { //$NON-NLS-1$
114
try {
115                     if (document.getChar(fReplacementOffset - 1) == '<') {
116                         enteredText= enteredText.substring(1);
117                     }
118                 } catch (BadLocationException e) {
119                 }
120             } else if (enteredText.startsWith("<")) { //$NON-NLS-1$
121
enteredText= enteredText.substring(1);
122             }
123         }
124         boolean valid= fDisplayString.toLowerCase().startsWith(enteredText.toLowerCase());
125         if (valid) {
126             fReplacementLength= enteredLength;
127         }
128         return valid;
129     }
130
131     /* (non-Javadoc)
132      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
133      */

134     public void apply(IDocument document) {
135         try {
136             document.replace(fReplacementOffset, fReplacementLength, fReplacementString);
137         } catch (BadLocationException x) {
138             // ignore
139
}
140     }
141
142     /* (non-Javadoc)
143      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
144      */

145     public Point getSelection(IDocument document) {
146         return new Point(fReplacementOffset + fCursorPosition, 0);
147     }
148
149     /* (non-Javadoc)
150      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
151      */

152     public String JavaDoc getAdditionalProposalInfo() {
153         return fAdditionalProposalInfo;
154     }
155
156     /* (non-Javadoc)
157      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
158      */

159     public String JavaDoc getDisplayString() {
160         if (fDisplayString != null){
161             return fDisplayString;
162         }
163         return fReplacementString;
164     }
165
166     /* (non-Javadoc)
167      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
168      */

169     public Image getImage() {
170         return fImage;
171     }
172
173     /* (non-Javadoc)
174      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
175      */

176     public IContextInformation getContextInformation() {
177         return null;
178     }
179     
180     /* (non-Javadoc)
181      * @see java.lang.Object#toString()
182      */

183     public String JavaDoc toString() {
184         return getDisplayString();
185     }
186
187     /**
188      * @return Returns the type of the completion proposal
189      */

190     public int getType() {
191         return fType;
192     }
193 }
194
Popular Tags