X-Git-Url: http://secure.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierProposal.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierProposal.java index f104804..ec4eef6 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierProposal.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/IdentifierProposal.java @@ -3,68 +3,57 @@ * All Rights Reserved. */ package net.sourceforge.phpdt.internal.ui.text.template; - -import net.sourceforge.phpdt.internal.corext.template.Template; -import net.sourceforge.phpdt.internal.corext.template.TemplateBuffer; -import net.sourceforge.phpdt.internal.corext.template.TemplateContext; import net.sourceforge.phpdt.internal.corext.template.TemplateMessages; -import net.sourceforge.phpdt.internal.corext.template.TemplatePosition; -import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext; -import net.sourceforge.phpdt.internal.corext.template.php.PHPTemplateMessages; -import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal; -import net.sourceforge.phpeclipse.PHPeclipsePlugin; -import org.eclipse.core.runtime.CoreException; +import net.sourceforge.phpdt.internal.corext.template.php.JavaContext; import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager; import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI; -import org.eclipse.jface.dialogs.MessageDialog; +import net.sourceforge.phpeclipse.PHPeclipsePlugin; + import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.contentassist.IContextInformation; +import org.eclipse.jface.text.templates.TemplateContext; import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.widgets.Shell; -//import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager; -//import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI; -//import org.eclipse.jdt.internal.ui.util.ExceptionHandler; - +//import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager; +//import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI; +//import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler; /** * A PHP identifier proposal. */ -public class IdentifierProposal implements IPHPCompletionProposal { - - private final String fTemplate; +public class IdentifierProposal extends AbstractProposal { //implements + // IPHPCompletionProposal + // { private final TemplateContext fContext; - private final ITextViewer fViewer; private final Image fImage_fun; private final Image fImage_var; - private final IRegion fRegion; - //private TemplateBuffer fTemplateBuffer; private String fOldText; - private IRegion fSelectedRegion; // initialized by apply() - + private final IRegion fRegion; + // private IRegion fSelectedRegion; // initialized by apply() + private final String fTemplate; + // private final ITextViewer fViewer; /** * Creates a template proposal with a template and its context. - * @param template the template - * @param context the context in which the template was requested. - * @param image the icon of the proposal. + * + * @param template + * the template + * @param context + * the context in which the template was requested. + * @param image + * the icon of the proposal. */ - public IdentifierProposal(String template, TemplateContext context, IRegion region, ITextViewer viewer, Image image_fun, Image image_var) { - // Assert.isNotNull(template); - // Assert.isNotNull(context); - // Assert.isNotNull(region); - // Assert.isNotNull(viewer); - + public IdentifierProposal(String template, TemplateContext context, + IRegion region, ITextViewer viewer, Image image_fun, Image image_var) { + super(viewer); fTemplate = template; fContext = context; - fViewer = viewer; - fImage_fun= image_fun; - fImage_var= image_var; + // fViewer = viewer; + fImage_fun = image_fun; + fImage_var = image_var; fRegion = region; } - /* * @see ICompletionProposal#apply(IDocument) */ @@ -72,14 +61,11 @@ public class IdentifierProposal implements IPHPCompletionProposal { try { // if (fTemplateBuffer == null) // fTemplateBuffer= fContext.evaluate(fTemplate); - int start = fRegion.getOffset(); int end = fRegion.getOffset() + fRegion.getLength(); - // insert template string - // String templateString = fTemplate; // fTemplateBuffer.getString(); + // String templateString = fTemplate; // fTemplateBuffer.getString(); document.replace(start, end - start, fTemplate); - // translate positions LinkedPositionManager manager = new LinkedPositionManager(document); // TemplatePosition[] variables= fTemplateBuffer.getVariables(); @@ -95,44 +81,19 @@ public class IdentifierProposal implements IPHPCompletionProposal { // for (int j= 0; j != offsets.length; j++) // manager.addPosition(offsets[j] + start, length); // } - LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager); - editor.setFinalCaretOffset(fTemplate.length()+start); - // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start); + editor.setFinalCaretOffset(fTemplate.length() + start); + // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start); editor.enter(); - fSelectedRegion = editor.getSelectedRegion(); - } catch (BadLocationException e) { PHPeclipsePlugin.log(e); openErrorDialog(e); - } // catch (CoreException e) { // handleException(e); - // } + // } } - -// private static int getCaretOffset(TemplateBuffer buffer) { -// TemplatePosition[] variables = buffer.getVariables(); -// for (int i = 0; i != variables.length; i++) { -// TemplatePosition variable = variables[i]; -// -// if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$ -// return variable.getOffsets()[0]; -// } -// -// return buffer.getString().length(); -// } - - /* - * @see ICompletionProposal#getSelection(IDocument) - */ - public Point getSelection(IDocument document) { - return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength()); - // return null; - } - /* * @see ICompletionProposal#getAdditionalProposalInfo() */ @@ -140,110 +101,51 @@ public class IdentifierProposal implements IPHPCompletionProposal { // try { // if (fTemplateBuffer == null) // fTemplateBuffer= fContext.evaluate(fTemplate); - return textToHTML(fTemplate); // fTemplateBuffer.getString()); - // } catch (CoreException e) { - // handleException(e); + // handleException(e); // return null; // } } - + /* + * @see ICompletionProposal#getContextInformation() + */ + public IContextInformation getContextInformation() { + return null; + } /* * @see ICompletionProposal#getDisplayString() */ public String getDisplayString() { - return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$ - // return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$ + return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ } - /* * @see ICompletionProposal#getImage() */ public Image getImage() { - if (fTemplate.charAt(0)=='$') { + if (fTemplate.charAt(0) == '$') { return fImage_var; } - return fImage_fun; - } - - /* - * @see ICompletionProposal#getContextInformation() - */ - public IContextInformation getContextInformation() { - return null; - } - - private static String textToHTML(String string) { - StringBuffer buffer = new StringBuffer(string.length()); - buffer.append("
"); //$NON-NLS-1$
-
-    for (int i = 0; i != string.length(); i++) {
-      char ch = string.charAt(i);
-
-      switch (ch) {
-        case '&' :
-          buffer.append("&"); //$NON-NLS-1$
-          break;
-
-        case '<' :
-          buffer.append("<"); //$NON-NLS-1$
-          break;
-
-        case '>' :
-          buffer.append(">"); //$NON-NLS-1$
-          break;
-
-        case '\t' :
-          buffer.append("    "); //$NON-NLS-1$
-          break;
-
-        case '\n' :
-          buffer.append("
"); //$NON-NLS-1$ - break; - - default : - buffer.append(ch); - break; - } - } - - buffer.append("
"); //$NON-NLS-1$ - return buffer.toString(); + return fImage_fun; } - - private void openErrorDialog(BadLocationException e) { - Shell shell = fViewer.getTextWidget().getShell(); - MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$ - } - - private void handleException(CoreException e) { - Shell shell = fViewer.getTextWidget().getShell(); - PHPeclipsePlugin.log(e); - // ExceptionHandler.handle(e, shell, TemplateMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$ - } - /* * @see IJavaCompletionProposal#getRelevance() */ public int getRelevance() { - - if (fContext instanceof PHPUnitContext) { - PHPUnitContext context = (PHPUnitContext) fContext; + if (fContext instanceof JavaContext) { + JavaContext context = (JavaContext) fContext; switch (context.getCharacterBeforeStart()) { // high relevance after whitespace case ' ' : case '\r' : case '\n' : case '\t' : - return 90; - + return 50; default : return 0; } } else { - return 90; + return 50; } } - } \ No newline at end of file