Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to load a formatter profile to a workspace configuration
How to load a formatter profile to a workspace configuration [message #253575] Wed, 28 May 2008 16:07 Go to next message
john is currently offline johnFriend
Messages: 10
Registered: July 2009
Junior Member
Hi,

I'm developing a plug-in for eclipse. I am trying to find a way to load
"Code Style" profile into the workspace configuration at start-up. I have
searched the web and looked through the api, but cannot find the packages
for the tools.

I know how to load a profile through the GUI, I'd like to replace the
formatter profile for projects using the plug-in I'm developing.

Thank you.
Re: How to load a formatter profile to a workspace configuration [message #253583 is a reply to message #253575] Thu, 29 May 2008 08:55 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3801
Registered: July 2009
Senior Member
John wrote:
> Hi,
>
> I'm developing a plug-in for eclipse. I am trying to find a way to
> load "Code Style" profile into the workspace configuration at
> start-up. I have searched the web and looked through the api, but
> cannot find the packages for the tools.
>
> I know how to load a profile through the GUI, I'd like to replace the
> formatter profile for projects using the plug-in I'm developing.
Set the code style / formatter settings for your project and then store
it in the repository.

Dani
>
> Thank you.
>
Re: How to load a formatter profile to a workspace configuration [message #253639 is a reply to message #253583] Fri, 30 May 2008 17:50 Go to previous messageGo to next message
john is currently offline johnFriend
Messages: 10
Registered: July 2009
Junior Member
Thank you, but I think I was not clear enough.

In the plugin I'm working on, I have a hook that runs a job during
eclipse's startup. I'm trying to find a way to review the current
formatter profile configuration. After reviewing the current profile, I'd
like this plugin to create a new generate a new profile from an xml file
bundled with the plugin. I'd like to load the profile into eclipse's
preferences at run time, and apply the new profile once that's been
accomplished.

I tried using JavaCore.getOptions() and JavaCore.getDefaultOptions to
review the current profile loaded into eclipse. Then I tried using
JavaCore.setOptions(Hashmap options) to load the desired options into the
environment. When I've done this, JavaCore believes that the options have
been loaded, but the profile is not visible through the standard
preferences dialogue (preferences>Java>Code Style>formatter). So this is
not sufficient, I actually need to create a new profile and apply it
automatically at startup.

I tried grabbing the workspace preferences using
getInstancePreferences("org.eclipse.jdt.ui"). Note, I also try and grab
the another preference sheet.

Now, this second tactic is effective in retrieving the values of a custom
profile loaded into eclipse. However, trying to use Preferences
myNode.put(myNewPrefName, myNewPrefValue) does not appear successful.
That is to say, the "put" values are not actually loaded into the
preference store.

It may be that I am just being very foolish about this, and there's
something very easy to accomplish the task of having a plugin load
formatter preferences at eclipse startup. I hope I am, and that someone
will tell me about it.

thank you
Re: How to load a formatter profile to a workspace configuration [message #253673 is a reply to message #253639] Mon, 02 June 2008 13:22 Go to previous messageGo to next message
john is currently offline johnFriend
Messages: 10
Registered: July 2009
Junior Member
Ok, for the benefit of others:

To load a custom code style formatter into an eclipse workspace from a
plugin:

-create a new object referencing the eclipse workspace preferences, create
an instance of org.eclipse.core.runtime.preferences.InstanceScope
-use the getNode(String preferenceName) method to retrieve an instance of
Preferences representing that preference file.
//"org.eclipse.jdt.ui" is a useful one, for example.
//The actual preference files can be found in the workspace.
//They're located at:
//yourWorkspace/.metadata/.plugins/org.eclipse.core.runtime/ .settings

-load values into the Preferences object using put.
-load the values into memory using the "flush" function.

Now, you can tweak all the formatting rules by altering
"org.eclipse.jdt.core"
However, its necessary to then specify the name of this preference scheme
in "org.eclipse.jdt.core", including a name. To actually have the this
awesome new profile show up in eclipse preference, you need load a value
into the org.eclipse.jdt.ui.pref Preferences. You need to load the name
of the your new profile as "org.eclipse.jdt.ui.formatterprofiles".
Except, you need to load a String that's actually "_"+(String)yourName

Yeah, so, if you flush that, you can bundle and configure your own
"default" code style formatter into eclipse with your plugin. Yay.
Re: How to load a formatter profile to a workspace configuration [message #800050 is a reply to message #253673] Thu, 16 February 2012 16:21 Go to previous messageGo to next message
jerome bermond is currently offline jerome bermondFriend
Messages: 2
Registered: February 2012
Junior Member
Hello John,

What you did is exactly what I'm looking for !

Would it be possible you publish your code ?

Thanks from a newbie !
Jerome

Re: How to load a formatter profile to a workspace configuration [message #802108 is a reply to message #800050] Sun, 19 February 2012 12:15 Go to previous messageGo to next message
jerome bermond is currently offline jerome bermondFriend
Messages: 2
Registered: February 2012
Junior Member
Done, with Helios version !

1) Define your Code styles profiles (C and Java for me), for example: myCodeStyle_C and myCodeStyle_Java
2) Export Preferences in file: File/Export/General/Preferences, then Checkbox "Export all" + name your file + finish
3) Edit this last file,
- for "C" Code style:
search "/instance/org.eclipse.cdt.ui/org.eclipse.cdt.ui.formatterprofiles=" [RefA] is rest of line
search /instance/org.eclipse.cdt.ui/formatter_profile=" [RefB] is the rest of line
NB: [RefB] starts with "_"
Copy/Paste [RefA] into an editor, then replace "\=" by "=", '"' by '\"', "\r\n" by nothing
prefix with cs_c_xml=", sufix with "; , then you should get something like this:

cs_c_xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><profiles version=\"1\"><profile kind=\"CodeFormatterProfile\" name=\"myCodeStyle_C\" version=\"1\"><setting id=\"org.eclipse.cdt.core.formatter.insert_space_before_opening_paren_in_method_declaration\" value=\"do not insert\"/> ... </profile></profiles>"

in the plugin code:

IEclipsePreferences rootCS = Platform.getPreferencesService().getRootNode();
String qualifierCS = "";
qualifierCS = "org.eclipse.cdt.ui";

cs_c_xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><profiles version=\"1\"><profile kind=\"CodeFormatterProfile\" name=\"myCodeStyle_C\" version=\"1\"><setting id=\"org.eclipse.cdt.core.formatter.insert_space_before_opening_paren_in_method_declaration\" value=\"do not insert\"/> ... </profile></profiles>";

rootCS.node(InstanceScope.SCOPE).node(qualifierCS).put("org.eclipse.cdt.ui.formatterprofiles", cs_c_xml);

// /instance/org.eclipse.cdt.ui/formatter_profile=_myCodeStyle_C
rootCS.node(InstanceScope.SCOPE).node(qualifierCS).put("formatter_profile", "_myCodeStyle_C");

qualifierCS = "org.eclipse.cdt.core";
// for each item of cs_c_xml starting with setting id=[refC].[refD]=
// example : <setting id=\"org.eclipse.cdt.core.formatter.insert_space_before_opening_paren_in_method_declaration\" value=\"do not insert\"/>
rootCS.node(InstanceScope.SCOPE).node(qualifierCS).put("org.eclipse.cdt.core.formatter.insert_space_before_opening_paren_in_method_declaration", "do not insert");
...

- for "Java" Code style, do the same as above with :
search "/instance/org.eclipse.jdt.ui/org.eclipse.jdt.ui.formatterprofiles=" [RefA] is rest of line
search /instance/org.eclipse.jdt.ui/formatter_profile=" [RefB] is the rest of line

qualifierCS = "org.eclipse.jdt.ui";
qualifierCS = "org.eclipse.jdt.core";



Hope it can help, Thanks John
Regards, Jerome

[Updated on: Mon, 20 February 2012 09:13]

Report message to a moderator

Re: How to load a formatter profile to a workspace configuration [message #1781206 is a reply to message #802108] Fri, 02 February 2018 18:36 Go to previous messageGo to next message
Gaurav Chodwadia is currently offline Gaurav ChodwadiaFriend
Messages: 3
Registered: January 2018
Junior Member
If I want to do this to load certain preferences when eclipse starts, where do I put this code.

Is a class implementing IStartup overriding the method earlyStartup() is the correct place?

Thanks
Re: How to load a formatter profile to a workspace configuration [message #1781217 is a reply to message #1781206] Sat, 03 February 2018 08:28 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29482
Registered: July 2009
Senior Member
With Oomph you can record your preferences, including JDT's profiles; Oomph is installed in all Eclipse packages.

https://www.eclipse.org/community/eclipse_newsletter/2014/november/article2.php

When recorded globally in your user.setup, those recorded preferences will be applied any time you start any Eclipse IDE. So no need to write any code.

Oomph does a whole lot more than just preference recording. You can completely automate the setup of a development environment for working with your specific project, including installing the right tools, setting the appropriate preference, clone the appropriate Git repositories, importing the projects from the repos, and so on:

https://wiki.eclipse.org/Eclipse_Oomph_Authoring
Re: How to load a formatter profile to a workspace configuration [message #1781580 is a reply to message #1781217] Thu, 08 February 2018 22:13 Go to previous messageGo to next message
Gaurav Chodwadia is currently offline Gaurav ChodwadiaFriend
Messages: 3
Registered: January 2018
Junior Member
Thanks for your response..!!

Let me elaborate the use case.

I want to repackage Eclipse with different plugins and distribute it to a large dev team.

One of the requirement is that when a developer starts the eclipse app, certain code style should be pre-loaded / pre selected just like Eclipse's default style is selected normally.

For that, I am writing a plugin.

I don't understand where exactly should I put the code.

Thanks
Re: How to load a formatter profile to a workspace configuration [message #1781592 is a reply to message #1781580] Fri, 09 February 2018 05:33 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 29482
Registered: July 2009
Senior Member
Oomph can also be used to create an installer for different packages, but if you want to ship out pre-canned zips/tars, you can use a customization file: http://blog.vogella.com/2012/08/07/eclipse-papercut-9-default-preference-settings-via-plugin_customization-ini-type-filter-example/
Previous Topic:Modular project: How add dependency to other project only on runtime to solve cyclic dependency?
Next Topic:Eclipse seems to stop emitting warnings at a point in a very long source file
Goto Forum:
  


Current Time: Fri Aug 17 15:18:52 GMT 2018

Powered by FUDForum. Page generated in 0.02574 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top