Home > Eclipse CDT > Exploring CDT Programmatically :Part 1 – Creating CDT Project programmatically

Exploring CDT Programmatically :Part 1 – Creating CDT Project programmatically

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: , ,
  1. Parishrut
    October 16, 2011 at 11:15 am

    This is One Brilliant Code snippet i ever found on net. Perfect for new learners and beginners. I am sooooo impressed with your guidance.

    Please keep posting such basic but very crucial information all the time. Needy people like me find it absolutely helpful and undoubtedly light in the Dark !

    Cheers mate. Keep up the good work. God bless you 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: