4bbc2e68e74fe7a16d6b0f812b6a1492acbe89b7
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / AbstractProposal.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.corext.template.TemplateMessages;
8 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
10
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.jface.dialogs.MessageDialog;
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.IRegion;
16 import org.eclipse.jface.text.ITextViewer;
17 import org.eclipse.jface.text.contentassist.ContextInformation;
18 import org.eclipse.jface.text.contentassist.IContextInformation;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.widgets.Shell;
21
22
23 /**
24  * A PHP identifier proposal.
25  */
26 public abstract class AbstractProposal implements IPHPCompletionProposal {
27         protected IRegion fSelectedRegion; // initialized by apply()
28
29         protected final ITextViewer fViewer;
30
31         protected ContextInformation fContextInfo;
32
33         public AbstractProposal(ITextViewer viewer) {
34                 fContextInfo = null;
35                 fViewer = viewer;
36         }
37
38         protected static String textToHTML(String string) {
39                 StringBuffer buffer = new StringBuffer(string.length());
40                 buffer.append("<pre>"); //$NON-NLS-1$
41
42                 for (int i = 0; i != string.length(); i++) {
43                         char ch = string.charAt(i);
44
45                         switch (ch) {
46                         case '&':
47                                 buffer.append("&amp;"); //$NON-NLS-1$
48                                 break;
49
50                         case '<':
51                                 buffer.append("&lt;"); //$NON-NLS-1$
52                                 break;
53
54                         case '>':
55                                 buffer.append("&gt;"); //$NON-NLS-1$
56                                 break;
57
58                         case '\t':
59                                 buffer.append("    "); //$NON-NLS-1$
60                                 break;
61
62                         case '\n':
63                                 buffer.append("<br>"); //$NON-NLS-1$
64                                 break;
65
66                         default:
67                                 buffer.append(ch);
68                                 break;
69                         }
70                 }
71
72                 buffer.append("</pre>"); //$NON-NLS-1$
73                 return buffer.toString();
74         }
75
76         /*
77          * @see ICompletionProposal#getSelection(IDocument)
78          */
79         public Point getSelection(IDocument document) {
80                 return new Point(fSelectedRegion.getOffset(), fSelectedRegion
81                                 .getLength());
82         }
83
84         protected void handleException(CoreException e) {
85                 PHPeclipsePlugin.log(e);
86         }
87
88         protected void openErrorDialog(BadLocationException e) {
89                 Shell shell = fViewer.getTextWidget().getShell();
90                 MessageDialog.openError(shell, TemplateMessages
91                                 .getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
92         }
93
94         public IContextInformation getContextInformation() {
95                 return null;
96         }
97
98 }