Saturday 21 January 2017

TreeViewer: Display system directories in tree view

This is continuation to my previous post. In this post, I am going to explain how to display your computer data in tree viewer. I am not going to explain much here. Please go through my previous post, for detailed information.

Make sure you updated following variables in the file CustomTreeLabelProvider.java

private static String dirLocation = "C:\\Users \\Downloads\\icons\\folder.jpg";
private static String fileLocation = "C:\\Users \\Downloads\\icons\\document.png";

Following is the complete working application.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

public class CustomTreeLabelProvider implements ILabelProvider {

 private Image dir;
 private Image file;

 private static String dirLocation = "C:\\Users\\Downloads\\icons\\folder.jpg";
 private static String fileLocation = "C:\\Users\\Downloads\\icons\\document.png";

 public CustomTreeLabelProvider() {
  // Create the images
  try {
   dir = new Image(null, new FileInputStream(dirLocation));
   file = new Image(null, new FileInputStream(fileLocation));
  } catch (FileNotFoundException e) {
   // Swallow it; we'll do without images
  }
 }

 @Override
 public void addListener(ILabelProviderListener arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void dispose() {
  if (dir != null)
   dir.dispose();
 }

 @Override
 public boolean isLabelProperty(Object arg0, String arg1) {
  // TODO Auto-generated method stub
  return false;
 }

 @Override
 public void removeListener(ILabelProviderListener arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public Image getImage(Object arg0) {
  return ((File) arg0).isDirectory() ? dir : file;
 }

 @Override
 public String getText(Object arg0) {
  return arg0.toString();
 }

}

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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

public class CustomTreeContentProvider implements ITreeContentProvider {
 private Map<String, String[]> contentMap = new HashMap<>();

 public CustomTreeContentProvider() {
  initMap();
 }

 private void initMap() {
  String[] roots = { "root1", "root2" };

  contentMap.put("roots", roots);

  contentMap.put("root1", new String[] { "root1_child1", "root1_child2", "root1_child3" });
  contentMap.put("root1_child1", new String[] { "root1_child1_child1", "root1_child1_child2" });
 }

 @Override
 public Object[] getElements(Object arg0) {
  return File.listRoots();
 }

 @Override
 public void dispose() {
  // TODO Auto-generated method stub

 }

 @Override
 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
  // TODO Auto-generated method stub

 }

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

 @Override
 public Object getParent(Object arg0) {
  return ((File) arg0).getParentFile();
 }

 @Override
 public boolean hasChildren(Object arg0) {
  // Get the children
  Object[] obj = getChildren(arg0);

  // Return whether the parent has children
  return obj == null ? false : obj.length > 0;
 }

 private String getParentOfEle(Object arg0) {
  Set<String> keys = contentMap.keySet();

  for (String key : keys) {
   String[] values = contentMap.get(key);

   for (String val : values) {
    if (val.equals(arg0)) {
     return key;
    }
   }
  }
  return null;
 }

}

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TreeViewerDemo extends ApplicationWindow {

 public TreeViewerDemo() {
  super(null);

 }

 /**
  * Runs the application
  */
 public void run() {
  // Don't return from open() until window closes
  setBlockOnOpen(true);

  // Open the main window
  open();

  // Dispose the display
  Display.getCurrent().dispose();
 }

 protected void configureShell(Shell shell) {
  super.configureShell(shell);

  // Set the title bar text and the size
  shell.setText("File Tree");
  shell.setSize(400, 400);
 }

 protected Control createContents(Composite parent) {
  Composite composite = new Composite(parent, SWT.NONE);
  composite.setLayout(new GridLayout(1, false));

  final TreeViewer tv = new TreeViewer(composite);
  tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
  tv.setContentProvider(new CustomTreeContentProvider());
  tv.setLabelProvider(new CustomTreeLabelProvider());

  tv.setInput("root");

  return composite;
 }

}


public class TreeViewerDemoTest {
 public static void main(String args[]) {
  TreeViewerDemo treeViewer = new TreeViewerDemo();
  treeViewer.run();
 }
}


Run the class ‘TreeViewerDemoTest.java’, you can able to see following window.




Previous                                                 Next                                                 Home

No comments:

Post a Comment