use of org.eclipse.jface.text.Document in project tdi-studio-se by Talend.
the class TalendJavaSourceViewer method createViewerWithVariables.
public static ReconcilerViewer createViewerWithVariables(Composite composite, int styles, IExpressionDataBean dataBean) {
IDocument document = new Document();
StringBuffer buff = new StringBuffer();
//$NON-NLS-1$
buff.append("\npackage internal;\n\n");
buff.append(getImports());
//$NON-NLS-1$ //$NON-NLS-2$
buff.append("public class " + VIEWER_CLASS_NAME + currentId + " {\n");
IRunProcessService runProcessService = getRunProcessService();
if (runProcessService != null && runProcessService.getSelectedContext() != null) {
List<IContextParameter> params = runProcessService.getSelectedContext().getContextParameterList();
buff.append(TEXT_1);
for (IContextParameter ctxParam : params) {
buff.append(TEXT_2);
buff.append(ctxParam.getName());
buff.append(TEXT_3);
buff.append(ctxParam.getName());
buff.append(TEXT_4);
}
buff.append(TEXT_5);
for (IContextParameter ctxParam : params) {
if (//$NON-NLS-1$ //$NON-NLS-2$
ctxParam.getType().equals("id_List Of Value") || ctxParam.getType().equals("id_File") || ctxParam.getType().equals("id_Directory") || ctxParam.getType().equals("id_Character")) {
//$NON-NLS-1$ //$NON-NLS-2$
buff.append(TEXT_6);
buff.append(ctxParam.getName());
buff.append(TEXT_7);
} else {
buff.append(TEXT_8);
buff.append(JavaTypesManager.getTypeToGenerate(ctxParam.getType(), true));
buff.append(TEXT_9);
buff.append(ctxParam.getName());
buff.append(TEXT_10);
}
}
buff.append(TEXT_11);
//$NON-NLS-1$
buff.append("\tprivate static ContextProperties context = new ContextProperties();\n");
//$NON-NLS-1$
buff.append("\tprivate static final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();\n");
if (dataBean != null) {
buff.append(parseVariables(dataBean.getVariables()));
//$NON-NLS-1$ //$NON-NLS-2$
buff.append("\tpublic " + dataBean.getExpressionType() + " myFunction(){\n");
}
//$NON-NLS-1$
buff.append("\t\treturn \n");
}
int length = buff.toString().length();
//$NON-NLS-1$
String defaultValue = "";
//$NON-NLS-1$
buff.append(defaultValue + "\n;\t\n}\n}");
document.set(buff.toString());
return initializeViewer(composite, styles, true, document, length);
}
use of org.eclipse.jface.text.Document in project tdi-studio-se by Talend.
the class TalendJavaSourceViewer method createViewer.
public static ISourceViewer createViewer(Composite composite, int styles, boolean checkCode) {
/*
* Special for cProcessor https://jira.talendforge.org/browse/TESB-3687 for cJavaDSLProcessor
* https://jira.talendforge.org/browse/TESB-7615
*/
boolean isRouteProcess = isRouteProcess();
StringBuffer buff = new StringBuffer();
//$NON-NLS-1$
buff.append("package internal;\n\n");
buff.append(getImports());
// add special imports for RouteBuilder
if (isRouteProcess) {
buff.append("import org.apache.camel.*;");
}
//$NON-NLS-1$ //$NON-NLS-2$
buff.append("public class " + VIEWER_CLASS_NAME + currentId + " {\n");
//$NON-NLS-1$
buff.append("\tprivate static java.util.Properties context = new java.util.Properties();\n");
//$NON-NLS-1$
buff.append("\tprivate static final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();\n");
/*
* Special for cProcessor https://jira.talendforge.org/browse/TESB-3687
*/
if (isRouteProcess) {
//$NON-NLS-1$
buff.append("\tprivate org.apache.camel.Exchange exchange;\n");
//$NON-NLS-1$
buff.append("\torg.apache.camel.impl.DefaultCamelContext camelContext;\n");
//$NON-NLS-1$
buff.append("\tjavax.jms.ConnectionFactory jmsConnectionFactory;\n");
}
// end of https://jira.talendforge.org/browse/TESB-3687
//$NON-NLS-1$
buff.append("\tpublic void myFunction(){\n");
//$NON-NLS-1$
buff.append("\t if( \n");
/*
* for cJavaDSLProcessor https://jira.talendforge.org/browse/TESB-7615
*/
if (isRouteProcess) {
buff.append("new org.apache.camel.model.RouteDefinition()\n");
}
// End of https://jira.talendforge.org/browse/TESB-7615
int documentOffset = buff.toString().length();
//$NON-NLS-1$
buff.append("){\n\t}");
//$NON-NLS-1$
buff.append("\n\t\n}\n}");
IDocument document = new Document();
document.set(buff.toString());
return initializeViewer(composite, styles, checkCode, document, documentOffset);
}
use of org.eclipse.jface.text.Document in project bndtools by bndtools.
the class FileUtils method readFully.
public static IDocument readFully(IFile file) throws CoreException, IOException {
if (file.exists()) {
InputStream stream = file.getContents();
byte[] bytes = readFully(stream);
String string = new String(bytes, file.getCharset());
return new Document(string);
}
return null;
}
use of org.eclipse.jface.text.Document in project che by eclipse.
the class JavaContext method evaluateTemplate.
/**
* Evaluates a 'java' template in the context of a compilation unit
*
* @param template the template to be evaluated
* @param compilationUnit the compilation unit in which to evaluate the template
* @param position the position inside the compilation unit for which to evaluate the template
* @return the evaluated template
* @throws CoreException in case the template is of an unknown context type
* @throws BadLocationException in case the position is invalid in the compilation unit
* @throws TemplateException in case the evaluation fails
*/
public static String evaluateTemplate(Template template, ICompilationUnit compilationUnit, int position) throws CoreException, BadLocationException, TemplateException {
TemplateContextType contextType = JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(template.getContextTypeId());
if (!(contextType instanceof CompilationUnitContextType))
throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.ID_PLUGIN, IStatus.ERROR, JavaTemplateMessages.JavaContext_error_message, null));
IDocument document = new Document();
if (compilationUnit != null && compilationUnit.exists())
document.set(compilationUnit.getSource());
CompilationUnitContext context = ((CompilationUnitContextType) contextType).createContext(document, position, 0, compilationUnit);
context.setForceEvaluation(true);
TemplateBuffer buffer = context.evaluate(template);
if (buffer == null)
return null;
return buffer.getString();
}
use of org.eclipse.jface.text.Document in project che by eclipse.
the class ReplaceInvocationsRefactoring method resolveSourceProvider.
private SourceProvider resolveSourceProvider(IMethodBinding methodBinding, RefactoringStatus status) throws JavaModelException {
final IMethod method = (IMethod) methodBinding.getJavaElement();
ITypeRoot typeRoot;
IDocument source;
CompilationUnit methodDeclarationAstRoot;
ICompilationUnit methodCu = (method).getCompilationUnit();
if (methodCu != null) {
typeRoot = methodCu;
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setSource(methodCu);
parser.setFocalPosition(method.getNameRange().getOffset());
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
MethodDeclaration methodDecl = (MethodDeclaration) NodeFinder.perform(compilationUnit, method.getNameRange()).getParent();
AST ast = compilationUnit.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
Block newBody = ast.newBlock();
newBody.statements().add(rewrite.createStringPlaceholder(fBody, ASTNode.EMPTY_STATEMENT));
rewrite.replace(methodDecl.getBody(), newBody, null);
List<SingleVariableDeclaration> parameters = methodDecl.parameters();
for (int i = 0; i < parameters.size(); i++) {
SingleVariableDeclaration parameter = parameters.get(i);
rewrite.set(parameter.getName(), SimpleName.IDENTIFIER_PROPERTY, fParameterNames[i], null);
}
TextEdit textEdit = rewrite.rewriteAST();
Document document = new Document(methodCu.getBuffer().getContents());
try {
textEdit.apply(document);
} catch (MalformedTreeException e) {
JavaPlugin.log(e);
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
source = document;
methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(source.get(), methodCu, true, true, null);
} else {
IClassFile classFile = method.getClassFile();
//TODO: use source if available?
StubCreator stubCreator = new StubCreator(true) {
@Override
protected void appendMethodBody(IMethod currentMethod) throws JavaModelException {
if (currentMethod.equals(method)) {
fBuffer.append(fBody);
} else {
super.appendMethodBody(currentMethod);
}
}
/*
* @see org.eclipse.jdt.internal.corext.refactoring.binary.StubCreator#appendMethodParameterName(org.eclipse.jdt.core.IMethod, int)
*/
@Override
protected void appendMethodParameterName(IMethod currentMethod, int index) {
if (currentMethod.equals(method)) {
fBuffer.append(fParameterNames[index]);
} else {
super.appendMethodParameterName(currentMethod, index);
}
}
};
String stub = stubCreator.createStub(classFile.getType(), null);
source = new Document(stub);
methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(stub, classFile, true, true, null);
typeRoot = classFile;
}
ASTNode node = methodDeclarationAstRoot.findDeclaringNode(methodBinding.getKey());
if (node instanceof MethodDeclaration) {
return new SourceProvider(typeRoot, source, (MethodDeclaration) node);
} else {
status.addFatalError(RefactoringCoreMessages.ReplaceInvocationsRefactoring_cannot_find_method_declaration);
return null;
}
}
Aggregations