Is it possible to override default code formatt of Eclipse - java

I have java code style formatter of Eclipse (for that matter it can be formatter of the other language also). When I import he format then it is saved in the workspace and that is applicable for that workspace.
I want code format/style imported and saved in the Eclipse setting itself so that this Eclipse used by anyone gets the format whatever workspace they use.
Is it possible to override the default format? Do I have to write my own plugin?
Thanks,

I can suggest the following in the Java perspective, Eclipse version Indigo...
click Window -> Preferences
expand Java -> Code Style
click Formatter
This will get you to the Formatter screen below. Follow the link on the top right
and see if this provides you with what you are requesting.
Configure Project Specific Settings screen

I wanted settings to be present in the eclipse packaging itself so that these settings (code format, template, spacing strategy etc.) are not repeated at every project and workspace level.
I got a way (at least it works on Linux with neon version). Here are the steps
Open eclipse with some workspace location
Apply all the settings like code formatting, template, Editor
preferences and all other preferred settings present in Window ->
Preferences Press OK. it will save the preferences.
Go to File -> Export -> General -> Preferences and press Next
Make sure Export all is checked and then provide the To
Preference File location and then Click Finish
Now open the exported file in you favorite editor
You will observe it is like a properties file with each property prefixed with /instance/ or /configuration/ (these are preference scopes). Remove all these prefixes (after that all the properties will look like org.* or net.* etc.). You will also see some workspace path related properties are present in this file, either remove them or change to appropriate one.
Save this file with some name say my-eclipse-preference.epf and copy
this file to your eclipse location (where eclipse start file is
present, say ECLIPSE_HOME)
Open $ECLIPSE_HOME/configuration/config.ini and add a line with
the property puluginCoustomization=my-eclipse-preference.epf at the end and save config.ini.
Restart the eclipse, you will find your settings are present even if you open with empty/new workspace. If you are starting from terminal then got to
the ECLIPSE_HOME directory first and then start.
If you want to start from the terminal but you want to avoid going
to the directory to start (i.e. starting from any location) then you should store the file my-eclipse-preference.epf at some standard location and have the value of puluginCoustomization with full path in config.ini.
Hope this helps.

Related

Eclipse automatically opening some files in system editor

