How to persist the editor input in e4 - eclipse

In eclipse 3 there was the EditorInput which managed the persistence of editors and its input, but how to manage this with eclipse e4 where editors and views pretty much the same thing ...
Is there any recommended way of doing this or do I have to implemented some kind of editor input registry where I map all editor input objects to some kind of unique key and then after restarting the application I retrieve the editor input object from this registry via the key?
I've seen that I can only put strings into the persisted state of a MPart ...

The getPersistedState() method of any MApplicationElement (which include MPart) returns a Map where you can store any string data that you want to be persisted.
If you want to save objects you will have to use the state location for your plugin in the workspace .metadata. Get the location of that with:
IPath stateLoc = Platform.getStateLocation(bundle);
The path will usually be .metadata/.plugins/<your plugin id>. You can put anything you like it this area, but it is up to you to manage it.
You can use the #Persist annotation to get a method run when a part needs to be persisted:
#Persist
void persist()
{
... save data
}
The persisted state data is saved in the workspace .metadata and only cleared if you specify the -clearPersistedState option when starting your RCP.

Related

How do I programmatically set the length of Most Recent Used files in Eclipse

In Eclipse, I'm aware of the Preference setting for the number of recently opened files to offer:
For users of my RCP application I'd like to change the default length from 4 to 10.
I'm aware of the PreferenceManager, and can navigate to the correct node using this:
IPreferenceNode editorPrefs = preferenceManager.find
("/org.eclipse.ui.preferencePages.Workbench/org.eclipse.ui.preferencePages.Editors");
But, once I've found the node, I can't see how to access the specific property, in order to modify a value.
Anyone one done this before? Any tips?
Alternatively, I'm happy to do it via extension-point, but I couldn't get even this far via that mechanism.
This preference is set in the preferences for the org.eclipse.ui.workbench plugin. You can access this using ScopedPreferenceStore
IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, "org.eclipse.ui.workbench");
The key for recent files is RECENT_FILES so:
store.setValue("RECENT_FILES", value);
You may need to call the save() method to store the changes.
Note: it should also be possible [1] to update the preference from the .ini file. But it didn't work for me.
[1] https://bugs.eclipse.org/bugs/show_bug.cgi?id=128411#c2

Retrieving arbitrary Eclipse preferences programmatically

I am writing a plugin for Pig files. I would like to retrieve the Eclipse "file associations" preference - the one under General -> Editors -> File Associations -> File Types / Associated Editors.
Once I have this preference, my plugin can know which file types are being used, and act accordingly when iterating over the workspace files (in searches and the like).
I couldn't find a "directory" of preferences anywhere, nor an API that I could iterate over until I found it. Searching the file system of my workspace didn't seem to work either - possibly the preferences are being held in a binary format.
1) What is the key for retrieving this preference from the PreferenceStore?
2) What is the best way, in general, for finding the key for a given preference?
Use
IFileEditorMapping[] mapping = PlatformUI.getWorkbench().getEditorRegistry().getFileEditorMappings();
to get the mappings between file types and their supported editors. Look at this javadoc to see everything you could ever want to know about the mappings
I'll try to give you some hint, someone may have a better solution :
1 : id org.eclipse.ui.preferencePages.ContentTypes
2 : Use the Plug-ins Spy Press Alt-Shift-F1 on the desire page/widget on eclipse, it will display contextual informations
There is no overall API which will give you all the preferences.
Many preferences are stored in the workspace .metadata/.plugins/org.eclipse.core.runtime/.settings directory in 'plugin-id.prefs' files (Java Property file format). You can access these with
IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode("plugin id");
String value = prefs.get("preference id", defaultValue);
So you need to know the id of the plugin that owns the preference and the id of the preference itself. This information can be difficult to find and may involve reading the source of the preference page.
Other preferences are stored in the Eclipse configuration directory. Yet others are in some format only known to a particular plugin (but there is usually some API to access the information).

How to specify and read properties in an Eclipse plugin

