Eclipse Papercut #1 – The annoying creation of the project package

This series is about simplifying the handling of Eclipse for simple but repetitive tasks. See 7 Paper Cuts in Eclipse.

Ok lets start with something simple.

By convension Eclipse projects are named based on reverse URL’s, e.g. “org.eclipse.jdt” or “de.vogella.test”. Also this convension suggests to create a package with the same name as the project. For this you have to select the src folder in the project, right click on it, select New – Package then copy the name of the project from the first line and paste it into the second line of the dialog.

papercut1_10

While you may argue that this is not to bad as the amount of project you are creating is limited; I personally find this annoying as I’m writting lots of example for my articles on http://www.vogella.de/.

Time to simplify (at least a little bit).

Create an Eclipse plug-in called “de.vogella.jdt.packageexplorer”. Add a Eclipse command to it with the following default handler.


package de.vogella.jdt.packageexplorer.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFolder;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

public class AddPackage extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		IStructuredSelection selection = (IStructuredSelection) HandlerUtil
				.getActiveMenuSelection(event);
		Object firstElement = selection.getFirstElement();
		if (firstElement instanceof IJavaProject) {
			IJavaProject javaProject = (IJavaProject) firstElement;
			try {
				IFolder folder = javaProject.getProject().getFolder("src");
				// folder.create(true, true, null);
				IPackageFragmentRoot srcFolder = javaProject
						.getPackageFragmentRoot(folder);
				srcFolder.createPackageFragment(javaProject.getProject()
						.getName(), true, null);
			} catch (JavaModelException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}


Add this command to the content menu in the package explorer as described in Eclipse plug-in Development.

The resulting plugin.xml looks like the following:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
         <command
               commandId="de.vogella.jdt.packageexplorer.AddPackage"
               label="Add Default Package"
               style="push">
         </command>
      </menuContribution>
   </extension>
   <extension
         point="org.eclipse.ui.commands">
      <command
            defaultHandler="de.vogella.jdt.packageexplorer.handler.AddPackage"
            id="de.vogella.jdt.packageexplorer.AddPackage"
            name="Add Default Package">
      </command>
   </extension>


</plugin>

Export your plug-in and put it into the dropin folder in your Eclipse installation. Restart Eclipse.

If you now create a new project you can right click on the project and directly create your project / default package.

papercut1_30

If you want to improve this further you can assign a keybinding to your command and use a shortcut to create the package. Or if you have more artifacts to create you can use the handler to create .java files, more packages, etc.

Here is the source bundle to download:
de.vogella.jdt.packageexplorer.source_1.0.0

Here is the exported plugin (you need you rename zip to jar before putting this into the Eclipse/dropin folder)

de.vogella.jdt.packageexplorer_1.0.0

See Eclipse Papercuts to get all posts in this series.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Eclipse, Papercut and tagged , , , . Bookmark the permalink.

3 Responses to Eclipse Papercut #1 – The annoying creation of the project package

  1. marek says:

    cool idea

  2. Pingback: Eclipse Papercut #2 – Sharing is good » Eclipse Papercuts

  3. Etienne says:

    Your article gave me great ideas of small useful time saving plugins I could do…

Comments are closed.