Small snuggets for eclipse plugin development are small snippets of code useful in eclipse plugin development.
In eclipse windows that you see contains pages . Each of this page hosts a view or an editor .
Generally these editors are not hosted directly but indirectly via references ( aka proxies to the actual editor / views )
So to get reference to the actual editors / views from the pages use code like this :
IWorkbenchPage page = getPage();
IEditorReference[] editors = page.getEditorReferences();
Once you have the references you can get the actual hosted editor / view.
Editors in eclipse implement IEditorPart , so to get an editor some thing like this would work:
IEditorPart editor = editor.getEditor(true);
To reveal IJavaElement in the editor ( basically any java method , entity ) using JDT ( eclipse for java development is based on it )
JavaUI.revealInEditor(part , javaElement);
To add an action (aka tool bar item ) in the toolbar for your view something like this would work :
IActionBars actionBars = view.getViewSite().getActionBars();
IToolBarManager manager = actionBars.getToolBarManager();
manager.add(new MyToolBarItem());
Showing posts with label eclipse-plugin-development. Show all posts
Showing posts with label eclipse-plugin-development. Show all posts
Monday, March 17, 2008
Thursday, March 13, 2008
Eclipse Plugin Development : Programatically saving all open editors
In Eclipse editors are contained in WorkbenchWindow . To save all open editors one can call code like this
IWorkbench workbench = PlatformUI.getWorkbench();
return workbench.saveAllEditors(false);
This will get the current workbench and save all open editors.
IWorkbench workbench = PlatformUI.getWorkbench();
return workbench.saveAllEditors(false);
This will get the current workbench and save all open editors.
Monday, March 03, 2008
Eclipse : Adding nature to a project
In Eclipse plugin development terminology adding a nature to a project is like tagging the project with a specific tag . Generally natures are used to install various builders for the project. I will talk about builders in a later post . Lets look at some code and see how how natures are added to projects:
IProjectDescription projectDescription = project.getDescription();
description is a description of the project . It gives a lot of information including the entire list of
nature ids and build commands.
String[] ids = projectDescription.getNatureIds();
String[] newIds = new String[ids.length + 1];
System.arraycopy(ids,0,newIds,0,ids.length);
newIds[ids.length] = YOUR_NATURE_ID;
projectDescription.setNatureIds(newIds);
project.setDescription(projectDescription,null);
the remove nature id code is kind of similar .
To actually implement your nature , you need to contribute to the extension point
org.eclipse.core.resources.natures
Basically that boils down to adding something like this in your plugin.xml
id="yourNatureID"
name="Your Nature Name">
"id" here represents the id of the nature .
"name" represents the name of the nature .
"class" represents the class implements the IProjectNature .
the requires-nature here represents the nature id that is required for this nature to be successfully installed .
Next , finally the class that implements the IProjectNature interface.
public class YourProjectNature implements IProjectNature {
IProject project;
public YourProjectTestNature() {
}
public IProject getProject() {
return project;
}
public void setProject(IProject project) {
this.project= project;
}
}
IProjectDescription projectDescription = project.getDescription();
description is a description of the project . It gives a lot of information including the entire list of
nature ids and build commands.
String[] ids = projectDescription.getNatureIds();
String[] newIds = new String[ids.length + 1];
System.arraycopy(ids,0,newIds,0,ids.length);
newIds[ids.length] = YOUR_NATURE_ID;
projectDescription.setNatureIds(newIds);
project.setDescription(projectDescription,null);
the remove nature id code is kind of similar .
To actually implement your nature , you need to contribute to the extension point
org.eclipse.core.resources.natures
Basically that boils down to adding something like this in your plugin.xml
name="Your Nature Name">
"id" here represents the id of the nature .
"name" represents the name of the nature .
"class" represents the class implements the IProjectNature .
the requires-nature here represents the nature id that is required for this nature to be successfully installed .
Next , finally the class that implements the IProjectNature interface.
public class YourProjectNature implements IProjectNature {
IProject project;
public YourProjectTestNature() {
}
public IProject getProject() {
return project;
}
public void setProject(IProject project) {
this.project= project;
}
}
Thursday, January 31, 2008
Tweaking JDT : eclipse
To create a new Java Class/Interface using JDT you can use the following code :
IJavaproject prj;
IPackage package;
prj.createType(package, "your class body");
To find subclasses of a given type using JDT use :
IType type = prj.findType("class name");
ITypeHierarchy typeHierarchy = type.newTypeHierarchy(prj,new NullProgressMonitor());
IType[] subclasses = typeHierarchy.getAllSubtypes(prj);
To check if the given class aka IType is part of this project
IType type;
IResource resource = type.getUnderlyingResource();
boolean isPartOfClass = resource.getProject().equals(project.getProject());
IJavaproject prj;
IPackage package;
prj.createType(package, "your class body");
To find subclasses of a given type using JDT use :
IType type = prj.findType("class name");
ITypeHierarchy typeHierarchy = type.newTypeHierarchy(prj,new NullProgressMonitor());
IType[] subclasses = typeHierarchy.getAllSubtypes(prj);
To check if the given class aka IType is part of this project
IType type;
IResource resource = type.getUnderlyingResource();
boolean isPartOfClass = resource.getProject().equals(project.getProject());
Marker Resolutions for markers : Eclipse
In Eclipse it is possible to associate marker resolutions with markers . This is more commonly known as Quick Fixes also evoked by using the key combination Ctrl + 1.
You need to implement the extension point with id :
"org.eclipse.ui.markerResolution"
for the markerType attribute under that extension point specify the marker id
you want to associate this resolution with .
Then in the "class" attribute specify the class that implements the MarkerResolutionGenerator
You need to implement the extension point with id :
"org.eclipse.ui.markerResolution"
for the markerType attribute under that extension point specify the marker id
you want to associate this resolution with .
Then in the "class" attribute specify the class that implements the MarkerResolutionGenerator
Monday, January 28, 2008
Fnding markers in a workspace : eclipse
If you have a marker or you know a marker id :
let the id be : org.my.marker
then all markers of that type can be easily found . The way to found those markers is :
IWorkspaceRoot root = ResourcePlugin.getWorkspace().getRoot();
root.findMarkers("org.my.marker",false,IResource.DEPTH_INFINITE);
To create a marker :
getProject().createMarker("org.my.marker",false,IResource.DEPTH_INFINITE);
generally since workspace changes are expensive , an IWorkspaceRunnable should be used
to batch many workspace operations together .
IWorkspaceRunnable runnable = new IWorkspaceRunnable(){
public void run(IProgressMonitor monitor) {
IMarker marker = resource.createMarker(markerId);
setAttributes(marker);
}
};
the setAttributes method sets the marker . This generally boild down to populating a hashtable
and setting it .
resource.getWorkspace().run(runnable , null);
An imageProvider can be specified in the marker definition to define an image for the marker .
let the id be : org.my.marker
then all markers of that type can be easily found . The way to found those markers is :
IWorkspaceRoot root = ResourcePlugin.getWorkspace().getRoot();
root.findMarkers("org.my.marker",false,IResource.DEPTH_INFINITE);
To create a marker :
getProject().createMarker("org.my.marker",false,IResource.DEPTH_INFINITE);
generally since workspace changes are expensive , an IWorkspaceRunnable should be used
to batch many workspace operations together .
IWorkspaceRunnable runnable = new IWorkspaceRunnable(){
public void run(IProgressMonitor monitor) {
IMarker marker = resource.createMarker(markerId);
setAttributes(marker);
}
};
the setAttributes method sets the marker . This generally boild down to populating a hashtable
and setting it .
resource.getWorkspace().run(runnable , null);
An imageProvider can be specified in the marker definition to define an image for the marker .
Monday, October 08, 2007
Contributing to the Eclipse Cool Bar
To add stuff to the eclipse cool bar , u need to
1) implement the extension point org.eclipse.ui.editor .
2) Specify a contributorClass for the above mentioned extension point.
This contributor class will provide actions that would be contributed to the
coolbar.
Lets call this calls MyContributorClass.
3) Have this class extend BaseEditorContributor , It should override contributeToParentCoolbar .
4) In the given method create a ContributionItem that would be contributed to the coolbar.
Lets call this myContributionItem.
5) Use the parentCoolBarManager to add create a ToolBarManager.
IToolBarManager toolbar = new ToolBarManager(parentCoolBarManager.getStyle())
6) Add myContributionItem to toolbar.
toolbar.add(myContributionItem);
7) Create a ToolBarContributionItem and add it to the parent cool bar.
ToolBarContributionItem toolBarItem = new ToolBarContributionItem(toolbar,myContributionItem.getId())
parentCoolBarManager.add(toolBarItem);
and finally
coolBarItems.add(toolBarItem);
parentCoolBarManager and coolBarItem are protected members and are available from the base class .
1) implement the extension point org.eclipse.ui.editor .
2) Specify a contributorClass for the above mentioned extension point.
This contributor class will provide actions that would be contributed to the
coolbar.
Lets call this calls MyContributorClass.
3) Have this class extend BaseEditorContributor , It should override contributeToParentCoolbar .
4) In the given method create a ContributionItem that would be contributed to the coolbar.
Lets call this myContributionItem.
5) Use the parentCoolBarManager to add create a ToolBarManager.
IToolBarManager toolbar = new ToolBarManager(parentCoolBarManager.getStyle())
6) Add myContributionItem to toolbar.
toolbar.add(myContributionItem);
7) Create a ToolBarContributionItem and add it to the parent cool bar.
ToolBarContributionItem toolBarItem = new ToolBarContributionItem(toolbar,myContributionItem.getId())
parentCoolBarManager.add(toolBarItem);
and finally
coolBarItems.add(toolBarItem);
parentCoolBarManager and coolBarItem are protected members and are available from the base class .
Sunday, July 29, 2007
Programatically Creating a java package in Eclipse
In my last post we saw how we can create a java project programatically in eclipse. In this post we will see how to create a package in this java project.
The steps needed to do this are :
1. Create a source folder under the java project created earlier .
IFolder src = javaProject.getFolder("src");
folder.create(force , local , nullProgressMonitor);
IPackageFragmentRoot packageRoot = javaProject.getPackageFragmentRoot(folder);
IClasspathEntry[] classPath = javaProject.getRawClasspath();
List entries = new ArrayList(Arrays.asList(classpath));
entries.add(JavaCore.newSourceEntry(root.getPath()));
javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[0]) , null ProgressMonitor);
The code becomes pretty clear if you had followed my previous post.
2. Create the actual package .
src.createPackageFragment(packageName, force , nullProgressMonitor);
Yeah thats done , in the next post well see how to create a class programatically
The steps needed to do this are :
1. Create a source folder under the java project created earlier .
IFolder src = javaProject.getFolder("src");
folder.create(force , local , nullProgressMonitor);
IPackageFragmentRoot packageRoot = javaProject.getPackageFragmentRoot(folder);
IClasspathEntry[] classPath = javaProject.getRawClasspath();
List
entries.add(JavaCore.newSourceEntry(root.getPath()));
javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[0]) , null ProgressMonitor);
The code becomes pretty clear if you had followed my previous post.
2. Create the actual package .
src.createPackageFragment(packageName, force , nullProgressMonitor);
Yeah thats done , in the next post well see how to create a class programatically
Wednesday, July 25, 2007
Creating a new project in Eclipse Programatically
If you have done Eclipse plugin development you would know that any non trivial task is not obvious , unless you have gone and dug into the eclipse source code. Going through the eclipse source code is not easy cause its a massive rhino . Finding the right code requires tons of patience for a newbie .
Here i am presenting a small snippet that creates a java project programatically
This code can also be found in the eclipse source tree , i am presenting here just for handy reference .
IProject project = root.getProject("dummyProject");
IProgressMonitor nullProgressMonitor = null;
project.create(nullProgressMonitor);
ResourcesPlugin is the plugin that gives us access to all the resources in eclipse including the workspace . When eclipse creates a project it shows a progress bar , we are passing null for that.
2. Open the above created java Project
IJavaProject javaProject = JavaCore.create(project)
'create' is a misonomer . This creates a decorated Java version of an eclipse project.
3. Add the java nature to the project
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[]{JavaCore.NATURE_ID});
project.setDescription(description,null);
Eclipse has the concept of natures , its kinda tagging a project with java label.
4. Set the class path for the java project
IClasspathEntry[] rawClassPath = javaProject.getRawClassPath();
List classPath = new ArrayList(
Arrays.asList(rawClassPath));
classPath.add(JavaRuntime.getDefaultJREContainerEntry());
javaProject.setRawClasspath(classPath, nullProgressMonitor);
Since java and class paths are closely tied . We need to add the different class libraries to the project class path . In Eclipse every plugin has its own class loader . By adding add class libraries to the project class path we make class libraries accessible to the class loader .
5. Create the bin folder and mark it as output folder
boolean force = true;
boolean local = true;
IFolder binFolder = project.getFolder("bin");
binFolder.create(force,local,nullProgressMonitor);
IPath fullPath = binFolder.getFullPath();
javaProject.setOutputLocation(fullPath,nullProgressMonitor);
This code will create a project in the Runtime Eclipse Workbench that appears when you launch a new eclipse instance from your eclipse instance.
Here i am presenting a small snippet that creates a java project programatically
This code can also be found in the eclipse source tree , i am presenting here just for handy reference .
- Create an IProject with a name
IProject project = root.getProject("dummyProject");
IProgressMonitor nullProgressMonitor = null;
project.create(nullProgressMonitor);
ResourcesPlugin is the plugin that gives us access to all the resources in eclipse including the workspace . When eclipse creates a project it shows a progress bar , we are passing null for that.
2. Open the above created java Project
IJavaProject javaProject = JavaCore.create(project)
'create' is a misonomer . This creates a decorated Java version of an eclipse project.
3. Add the java nature to the project
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[]{JavaCore.NATURE_ID});
project.setDescription(description,null);
Eclipse has the concept of natures , its kinda tagging a project with java label.
4. Set the class path for the java project
IClasspathEntry[] rawClassPath = javaProject.getRawClassPath();
List
Arrays.asList(rawClassPath));
classPath.add(JavaRuntime.getDefaultJREContainerEntry());
javaProject.setRawClasspath(classPath, nullProgressMonitor);
Since java and class paths are closely tied . We need to add the different class libraries to the project class path . In Eclipse every plugin has its own class loader . By adding add class libraries to the project class path we make class libraries accessible to the class loader .
5. Create the bin folder and mark it as output folder
boolean force = true;
boolean local = true;
IFolder binFolder = project.getFolder("bin");
binFolder.create(force,local,nullProgressMonitor);
IPath fullPath = binFolder.getFullPath();
javaProject.setOutputLocation(fullPath,nullProgressMonitor);
This code will create a project in the Runtime Eclipse Workbench that appears when you launch a new eclipse instance from your eclipse instance.
Subscribe to:
Posts (Atom)
Labels
. linux
(1)
algorithm
(15)
analytics
(1)
bash
(2)
bigoh
(1)
bruteforce
(1)
c#
(1)
c++
(40)
collections
(1)
commands
(2)
const
(1)
cosine similarity
(1)
creating projects
(1)
daemon
(1)
device_drivers
(1)
eclipse
(6)
eclipse-plugin-development
(9)
equals
(1)
formatting
(1)
freebsd
(1)
game programming
(1)
hashcode
(1)
heap
(1)
heaps
(1)
immutable-objects
(1)
java
(19)
JDT
(1)
kernel
(1)
linux
(4)
little sugar
(23)
logging
(1)
machine learning
(1)
marker-resolution
(1)
markers
(1)
mergesort
(1)
mixins
(1)
numbers
(1)
opengl
(2)
patterns
(2)
priority-queue
(1)
programming
(51)
ps
(1)
ranking
(1)
refactoring
(3)
references
(1)
security
(1)
set
(1)
shell
(1)
similarity
(1)
statistics
(1)
stl
(1)
tetris
(1)
threads
(1)
trees
(2)
unicode
(1)
unix
(2)
views
(2)
windows programming
(2)
XNA
(1)