Exploring CDT Programmatically :Part 1 – Creating CDT Project programmatically

January 27, 2011 1 comment

In this post we are going to see how we can create a CDT project programmatically. I have divided this into two parts. “UI and Core” parts.

UI:

First, we need a wizard to get the project details from the user. This wizard will serve as the user interface in which user can enter the details like project name, location etc.

We need to create a plugin project. We will name the plugin as com.dld.blog.expcdt.

The above shown is the overview of  plugin.xml. The new wizard creation can be done using the extension point org.eclipse.ui.newWizards. Add the following piece of code to your plugin.xml.

<extension
point = "org.eclipse.ui.newWizards">
1 <category
id = "org.eclipse.cdt.ui.newCWizards"
name="C/C++ project">
</category>
<wizard
id = "com.dld.blog.expcdt.mywizard"
name = "New CDT project"
class="com.dld.blog.expcdt.newwizard.CDTCreationWizard"
2 category="org.eclipse.cdt.ui.newCWizards"
3 project="true"
icon="">
<description>Choose to create a new  Project</description>
4<selection class="org.eclipse.core.resources.IResource"/>
</wizard>
 

We will see what are the  numbered sections in the above piece of code .First one is nothing but the category. When we add a wizard we can add it under a category.If we are not using any categories , by default eclipse will add it in the “File->New->Others” category.We can  define our own(Customized) category also.Second one is just a follow up of the first one when we are defining our wizard we are defining the category also.

Third one has a important role. If we are not using this  then your  wizard will not appear in “File->New->Project” category.It will appear only in “File->New->Others” category.So to make your wizard appear directly under “File->New->Project” category you  must add this line  ‘project=”true” ‘

Fourth one is nothing but the selection source ,  when we select something in navigator view its nothing but a resource of type file , folder. Our wizard will get enabled for any type of resource selection.

We have to create a class which serves as the wizard.Create a package “com.dld.blog.expcdt.newwizard” and create a class “CDTCreationWizard” the class must extend org.eclipse.jface.wizard.Wizard ,must implement org.eclipse.ui.INewWizard and may implement org.eclipse.jface.operation.IRunnableWithProgress.The class may look similar to what is shown below.


public class CDTCreationWizard extends Wizard implements INewWizard,IRunnableWithProgress {

 public CDTCreationWizard(){
 super();
 }

 @Override
 public void run(IProgressMonitor arg0) throws InvocationTargetException,
 InterruptedException {

 }

 @Override
 public boolean performFinish() {
 return true;
 }

 @Override
 public void init(IWorkbench arg0, IStructuredSelection arg1) {

 }

Now,It’s time to check our UI addition. Right click on plugin.xml->Run As->Eclipse Application. Once the application is launched choose File-New->Project. You will choose a wizard similar as shown below.

You can notice now our wizard is added in.

The next job is to add the wizard page which acts as the page where we add our UI components.

Create a class “CDTWizardPage” it must extend org.eclipse.jface.wizard.WizardPage.This class is going to serve as our UI class.Your wizardpage class may look like this.


public class CDTWizardPage extends WizardPage {

 protected CDTWizardPage(String pageName) {
 super(pageName);
 }

 @Override
 public void createControl(Composite parent) {

 }
}

Now we have to add the wizard page to wizard. The addPages() of wizard will do the job for us. After we add the code the wizardcode may look like this.


public class CDTCreationWizard extends Wizard implements INewWizard,IRunnableWithProgress {

 private CDTWizardPage cdtPage;

 public CDTCreationWizard(){
 super();
 }

 public void addPages(){
 super.addPages();
 cdtPage = new CDTWizardPage("New CDT Project");
 addPage(cdtPage);
 }

 @Override
 public void run(IProgressMonitor arg0) throws InvocationTargetException,
 InterruptedException {

 }
 @Override
 public boolean performFinish() {
 return true;
 }

 @Override
 public void init(IWorkbench arg0, IStructuredSelection arg1) {

 }

We will add now labels and text boxes and a browse button that constitute our wizard page. We will also add the necessary listeners to components.


public class CDTWizardPage extends WizardPage {

 private Button browse;
 private Text prjName;
 private Text locTxt;

 protected CDTWizardPage(String pageName) {
 super(pageName);
 setTitle(pageName);
 setDescription("Creates new CDT Project");
//        setPageComplete(false);
 }