I have an Eclipse product which uses my own plugins. I want to read some properties based on user inputs. I want to persist these properties on some user action, and read those properties back when required. Can this be achieved using some Eclipse API?
A more elaborate description of the above problem:
Say I have a property abc=xyz in a config file myconfig.ini. This property is read by the perspective during the bootstrapping process. During use of the perspective, some action sets this property to a new value xyz=def. Now, I should be able to save the new value in myconfig.ini. So next time the bootstrapping happens, the value of xyz is read as def instead of abc. However, I can also choose to manually set it to abc by editing the myconfig.ini file.
How would I manage myconfig.ini? Where should it exist within my eclipse product project?
What is the best API to manage reading, writing and updating properties in myconfig.ini?
You can use resource markers mechanism:
IMarker marker = file.createMarker(IMarker.MARKER);
marker.setAttribute(IMarker.MESSAGE, "blabla");
marker.setAttribute("attr", 5);
You can search for markers by using the findMarkers methods on IResource.
See FAQ also
You should consider using the apache configurations API http://commons.apache.org/proper/commons-configuration/
It can read and write INI files and if you want to change the configuration file type or add more configuration options you can simply configure it.
I would add a hidden directory to the workspace root e.g. ${WORKSPACE}/.productName/product.ini
and add an ISaveParticipant that ensures the ini file gets updated on shutdown.
You can get the Workspace using the ResourcesPlugin
IWorkspace workspace = ResourcesPlugin.getWorkspace();
and resolve it to an absolute path
IWorkspaceRoot wsRoot = workspace.getRoot();
IPath wsPath = wsRoot.getRawLocation();
IPath absoluteWsPath = wsPath.makeAbsolute();

JFace TreeViewer restore selection

I'm doing my first RCP example using the TreeViewer for directory browsing.
My work did start from the example http://www.ibm.com/developerworks/opensource/library/os-ecgui1/
For the TreeViewer of file entries I want to make the last selection persistent
to open the viewer by the same selection next time.
From the selected file I catched the path and store/retrieve this single string.
On reopening I traverse the ITreeContentProvider and find the corresponding
File node (and capture the nodes in between).
Next I use
window.getTree().setSelection(new StructuredSelection(target));
with target as a list of Files holding the path.
This works for the top level directories under C:
But restoring a deeper path fails. The next level is initially not
expanded and fails in the AbstractTreeViewer.setSelectionToWidget().
My feeling is that I do not handle the setSelection() parameter well
but I found no suitable example.
Do you have a simple example for me showing how to restore such
a path selection?
thanks in advance
Wolfgang R.
I've found it. The used example code has a small bug.
public class FileTreeContentProvider implements ITreeContentProvider
{
...
public Object getParent(Object element)
{
// wrong return ((File)element).getParent();
return ((File)element).getParentFile();
}

How to hijack files from clearcase from eclipse plugin

I am developing a component which generates code based on templates inside java class. The project use clearcase as SCM. After the code update, the files are in read-only state. If i am adding anything to any java class, i have to make it hijack and paste the source code templates inside the class. Let's suppose the jAutoDoc plugin which is used for adding comment. If user select a class, click on generate comment. The comment will not paste if the file is not in write mode.
Clearcase Plugin Vendor : IBM Rational.
Eclipse Version : 3.5
Please help. Is there any way to do hijack a file from java code?
Thanks in advance..
Thanks VonC.
For making a java file in write mode through eclipse JDT API. This method will set the preference "read only" of the resource to "false".
private static void setCompilationUnitWriteMode(ICompilationUnit cu) throws CoreException {
ResourceAttributes resourceAttributes = cu.getResource().getResourceAttributes();
if (resourceAttributes != null) {
// Setting Writemode true
resourceAttributes.setReadOnly(false);
cu.getResource().setResourceAttributes(resourceAttributes);
}
}
For Non Java Resource
First create the IFile object, set the preference "read only" of the resource to "false".
IFile file = path.getFile()
file.getFile().getResourceAttributes();
if (resourceAttributes != null) {
resourceAttributes.setReadOnly(false);
file.setResourceAttributes(resourceAttributes);
}
Hijacking files only means making them read - write through an OS-based operation. (for snapshot view only, not dynamic ones)
The question is though: do you need to version your classes completed by your plugin?
Because in that case, a cleartool checkout is more appropriate.
Otherwise, read write is enough, changing file attribute through java.