refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / 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
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.ui.WebUI;
10 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
11
12 import org.eclipse.jface.text.BadLocationException;
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.IRegion;
15 import org.eclipse.jface.text.ITextViewer;
16 import org.eclipse.jface.text.contentassist.IContextInformation;
17 import org.eclipse.jface.text.templates.TemplateContext;
18 import org.eclipse.swt.graphics.Image;
19
20 /**
21  * A PHP identifier proposal.
22  */
23 public class SQLProposal extends AbstractProposal { 
24         private final TemplateContext fContext;
25
26         private final Image fImage_var;
27
28         private final IRegion fRegion;
29
30         private final String fColumnName;
31
32         private final String fTableName;
33
34         private int fRelevance;
35
36         /**
37          * Creates a template proposal with a template and its context.
38          * 
39          * @param template
40          *            the template
41          * @param context
42          *            the context in which the template was requested.
43          * @param image
44          *            the icon of the proposal.
45          */
46         public SQLProposal(String tableName, TemplateContext context,
47                         IRegion region, ITextViewer viewer, Image image_var) {
48                 super(viewer);
49                 fTableName = tableName;
50                 fColumnName = null;
51                 fContext = context;
52                 fImage_var = image_var;
53                 fRegion = region;
54                 fRelevance = 0;
55         }
56
57         public SQLProposal(String tableName, String columnName,
58                         TemplateContext context, IRegion region, ITextViewer viewer,
59                         Image image_var) {
60                 super(viewer);
61                 fTableName = tableName;
62                 fColumnName = columnName;
63                 fContext = context;
64                 fImage_var = image_var;
65                 fRegion = region;
66                 fRelevance = 0;
67         }
68
69         /*
70          * @see ICompletionProposal#apply(IDocument)
71          */
72         public void apply(IDocument document) {
73                 try {
74                         int start = fRegion.getOffset();
75                         int end = fRegion.getOffset() + fRegion.getLength();
76                         String resultString = fTableName;
77                         if (fColumnName != null) {
78                                 resultString = fColumnName;
79                         }
80                         // insert template string
81                         document.replace(start, end - start, resultString);
82                         // translate positions
83                         LinkedPositionManager manager = new LinkedPositionManager(document);
84                         LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
85                         editor.setFinalCaretOffset(resultString.length() + start);
86                         editor.enter();
87                         fSelectedRegion = editor.getSelectedRegion();
88                 } catch (BadLocationException e) {
89                         WebUI.log(e);
90                         openErrorDialog(e);
91                 }
92         }
93
94         /*
95          * @see ICompletionProposal#getAdditionalProposalInfo()
96          */
97         public String getAdditionalProposalInfo() {
98                 if (fColumnName == null) {
99                         return textToHTML(fTableName);
100                 }
101                 return fColumnName + " (Table: " + fTableName + ")";
102         }
103
104         /*
105          * @see ICompletionProposal#getContextInformation()
106          */
107         public IContextInformation getContextInformation() {
108                 return null;
109         }
110
111         /*
112          * @see ICompletionProposal#getDisplayString()
113          */
114         public String getDisplayString() {
115                 if (fColumnName == null) {
116                         return fTableName;
117                 }
118                 return fColumnName + " (Table: " + fTableName + ")"; // $NON-NLS-1$
119         }
120
121         /*
122          * @see ICompletionProposal#getImage()
123          */
124         public Image getImage() {
125                 return fImage_var;
126         }
127
128         /*
129          * @see IJavaCompletionProposal#getRelevance()
130          */
131         public int getRelevance() {
132                 return fRelevance;
133         }
134
135         /**
136          * @param relevance
137          *            The relevance to set.
138          */
139         public void setRelevance(int relevance) {
140                 fRelevance = relevance;
141         }
142 }