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