A massive organize imports and formatting of the sources using default Eclipse code...
[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 { // implements
23         // IPHPCompletionProposal
24         // {
25         private final TemplateContext fContext;
26
27         private final Image fImage_var;
28
29         private final IRegion fRegion;
30
31         private final String fColumnName;
32
33         private final String fTableName;
34
35         private int fRelevance;
36
37         /**
38          * Creates a template proposal with a template and its context.
39          * 
40          * @param template
41          *            the template
42          * @param context
43          *            the context in which the template was requested.
44          * @param image
45          *            the icon of the proposal.
46          */
47         public SQLProposal(String tableName, TemplateContext context,
48                         IRegion region, ITextViewer viewer, Image image_var) {
49                 super(viewer);
50                 fTableName = tableName;
51                 fColumnName = null;
52                 fContext = context;
53                 fImage_var = image_var;
54                 fRegion = region;
55                 fRelevance = 0;
56         }
57
58         public SQLProposal(String tableName, String columnName,
59                         TemplateContext context, IRegion region, ITextViewer viewer,
60                         Image image_var) {
61                 super(viewer);
62                 fTableName = tableName;
63                 fColumnName = columnName;
64                 fContext = context;
65                 fImage_var = image_var;
66                 fRegion = region;
67                 fRelevance = 0;
68         }
69
70         /*
71          * @see ICompletionProposal#apply(IDocument)
72          */
73         public void apply(IDocument document) {
74                 try {
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;
82                         }
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];
92                         //
93                         // if (variable.isResolved())
94                         // continue;
95                         //                              
96                         // int[] offsets= variable.getOffsets();
97                         // int length= variable.getLength();
98                         //                              
99                         // for (int j= 0; j != offsets.length; j++)
100                         // manager.addPosition(offsets[j] + start, length);
101                         // }
102                         LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
103                         editor.setFinalCaretOffset(resultString.length() + start);
104                         // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) +
105                         // start);
106                         editor.enter();
107                         fSelectedRegion = editor.getSelectedRegion();
108                 } catch (BadLocationException e) {
109                         PHPeclipsePlugin.log(e);
110                         openErrorDialog(e);
111                 }
112                 // catch (CoreException e) {
113                 // handleException(e);
114                 // }
115         }
116
117         /*
118          * @see ICompletionProposal#getAdditionalProposalInfo()
119          */
120         public String getAdditionalProposalInfo() {
121                 if (fColumnName == null) {
122                         return textToHTML(fTableName);
123                 }
124                 return fColumnName + " (Table: " + fTableName + ")";
125         }
126
127         /*
128          * @see ICompletionProposal#getContextInformation()
129          */
130         public IContextInformation getContextInformation() {
131                 return null;
132         }
133
134         /*
135          * @see ICompletionProposal#getDisplayString()
136          */
137         public String getDisplayString() {
138                 if (fColumnName == null) {
139                         return fTableName;
140                 }
141                 return fColumnName + " (Table: " + fTableName + ")"; // $NON-NLS-1$
142         }
143
144         /*
145          * @see ICompletionProposal#getImage()
146          */
147         public Image getImage() {
148                 return fImage_var;
149         }
150
151         /*
152          * @see IJavaCompletionProposal#getRelevance()
153          */
154         public int getRelevance() {
155                 return fRelevance;
156         }
157
158         /**
159          * @param relevance
160          *            The relevance to set.
161          */
162         public void setRelevance(int relevance) {
163                 fRelevance = relevance;
164         }
165 }