Eclipse RCP File browser

Eclipse RCP allow you to create easily utility programs.

Lets for example create a mini-file browser which will open a folder / file if you double-click the selection via the standard program.

To create a RCP based file browser create a new Eclipse RCP project, e.g. de.vogella.rcp.intro.filebrowser.

Create a package “de.vogella.rcp.intro.filebrowser.provider” and create the following label and content provider.


package de.vogella.rcp.intro.filebrowser.provider;

import java.io.File;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class FileContentProvider implements ITreeContentProvider {

@Override
public Object[] getChildren(Object parent) {
File file = (File) parent;
return file.listFiles();
}

public Object[] getElements(Object inputElement) {
return (Object[]) inputElement;
}

@Override
public Object getParent(Object element) {
File file = (File) element;
return file.getParentFile();
}

@Override
public boolean hasChildren(Object parent) {
File file = (File) parent;
return file.isDirectory();
}

@Override
public void dispose() {

}

@Override
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
}

}

package de.vogella.rcp.intro.filebrowser.provider;

import java.io.File;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class FileLabelProvider extends LabelProvider {
private static final Image folderImage = AbstractUIPlugin
.imageDescriptorFromPlugin("de.vogella.rcp.intro.filebrowser",
"icons/folder.gif").createImage();
private static final Image driveImage = AbstractUIPlugin
.imageDescriptorFromPlugin("de.vogella.rcp.intro.filebrowser",
"icons/filenav_nav.gif").createImage();
private static final Image fileImage = AbstractUIPlugin
.imageDescriptorFromPlugin("de.vogella.rcp.intro.filebrowser",
"icons/file_obj.gif").createImage();

@Override
public Image getImage(Object element) {
File file = (File) element;
if (file.isDirectory())
return file.getParent() != null ? folderImage : driveImage;
return fileImage;
}

@Override
public String getText(Object element) {
String fileName = ((File) element).getName();
if (fileName.length() > 0) {
return fileName;
}
return ((File) element).getPath();
}
}

For the label provider you need the referred icons in your folder icons. You can use the following download.

Download the Icons

Adjust then the view.

package de.vogella.rcp.intro.filebrowser;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import de.vogella.rcp.intro.filebrowser.provider.FileContentProvider;
import de.vogella.rcp.intro.filebrowser.provider.FileLabelProvider;

public class View extends ViewPart {
	public static final String ID = "de.vogella.rcp.intro.filebrowser.view";
	private TreeViewer viewer;

	public void createPartControl(Composite parent) {
		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new FileContentProvider());
		viewer.setLabelProvider(new FileLabelProvider());
		viewer.setInput(File.listRoots());
		viewer.addOpenListener(new IOpenListener() {

			@Override
			public void open(OpenEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event
						.getSelection();

				File file = (File) selection.getFirstElement();
				if (Desktop.isDesktopSupported()) {
					Desktop desktop = Desktop.getDesktop();
					if (desktop.isSupported(Desktop.Action.OPEN)) {
						try {
							desktop.open(file);
						} catch (IOException e) {
							// DO NOTHING
						}
					}
				}
			}
		});
	}

	public void setFocus() {
		viewer.getControl().setFocus();
	}
}

About Lars Vogel

Lars Vogel works as an independent Android and Eclipse trainer, consultant and book author. He is also a regular speaker at international conferences. With more then one million visitors per month Lars website vogella.com is an important source for Android and Eclipse related programming topics.
This entry was posted in Eclipse and tagged , , . Bookmark the permalink.

Comments are closed.