Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Eclipse plugin to explore/zip selected resources.

4.96/5 (24 votes)
26 Dec 2012CPOL1 min read 13.3K  
An Eclipse plugin to explore/zip the selected resources ( project, files, source code...).

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.

Source:  download
Bin:  download 

Introduction 

Sometime, in developing in Eclipse, you want to know that the location of the selected resources (project, files, source code...). Especially, when your co-worker would like you to share your code or you've finished a comprehensive demo, so you want to store it ( be compressed to a file) on free cloud service such as Google Drive. 

Normally, in Eclipse, you will have to right click on the selected resources, then select Properties, navigate to Resource, copy the path, then paste it in Run. These steps is a little bit complicated. This features has been supported as well in Visual Studio by default, but in Eclipse is not.

The main features are:

 + Explore the selected resources by a simplest way: just need 1 mouse click.

 + Compress the selected resources ( in zip format ).

 + Copy the path of the selected resources into clipboard.  

Background

This Eclipse plugin is useful if you often open the path of the selected resouces in Eclipse. Or, you want to compress them in zip format. It will save time by reducing some steps. It's also helpful in case of studing how to develop a simple plugin for Eclipse.

Using the code 

Need to declare some configuration in plugin.xml as below: 

HTML
<?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">
         <menu
               icon="res/resource_icon.png"
               id="pb.plugin.openfolder.menu7"
               label="Folder Utils">
            <separator
                  name="pb.plugin.openfolder.separator5">
            </separator>
            <command
                  label="Explore folder"
                  style="push"
                  tooltip="Open the directory that contains the selected resource."
                  commandId="pb.plugin.openfolder.folder">
                <parameter
                      name="pb.plugin.openfolder"
                      value="pb.plugin.openfolder.openfolder">
                </parameter>
            </command>
            <command
                  icon="res/copy_icon.png"
                  label="Copy location to clipboard"
                  tooltip="Copy the location of the selected resource to clipboard"
                  style="push"
                  commandId="pb.plugin.openfolder.folder">
                <parameter
                      name="pb.plugin.openfolder"
                      value="pb.plugin.openfolder.clipboard">
                </parameter>  
            </command>
            <command
                  label="Zip all resources"
                  icon="res/zip_icon.png"
                  tooltip="Copy the location of the selected resource to clipboard"
                  style="push"
                  commandId="pb.plugin.openfolder.folder">
               <visibleWhen
                    checkEnabled="true">
                 <with
                     variable="activeMenuSelection">
                  <iterate
                        ifEmpty="false"
                        operator="or">
                        <not>
                            <adapt
                                type="org.eclipse.core.resources.IFile">
                            </adapt>
                        </not>
                        <not>
                            <adapt
                                type="org.eclipse.jdt.core.ICompilationUnit">
                            </adapt>
                        </not>
                  </iterate>
                </with>
              </visibleWhen>
              <parameter
                    name="pb.plugin.openfolder"
                    value="pb.plugin.openfolder.zipfolder">
              </parameter>
            </command>
         </menu>
      </menuContribution>
   </extension>
   
   <extension
         point="org.eclipse.ui.commands">
      <command
            defaultHandler="pb.plugin.openfolder.handler.FolderHandler"
            id="pb.plugin.openfolder.folder"
            name="Folder">
            <commandParameter
                  name="Open folder"
                  id="pb.plugin.openfolder"/>
            <commandParameter
                  name="Copy location to clipboard"
                  id="pb.plugin.openfolder"/>
            <commandParameter
                  name="Compress the selected resources"
                  id="pb.plugin.openfolder"/>
      </command>
   </extension>

</plugin>

 The main class is  pb.plugin.openfolder.handler.FolderHandler.java<code> 

Some inportant routines:

Java
private void openFolder( String location )
{
    Desktop desktop = null;
    // Before more Desktop API is used, first check
    // whether the API is supported by this particular
    // virtual machine (VM) on this particular host.
    if( Desktop.isDesktopSupported() )
    {
        desktop = Desktop.getDesktop();
    }
    File file = new File( location );
    try
    {
        desktop.open( file );
    }
    catch( IOException e )
    {
        e.printStackTrace();
        // TODO exception handling.
    }
}

 

