2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
7 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
8 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
11 import org.eclipse.jface.text.BadLocationException;
12 import org.eclipse.jface.text.IDocument;
13 import org.eclipse.jface.text.IRegion;
14 import org.eclipse.jface.text.ITextViewer;
15 import org.eclipse.jface.text.contentassist.IContextInformation;
16 import org.eclipse.jface.text.templates.TemplateContext;
17 import org.eclipse.swt.graphics.Image;
20 * A PHP identifier proposal.
22 public class SQLProposal extends AbstractProposal { // implements
23 // IPHPCompletionProposal
25 private final TemplateContext fContext;
27 private final Image fImage_var;
29 private final IRegion fRegion;
31 private final String fColumnName;
33 private final String fTableName;
35 private int fRelevance;
38 * Creates a template proposal with a template and its context.
43 * the context in which the template was requested.
45 * the icon of the proposal.
47 public SQLProposal(String tableName, TemplateContext context,
48 IRegion region, ITextViewer viewer, Image image_var) {
50 fTableName = tableName;
53 fImage_var = image_var;
58 public SQLProposal(String tableName, String columnName,
59 TemplateContext context, IRegion region, ITextViewer viewer,
62 fTableName = tableName;
63 fColumnName = columnName;
65 fImage_var = image_var;
71 * @see ICompletionProposal#apply(IDocument)
73 public void apply(IDocument document) {
75 // if (fTemplateBuffer == null)
76 // fTemplateBuffer= fContext.evaluate(fTemplate);
77 int start = fRegion.getOffset();
78 int end = fRegion.getOffset() + fRegion.getLength();
79 String resultString = fTableName;
80 if (fColumnName != null) {
81 resultString = fColumnName;
83 // insert template string
84 // String templateString = fTemplate; //
85 // fTemplateBuffer.getString();
86 document.replace(start, end - start, resultString);
87 // translate positions
88 LinkedPositionManager manager = new LinkedPositionManager(document);
89 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
90 // for (int i= 0; i != variables.length; i++) {
91 // TemplatePosition variable= variables[i];
93 // if (variable.isResolved())
96 // int[] offsets= variable.getOffsets();
97 // int length= variable.getLength();
99 // for (int j= 0; j != offsets.length; j++)
100 // manager.addPosition(offsets[j] + start, length);
102 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
103 editor.setFinalCaretOffset(resultString.length() + start);
104 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) +
107 fSelectedRegion = editor.getSelectedRegion();
108 } catch (BadLocationException e) {
109 PHPeclipsePlugin.log(e);
112 // catch (CoreException e) {
113 // handleException(e);
118 * @see ICompletionProposal#getAdditionalProposalInfo()
120 public String getAdditionalProposalInfo() {
121 if (fColumnName == null) {
122 return textToHTML(fTableName);
124 return fColumnName + " (Table: " + fTableName + ")";
128 * @see ICompletionProposal#getContextInformation()
130 public IContextInformation getContextInformation() {
135 * @see ICompletionProposal#getDisplayString()
137 public String getDisplayString() {
138 if (fColumnName == null) {
141 return fColumnName + " (Table: " + fTableName + ")"; // $NON-NLS-1$
145 * @see ICompletionProposal#getImage()
147 public Image getImage() {
152 * @see IJavaCompletionProposal#getRelevance()
154 public int getRelevance() {
160 * The relevance to set.
162 public void setRelevance(int relevance) {
163 fRelevance = relevance;