 @Override
 public void createControl(Composite parent) {

 Composite container = new Composite(parent, SWT.NULL);
 container.setBounds(15, 25, 300, 400);

 Label name = new Label(container,SWT.NONE);
 name.setText("Enter the proj name:");
 name.setBounds(50,50,120,27);

 prjName = new Text(container,SWT.BOLD | SWT.BORDER);
 prjName.setBounds(180, 50, 170, 27);

 Label loc = new Label(container,SWT.NONE);
 loc.setText("Enter the location:");
 loc.setBounds(50,90,120,27);

 locTxt = new Text(container,SWT.BOLD | SWT.BORDER);
 locTxt.setBounds(180, 90, 170, 27);

 browse = new Button(container,SWT.NONE);
 browse.setText("Browse");
 browse.setBounds(370, 90, 80, 27);

 addListeners();

 setControl(container);
 }

 private void addListeners() {
 browse.addMouseListener(new MouseListener() {

 @Override
 public void mouseUp(MouseEvent e) {

 }

 @Override
 public void mouseDown(MouseEvent e) {
 DirectoryDialog dirDialog = new DirectoryDialog(browse.getShell());
 dirDialog.setText("Select the parent directory for project");
 String path = dirDialog.open();
 }

 @Override
 public void mouseDoubleClick(MouseEvent e) {

 }

 });
 }

 public String getProjName(){
 return prjName.getText();
 }

 public String getLocation(){
 return locTxt.getText();
 }

}

After adding the above code our wizard page will look like this.

I have added mouselistener to the browse button ,so by clicking the browse you will get a directory dialog.

Now we will modify the wizard code.


public class CDTCreationWizard extends Wizard implements INewWizard,IRunnableWithProgress {

 private CDTWizardPage cdtPage;

 public CDTCreationWizard(){
 super();
 }

 public void addPages(){
 super.addPages();
 cdtPage = new CDTWizardPage("New CDT Project");
 addPage(cdtPage);
 }

 @Override
 public void run(IProgressMonitor arg0) throws InvocationTargetException,
 InterruptedException {
 //Proj creation code should be called here

 }

