--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jdt.internal.corext.template.java;
+
+import java.util.Collection;
+
+import org.eclipse.swt.graphics.Image;
+
+import org.eclipse.core.runtime.CoreException;
+
+import org.eclipse.jface.viewers.StyledCellLabelProvider;
+import org.eclipse.jface.viewers.StyledString;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.DocumentEvent;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.Position;
+import org.eclipse.jface.text.Region;
+import org.eclipse.jface.text.templates.DocumentTemplateContext;
+import org.eclipse.jface.text.templates.Template;
+import org.eclipse.jface.text.templates.TemplateBuffer;
+import org.eclipse.jface.text.templates.TemplateContextType;
+import org.eclipse.jface.text.templates.TemplateException;
+
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.Statement;
+
+import org.eclipse.jdt.internal.corext.util.Messages;
+
+import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
+
+import org.eclipse.jdt.internal.ui.JavaPlugin;
+import org.eclipse.jdt.internal.ui.JavaPluginImages;
+import org.eclipse.jdt.internal.ui.text.correction.AssistContext;
+import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;
+import org.eclipse.jdt.internal.ui.text.correction.QuickTemplateProcessor;
+import org.eclipse.jdt.internal.ui.text.correction.SurroundWith;
+import org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariableGuess;
+import org.eclipse.jdt.internal.ui.text.template.contentassist.SurroundWithTemplateProposal;
+import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal;
+
+
+/**
+ * A compilation unit context.
+ */
+public abstract class CompilationUnitContext extends DocumentTemplateContext {
+
+ /** The compilation unit, may be <code>null</code>. */
+ private final ICompilationUnit fCompilationUnit;
+ /** A flag to force evaluation in head-less mode. */
+ protected boolean fForceEvaluation;
+ /** A global state for proposals that change if a master proposal changes. */
+ protected MultiVariableGuess fMultiVariableGuess;
+ /** <code>true</code> if the context has a managed (i.e. added to the document) position, <code>false</code> otherwise. */
+ protected final boolean fIsManaged;
+
+ /**
+ * Creates a compilation unit context.
+ *
+ * @param type the context type
+ * @param document the document
+ * @param completionOffset the completion position within the document
+ * @param completionLength the completion length within the document
+ * @param compilationUnit the compilation unit (may be <code>null</code>)
+ */
+ protected CompilationUnitContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength, ICompilationUnit compilationUnit) {
+ super(type, document, completionOffset, completionLength);
+ fCompilationUnit= compilationUnit;
+ fIsManaged= false;
+ }
+
+ /**
+ * Creates a compilation unit context.
+ *
+ * @param type the context type
+ * @param document the document
+ * @param completionPosition the position defining the completion offset and length
+ * @param compilationUnit the compilation unit (may be <code>null</code>)
+ * @since 3.2
+ */
+ protected CompilationUnitContext(TemplateContextType type, IDocument document, Position completionPosition, ICompilationUnit compilationUnit) {
+ super(type, document, completionPosition);
+ fCompilationUnit= compilationUnit;
+ fIsManaged= true;
+ }
+
+ /**
+ * Returns the compilation unit if one is associated with this context,
+ * <code>null</code> otherwise.
+ *
+ * @return the compilation unit of this context or <code>null</code>
+ */
+ public final ICompilationUnit getCompilationUnit() {
+ return fCompilationUnit;
+ }
+
+ /**
+ * Returns the enclosing element of a particular element type,
+ * <code>null</code> if no enclosing element of that type exists.
+ *
+ * @param elementType the element type
+ * @return the enclosing element of the given type or <code>null</code>
+ */
+ public IJavaElement findEnclosingElement(int elementType) {
+ if (fCompilationUnit == null)
+ return null;
+
+ try {
+ IJavaElement element= fCompilationUnit.getElementAt(getStart());
+ if (element == null) {
+ element= fCompilationUnit;
+ }
+
+ return element.getAncestor(elementType);
+
+ } catch (JavaModelException e) {
+ return null;
+ }
+ }
+
+ /**
+ * Sets whether evaluation is forced or not.
+ *
+ * @param evaluate <code>true</code> in order to force evaluation,
+ * <code>false</code> otherwise
+ */
+ public void setForceEvaluation(boolean evaluate) {
+ fForceEvaluation= evaluate;
+ }
+
+ /**
+ * Returns the multi-variable guess.
+ *
+ * @return the multi-variable guess
+ */
+ public MultiVariableGuess getMultiVariableGuess() {
+ return fMultiVariableGuess;
+ }
+
+ /**
+ * @param multiVariableGuess The multiVariableGuess to set.
+ */
+ void setMultiVariableGuess(MultiVariableGuess multiVariableGuess) {
+ fMultiVariableGuess= multiVariableGuess;
+ }
+
+ protected IJavaProject getJavaProject() {
+ ICompilationUnit compilationUnit= getCompilationUnit();
+ IJavaProject project= compilationUnit == null ? null : compilationUnit.getJavaProject();
+ return project;
+ }
+
+ public void generated_2978296146101812129(IDocument document, ICompilationUnit cu, int offset, int length, Collection<IJavaCompletionProposal> result, QuickTemplateProcessor quicktemplateprocessor)
+ throws BadLocationException, CoreException {
+ setVariable("selection", document.get(offset, length)); //$NON-NLS-1$
+ setForceEvaluation(true);
+
+ int start= getStart();
+ int end= getEnd();
+ IRegion region= new Region(start, end - start);
+
+ AssistContext invocationContext= new AssistContext(cu, start, end - start);
+ Statement[] selectedStatements= SurroundWith.getSelectedStatements(invocationContext);
+
+ Template[] templates= JavaPlugin.getDefault().getTemplateStore().getTemplates();
+ for (int i= 0; i != templates.length; i++) {
+ Template currentTemplate= templates[i];
+ if (quicktemplateprocessor.canEvaluate(this, currentTemplate)) {
+
+ if (selectedStatements != null) {
+ Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
+ TemplateProposal proposal= new SurroundWithTemplateProposal(cu, currentTemplate, this, region, image, selectedStatements);
+ String[] arg= new String[] { currentTemplate.getName(), currentTemplate.getDescription() };
+ String decorated= Messages.format(CorrectionMessages.QuickTemplateProcessor_surround_label, arg);
+ proposal.setDisplayString(StyledCellLabelProvider.styleDecoratedString(decorated, StyledString.QUALIFIER_STYLER, new StyledString(currentTemplate.getName())));
+ result.add(proposal);
+ } else {
+ TemplateProposal proposal= new TemplateProposal(currentTemplate, this, region, JavaPluginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE)) {
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean validate(IDocument doc, int off, DocumentEvent event) {
+ return false;
+ }
+ };
+ String[] arg= new String[] { currentTemplate.getName(), currentTemplate.getDescription() };
+ String decorated= Messages.format(CorrectionMessages.QuickTemplateProcessor_surround_label, arg);
+ proposal.setDisplayString(StyledCellLabelProvider.styleDecoratedString(decorated, StyledString.QUALIFIER_STYLER, new StyledString(currentTemplate.getName())));
+ result.add(proposal);
+ }
+ }
+ }
+ }
+
+ public String generated_7438537663912731634(IDocument document, SurroundWithTemplateProposal surroundwithtemplateproposal) throws BadLocationException {
+ int offset= getCompletionOffset();
+ int start= getStart();
+ int end= getEnd();
+ IRegion region= new Region(start, end - start);
+
+ setReadOnly(false);
+ TemplateBuffer templateBuffer;
+ try {
+ templateBuffer= evaluate(surroundwithtemplateproposal.fTemplate);
+ } catch (TemplateException e1) {
+ JavaPlugin.log(e1);
+ return null;
+ }
+
+ start= region.getOffset();
+ end= region.getOffset() + region.getLength();
+ end= Math.max(end, offset);
+
+ String templateString= templateBuffer.getString();
+ document.replace(start, end - start, templateString);
+
+ return document.get();
+ }
+
+ public void generated_5472318693531917302(ITextViewer viewer, char trigger, int stateMask, SurroundWithTemplateProposal surroundwithtemplateproposal) {
+ int start= getStart();
+ int end= getEnd();
+ IRegion region= new Region(start, end - start);
+
+ //Evaluate the template within the new context
+ surroundwithtemplateproposal.fProposal= new TemplateProposal(surroundwithtemplateproposal.fTemplate, this, region, null);
+ surroundwithtemplateproposal.fProposal.apply(viewer, trigger, stateMask, getCompletionOffset());
+ }
+
+ public CompilationUnitContext generated_6219744471917571491() {
+ setForceEvaluation(true);
+ return this;
+ }
+
+ public void generated_551997803931588642() {
+ if (getKey().length() == 0)
+ setForceEvaluation(true);
+ }
+}