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:
<?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:
private void openFolder( String location )
{
Desktop desktop = null;
if( Desktop.isDesktopSupported() )
{
desktop = Desktop.getDesktop();
}
File file = new File( location );
try
{
desktop.open( file );
}
catch( IOException e )
{
e.printStackTrace();
}
}
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;
exp.printStackTrace();
}
catch( IOException exp )
{
isOK = false;
exp.printStackTrace();
}
finally
{
try
{
if( zout != null )
{
zout.close();
}
if( out != null )
{
out.close();
}
}
catch( IOException exp )
{
isOK = false;
exp.printStackTrace();
}
if( isOK )
{
MessageDialog.openInformation( null, DIALOG_TITLE, DIALOG_CONTENT_ZIP_OK );
}
else
{
MessageDialog.openInformation( null, DIALOG_TITLE, DIALOG_CONTENT_ZIP_FAIL );
}
}
}
@Override
public Object execute( ExecutionEvent event )
throws ExecutionException
{
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( firstElement instanceof ICompilationUnit || firstElement instanceof IFile )
{
path = selectedResource.getParent().getLocation();
}
else
{
path = selectedResource.getLocation();
}
folderNameOfParent = path.lastSegment();
location = path.toString();
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 ) )
{
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.