Search in sources :

Example 46 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project linuxtools by eclipse.

the class SpecfileElementHyperlinkDetector method prepareHyperlink.

private IHyperlink[] prepareHyperlink(IRegion lineInfo, String line, String word, SpecfileElement source, int lineIndex) {
    IRegion urlRegion = new Region(lineInfo.getOffset() + line.indexOf(word, lineIndex), word.length());
    // will only work with 1 active page
    // does not work with CompareEditor
    IWorkbench wb = PlatformUI.getWorkbench();
    IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
    IWorkbenchPage page = win.getActivePage();
    IEditorPart editor = page.getActiveEditor();
    // we can only provide this functionality for resources inside the workbench.
    if (editor.getEditorInput() instanceof FileEditorInput) {
        IFile original = ((FileEditorInput) editor.getEditorInput()).getFile();
        return new IHyperlink[] { new SpecfileElementHyperlink(urlRegion, source, original) };
    } else {
        return null;
    }
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) FileEditorInput(org.eclipse.ui.part.FileEditorInput) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) IRegion(org.eclipse.jface.text.IRegion)

Example 47 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project linuxtools by eclipse.

the class PrepareChangeLogAction method guessFunctionNames.

/**
 * Guesses the function effected/modified by the patch from local file(newer
 * file).
 *
 * @param patchFileInfo
 *            patch file
 * @return array of unique function names
 */