Was finding this problem with Eclipse(Kepler) over the last week, so downloaded Eclipse (Luna) and checked out the relevent project into an entirely new workspace.
I'm using *.str files for a particular project (they are standard editor files anyway - short for 'string'). But Eclipse wants to open such files as macVim files:
I have NO file associations set for *.str files:
...althought I do use macVim to open them in the underlying operating system. So I change the system editor to TextEdit and Eclipse happily now wants them to open in TextEdit - but I want to open them in the eclipse editor! (appart from anything else, I've got to develop an editor plugin for *.str files.
How can I convince Eclipse that it can open *.str in it's own editors without having to resort to the system editor?
EDIT - follow the answers - my understanding is this. If Eclipse doens't know what the file is, it will ask the system. If you ask it to open in a particular editor it will overlay that information on that particular file, and to make it happen in general (and change the icon) you need to add a file association.
You can add a a File Associations for '*.str' and set the Associated Editor to 'Text Editor'. Eclipse will use this for files that you have not yet opened.
For files that you have already opened Eclipse will be remembering the editor you last used, so for these right click on the file and use 'Open With > Text Editor'. You should only have to do this once.
Right click on the file, select open with, and there should be three options (Text Editor/System Editor/Default). Selecting text editor should associate it with eclipse (from within eclipse only) in future.

Eclipse: Import+Export actions in "Save Actions" option

It is possible to Import+Export Clean Up setting as xml file at Preferences->Java->Code Style->Clean Up.
I want these setting to be applied on the code each time I save a file.
Unfortunately, when I go to Preferences->Java->Editor->Save Actions I don't have an option to Import+Export Clean Up settings (even though I have the option to configure them manually).
Is there a way to Import+Export these settings?
Clarification: My goal is to create Clean Up setting xml that I can share with my team members so that if any of us saves a file these Clean Up settings will be automatically applied on the code.
I am using Eclipse 4.3 Kepler.
These settings are included in the Export Preferences but only when you specify 'Export all'.
There does not seem to be any export that just exports these settings.
The settings are stored in the workspace in the file .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs but again they are mixed in with lots of other settings.
Open the old ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs" workspace file.
Copy all "sp_cleanup.*" properties from the old workspace file and paste into the new one (replacing the old same properties).
I just ran some diffs with the generated preferences file (.epf) and identified the specific entries that need to be added to your preferences file to enable Post Save Actions
They are as follows (Tested in Eclipse Mars.1):
/instance/org.eclipse.jdt.ui/sp_cleanup.format_source_code=true
/instance/org.eclipse.jdt.ui/sp_cleanup.organize_imports=true
/instance/org.eclipse.jdt.ui/editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
/instance/org.eclipse.jdt.ui/sp_cleanup.on_save_use_additional_actions=false
They will translate into:
Window > Preferences > Java > Editor > Save Actions
Perform the selected actions on save (checked)
Format source code (checked)
Format edited lines (unchecked)
Organize imports (checked)
Additional Actions (unchecked)

Share Eclipse config preferences

I am new to Java and I am using Eclipse for writing code and when I began I made a lot of adjustments to make Eclipse better to me, like changing the font and font size, toggling line numbers apparition and all these things.
I wanna know if is there a way for me to share these preferences, in a file or directory for example, so when I need to use another computer I can just enable my preferences and not have to do all manual tweaks again.
Thanks in advance!
It is pity that eclipse stores its preferences in the workspace so when switching to another one all the settings are lost.
You can first Export the preferences from the original workspace using File -> Export -> Preferences file. Then after the switch they can be imported back using File -> Import -> Preferences.
In case of another computer the only solution is to save the preferences file to a shared folder or use some service such as Dropbox to have the file under the auto-sync.

Viewing Java documentation with Eclipse on Mac OS X

I'm trying to accomplish a very basic task and somehow can't seem to find how... I would like to have my Eclipse environment set in a way that I can get help and documentation on any standard class/method in the JDK, like I used to do a few years ago with Eclipse on Windows, where having the cursor on a class name (e.g. PrintWriter), and clicking Ctrl+F2 would open up the Java documentation for the PrintWriter class.
Here's my environment:
Running OS X version 10.6.6.
Just downloaded and installed the Java Developer Package for Mac OS X 10.6 Update 4 from connect.apple.com
I have Eclipse Galileo installed.
Under /Library/Java/JavaVirtualMachines I have a file named 1.6.0_24-b07-334.jdk, which seems to be the new JDK I just installed. However, it's a single file, not expanded into directories and files. Right-clicking it and selecting "Show Package Content" shows me that deep inside it contains the files docs.jar and src.jar. However, not sure what I should be doing with the 1.6.0_24-b07-334.jdk file - should I leave it as is, or perhaps expand it to a full directory structure?
Under Eclipse Preferences, Java/Installed JREs I have JVM 1.6.0 (MacOS X Default) selected. However, the path points to /System/Library... and not to /Library...
Anyway, in Eclipse, putting the mouse over a class name, I get a brown dialog with a short explanation of the class. However, I don't know how to open up the full java documentation of the class. Also couldn't find anywhere in Eclipse a place to indicate where to take the java documentation from, nor which hotkey would bring the java documentation up.
I apologize for the many details, I'm just assuming they may be necessary to get a good answer.
Thanks!
/A
I've had the same problem — previously I had been able access Java documentation and source directly in Eclipse, but that disappeared after one of the Java updates from Apple last year.
My solution was to manually add the source and Javadoc to the JRE definition in Eclipse. Here's how:
Navigate to Preferences --> Java --> Installed JREs
Select your preferred JRE and click Edit...
Select the classes.jar library (should be the first one in the list) and click Javadoc Location...
Click Javadoc in archive
Set Archive path to /Library/Java/JavaVirtualMachines/1.6.0_24-b07-334.jdk/Contents/Home/docs.jar
Set Path within archive to docs/api
Click OK
now, optionally, you can set the source code too...
With classes.jar still selected, click Source Attachment...
Set Location path to /Library/Java/JavaVirtualMachines/1.6.0_24-b07-334.jdk/Contents/Home/src.jar
Click OK
In fact that 1.6.0_24-b07-334.jdk package is actually just a folder; it is treated as a single file by OS X but you can access its contents directly if you know the right path. What if you don't know what path the enter? You can browse the contents from Eclipse if you know this trick:
While attaching source code or Javadoc in Eclipse, click on the External File... or External Folder... button etc to bring up a Finder window
Navigate to your Java JDK folder (usually /Library/Java/JavaVirtualMachines)
You should see a list of your JDKs, for example 1.6.0_22-b04-307.jdk and 1.6.0_24-b07-334.jdk)
Press / on your keyboard to bring up the Go to the folder dialog box
Delete the / from the dialog box, and start typing the name of JDK you want to navigate. You can press Tab to autocomplete the name if you like.
Click Go
You are now browsing the contents of the JDK package; you can now click through to Contents/Home etc where you should see appledocs.jar, docs.jar, src.jar and other goodies.
For everyone finding this StackOverflow-Post and not finding the src.jar:
Apple removed it from the SDK and it's not supplied by default, however you can download it from Apple self at:
http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wo/5.1.17.2.1.3.3.1.0.1.1.0.3.9.3.3.1
http://danbim.blogspot.com/2011/01/java-for-mac-os-x-106-update-3-and.html
P.S. Apple account is required (free registration)
To bring up the Java documentation in Mac, move your mouse to the desired class, then click SHIFT+FN+F2.
By the way, you can find out the shortcut key from Preferences -> General -> Keys -> type "Open Attached Javadoc". Here's my screenshot:-
Under /Library/Java/JavaVirtualMachines I have a file named 1.6.0_24-b07-334.jdk, which seems to be the new JDK I just installed. However, it's a single file, not expanded into directories and files. Right-clicking it and selecting "Show Package Content" shows me that deep inside it contains the files docs.jar and src.jar. However, not sure what I should be doing with the 1.6.0_24-b07-334.jdk file - should I leave it as is, or perhaps expand it to a full directory structure?
From what I remember from Mac OS (I had to use one two years ago for some months) I think this is not really a "single file", it only looks so in the file manager application. Look if you can navigate inside it in the file-chooser dialog of eclipse where you can select the docs.jar for your documentation.
I was having a similar problem, and was having trouble accessing the Apple Developer page to download a local copy of the docs.jar. I went to Preferences > Java > Installed JREs > Edit, and then looked at the "Javadoc Location..." value for the jars. The Javadoc URI was still set to "http://java.sun.com/javase/6/docs/api/". When I changed it to "http://docs.oracle.com/javase/6/docs/api/" the tooltips started showing up again. Of course, this will only work when you are online.

