Update two editor instance showing same file - java

I have eclipse rcp application, editor with ctabfolder in certain scenerio i am having editor instance further ctabfolder page those are showing file content. My problem is that if i opened same file in first editor and afer change made at file i opened another editor at application, now it showing the previous opened file not the updated one while i have made avaliable changed file for all process for opening another ediotr.
I am using this for creating editor input, I think this is culprit as it is in a singleton pattern and returning the already invoked instance of ctab page.
IFileStore fileStore = EFS.getLocalFileSystem().getStore("filepath");
if yes then tell me the appropriate replacement for this.

To make an editor be aware that the file it is editing has been changed by another editor you need to make the editor track resource changes using an IResourceChangeListener. Set this up with something like:
IResourceChangeListener resourceChange = new ResourceChange();
ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceChange, IResourceChangeEvent.POST_CHANGE);
The ResourceChange class would be:
private class ResourceChange implements IResourceChangeListener
{
#Override
public void resourceChanged(final IResourceChangeEvent event)
{
final IResourceDelta eventDelta = event.getDelta();
final IResourceDelta trackDelta = eventDelta.findMember(editFile);
if (trackDelta != null)
{
if ((trackDelta.getKind() & IResourceDelta.CHANGED) != 0 &&
(trackDelta.getFlags() & IResourceDelta.CONTENT) != 0)
{
// TODO handle change
}
}
}
}
editFile is the IFile the editor is working with.
You need to be careful how to handle the change because this will be invoked during the Save operation for the editor.

Related

Eclipse Plugin: Get editor state information

i have the following problem and am grateful for any help.
I start a batch processing for a file in my plugin and unfortunately I have to make sure that the file is closed in the text editor.
I also have to make sure that other editor references for the same file are closed.
Examples:
"Menu => Window => new Window
"Menu => Editor => Toggle Split Horizontal/Vertical and Clone.
This I have also hopefully managed to do
Here's my code:
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
for (IWorkbenchWindow window : windows) {
IWorkbenchPage page = window.getActivePage();
IEditorReference[] editorReferences = page.getEditorReferences();
for (IEditorReference editorReference : editorReferences) {
String name = editorReference.getName();
IEditorPart editorPart = editorReference.getEditor(false);
if (editorPart instanceof ITextEditor && name != null) {
// Here I note down various attributes, e.g. split
if (name.equals(memberName)) {
MPart mPart = editorPart.getSite().getService(MPart.class);
if (mPart != null) {
List<String> tags = mPart.getTags();
if (tags.contains(IPresentationEngine.SPLIT_HORIZONTAL)) {
openConfiguration.setSplitHorizontal();
} else if (tags.contains(IPresentationEngine.SPLIT_VERTICAL)) {
openConfiguration.setSplitVertical();
}
}
... // another attributes
// close Editor
page.closeEditor(editorPart, false);
}
}
}
}
Now I have the problem that the file is restored in the same state after the batch processing is finished. For example, if the user has done a "horizontal split" or a "clone" on the previously closed file, this should now be displayed exactly as before. When splitting, for example, also with the same ratio as before.
My solution is that I remember the relevant data before (when closing, see code above) and set it again after opening.
Question: Is this the right approach or is there a more elegant way?
Question: How do I determine the ratio for a split?
Question: How do I recognize a clone reference? ("Menu => Editor => Clone")
The plugin must run in an RCA that is still based on Eclipse 4.8 and supports the Eclipse 3.x API.

Eclipse RCP application Active Title Bar

I am working in an RCP application similar to Eclipse where the user can navigate in Project Explorer tree and opens any file in the Editor
i am Setting the RCP application title in a class which extends "WorkbenchWindowAdvisor" as the following:
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setTitle("My RCP Application title");
But what i need to show up in title bar the perspective name and the opened file path like in normal eclipse:
any suggestions
Thanks
This is requires listening to a lot of events in your WorkbenchWindowAdvisor.
In the preWindowOpen method you need to add listeners for:
Page activation and closing using configurer.getWindow().addPageListener(listener) The pageActivated and pageClosed listener methods need to update the title.
Perspective changes using configurer.getWindow().addPerspectiveListener(listener). The perspectiveActivated, perspectiveSavedAs, perspectiveDeactivated methods need to update the title.
Part activations using configurer.getWindow().getPartService().addPartListener(listener). This need to use an IPartListener2. The partActivated, partBroughtToTop, partClosed, partHidden, partVisible methods need to update the title.
You get the open file path from the active editor:
IWorkbenchPage currentPage = configurer.getWindow().getActivePage();
IEditorPart activeEditor = currentPage.getActiveEditor();
if (activeEditor != null) {
path = activeEditor.getTitleToolTip();
}
and the perspective name:
IPerspectiveDescriptor persp = currentPage.getPerspective();
if (persp != null) {
label = persp.getLabel();
}
The full, even more complex, code for this is in org.eclipse.ui.internal.ide.application.IDEWorkbenchWindowAdvisor

