Archive

Archive for the ‘Eclipse CDT’ Category

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: , ,