How to Import Preferences (.epf) in eclipse via command prompt? - eclipse

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).

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

go to file in eclipse by using location datatype

When clicking on a location type in the output window of eclipse, you can go to that file (location).
I would like to be able to trigger this with a method in rascal.
So to be clear, I have the location of a java method, I would like to trigger eclipse to focus on this file through rascal.
Take a look at the util::Editors module. It contains an edit function that opens any file you pass it, with the relevant editor, with optional highlights.
Note, that if you have a logical location like java+method://... you will have to lookup the actual physical location of the method in the m3 model using IO:resolveLocation, and use that. For example:
rascal>import IO;
ok
rascal>resolveLocation(|java+method:///io/usethesource/impulse/language/LanguageRegistry/IMPFileEditorMapping/setTheDefaultEditor(org.eclipse.ui.IEditorDescriptor)|)
loc: |project://impulse/src/io/usethesource/impulse/language/LanguageRegistry.java|(15638,134,<433,8>,<436,9>)
rascal>openEditor(resolveLocation(|java+method:///io/usethesource/impulse/language/LanguageRegistry/IMPFileEditorMapping/setTheDefaultEditor(org.eclipse.ui.IEditorDescriptor)|))
You are looking for openEditor in util::ValueUI.
First import util::ValueUI;, then try
openEditor(|project://rascal/src/org/rascalmpl/library/Map.rsc|);
and an editor for the Map module will open.

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).

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

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

OnSave actions in NetBeans 6.9

Is there any way to tell NetBeans to execute a specific action when saving a file? e.g. removing unused imports while saving a source file?
This was an interesting question... as I believe you would have to write a custom NetBeans plugin to do what you're wanting (the functionality isn't available out-of-the-box), and I've been looking for an excuse to explore NetBeans plugin development.
However, after spending a couple of hours reading tutorials and crawling through the javadocs... it's become clear that this subject is quite a big bite to chew, and probably way more involved than you're wanting.
I think the BEST suggestion is forget about removing unused imports at save-time, and instead perform this step at build-time. NetBeans offers great integration with Ant and/or Maven (for build purposes it's basically just a GUI wrapper around those tools), and there are a number of Ant tasks out there that can do what you're wanting. See:
http://ant.apache.org/external.html
(look for the "CleanImports" and "Importscrubber" tasks)
If your NetBeans project(s) are Maven-based, then you can always plug in one of these Ant tasks there using the AntRun plugin for Maven.
If you're not used to dealing with Ant or Maven directly in NetBeans, then just switch to the "Files" tab and look at your project's root directory. If its a Maven project, the build script will be named pom.xml. Otherwise, your project will generally be Ant-based and the build script will be named build.xml. The documentation for these items above should make it fairly clear how to move forward from there.
I notice that those two Ant tasks haven't been updated in awhile, so if you run into issues you might want to check out the very popular and up-to-date PMD system, which has its own documentation for integrating with NetBeans. However, the issue there is PMD is primarily for generating reports... I don't know if it can be used to actually take action and change source files.
Not exactly an answer to your question, but note that NB 7.1 lets you fix imports on the whole project at once: http://wiki.netbeans.org/NewAndNoteworthyNB71#Organize_Imports_Hint
This is not a good practice and NetBeans does not support it.
I resurrect this topic.
Well this code code is tested with Netbeans 7.4.
here I'm overriding the default save action in the actionPerformed method.
If you choose to do this by yourself create a new Action using the wizard then call the save action inside actionPerformed method.
package yourpackage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
#ActionID(
category = "File",
id = "BZ.SaveAction"
)
#ActionRegistration(
iconBase = "BZ/Save.png",
displayName = "#CTL_SaveAction"
)
#ActionReferences({
#ActionReference(path = "Menu/File", position = 750),
#ActionReference(path = "Toolbars/File", position = 0),
#ActionReference(path = "Shortcuts", name = "D-S")
})
#Messages("CTL_SaveAction=Save")
public final class SaveAction implements ActionListener {
org.openide.actions.SaveAction sa = org.openide.util.actions.CallbackSystemAction.get(org.openide.actions.SaveAction.class);
#Override
public void actionPerformed(ActionEvent e) {
// custom code
JOptionPane.showMessageDialog(null, "custum message ");
sa.performAction();
}
}
Goto Tools-> Options select Editor there select On Save Tab now select Java from drop down menu. So, now select Organize Imports option. Hope this will help you.