1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.template.contentassist;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.jface.text.Assert;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.DocumentEvent;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.contentassist.ICompletionProposal;
26 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
27 import org.eclipse.jface.text.contentassist.IContextInformation;
31 * Global state for templates. Selecting a proposal for the master template variable
32 * will cause the value (and the proposals) for the slave variables to change.
36 public class MultiVariableGuess {
39 * Implementation of the <code>ICompletionProposal</code> interface and extension.
41 class Proposal implements ICompletionProposal, ICompletionProposalExtension2 {
43 /** The string to be displayed in the completion proposal popup */
44 private String fDisplayString;
45 /** The replacement string */
46 String fReplacementString;
47 /** The replacement offset */
48 private int fReplacementOffset;
49 /** The replacement length */
50 private int fReplacementLength;
51 /** The cursor position after this proposal has been applied */
52 private int fCursorPosition;
53 /** The image to be displayed in the completion proposal popup */
55 /** The context information of this proposal */
56 private IContextInformation fContextInformation;
57 /** The additional info of this proposal */
58 private String fAdditionalProposalInfo;
61 * Creates a new completion proposal based on the provided information. The replacement string is
62 * considered being the display string too. All remaining fields are set to <code>null</code>.
64 * @param replacementString the actual string to be inserted into the document
65 * @param replacementOffset the offset of the text to be replaced
66 * @param replacementLength the length of the text to be replaced
67 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
69 public Proposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition) {
70 this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null);
74 * Creates a new completion proposal. All fields are initialized based on the provided information.
76 * @param replacementString the actual string to be inserted into the document
77 * @param replacementOffset the offset of the text to be replaced
78 * @param replacementLength the length of the text to be replaced
79 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
80 * @param image the image to display for this proposal
81 * @param displayString the string to be displayed for the proposal
82 * @param contextInformation the context information associated with this proposal
83 * @param additionalProposalInfo the additional information associated with this proposal
85 public Proposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo) {
86 Assert.isNotNull(replacementString);
87 Assert.isTrue(replacementOffset >= 0);
88 Assert.isTrue(replacementLength >= 0);
89 Assert.isTrue(cursorPosition >= 0);
91 fReplacementString= replacementString;
92 fReplacementOffset= replacementOffset;
93 fReplacementLength= replacementLength;
94 fCursorPosition= cursorPosition;
96 fDisplayString= displayString;
97 fContextInformation= contextInformation;
98 fAdditionalProposalInfo= additionalProposalInfo;
102 * @see ICompletionProposal#apply(IDocument)
104 public void apply(IDocument document) {
106 document.replace(fReplacementOffset, fReplacementLength, fReplacementString);
107 } catch (BadLocationException x) {
113 * @see ICompletionProposal#getSelection(IDocument)
115 public Point getSelection(IDocument document) {
116 return new Point(fReplacementOffset + fCursorPosition, 0);
120 * @see ICompletionProposal#getContextInformation()
122 public IContextInformation getContextInformation() {
123 return fContextInformation;
127 * @see ICompletionProposal#getImage()
129 public Image getImage() {
134 * @see ICompletionProposal#getDisplayString()
136 public String getDisplayString() {
137 if (fDisplayString != null)
138 return fDisplayString;
139 return fReplacementString;
143 * @see ICompletionProposal#getAdditionalProposalInfo()
145 public String getAdditionalProposalInfo() {
146 return fAdditionalProposalInfo;
150 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
152 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
153 apply(viewer.getDocument());
157 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
159 public void selected(ITextViewer viewer, boolean smartToggle) {
163 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
165 public void unselected(ITextViewer viewer) {
169 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
171 public boolean validate(IDocument document, int offset, DocumentEvent event) {
173 String content= document.get(fReplacementOffset, fReplacementLength);
174 if (content.startsWith(fReplacementString))
176 } catch (BadLocationException e) {
177 // ignore concurrently modified document
183 private final List fSlaves= new ArrayList();
185 private MultiVariable fMaster;
190 public MultiVariableGuess(MultiVariable mv) {
198 public ICompletionProposal[] getProposals(MultiVariable variable, int offset, int length) {
199 if (variable.equals(fMaster)) {
200 String[] choices= variable.getValues();
202 ICompletionProposal[] ret= new ICompletionProposal[choices.length];
203 for (int i= 0; i < ret.length; i++) {
204 ret[i]= new Proposal(choices[i], offset, length, offset + length) {
207 * @see org.eclipse.jface.text.link.MultiVariableGuess.Proposal#apply(org.eclipse.jface.text.IDocument)
209 public void apply(IDocument document) {
210 super.apply(document);
213 Object old= fMaster.getSet();
214 fMaster.setSet(fReplacementString);
215 if (!fReplacementString.equals(old)) {
216 for (Iterator it= fSlaves.iterator(); it.hasNext();) {
217 VariablePosition pos= (VariablePosition) it.next();
218 String[] values= pos.getVariable().getValues(fReplacementString);
220 document.replace(pos.getOffset(), pos.getLength(), values[0]);
223 } catch (BadLocationException e) {
224 // ignore and continue
235 String[] choices= variable.getValues(fMaster.getSet());
237 if (choices == null || choices.length < 2)
240 ICompletionProposal[] ret= new ICompletionProposal[choices.length];
241 for (int i= 0; i < ret.length; i++) {
242 ret[i]= new Proposal(choices[i], offset, length, offset + length);
252 public void addSlave(VariablePosition position) {
253 fSlaves.add(position);