List of usage examples for org.eclipse.jdt.internal.core DocumentAdapter DocumentAdapter
public DocumentAdapter(IBuffer buffer)
From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java
License:Open Source License
public UndoEdit applyTextEdit(TextEdit edit, IProgressMonitor monitor) throws JavaModelException { IBuffer buffer = getBuffer();//from ww w. ja v a 2 s .c o m if (buffer instanceof IBuffer.ITextEditCapability) { return ((IBuffer.ITextEditCapability) buffer).applyTextEdit(edit, monitor); } else if (buffer != null) { IDocument document = buffer instanceof IDocument ? (IDocument) buffer : new DocumentAdapter(buffer); try { UndoEdit undoEdit = edit.apply(document); return undoEdit; } catch (MalformedTreeException e) { throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION); } catch (BadLocationException e) { throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION); } } return null; // can not happen, there are no compilation units without buffer }
From source file:com.motorola.studio.android.model.ActivityBasedOnTemplate.java
License:Apache License
/** * Creates the Java Class file based on text template file * //from w w w . j a va2 s . c om * @param sourcePath The path to the file * @param monitor The progress monitor * @throws JavaModelException * @throws AndroidException */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected void createJavaClassFileFromTemplate(String[] sourcePath, IProgressMonitor monitor) throws JavaModelException, AndroidException { //only one class supported for (int i = 0; i < sourcePath.length; i++) { String loadedTemplate = EclipseUtils.readEmbeddedResource(CodeUtilsActivator.getDefault().getBundle(), sourcePath[i]); String packageName = getPackageFragment().getElementName(); loadedTemplate = loadedTemplate.replaceAll(CLASS_REPLACE_TAG, getName()); loadedTemplate = loadedTemplate.replaceAll(PACKAGE_REPLACE_TAG, packageName); loadedTemplate = loadedTemplate.replaceAll("#FILL_PARENT_LPARAM#", getApiVersion() > 7 ? "MATCH_PARENT" : "FILL_PARENT"); try { loadedTemplate = loadedTemplate.replaceAll("#ManifestPackageName#", //$NON-NLS-1$ getManifestPackageName(getProject())); } catch (CoreException e) { throw new AndroidException("Failed to get manifest file to add import from R", e); //$NON-NLS-1$ } if ((sample != null) && sample.equals(ActivityBasedOnTemplate.DATABASE_LIST_SAMPLE_LOCALIZED)) { collector = getDatabaseSampleActivityParametersWizardCollector(); if (collector != null) { collector.setDatabaseName(this.collectorDatabaseName); collector.setTable(this.collectorTable); collector.setSelectedColumns(collectorColumnList); collector.setSqlOpenHelperClassName(getSqlOpenHelperClassName()); collector.setSqlOpenHelperPackageName(getSqlOpenHelperPackageName()); collector.setCreateOpenHelper(isCreateOpenHelper()); //using Database list sample - it is an special case because it get data from an specific .db table loadedTemplate = loadedTemplate.replaceAll("#dbName#", collector.getDatabaseName()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#tableName#", collector.getTableName()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#tableNameUpperCase#", collector.getTableName() //$NON-NLS-1$ .toUpperCase()); loadedTemplate = loadedTemplate.replaceAll("#tableNameLowerCase#", collector.getTableName() //$NON-NLS-1$ .toLowerCase()); loadedTemplate = loadedTemplate.replaceAll("#columsNames#", collector.getColumnsNames()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#constColumnsNames#", //$NON-NLS-1$ collector.getConstColumnsNames()); loadedTemplate = loadedTemplate.replaceAll("#columnGetValues#", collector.getCursorValues()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#columnAddRows#", collector.getAddColumnsToRow()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#sqlOpenHelperName#", //$NON-NLS-1$ getSqlOpenHelperClassName()); loadedTemplate = loadedTemplate.replaceAll("#imports#", collector.getImports()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#getReadableDatabase#", //$NON-NLS-1$ collector.getReadableDatabase()); if (isCreateOpenHelper()) { collector.createSqlOpenHelper(getProject(), monitor); } } } //replace the main activity of the project, first used by action_bar template try { //assume mainActivity be the activity we are creating (if the project has no main activity). Try to find the real main activity if the isMainActivity flag is false. String mainActivityName = getName(); if (!isMainActivity) { ActivityNode mainActivityNode = AndroidProjectManifestFile.getFromProject(getProject()) .getMainActivity(); if (mainActivityNode != null) { mainActivityName = mainActivityNode.getNodeProperties().get("android:name"); //remove a possible '.' that activities may contain before the name if ((mainActivityName.length() > 0) && (mainActivityName.charAt(0) == '.')) { mainActivityName = mainActivityName.substring(1, mainActivityName.length()); } } } loadedTemplate = loadedTemplate.replaceAll(MAIN_ACTIVITY_REPLACE_TAG, mainActivityName); } catch (CoreException e) { StudioLogger.error("Could not get Android Manifest File from project."); } loadedTemplate = replaceResourceNames(loadedTemplate); IPackageFragment targetPackage = getPackageFragmentRoot() .getPackageFragment(getPackageFragment().getElementName()); if (!targetPackage.exists()) { getPackageFragmentRoot().createPackageFragment(targetPackage.getElementName(), true, monitor); } /* * Create activity class. Only the first src resource will become the Activity subclass. * The other classes will be copied AS IS */ String resourceName = i == 0 ? getName() + JAVA_EXTENSION : Path.fromPortableString(sourcePath[i]).lastSegment(); ICompilationUnit cu = targetPackage.createCompilationUnit(resourceName, loadedTemplate, true, monitor); //indent activity class try { ICompilationUnit workingCopy = cu.getWorkingCopy(monitor); IDocument document = new DocumentAdapter(workingCopy.getBuffer()); //get project indentation configuration Map mapOptions = JavaCore.create(getProject()).getOptions(true); //changes to be applyed to the document TextEdit textEdit = CodeFormatterUtil.format2( CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, document.get(), 0, System.getProperty("line.separator"), mapOptions); //$NON-NLS-1$ workingCopy.applyTextEdit(textEdit, monitor); workingCopy.reconcile(ICompilationUnit.NO_AST, false, null, null); workingCopy.commitWorkingCopy(true, monitor); workingCopy.discardWorkingCopy(); } catch (Exception ex) { //do nothing - indentation fails } } }
From source file:org.eclim.plugin.jdt.util.JavaUtils.java
License:Open Source License
/** * Gets the IDocument for the supplied src file. * <p/>/*from w w w . ja v a 2 s . c o m*/ * Code borrowed from org.eclipse.jdt.internal.core.JavaModelOperation. * * @param src The src file. * @return The IDocument. */ public static IDocument getDocument(ICompilationUnit src) throws Exception { IBuffer buffer = src.getBuffer(); if (buffer instanceof IDocument) { return (IDocument) buffer; } return new DocumentAdapter(buffer); }
From source file:org.eclipse.andmore.android.model.ActivityBasedOnTemplate.java
License:Apache License
/** * Creates the Java Class file based on text template file * /*from ww w .ja v a 2 s .c om*/ * @param sourcePath * The path to the file * @param monitor * The progress monitor * @throws JavaModelException * @throws AndroidException */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected void createJavaClassFileFromTemplate(String[] sourcePath, IProgressMonitor monitor) throws JavaModelException, AndroidException { // only one class supported for (int i = 0; i < sourcePath.length; i++) { String loadedTemplate = EclipseUtils.readEmbeddedResource(CodeUtilsActivator.getDefault().getBundle(), sourcePath[i]); String packageName = getPackageFragment().getElementName(); loadedTemplate = loadedTemplate.replaceAll(CLASS_REPLACE_TAG, getName()); loadedTemplate = loadedTemplate.replaceAll(PACKAGE_REPLACE_TAG, packageName); loadedTemplate = loadedTemplate.replaceAll("#FILL_PARENT_LPARAM#", getApiVersion() > 7 ? "MATCH_PARENT" : "FILL_PARENT"); try { loadedTemplate = loadedTemplate.replaceAll("#ManifestPackageName#", //$NON-NLS-1$ getManifestPackageName(getProject())); } catch (CoreException e) { throw new AndroidException("Failed to get manifest file to add import from R", e); //$NON-NLS-1$ } if ((sample != null) && sample.equals(ActivityBasedOnTemplate.DATABASE_LIST_SAMPLE_LOCALIZED)) { collector = getDatabaseSampleActivityParametersWizardCollector(); if (collector != null) { collector.setDatabaseName(this.collectorDatabaseName); collector.setTable(this.collectorTable); collector.setSelectedColumns(collectorColumnList); collector.setSqlOpenHelperClassName(getSqlOpenHelperClassName()); collector.setSqlOpenHelperPackageName(getSqlOpenHelperPackageName()); collector.setCreateOpenHelper(isCreateOpenHelper()); // using Database list sample - it is an special case // because it get data from an specific .db table loadedTemplate = loadedTemplate.replaceAll("#dbName#", collector.getDatabaseName()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#tableName#", collector.getTableName()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#tableNameUpperCase#", collector.getTableName() //$NON-NLS-1$ .toUpperCase()); loadedTemplate = loadedTemplate.replaceAll("#tableNameLowerCase#", collector.getTableName() //$NON-NLS-1$ .toLowerCase()); loadedTemplate = loadedTemplate.replaceAll("#columsNames#", collector.getColumnsNames()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#constColumnsNames#", //$NON-NLS-1$ collector.getConstColumnsNames()); loadedTemplate = loadedTemplate.replaceAll("#columnGetValues#", collector.getCursorValues()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#columnAddRows#", collector.getAddColumnsToRow()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#sqlOpenHelperName#", //$NON-NLS-1$ getSqlOpenHelperClassName()); loadedTemplate = loadedTemplate.replaceAll("#imports#", collector.getImports()); //$NON-NLS-1$ loadedTemplate = loadedTemplate.replaceAll("#getReadableDatabase#", //$NON-NLS-1$ collector.getReadableDatabase()); if (isCreateOpenHelper()) { collector.createSqlOpenHelper(getProject(), monitor); } } } // replace the main activity of the project, first used by // action_bar template try { // assume mainActivity be the activity we are creating (if the // project has no main activity). Try to find the real main // activity if the isMainActivity flag is false. String mainActivityName = getName(); if (!isMainActivity) { ActivityNode mainActivityNode = AndroidProjectManifestFile.getFromProject(getProject()) .getMainActivity(); if (mainActivityNode != null) { mainActivityName = mainActivityNode.getNodeProperties().get("android:name"); // remove a possible '.' that activities may contain // before the name if ((mainActivityName.length() > 0) && (mainActivityName.charAt(0) == '.')) { mainActivityName = mainActivityName.substring(1, mainActivityName.length()); } } } loadedTemplate = loadedTemplate.replaceAll(MAIN_ACTIVITY_REPLACE_TAG, mainActivityName); } catch (CoreException e) { AndmoreLogger.error("Could not get Android Manifest File from project."); } loadedTemplate = replaceResourceNames(loadedTemplate); IPackageFragment targetPackage = getPackageFragmentRoot() .getPackageFragment(getPackageFragment().getElementName()); if (!targetPackage.exists()) { getPackageFragmentRoot().createPackageFragment(targetPackage.getElementName(), true, monitor); } /* * Create activity class. Only the first src resource will become * the Activity subclass. The other classes will be copied AS IS */ String resourceName = i == 0 ? getName() + JAVA_EXTENSION : Path.fromPortableString(sourcePath[i]).lastSegment(); ICompilationUnit cu = targetPackage.createCompilationUnit(resourceName, loadedTemplate, true, monitor); // indent activity class try { ICompilationUnit workingCopy = cu.getWorkingCopy(monitor); IDocument document = new DocumentAdapter(workingCopy.getBuffer()); // get project indentation configuration Map mapOptions = JavaCore.create(getProject()).getOptions(true); // changes to be applyed to the document TextEdit textEdit = CodeFormatterUtil.format2( CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, document.get(), 0, System.getProperty("line.separator"), mapOptions); //$NON-NLS-1$ workingCopy.applyTextEdit(textEdit, monitor); workingCopy.reconcile(ICompilationUnit.NO_AST, false, null, null); workingCopy.commitWorkingCopy(true, monitor); workingCopy.discardWorkingCopy(); } catch (Exception ex) { // do nothing - indentation fails } } }
From source file:org.eclipse.che.jdt.CodeAssist.java
License:Open Source License
public Proposals computeProposals(IJavaProject project, String fqn, int offset, final String content) throws JavaModelException { WorkingCopyOwner copyOwner = new WorkingCopyOwner() { @Override//from w ww . jav a2s .c o m public IBuffer createBuffer(ICompilationUnit workingCopy) { return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy, workingCopy.getPath(), content); } }; ICompilationUnit compilationUnit; IType type = project.findType(fqn); if (type == null) { return null; } if (type.isBinary()) { compilationUnit = type.getClassFile().getWorkingCopy(copyOwner, null); } else { compilationUnit = type.getCompilationUnit().getWorkingCopy(copyOwner, null); } IBuffer buffer = compilationUnit.getBuffer(); IDocument document; if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) { document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument(); } else { document = new DocumentAdapter(buffer); } TextViewer viewer = new TextViewer(document, new Point(offset, 0)); JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit); List<ICompletionProposal> proposals = new ArrayList<>(); proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null)); proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null)); Collections.sort(proposals, new RelevanceSorter()); return convertProposals(offset, compilationUnit, viewer, proposals); }
From source file:org.eclipse.che.jdt.CompletionJavadocTest.java
License:Open Source License
private static List<ICompletionProposal> computeProposals(ICompilationUnit compilationUnit, int offset) throws JavaModelException { IBuffer buffer = compilationUnit.getBuffer(); IDocument document;//from ww w .j ava 2 s . c o m if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) { document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument(); } else { document = new DocumentAdapter(buffer); } TextViewer viewer = new TextViewer(document, new Point(offset, 0)); JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit); List<ICompletionProposal> proposals = new ArrayList<>(); proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null)); // proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null)); Collections.sort(proposals, new RelevanceSorter()); return proposals; }
From source file:org.eclipse.che.jdt.search.SearchManager.java
License:Open Source License
private IDocument getDocument(ICompilationUnit ancestor) throws JavaModelException { IDocument document;// ww w. j ava2 s . c om IBuffer buffer = ancestor.getBuffer(); if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) { document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument(); } else { document = new DocumentAdapter(buffer); } return document; }
From source file:org.eclipse.flux.jdt.services.QuickAssistService.java
License:Open Source License
private JSONObject applyProposals(int offset, int length, boolean applyFix, ICompilationUnit liveEditUnit, IProblemLocation problem) throws CoreException, JavaModelException, JSONException { IInvocationContext context = new AssistContext(liveEditUnit, offset, length); QuickFixProcessor processor = new QuickFixProcessor(); IJavaCompletionProposal[] proposals = processor.getCorrections(context, new IProblemLocation[] { problem }); if (proposals == null || proposals.length == 0) { return null; }/* w w w . jav a 2 s .co m*/ if (applyFix) { IBuffer buffer = liveEditUnit.getBuffer(); if (buffer != null) { IDocument document = buffer instanceof IDocument ? (IDocument) buffer : new DocumentAdapter(buffer); if (proposals[0] instanceof CUCorrectionProposal) { CUCorrectionProposal proposal = (CUCorrectionProposal) proposals[0]; String preview = proposal.getPreviewContent(); System.out.println(document.getLength()); System.out.println(preview.length()); try { document.addDocumentListener(this.documentListener); document.replace(0, preview.length(), preview); //proposal.apply(document); liveEditUnit.getBuffer().setContents(proposal.getPreviewContent()); liveEditUnit.reconcile(ICompilationUnit.NO_AST, true, null, null); } catch (BadLocationException e) { e.printStackTrace(); } } } return null; } else { List<JSONObject> jsonProposals = new ArrayList<JSONObject>(proposals.length); for (IJavaCompletionProposal proposal : proposals) { JSONObject jsonDescription = getDescription(proposal); JSONObject jsonProposal = new JSONObject(); jsonProposal.put("description", jsonDescription); jsonProposal.put("relevance", proposal.getRelevance()); jsonProposals.add(jsonProposal); } JSONObject result = new JSONObject(); result.put("quickfix", jsonProposals); return result; } }