How to detect modified files and save in eclipse?

I have a plugin which work with special json config files,and it can be runned by the Run button.
I make changes to the config without saving the file.
If I the hit the run button to try it out, plugin will use the config as it is on disk...
It would be nice if the tool would either save all files before running -
or at least give a warning message that tells me that some files are not saved..
How can i detect modified files and get a list of this files?
How can i save changes in this files?
You can look at all the editors which are marked 'dirty' (have modified data) using something like:
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
for (IWorkbenchWindow window : windows)
{
IWorkbenchPage[] pages = window.getPages();
for (IWorkbenchPage page : pages)
{
IEditorPart[] editors = page.getDirtyEditors();
for (IEditorPart editor : editors)
{
IEditorInput input = editor.getEditorInput();
if (input instanceof FileEditorInput)
{
IFile file = ((FileEditorInput)input).getFile();
// TODO ... deal with file
}
}
}
}
Note: Not all editors use FileEditorInput so you might need more code if the editor is using a different input.
When you have an IEditorPart you can call the
public void doSave(IProgressMonitor monitor);
method to get the editor to save its data - but you should confirm with the user first that they want to do this.

Eclipse plugin - accessing the editor

So, I’m currently developing a plugin for the eclipse IDE. In a nutshell, the plugin is a collaborative real time code editor where the editor is eclipse (which is something like Google documents but with the code and on eclipse). Meaning that when I install the plugin, I would be able to connect -using my Gmail account- eclipse to the partner’s eclipse. And when I start coding on my machine, my partner would be seeing what I write and vice versa.
The problem I’m currently facing is accessing eclipse’s editor. For example, I have to monitor all the changes in the active document so that every time a change happens, the other partner’s IDE would be notified with this change.
I found and read about the IDcoumentProvider, IDocument and IEditorInput classes and they’re somehow connected but I can’t understand this connection or how to use it. So if someone can explain this connection I would really appreciate it. Also if there is another way to achieve my goal?
You can access the IEditorPart via the IWorkbenchPage.
IEditorPart editor = ((IWorkbenchPage) PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()).getActiveEditor();
From there, you have access to various other classes, including the editor's IEditorInput, the File loaded by that editor, or the underlying GUI Control element. (Note that depending on the kind of editor (text files, diagram, etc.) you may have to cast to different classes.)
FileEditorInput input = (FileEditorInput) editor.getEditorInput();
StyledText editorControl = ((StyledText) editor.getAdapter(Control.class));
String path = input.getFile().getRawLocationURI().getRawPath();
Now, you can add a listener to the Control, e.g. a KeyAdapter for monitoring all key strokes occurring in the respective editor.
editorControl.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
System.out.println("Editing in file " + path);
}
});
Or, if monitoring all key strokes is too much, you can register an IPropertyListener to the editor. This listener will e.g. be notified whenever the editor gets 'dirty' or when it is saved. The meaning of propId can be found in IWorkbenchPartConstants.
editor.addPropertyListener(new IPropertyListener() {
#Override
public void propertyChanged(Object source, int propId) {
if (propId == IWorkbenchPartConstants.PROP_DIRTY) {
System.out.println("'Dirty' Property Changed");
}
}
});

Take action on NetBean's editor's window closing

We are using NetBeans Platform 7.0.1, and have implemented support for a new language using this (now “obsolete”) tutorial.
Since all our contents are stored in a database, and not on files, we open them like this:
FileSystem fs = FileUtil.createMemoryFileSystem();
FileObject fo = fs.getRoot().createData(fileName, fileExtension);
… write contents from database to `fo` ….
DataObject data = MyMultiDataObject.find(fo);
EditorCookie.Observable cookie = data.getCookie(EditorCookie.Observable.class);
cookie.open();
… forces undock of editor window …
And, in our layer.xml, have added a custom button to Save that sends the content back to the database.
However, when the user closes the file (by either closing the tab or the window), we haven’t figured a way of saving it.
Adding a PropertyChangeListener to the Cookie and watching for PROP_DOCUMENT (and newValue() == null) seems to do the trick for when the window is closed. But how does one get the return value from the confirmation window (I’m referring to when the file is closed after changes, the message File xxx.xxx is modified. Save it?)?
Well, it seems we've been approaching the problem in the wrong way.
Since we are opening the file in-memory, it was suggested in the netbeans-dev list that we should listen for changes in the file itself, by using
fo.addFileChangeListener(new CustomFileChangeListener());
public class CustomFileChangeListener implements FileChangeListener {
#Override
public void fileChanged(FileEvent fe) {
... file has been saved in the editor, sync with database ...
}
}
And keep it synchronized that way, taking advantage of the built-in NetBeans Platform "save" functionality.

Categories

Resources