When creating an Eclipse-based Product, how can I set the default workspace? - eclipse

I've built an eclipse-based product, and I want to set the default workspace used by the Product. Currently, when the "Workspace Launcher" pops up for the first time, the default workspace location is just in the same directory as the Eclipse Product executable. I'd like to change to something like USER_HOME/myworkspace.
I can't seem to find a setting for this, but I'm guessing / hoping its a setting in my product_configuration.ini.
Cheers!

here is a more easy way
Once you have Eclipse up and running you can open Window-->Preferences-->Editors-->Startup and Shutdown. Click the first box that says Prompt for workspace on startup.
Or In your config.ini file ull've this line (or look in configuration.settings\org.eclipse.ui.ide.prefs)
//The default workspace location
Osgi.instance.area.default=#user.home/workspace
try changing this

Here is what needs to be done.
Wherever eclipse is installed go to the "configuration" directory and open the config.ini file in there.
Windows paths normally look like this:
C:\Users\Wilbert\Documents\Installers\Eclipse\eclipse
You will probably find something like this in the config.ini file:
osgi.instance.area.default=#user.home/workingspace
You need to change that to[Getting rid of the "#" and using forward slashes instead of back slash]:
osgi.instance.area.default=C:/Users/Wilbert/Documents/Programs/CS111B(Java)/Practice Programs/Projects
I just did it and it worked.

In your product (.product), go to the "Configuration" tab. Under the "Properties" section, add the property 'osgi.instance.area.default' with a value of '#user.home/myworkspace'. When you export your product, this property will be automatically added to your product's configuration file (just as ayush and Wilbert Sequeira were manually doing).
Note that only an exported product will use that configuration. When running your product in the Eclipse IDE, the workspace location will be overridden by your IDE's configurations.

The now-defunct Symbian WRT product did this. Looking through the sources, it seems to be done by a p2.inf file in the product package. See the screenshot below:
The first yellow arrow is for Windows and the second for Mac and Linux

In your .product file you can specify this as part of the programArgs element.
<programArgs>-data #user.home/MyWorkspace</programArgs>

Note that you can customize config.ini for individual platforms in the product descriptor (*.product) editor. But it never worked for me - hence that hack using P2. It may be working now as I was working with either 3.5 or early 3.6 when I last tried it.

Have a look at the following tutorial: http://hexapixel.com/2009/01/12/rcp-workspaces.
You said in your comment to the question "I just want to prepopulate the selector window with a certain default location".
You can do just that in PickWorkspaceDialog's (from the tutorial) getWorkspacePathSuggestion() method:
private String getWorkspacePathSuggestion() {
StringBuffer buf = new StringBuffer();
String uHome = System.getProperty("user.home");
if (uHome == null) {
uHome = "c:" + File.separator + "temp";
}
buf.append(uHome);
buf.append(File.separator);
buf.append("My App Name");
buf.append("_Workspace");
return buf.toString();
}
For this to work, you do have to create your own dialog though, and I can't tell if that's an option from your question...

In your .product file within the block add:
<property name="osgi.instance.area.default" value="#user.home/workspace" />
And when you build your product, the default config.ini will have this property set.
Details are in the Eclipse docs regarding the various variables.

To set the workspace location programmatically, use:
Platform.getInstanceLocation().set(new URL(...));

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 Import Preferences (.epf) in eclipse via command prompt?

I wanted to run a bat file in which it can import preferences from a location (which was exported manually). I searched for the command which would import preferences but, could not find any.
There is no existing code to do this. You would have to write an Eclipse headless application that does something like this:
IPreferencesService service = Platform.getPreferencesService();
IExportedPreferences prefs = service.readPreferences(file input stream);
// TODO create IPreferenceFilter array to filter what you want
service.applyPreferences(prefs, filter array);
See the source of the import preferences page org.eclipse.ui.internal.wizards.preferences.WizardPreferencesImportPage1 for an example.
Backstory: I was looking for something similar and, a few tabs back, I've stumbled on a "half-an-answer"/alternative solution. Even if the thread is old might still turn in handy ...
In this page the author talks about using -pluginCustomization parameter inside the eclipse.ini file
-pluginCustomization
plugin_customization.ini
-startup
plugins/org.eclipse....
The plugin_customization.ini file is similar to the *.epf file, same variables minus the /instance/ prefix (maybe because this way they are interpreted/applied at product(eclipse) level and not as per-workspace preferences).

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();

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.