 @Override
 public boolean performFinish() {
 try {
 getContainer().run(false, true, this);
 } catch (InvocationTargetException e) {
 e.printStackTrace();
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 return true;
 }

 @Override
 public void init(IWorkbench arg0, IStructuredSelection arg1) {

 }

}

You can notice that inside run method the code which is responsible for creating the CDT project should be called.

Now the UI part is Finished. In the core part we will write a class which does the magic of creating the CDT project. I am naming the class as “CDTProjectManager” ;-).

Add the following code in the CDTProjectManager class.


public class CDTProjectManager {

public void createCDTProj(String pName,String path,IProgressMonitor pMonitor){

IWorkspaceRoot wrkSpaceRoot = ResourcesPlugin.getWorkspace().getRoot();

IProject newProjectHandle = wrkSpaceRoot.getProject(pName);

try {

pMonitor.beginTask("creating CDT Project );

IProjectDescription projDesc = ResourcesPlugin.getWorkspace()

.newProjectDescription(newProjectHandle.getName());

if (!("".equals(path)) && path != null) {

Path myPath = new Path(path);

projDesc.setLocation(myPath);

}

IProject cdtProj = CCorePlugin.getDefault().createCDTProject(

projDesc, newProjectHandle, pMonitor);

}catch(Exception e){

e.printStackTrace()

}

}

}

Now include the below shown code in the run method of wizard.


public void run(IProgressMonitor arg0) throws InvocationTargetException,InterruptedException {

new CDTProjectManager().createCDTProj(cdtPage.getProjName(),cdtPage.getLocation(),arg0);

}

That’s it. We are finished now. 🙂

Advertisements
Categories: Eclipse CDT Tags: , ,

Exploring CDT – Programmatically

December 29, 2010 Leave a comment

From last couple of  months I am working on eclipse CDT platform. It’s quite new to me since I haven’t worked on CDT before and I am familiar with only JDT.It’s good that you always learn new things.. J and that too based on my favorite platform eclipse.

CDT is nothing but the C/C++ developer platform built on top of eclipse.It provides lots of functionalities like project creation , managed build , managing toolchains etc.,To know more about CDT you can refer here.

We will see the basic flow how a cdt project is created and launched.

Once you  create a new C/C++ project  , you will create all your folders and as good practice you will keep all your source files in src/ folder and all the header files in inc/ folder.

You need to do some project settings which can be done here your project – > properties->C/C++ Build ->Settings. Here you will set all the tool chains settings and build artifact settings. In  project – > properties->C/C++ General->Paths and Symbols, you will set all your macros and libraries.

By default CDT has two configurations namely “Debug” and “Release”. At any point of time only one config will be active.After all this done you will launch C/C++ application. All the source and header files are being compiled using the tool chain you have set and they will be converted into object files and at last they are bundled as an exe and eclipse will launch that exe when you choose Launch C/C++ application in the Navigator/Project explorer context menu.

You can see the created exe in navigator view under the folder which is named as per the configuration used.

In further posts I am going to discuss how we can build a CDT project , set the macros , libs etc., programmatically.I will also share my explorations on eclipse cdt 😉

Categories: Eclipse CDT Tags: , ,

Understanding basic concepts in eclipse

September 22, 2010 1 comment

In my earlier post i have introduced you to eclipse.Now, before we start using eclipse api’s we need to understand some very basic key concepts in eclipse architecture.In this post i am going to have a walk through of the basic key concepts in eclipse plugin architecture.

Plugin:

The first and the most basic thing of eclipse is nothing but a plugin. A plugin is nothing but  an individual runnable component  which may be plugged into or may  have pluggable components for another runnable component to get plugged in for running it.For more detailed reference you can refer here.

A simple plugin.xml will look as shown below.

In the above shown plugin.xml , it defines a perspective and a view which are there in the plugin. You can notice here that the terms extension point and extension is mentioned in the file. The next thing we are going to see is about extension and extension points.

Extension & Extension Points:

The next basic things in eclipse are nothing but extension and extension points.

An extension is nothing but a pluggable component defined in a plugin.In real life scenario you can imagine an extension as a memory card. So to read a memory card you need a card reader, right?

the card reader slot is nothing but the extension point. An extension point defines some rules (as there in real life scenarios) for it and whichever extensions which are having compliance to those rules can be plugged in to it. Extension and extension points is the basic way of how pulgins are interacting between each other. So you can understand from here that the “extensions” have the functionality written in them, if any other plugin wants to use the similar functionality it can be plugged into it and can be used.

In the above plugin.xml shown it uses two extensions

1)  org.eclipse.ui.perspectives

2)  org.eclipse.ui.views

LifeCycle of a plugin:

The Life cycle of a plugin is controlled by a class called “Activator.java” which will be created by eclipse itself when a plugin is created. A sample activator class will look look this.

The start method is called whenever the plugin is getting loaded.

When does a plugin gets started?

Eclipse by default follows lazy loading concept, i.e a plugin will be loaded only if one of its class is getting called. We can override this lazy loading option also so that the plugin gets loaded whenever your application is loaded.

There are three scenarios a plugin will be activated.

  • If your plugin has an executable extension, and if another plugin is using/running  it, that time your  plugin will be automatically loaded.
  • If your  plugin exports any of its libraries (JAR files),  another plugin can reference and instantiate your plugin  classes directly. Loading a class belonging to your plugin causes it to be started automatically.
  • A plugin can be activated explicitly, using the API method Platform.getPlugin(). This method returns a fully initialized plugin instance.

For more details please refer here.

Categories: eclipse rcp

Eclipse! – A Perfect pick for developing stand alone tool

September 12, 2010 Leave a comment

Today in the world of software two major things are acting as the interface to the end user. First and the major(nowadays)  are the web applications(eg: Indian railway online booking) and the next thing is the stand alone tools(eg: MS Word,vlc media player) both of them have their own specific purpose  in their space. Here in this post i am going to talk about a beautiful ,a very popular editor and very well designed technology which helps any developer to develop and deliver a stand alone tool.If u have any guesses the answer here is nothing but Eclipse.

Eclipse has many advantages , to mention a few it is open source , the large community behind it developing such a kind of tool , it’s support for developers by having a good and neatly maintained documentation  and also  it’s powerful design.

The basic functional part of eclipse is called plug-in. In my own definition you can understand a plug-in as an individual runnable application/module  which may be plugged into or will accept another runnable application/module to run.The biggest advantage of eclipse is its plug-in architecture.It beautifully separates out the individual  responsibilities of different modules in an application.To see the definition of eclipse plug-in given by eclipse community refer here.

In my forthcoming posts i am going to write about how we are going to use the power of eclipse to develop a stand alone tool.

Categories: eclipse rcp

Hello world!

September 11, 2010 1 comment

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Categories: Uncategorized