Where to tweak an Eclipse to change the default settings used when creating a new Workspace?

We use Eclipse with projects in CVS. It has proven to be the simplest to create a new workspace when having to deal with another branch or application, and then use Team -> Import project set to get all the needed projects from CVS.
Unfortunately, I then have to do the following each and every time:
Change text font to Consolas 11 pt
Disable spell checking in text editors
Run everything in the background
plus some more of the same.
I'd like to change the standard values once and for all in the Eclipse distribution files after having unzipped the distribution (Windows). Where are these defaults located inside Eclipse?
EDIT: For now we just have a preference file which must be read in. An extra step, but works...
EDIT 2014: I've ended up creating a workspace with the settings I want, and then creating a new copy everytime I need a new one. Also handles Maven Central information etc. Accepted the oldest answer saying essentially this.
You can export your settings from a workspace and import them into any other (this basically does what VonC's answer says, but with some measure of error checking).
To do so, in the source workspace select File->Export...->General->Preferences, then select Export All and enter a file to export to, then Finish.
You can then import the preferences into any workspace by doing File->Import...->General->Preferences, browse to the preferences file and hitting Finish.
I have created a clean workspace with all settings i want to have. This workspace i have copied into a save folder i will never delete ;)
When i want to create a workspace for a new project, i copy the confugured workspace and thats it.
I have than configured the svn repository path, code format (you also can import preferences in eclipse for this), view configuration and so on.
Try checking:
<workspace>\.metadata\.plugins\org.eclipse.core.runtime\.settings
Some .prefs files could be interesting to copy from one workspace to the next.
(like org.eclipse.team.cvs.ui.prefs which contains any non-default value for CVS settings)
I want to fix the defaults inside Eclipse so the workspace is created with the defaults I want every time.
Hard core solution:
Change the .options file within <eclipse-SDK-3.5-win32>\eclipse\plugins\org.eclipse.team.cvs.core_3.3.200.I20090430-0408.jar (that is the name I have for eclipse3.5)
That is the file with the default values, at least for CVS.
Here's what I do:
Start up Eclipse
Without importing or starting any projects, edit the workspace and make all the config changes you want
Open up the workspace in windows explorer and create a copy of the folder.
Then, any time you wish to use it, copy this folder to your new workspace folder location - you may need to create the new workspace folder first (and definitely call it something different.)
This is what I do anyway. Yes, it's very dirty but it does get what I want pretty quickly!
When importing preferences (Rich Seller's approach above), especially those that were created by someone else, make sure you backup your Eclipse environment first. That's easy, since it's portable - just copy it to a temporary location.
I've totally hosed my Eclipse environment importing preferences in the past, and importing my own preferences, which I exported just prior to importing, did not fix my issues.
Fix the defaults inside Eclipse so the workspace is created with the defaults, If you want every time suggestions, then
Go to eclipse extract path -eclipse\configuration\.settings -> Edit -org.eclipse.ui.ide ->
Change value to true: SHOW_WORKSPACE_SELECTION_DIALOG=true
Similarly in this file you can directly change other default settings.
I find a way to do that:
open <eclipse>\plugins\org.eclipse.cpp.package.cpp_1.4.2.201210131-1456\plugin_customization.ini
and add
folding
org.eclipse.cdt.ui/editor_folding_enabled=true
org.eclipse.cdt.ui/editor_folding_preprocessor_enabled=true
then new project will enable folding by default.
So
You need to know where your prefer locate, in my case, folding is under org.eclipse.cdt.ui, you can try on an project then check <workspace>\.metadata\.plugins\org.eclipse.core.runtime\.settings to find it
"org.eclipse.cpp.package.cpp_1.4.2.201210131-1456" should be variable depends on what version you use. whatever it's, you should find "plugin_customization.ini "

Categories