Syntax highlighting is changeable.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / SQLProposal.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.template;
6 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
7 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
8 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
9
10 import org.eclipse.jface.text.BadLocationException;
11 import org.eclipse.jface.text.IDocument;
12 import org.eclipse.jface.text.IRegion;
13 import org.eclipse.jface.text.ITextViewer;
14 import org.eclipse.jface.text.contentassist.IContextInformation;
15 import org.eclipse.jface.text.templates.TemplateContext;
16 import org.eclipse.swt.graphics.Image;
17 /**
18  * A PHP identifier proposal.
19  */
20 public class SQLProposal extends AbstractProposal { //implements
21   // IPHPCompletionProposal
22   // {
23   private final TemplateContext fContext;
24   private final Image fImage_var;
25   private final IRegion fRegion;
26   private final String fColumnName;
27   private final String fTableName;
28   
29   private int fRelevance;
30   /**
31    * Creates a template proposal with a template and its context.
32    * 
33    * @param template
34    *            the template
35    * @param context
36    *            the context in which the template was requested.
37    * @param image
38    *            the icon of the proposal.
39    */
40   public SQLProposal(String tableName, TemplateContext context, IRegion region,
41       ITextViewer viewer, Image image_var) {
42     super(viewer);
43     fTableName = tableName;
44     fColumnName = null;
45     fContext = context;
46     fImage_var = image_var;
47     fRegion = region;
48     fRelevance = 0;
49   }
50   
51   public SQLProposal(String tableName, String columnName, TemplateContext context, IRegion region,
52       ITextViewer viewer, Image image_var) {
53     super(viewer);
54     fTableName = tableName;
55     fColumnName = columnName;
56     fContext = context;
57     fImage_var = image_var;
58     fRegion = region;
59     fRelevance = 0;
60   }
61   /*
62    * @see ICompletionProposal#apply(IDocument)
63    */
64   public void apply(IDocument document) {
65     try {
66       //                    if (fTemplateBuffer == null)
67       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
68       int start = fRegion.getOffset();
69       int end = fRegion.getOffset() + fRegion.getLength();
70       String resultString = fTableName;
71       if (fColumnName!=null) {
72         resultString = fColumnName;
73       }
74       // insert template string
75       //  String templateString = fTemplate; // fTemplateBuffer.getString();
76       document.replace(start, end - start, resultString);
77       // translate positions
78       LinkedPositionManager manager = new LinkedPositionManager(document);
79       //                        TemplatePosition[] variables= fTemplateBuffer.getVariables();
80       //                        for (int i= 0; i != variables.length; i++) {
81       //                                TemplatePosition variable= variables[i];
82       //
83       //                                if (variable.isResolved())
84       //                                        continue;
85       //                                
86       //                                int[] offsets= variable.getOffsets();
87       //                                int length= variable.getLength();
88       //                                
89       //                                for (int j= 0; j != offsets.length; j++)
90       //                                        manager.addPosition(offsets[j] + start, length);
91       //                        }
92       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
93       editor.setFinalCaretOffset(resultString.length() + start);
94       //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
95       editor.enter();
96       fSelectedRegion = editor.getSelectedRegion();
97     } catch (BadLocationException e) {
98       PHPeclipsePlugin.log(e);
99       openErrorDialog(e);
100     }
101     //      catch (CoreException e) {
102     //                  handleException(e);
103     //      }
104   }
105   /*
106    * @see ICompletionProposal#getAdditionalProposalInfo()
107    */
108   public String getAdditionalProposalInfo() {
109     if (fColumnName==null) {
110       return textToHTML(fTableName);  
111     }
112     return fColumnName+" (Table: "+fTableName+")";
113   }
114   
115   /*
116    * @see ICompletionProposal#getContextInformation()
117    */
118   public IContextInformation getContextInformation() {
119     return null;
120   }
121   /*
122    * @see ICompletionProposal#getDisplayString()
123    */
124   public String getDisplayString() {
125     if (fColumnName==null) {
126       return fTableName;  
127     }
128     return fColumnName+" (Table: "+fTableName+")"; // $NON-NLS-1$
129   }
130   /*
131    * @see ICompletionProposal#getImage()
132    */
133   public Image getImage() {
134     return fImage_var;
135   }
136   /*
137    * @see IJavaCompletionProposal#getRelevance()
138    */
139   public int getRelevance() {
140     return fRelevance;
141   }
142   /**
143    * @param relevance The relevance to set.
144    */
145   public void setRelevance(int relevance) {
146     fRelevance = relevance;
147   }
148 }