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;
}
}
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;
}
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);
}
}
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);
}
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;
}
Aggregations