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