Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add my own file template to Eclipse?

Tags:

eclipse

When you right click in a folder, Eclipse pops up a context menu that allows you to create different kinds of files. I'd like to add an option to add a kind of file of my own (with some constant data I'll want to put there). Is there an easy way to do it?

enter image description here

like image 370
devoured elysium Avatar asked Apr 10 '11 23:04

devoured elysium


People also ask

How do I import a template into eclipse?

To import a code template: Click Import to open the "Import Templates" browser. Select the relevant XML file containing the template information. Click Open.

How to edit template Eclipse?

The Templates Preferences page is accessed from Window | Preferences | PHP | Templates . To remove a template from the list of available options, unmark its checkbox in the list. To edit an existing template, select it from the list and click Edit.


2 Answers

Well, I know of two ways (one tested and of other I am not sure) in which you can extend the Default new submenu of package explorers popup menu.

The short, working and tested way is to use org.eclipse.ui.perspectiveExtensions.

>>Note: You will need a wizard to contribute to the Default new submenu of package explorers popup menu. You can do this using this link eSpeed development with Eclipse wizards (the same one provided by @Ed Burnette)

Steps to follow:

  1. For this example I have created a dummy test wizard with the id testwizard.wizards.TestWizard.
  2. Now create an extension of org.eclipse.ui.perspectiveExtensions. For this example I am just contributing to the Java development perspective. You can have multiple instance of it for different perspectives. Therefore, the targetId is org.eclipse.jdt.ui.JavaPerspective.
  3. Now right click on the perspectiveExtension and select newWizardShortcut
  4. Set the id of the newWizardShortcut as your custom wizards id i.e. testwizard.wizards.TestWizard in my case.
  5. Restart your application. Now don't forget to reset the perspective, otherwise your addition to the popup menu won't be visible.

>>Plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>    
   <extension
         point="org.eclipse.ui.newWizards">
      <category
            name="Test Wizards"
            id="TestWizard">
      </category>
      <wizard
            name="HTML Test Wizard"
            icon="icons/sample.gif"
            category="TestWizard"
            class="testwizard.wizards.TestWizard"
            id="testwizard.wizards.TestWizard">
      </wizard>
   </extension>
   <extension
         point="org.eclipse.ui.perspectiveExtensions">
      <perspectiveExtension
            targetID="org.eclipse.jdt.ui.JavaPerspective">
         <newWizardShortcut
               id="testwizard.wizards.TestWizard">
         </newWizardShortcut>
      </perspectiveExtension>
   </extension>
</plugin>

>>Output

Output

The long and untested way is to use org.eclipse.ui.navigator.navigatorContent. And I am not sure whether it will work or not. Providing it just for reading and exploration purpose

Use these links:

  1. Navigator Content 1
  2. Navigator Content 2

In the end I will suggest you to use first approach as it is simple and elegant. Read and use second method if you are writing a new perspective, view etc.

Hope this helps.

like image 147
Favonius Avatar answered Nov 03 '22 02:11

Favonius


You need to create an Eclipse plug-in and create a New File wizard. There's a great tutorial on how to do it over on developerworks:

  • http://www.ibm.com/developerworks/opensource/library/os-eclipse-custwiz/

See also:

  • http://www.eclipse-tips.com/tips/10
like image 26
Ed Burnette Avatar answered Nov 03 '22 00:11

Ed Burnette