public static void zipResources( File directory, File zipfile )
    {
        URI base = directory.toURI();
        Deque<File> queue = new LinkedList<File>();
        File[] listFiles = directory.listFiles();
        queue.push( directory );
        OutputStream out = null;
        boolean isOK = true;
        ZipOutputStream zout = null;
        try
        {
            int temp = 0;
            out = new FileOutputStream( zipfile );
            zout = new ZipOutputStream( out );
            while( !queue.isEmpty() )
            {
                if( temp != 0 )
                {
                    directory = queue.pop();
                    listFiles = directory.listFiles();
                }
                else
                {
                    queue.pop();
                    temp++;
                }
                for( File kid : listFiles )
                {
                    String name = base.relativize( kid.toURI() ).getPath();
                    if( kid.isDirectory() )
                    {
                        queue.push( kid );
                        name = name.endsWith( SLASH ) ? name : name + SLASH;
                        zout.putNextEntry( new ZipEntry( name ) );
                    }
                    else
                    {
                        zout.putNextEntry( new ZipEntry( name ) );
                        copyFile( kid, zout );
                        zout.closeEntry();
                    }
                }
            }
        }
        catch( FileNotFoundException exp )
        {
            isOK = false;
            // TODO Auto-generated catch block
            exp.printStackTrace();
        }
        catch( IOException exp )
        {
            isOK = false;
            // TODO Auto-generated catch block
            exp.printStackTrace();
        }
        finally
        {
            try
            {
                if( zout != null )
                {
                    zout.close();
                }
                if( out != null )
                {
                    out.close();
                }
            }
            catch( IOException exp )
            {
                isOK = false;
                // TODO Auto-generated catch block
                exp.printStackTrace();
            }
            if( isOK )
            {
                MessageDialog.openInformation( null, DIALOG_TITLE, DIALOG_CONTENT_ZIP_OK );
            }
            else
            {
                MessageDialog.openInformation( null, DIALOG_TITLE, DIALOG_CONTENT_ZIP_FAIL );
            }
        }
} 

Java
@Override
    public Object execute( ExecutionEvent event )
        throws ExecutionException
    {
        // Shell shell = HandlerUtil.getActiveShell( event );
        // ISelection sel = HandlerUtil.getActiveMenuSelection( event );
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if( window != null )
        {
            IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
            Object firstElement = selection.getFirstElement();
            if( firstElement instanceof IAdaptable )
            {
                IResource selectedResource = (IResource) ( (IAdaptable) firstElement ).getAdapter( IResource.class );
                String location, folderNameOfParent;
                IPath path;
                if( selectedResource == null )
                {
                    MessageDialog.openInformation( null, DIALOG_TITLE, DIALOG_CONTENT_OPERATION_FAIL );
                    return null;
                }
                // If selected resource is file type,, get the location of the folder that
                // contains that file ( get the location of parent folder )
                if( firstElement instanceof ICompilationUnit || firstElement instanceof IFile )
                {
                    path = selectedResource.getParent().getLocation();
                }
                else
                {
                    path = selectedResource.getLocation();
                }
                folderNameOfParent = path.lastSegment();
                location = path.toString();
                // determine with sub-menu is selected.
                String param = event.getParameter( COMMAND_ID );
                if( param.equals( COMMAND_VALUE_OPEN_FOLDER ) )
                {
                    openFolder( location );
                }
                else if( param.equals( COMMAND_VALUE_COPY_CLIPBOARD ) )
                {
                    copyLocationToClipboard( location );
                }
                else if( param.equals( COMMAND_VALUE_ZIP_FOLDER ) )
                {
                    // get the location of result file.
                    String zipFile = location.concat( SLASH ).concat( folderNameOfParent ).concat( ZIP_EXTENSION );
                    zipResources( new File( location ), new File( zipFile ) );
                    openFolder( location );
                }
            }
        }
        return null;
    }

 

Install plugin

Copy file pb.plugin.openfolder_1.0.0.201209171450.jar, then paste into the dropins folder of Eclipse. Then restart Eclipse.  

How to use: 

 Right click on the selected resources ( e.g : Java project ), then select Folder Utils, and choose the feature you want.

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)