Search in sources :

Example 51 with Document

use of org.eclipse.jface.text.Document in project dbeaver by serge-rider.

the class BaseTextEditor method saveToExternalFile.

public void saveToExternalFile() {
    IEditorInput editorInput = getEditorInput();
    IFile curFile = EditorUtils.getFileFromInput(editorInput);
    String fileName = curFile == null ? null : curFile.getName();
    final Document document = getDocument();
    final File saveFile = DialogUtils.selectFileForSave(getSite().getShell(), "Save SQL script", new String[] { "*.sql", "*.txt", "*", "*.*" }, fileName);
    if (document == null || saveFile == null) {
        return;
    }
    try {
        DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

            @Override
            public void run(final DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    StringReader cr = new StringReader(document.get());
                    ContentUtils.saveContentToFile(cr, saveFile, GeneralUtils.UTF8_ENCODING, monitor);
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InterruptedException e) {
    // do nothing
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(getSite().getShell(), "Save failed", null, e.getTargetException());
    }
    afterSaveToFile(saveFile);
}
Also used : IFile(org.eclipse.core.resources.IFile) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) Document(org.eclipse.jface.text.Document) IFile(org.eclipse.core.resources.IFile) IEditorInput(org.eclipse.ui.IEditorInput) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 52 with Document

use of org.eclipse.jface.text.Document in project dbeaver by serge-rider.

the class FileRefDocumentProvider method createDocument.

@Override
protected Document createDocument(Object element) throws CoreException {
    Document document = createEmptyDocument();
    IStorage storage = EditorUtils.getStorageFromInput(element);
    if (storage != null) {
        if (setDocumentContent(document, storage)) {
            setupDocument(document);
            return document;
        }
    }
    File file = EditorUtils.getLocalFileFromInput(element);
    if (file != null) {
        try (InputStream stream = new FileInputStream(file)) {
            setDocumentContent(document, stream, null);
            setupDocument(document);
            return document;
        } catch (IOException e) {
            throw new CoreException(GeneralUtils.makeExceptionStatus(e));
        }
    }
    throw new IllegalArgumentException("Project document provider supports only editor inputs which provides IStorage facility");
}
Also used : Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument)

Example 53 with Document

use of org.eclipse.jface.text.Document in project cubrid-manager by CUBRID.

the class PropDocumentProvider method getDocument.

/**
	 * Retrieves the document to be edited.
	 * 
	 * @param element Object
	 * @return IDocument
	 */
public IDocument getDocument(Object element) {
    IDocument document = null;
    if (element instanceof IEditorInput) {
        IEditorInput ei = ((IEditorInput) element);
        DocumentProvider dp = (DocumentProvider) ei.getAdapter(DocumentProvider.class);
        if (dp != null) {
            document = dp.getDocument(element);
        }
    }
    if (document == null) {
        document = new Document();
    }
    IDocumentPartitioner partitioner = new FastPartitioner(new PropPartitionScanner(), PropPartitionScanner.LEGAL_CONTENT_TYPES);
    partitioner.connect(document);
    document.setDocumentPartitioner(partitioner);
    return document;
}
Also used : IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) FastPartitioner(org.eclipse.jface.text.rules.FastPartitioner) DocumentProvider(com.cubrid.tool.editor.DocumentProvider) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) IEditorInput(org.eclipse.ui.IEditorInput)

Example 54 with Document

use of org.eclipse.jface.text.Document in project cubrid-manager by CUBRID.

the class XMLDocumentProvider method getDocument.

/**
	 * Retrieves the document to be edited.
	 * 
	 * @param element Object
	 * @return IDocument
	 */
public IDocument getDocument(Object element) {
    IDocument document = null;
    if (element instanceof IEditorInput) {
        IEditorInput ei = ((IEditorInput) element);
        DocumentProvider dp = (DocumentProvider) ei.getAdapter(DocumentProvider.class);
        if (dp != null) {
            document = dp.getDocument(element);
        }
    }
    if (document == null) {
        document = new Document();
    }
    IDocumentPartitioner partitioner = new FastPartitioner(new XMLPartitionScanner(), new String[] { XMLPartitionScanner.XML_TAG, XMLPartitionScanner.XML_COMMENT });
    partitioner.connect(document);
    document.setDocumentPartitioner(partitioner);
    return document;
}
Also used : IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) FastPartitioner(org.eclipse.jface.text.rules.FastPartitioner) DocumentProvider(com.cubrid.tool.editor.DocumentProvider) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) XMLPartitionScanner(com.cubrid.tool.editor.xml.scanner.XMLPartitionScanner) IDocument(org.eclipse.jface.text.IDocument) IEditorInput(org.eclipse.ui.IEditorInput)

Example 55 with Document

use of org.eclipse.jface.text.Document in project AutoRefactor by JnRouvignac.

the class RefactoringRulesTest method testRefactoring0.

private void testRefactoring0() throws Exception {
    final String sampleName = testName + "Sample.java";
    final File sampleIn = new File(SAMPLES_BASE_DIR, "samples_in/" + sampleName);
    assertTrue(testName + ": sample in file " + sampleIn + " should exist", sampleIn.exists());
    final File sampleOut = new File(SAMPLES_BASE_DIR, "samples_out/" + sampleName);
    assertTrue(testName + ": sample out file " + sampleOut + " should exist", sampleOut.exists());
    final String refactoringClassname = testName + "Refactoring";
    final RefactoringRule refactoring = getRefactoringClass(refactoringClassname);
    assertNotNull(testName + ": refactoring class " + refactoringClassname + " should exist.\n" + "Make sure you added it to the method getAllRefactoringRules() " + "of the " + AllRefactoringRules.class + ".", refactoring);
    final String sampleInSource = readAll(sampleIn);
    final String sampleOutSource = readAll(sampleOut);
    final IPackageFragment packageFragment = JavaCoreHelper.getPackageFragment(PACKAGE_NAME);
    final ICompilationUnit cu = packageFragment.createCompilationUnit(sampleName, sampleInSource, true, null);
    cu.getBuffer().setContents(sampleInSource);
    cu.save(null, true);
    final IDocument doc = new Document(sampleInSource);
    new ApplyRefactoringsJob(null, null, TEST_ENVIRONMENT).applyRefactoring(doc, cu, new AggregateASTVisitor(Arrays.asList(refactoring)), newJavaProjectOptions(Release.javaSE("1.7.0"), 4), new NullProgressMonitor());
    final String actual = normalizeJavaSourceCode(doc.get().replaceAll("samples_in", "samples_out"));
    final String expected = normalizeJavaSourceCode(sampleOutSource);
    assertEquals(testName + ": wrong output;", expected, actual);
}
Also used : ApplyRefactoringsJob(org.autorefactor.refactoring.ApplyRefactoringsJob) RefactoringRule(org.autorefactor.refactoring.RefactoringRule) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) File(java.io.File) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

Document (org.eclipse.jface.text.Document)59 IDocument (org.eclipse.jface.text.IDocument)48 BadLocationException (org.eclipse.jface.text.BadLocationException)26 TextEdit (org.eclipse.text.edits.TextEdit)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)9 CoreException (org.eclipse.core.runtime.CoreException)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 IRegion (org.eclipse.jface.text.IRegion)8 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)8 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)8 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 TemplateException (org.eclipse.jface.text.templates.TemplateException)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 Template (org.eclipse.jface.text.templates.Template)6 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)6 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)6 InputStream (java.io.InputStream)5 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)5 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)5