private String[] guessFunctionNames(PatchFile patchFileInfo) {
    // new files or not
    if (patchFileInfo.isNewfile() || patchFileInfo.isRemovedFile()) {
        return new String[] { "" };
    }
    String[] fnames = new String[0];
    // $NON-NLS-1$
    String editorName = "";
    try {
        IEditorDescriptor ed = org.eclipse.ui.ide.IDE.getEditorDescriptor(patchFileInfo.getPath().toOSString(), true, false);
        // $NON-NLS-1$
        editorName = ed.getId().substring(ed.getId().lastIndexOf(".") + 1);
    } catch (PartInitException e1) {
        ChangelogPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, e1.getMessage(), e1));
        return new String[0];
    }
    // check if the file type is supported
    // get editor input for target file
    IFileEditorInput fei = new FileEditorInput((IFile) patchFileInfo.getResource());
    SourceEditorInput sei = new SourceEditorInput(patchFileInfo.getStorage());
    MyDocumentProvider mdp = new MyDocumentProvider();
    MyStorageDocumentProvider msdp = new MyStorageDocumentProvider();
    try {
        // get document for target file (one for local file, one for repository storage)
        IDocument doc = mdp.createDocument(fei);
        IDocument olddoc = msdp.createDocument(sei);
        HashMap<String, String> functionNamesMap = new HashMap<>();
        ArrayList<String> nameList = new ArrayList<>();
        // for all the ranges
        for (PatchRangeElement tpre : patchFileInfo.getRanges()) {
            for (int j = tpre.fromLine; j <= tpre.toLine; j++) {
                String functionGuess = "";
                // right now it assumes it's java file.
                if (tpre.isLocalChange()) {
                    if ((j < 0) || (j > doc.getNumberOfLines() - 1))
                        // ignore out of bound lines
                        continue;
                    functionGuess = parseCurrentFunctionAtOffset(editorName, fei, doc.getLineOffset(j));
                } else {
                    if ((j < 0) || (j > olddoc.getNumberOfLines() - 1))
                        // ignore out of bound lines
                        continue;
                    functionGuess = parseCurrentFunctionAtOffset(editorName, sei, olddoc.getLineOffset(j));
                }
                // is helpful when trying to document a large set of changes.
                if (functionNamesMap.get(functionGuess) == null)
                    nameList.add(functionGuess);
                functionNamesMap.put(functionGuess, functionGuess);
            }
        }
        // dump all unique func. guesses in the order found
        fnames = new String[nameList.size()];
        fnames = nameList.toArray(fnames);
    } catch (CoreException | BadLocationException e) {
        ChangelogPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, ChangelogPlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e));
    }
    return fnames;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) HashMap(java.util.HashMap) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) ArrayList(java.util.ArrayList) CoreException(org.eclipse.core.runtime.CoreException) IFileEditorInput(org.eclipse.ui.IFileEditorInput) IFileEditorInput(org.eclipse.ui.IFileEditorInput) FileEditorInput(org.eclipse.ui.part.FileEditorInput) PartInitException(org.eclipse.ui.PartInitException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 48 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project yamcs-studio by yamcs.

the class OpenWithMenu method openEditor.

/**
 * Opens the given editor on the selected file.
 *
 * @param editor
 *            the editor descriptor, or null for the system editor
 * @param openUsingDescriptor
 *            use the descriptor's editor ID for opening if false (normal case), or use the descriptor itself if
 *            true (needed to fix bug 178235).
 */
private void openEditor(IEditorDescriptor editor, boolean openUsingDescriptor) {
    IFile file = getFileResource();
    if (file == null) {
        return;
    }
    try {
        if (openUsingDescriptor) {
            ((WorkbenchPage) page).openEditorFromDescriptor(new FileEditorInput(file), editor, true, null);
        } else {
            String editorId = editor == null ? IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID : editor.getId();
            ((WorkbenchPage) page).openEditor(new FileEditorInput(file), editorId, true, MATCH_BOTH);
            // only remember the default editor if the open succeeds
            IDE.setDefaultEditor(file, editorId);
        }
    } catch (PartInitException e) {
        DialogUtil.openError(page.getWorkbenchWindow().getShell(), "Problems Opening Editor", e.getMessage(), e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) WorkbenchPage(org.eclipse.ui.internal.WorkbenchPage) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) FileEditorInput(org.eclipse.ui.part.FileEditorInput) PartInitException(org.eclipse.ui.PartInitException)

Example 49 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project yamcs-studio by yamcs.

the class OPIEditor method performSave.

private void performSave() {
    try {
        String content = XMLUtil.XML_HEADER + XMLUtil.widgetToXMLString(displayModel, true);
        if (getEditorInput() instanceof FileEditorInput) {
            // $NON-NLS-1$
            InputStream in = new ByteArrayInputStream(content.getBytes("UTF-8"));
            try {
                IFile file = ((FileEditorInput) getEditorInput()).getFile();
                file.setContents(in, false, false, null);
                in.close();
            } catch (Exception e) {
                in.close();
                processSaveFailedError(e);
                return;
            }
        } else if (getEditorInput() instanceof FileStoreEditorInput) {
            try {
                File file = URIUtil.toPath(((FileStoreEditorInput) getEditorInput()).getURI()).toFile();
                BufferedWriter writer = new BufferedWriter(// $NON-NLS-1$
                new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
                writer.write(content);
                writer.flush();
                writer.close();
            } catch (Exception e) {
                processSaveFailedError(e);
                return;
            }
        }
    } catch (Exception e) {
        processSaveFailedError(e);
        return;
    }
    getCommandStack().markSaveLocation();
    firePropertyChange(IEditorPart.PROP_DIRTY);
}
Also used : IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileEditorInput(org.eclipse.ui.part.FileEditorInput) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IFile(org.eclipse.core.resources.IFile) File(java.io.File) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileStoreEditorInput(org.eclipse.ui.ide.FileStoreEditorInput) BufferedWriter(java.io.BufferedWriter)

Example 50 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project yamcs-studio by yamcs.

the class OPIEditor method getInputStream.

/**
 * Returns a stream which can be used to read this editors input data.
 *
 * @return a stream which can be used to read this editors input data
 */
private InputStream getInputStream() {
    InputStream result = null;
    IEditorInput editorInput = getEditorInput();
    if (editorInput instanceof FileEditorInput) {
        try {
            result = ((FileEditorInput) editorInput).getFile().getContents();
        } catch (CoreException e) {
            LOGGER.log(Level.WARNING, "Error reading file.", e);
        }
    } else if (editorInput instanceof FileStoreEditorInput) {
        IPath path = URIUtil.toPath(((FileStoreEditorInput) editorInput).getURI());
        try {
            result = new FileInputStream(path.toFile());
        } catch (FileNotFoundException e) {
            LOGGER.log(Level.WARNING, "Error reading file.", e);
        }
    }
    return result;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IPath(org.eclipse.core.runtime.IPath) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileEditorInput(org.eclipse.ui.part.FileEditorInput) FileNotFoundException(java.io.FileNotFoundException) IEditorInput(org.eclipse.ui.IEditorInput) FileStoreEditorInput(org.eclipse.ui.ide.FileStoreEditorInput) FileInputStream(java.io.FileInputStream)

Aggregations

FileEditorInput (org.eclipse.ui.part.FileEditorInput)87 IFile (org.eclipse.core.resources.IFile)55 CoreException (org.eclipse.core.runtime.CoreException)31 IEditorPart (org.eclipse.ui.IEditorPart)29 PartInitException (org.eclipse.ui.PartInitException)24 ArrayList (java.util.ArrayList)20 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)19 IXliffEditor (net.heartsome.cat.ts.ui.editors.IXliffEditor)18 IEditorReference (org.eclipse.ui.IEditorReference)17 XLFHandler (net.heartsome.cat.ts.core.file.XLFHandler)16 IEditorInput (org.eclipse.ui.IEditorInput)16 FileStoreEditorInput (org.eclipse.ui.ide.FileStoreEditorInput)15 IProject (org.eclipse.core.resources.IProject)13 File (java.io.File)11 XLIFFEditorImplWithNatTable (net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable)11 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)11 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 ExecutionException (org.eclipse.core.commands.ExecutionException)9 List (java.util.List)7